From 9ec82b9a44abcdf592448717a25b9a6e83de81c5 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 6 Sep 2018 13:50:37 -0600 Subject: [PATCH] graceful failure when receiving too-small profile data --- SignalServiceKit/src/Util/Cryptography.m | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/SignalServiceKit/src/Util/Cryptography.m b/SignalServiceKit/src/Util/Cryptography.m index 40b5b0568..2c6c0b239 100755 --- a/SignalServiceKit/src/Util/Cryptography.m +++ b/SignalServiceKit/src/Util/Cryptography.m @@ -802,10 +802,13 @@ const NSUInteger kAES256_KeyByteLength = 32; + (nullable NSData *)decryptAESGCMWithProfileData:(NSData *)encryptedData key:(OWSAES256Key *)key { - OWSAssert(encryptedData.length > kAESGCM256_IVLength + kAESGCM256_TagLength); - NSUInteger cipherTextLength; - ows_sub_overflow(encryptedData.length, (kAESGCM256_IVLength + kAESGCM256_TagLength), &cipherTextLength); + BOOL didOverflow + = __builtin_sub_overflow(encryptedData.length, (kAESGCM256_IVLength + kAESGCM256_TagLength), &cipherTextLength); + if (didOverflow) { + OWSFailDebug(@"unexpectedly short encryptedData.length: %lu", (unsigned long)encryptedData.length); + return nil; + } // encryptedData layout: initializationVector || ciphertext || authTag NSData *initializationVector = [encryptedData subdataWithRange:NSMakeRange(0, kAESGCM256_IVLength)];