session-ios/SignalUtilitiesKit/Database/Utilities/SignalKeyingStorage.m

83 lines
2.4 KiB
Mathematica
Raw Normal View History

2014-07-11 00:33:51 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2014-07-11 00:33:51 +02:00
//
2017-02-13 17:40:30 +01:00
#import "SignalKeyingStorage.h"
#import <SignalUtilitiesKit/OWSPrimaryStorage.h>
#import <SignalUtilitiesKit/YapDatabaseConnection+OWS.h>
2020-11-12 00:41:45 +01:00
#import <SignalCoreKit/Randomness.h>
2014-07-11 00:33:51 +02:00
#define SignalKeyingCollection @"SignalKeyingCollection"
#define SIGNALING_MAC_KEY_LENGTH 20
2014-07-11 00:33:51 +02:00
#define SIGNALING_CIPHER_KEY_LENGTH 16
#define SIGNALING_EXTRA_KEY_LENGTH 4
@implementation SignalKeyingStorage
2014-07-11 00:33:51 +02:00
+ (void)generateSignaling
{
2018-07-27 20:40:58 +02:00
[self storeData:[Randomness generateRandomBytes:SIGNALING_MAC_KEY_LENGTH] forKey:SIGNALING_MAC_KEY];
[self storeData:[Randomness generateRandomBytes:SIGNALING_CIPHER_KEY_LENGTH] forKey:SIGNALING_CIPHER_KEY];
[self storeData:[Randomness generateRandomBytes:SIGNALING_EXTRA_KEY_LENGTH] forKey:SIGNALING_EXTRA_KEY];
2014-07-11 00:33:51 +02:00
}
+ (int64_t)getAndIncrementOneTimeCounter
{
2014-07-11 00:33:51 +02:00
__block int64_t oldCounter;
oldCounter = [[self stringForKey:PASSWORD_COUNTER_KEY] longLongValue];
int64_t newCounter = (oldCounter == INT64_MAX) ? INT64_MIN : (oldCounter + 1);
[self storeString:[@(newCounter) stringValue] forKey:PASSWORD_COUNTER_KEY];
2014-07-11 00:33:51 +02:00
return newCounter;
}
+ (NSData *)signalingCipherKey
{
2014-07-11 00:33:51 +02:00
return [self dataForKey:SIGNALING_CIPHER_KEY andVerifyLength:SIGNALING_CIPHER_KEY_LENGTH];
}
+ (NSData *)signalingMacKey
{
2014-07-11 00:33:51 +02:00
return [self dataForKey:SIGNALING_MAC_KEY andVerifyLength:SIGNALING_MAC_KEY_LENGTH];
}
+ (NSData *)signalingExtraKey
{
2014-07-11 00:33:51 +02:00
return [self dataForKey:SIGNALING_EXTRA_KEY andVerifyLength:SIGNALING_EXTRA_KEY_LENGTH];
}
#pragma mark Keychain wrapper methods
+ (void)storeData:(NSData *)data forKey:(NSString *)key
{
[OWSPrimaryStorage.dbReadWriteConnection setObject:data forKey:key inCollection:SignalKeyingCollection];
2014-07-11 00:33:51 +02:00
}
+ (NSData *)dataForKey:(NSString *)key andVerifyLength:(uint)length
{
2014-07-11 00:33:51 +02:00
NSData *data = [self dataForKey:key];
2014-08-14 03:13:24 +02:00
if (data.length != length) {
OWSLogError(@"Length of data not matching. Got %lu, expected %u", (unsigned long)data.length, length);
2014-07-11 00:33:51 +02:00
}
2014-07-11 00:33:51 +02:00
return data;
}
+ (NSData *)dataForKey:(NSString *)key
{
return [OWSPrimaryStorage.dbReadConnection dataForKey:key inCollection:SignalKeyingCollection];
2014-07-11 00:33:51 +02:00
}
+ (NSString *)stringForKey:(NSString *)key
{
return [OWSPrimaryStorage.dbReadConnection stringForKey:key inCollection:SignalKeyingCollection];
2014-07-11 00:33:51 +02:00
}
+ (void)storeString:(NSString *)string forKey:(NSString *)key
{
[OWSPrimaryStorage.dbReadWriteConnection setObject:string forKey:key inCollection:SignalKeyingCollection];
2014-07-11 00:33:51 +02:00
}
@end