From d0b81ff3d45b16cd6c2b31db618048d3bd09b4ea Mon Sep 17 00:00:00 2001 From: Nora Trapp Date: Thu, 20 Feb 2020 10:46:32 -0800 Subject: [PATCH] Use a 16-byte IV for storage service encryption. --- .../OWSContactDiscoveryOperation.swift | 1 + .../src/Util/KeyBackupService.swift | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift index fe3cf7f3eb..1b29024bc5 100644 --- a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift +++ b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift @@ -357,6 +357,7 @@ class CDSBatchOperation: OWSOperation { let plaintext = Data.join([addressPlainTextData, noncePlainTextData]) guard let encryptionResult = Cryptography.encryptAESGCM(plainTextData: plaintext, + initializationVectorLength: kAESGCM256_DefaultIVLength, additionalAuthenticatedData: remoteAttestation.requestId, key: remoteAttestation.keys.clientKey) else { diff --git a/SignalServiceKit/src/Util/KeyBackupService.swift b/SignalServiceKit/src/Util/KeyBackupService.swift index 9fae71c88c..1b9984a416 100644 --- a/SignalServiceKit/src/Util/KeyBackupService.swift +++ b/SignalServiceKit/src/Util/KeyBackupService.swift @@ -292,6 +292,15 @@ public class KeyBackupService: NSObject { } } + var ivLength: UInt { + switch self { + case .storageService, .storageServiceManifest, .storageServiceRecord: + return 16 + default: + return kAESGCM256_DefaultIVLength + } + } + static var syncableKeys: [DerivedKey] { return [ .storageService @@ -353,8 +362,11 @@ public class KeyBackupService: NSObject { throw KBSError.assertion } - // TODO: Maybe rename this since it's no longer profile specific - guard let encryptedData = Cryptography.encryptAESGCMProfileData(plainTextData: data, key: key) else { + guard let encryptedData = Cryptography.encryptAESGCMWithDataAndConcatenateResults( + plainTextData: data, + initializationVectorLength: keyType.ivLength, + key: key + ) else { owsFailDebug("Failed to encrypt data") throw KBSError.assertion } @@ -368,8 +380,11 @@ public class KeyBackupService: NSObject { throw KBSError.assertion } - // TODO: Maybe rename this since it's no longer profile specific - guard let data = Cryptography.decryptAESGCMProfileData(encryptedData: encryptedData, key: key) else { + guard let data = Cryptography.decryptAESGCMConcatenatedData( + encryptedData: encryptedData, + initializationVectorLength: keyType.ivLength, + key: key + ) else { // TODO: Derive Storage Service Key - until we use the restored key for storage service, // this is expected after every reinstall. After that this should propably become an owsFailDebug Logger.info("failed to decrypt data") @@ -640,6 +655,7 @@ public class KeyBackupService: NSObject { guard let encryptionResult = Cryptography.encryptAESGCM( plainTextData: kbRequestData, + initializationVectorLength: kAESGCM256_DefaultIVLength, additionalAuthenticatedData: remoteAttestation.requestId, key: remoteAttestation.keys.clientKey ) else {