From d9a0893024569b0d4aebe40deefddae0c0f1aa42 Mon Sep 17 00:00:00 2001 From: Stefan Reitshamer Date: Mon, 10 Jul 2017 06:06:46 -0400 Subject: [PATCH] removed unused methods from EncryptionDatFile --- repo/EncryptionDatFile.h | 15 ------ repo/EncryptionDatFile.m | 106 --------------------------------------- 2 files changed, 121 deletions(-) diff --git a/repo/EncryptionDatFile.h b/repo/EncryptionDatFile.h index 5ad771c..5cfb5f0 100644 --- a/repo/EncryptionDatFile.h +++ b/repo/EncryptionDatFile.h @@ -45,21 +45,6 @@ NSData *masterKeys; } -+ (EncryptionDatFile *)createWithRandomMasterKeysAndEncryptionPassword:(NSString *)theEncryptionPassword - target:(Target *)theTarget - computerUUID:(NSString *)theComputerUUID - encryptionVersion:(int)theEncryptionVersion - targetConnectionDelegate:(id )theTCD - error:(NSError **)error; - -+ (EncryptionDatFile *)createWithMasterKeys:(NSData *)theMasterKeys - encryptionPassword:(NSString *)theEncryptionPassword - target:(Target *)theTarget - computerUUID:(NSString *)theComputerUUID - encryptionVersion:(int)theEncryptionVersion - targetConnectionDelegate:(id )theTCD - error:(NSError **)error; - + (EncryptionDatFile *)encryptionDatFileForTarget:(Target *)theTarget computerUUID:(NSString *)theComputerUUID encryptionPassword:(NSString *)theEncryptionPassword diff --git a/repo/EncryptionDatFile.m b/repo/EncryptionDatFile.m index 9016095..830c367 100644 --- a/repo/EncryptionDatFile.m +++ b/repo/EncryptionDatFile.m @@ -60,52 +60,6 @@ + (NSString *)errorDomain { return @"EncryptionDatFileErrorDomain"; } -+ (EncryptionDatFile *)createWithRandomMasterKeysAndEncryptionPassword:(NSString *)theEncryptionPassword - target:(Target *)theTarget - computerUUID:(NSString *)theComputerUUID - encryptionVersion:(int)theEncryptionVersion - targetConnectionDelegate:(id )theTCD - error:(NSError **)error { - // Generate a master encryption key. - NSMutableData *masterKeys = [NSMutableData dataWithLength:kCCKeySizeAES256 * 2]; - unsigned char *masterKeysBytes = (unsigned char *)[masterKeys mutableBytes]; - for (int i = 0; i < kCCKeySizeAES256 * 2; i++) { - masterKeysBytes[i] = (unsigned char)arc4random_uniform(256); - } - return [EncryptionDatFile createWithMasterKeys:masterKeys - encryptionPassword:theEncryptionPassword - target:theTarget - computerUUID:theComputerUUID - encryptionVersion:theEncryptionVersion - targetConnectionDelegate:theTCD - error:error]; -} - -+ (EncryptionDatFile *)createWithMasterKeys:(NSData *)theMasterKeys - encryptionPassword:(NSString *)theEncryptionPassword - target:(Target *)theTarget - computerUUID:(NSString *)theComputerUUID - encryptionVersion:(int)theEncryptionVersion - targetConnectionDelegate:(id )theTCD - error:(NSError **)error { - NSData *data = [EncryptionDatFile generateDataWithMasterKeys:theMasterKeys encryptionPassword:theEncryptionPassword encryptionVersion:theEncryptionVersion error:error]; - if (data == nil) { - return nil; - } - EncryptionDatFile *ret = [[[EncryptionDatFile alloc] initWithEncryptionPassword:theEncryptionPassword target:theTarget computerUUID:theComputerUUID encryptionVersion:theEncryptionVersion data:data masterKeys:theMasterKeys] autorelease]; - // Delete the local cached file to avoid cache inconsistency in the event of an error when saving to target. - if (![ret deleteLocalCache:error]) { - return NO; - } - if (![ret saveToTargetWithTargetConnectionDelegate:theTCD error:error]) { - return NO; - } - NSError *cacheError = nil; - if (![ret saveToLocalCache:&cacheError]) { - HSLogError(@"failed to save new EncryptionDatFile to local cache: %@", cacheError); - } - return ret; -} + (EncryptionDatFile *)encryptionDatFileForTarget:(Target *)theTarget computerUUID:(NSString *)theComputerUUID @@ -312,66 +266,6 @@ return self; } -+ (NSData *)generateDataWithMasterKeys:(NSData *)theMasterKeys encryptionPassword:(NSString *)theEncryptionPassword encryptionVersion:(int)theEncryptionVersion error:(NSError **)error { - if ([theMasterKeys length] != 64 && theEncryptionVersion != 1) { - SETNSERROR([self errorDomain], -1, @"master key data must be 64 bytes"); - return nil; - } - - // Create random salt. - NSData *salt = [NSData dataWithRandomBytesOfLength:SALT_LENGTH]; - - // Create iv. - NSData *iv = [NSData dataWithRandomBytesOfLength:IV_LENGTH]; - - // Derive 64-byte encryption key from theEncryptionPassword. - NSData *thePasswordData = [theEncryptionPassword dataUsingEncoding:NSUTF8StringEncoding]; - void *derivedEncryptionKey = malloc(kCCKeySizeAES256 * 2); - CCKeyDerivationPBKDF(kCCPBKDF2, [thePasswordData bytes], [thePasswordData length], [salt bytes], [salt length], kCCPRFHmacAlgSHA1, KEY_DERIVATION_ROUNDS, derivedEncryptionKey, kCCKeySizeAES256 * 2); - void *derivedHMACKey = derivedEncryptionKey + kCCKeySizeAES256; - - // Encrypt master keys using first 32 bytes of derived key and iv. - size_t encryptedMasterKeysLen = [theMasterKeys length] + kCCBlockSizeAES128; - NSMutableData *encryptedMasterKeys = [NSMutableData dataWithLength:encryptedMasterKeysLen]; - size_t encryptedMasterKeysActualLen = 0; - CCCryptorStatus status = CCCrypt(kCCEncrypt, - kCCAlgorithmAES128, - kCCOptionPKCS7Padding, - derivedEncryptionKey, - kCCKeySizeAES256, - [iv bytes], - [theMasterKeys bytes], - [theMasterKeys length], - [encryptedMasterKeys mutableBytes], - [encryptedMasterKeys length], - &encryptedMasterKeysActualLen); - if (status != kCCSuccess) { - free(derivedEncryptionKey); - SETNSERROR([self errorDomain], -1, @"failed to encrypt master keys"); - return nil; - } - [encryptedMasterKeys setLength:encryptedMasterKeysActualLen]; - - // Calculate HMACSHA256 of IV + encrypted master keys, using derivedHMACKey. - unsigned char hmacSHA256[CC_SHA256_DIGEST_LENGTH]; - CCHmacContext hmacContext; - CCHmacInit(&hmacContext, kCCHmacAlgSHA256, derivedHMACKey, kCCKeySizeAES256); - CCHmacUpdate(&hmacContext, [iv bytes], [iv length]); - CCHmacUpdate(&hmacContext, [encryptedMasterKeys bytes], [encryptedMasterKeys length]); - CCHmacFinal(&hmacContext, hmacSHA256); - - free(derivedEncryptionKey); - - // Concatenate header, salt, HMACSHA256, IV and encrypted master keys. - NSMutableData *ret = [NSMutableData data]; - [ret appendBytes:HEADER length:strlen(HEADER)]; - [ret appendData:salt]; - [ret appendBytes:hmacSHA256 length:CC_SHA256_DIGEST_LENGTH]; - [ret appendData:iv]; - [ret appendData:encryptedMasterKeys]; - - return ret; -} - (BOOL)loadPrivateKeyFromData:(NSError **)error { if ([data length] < (strlen(HEADER) + SALT_LENGTH + CC_SHA256_DIGEST_LENGTH + IV_LENGTH + 1)) { SETNSERROR([self errorDomain], -1, @"not enough bytes in dat file");