@@ -241,25 +241,31 @@ - (NSData *)zmDecryptPrefixedPlainTextIVWithKey:(NSData *)key
241241{
242242 VerifyReturnNil (key.length == kCCKeySizeAES256 );
243243
244+ // The encrypted payload is IV + ciphertext, so a valid
245+ // payload must be larger than the IV.
246+ VerifyReturnNil (self.length > kCCBlockSizeAES128 );
247+
244248 size_t copiedBytes = 0 ;
245249 NSMutableData *decryptedData = [NSMutableData dataWithLength: self .length+kCCBlockSizeAES128 ];
246250 NSData *dataWithoutIV = [NSData dataWithBytes: self .bytes+kCCBlockSizeAES128 length: self .length-kCCBlockSizeAES128 ];
247251 NSData *IV = [NSData dataWithBytes: self .bytes length: kCCBlockSizeAES128 ];
248252
249253 ZMLogDebug (@" Decrypt: IV is %@ . Data : %lu , Data w/out IV: %lu " , [IV base64EncodedStringWithOptions: 0 ], (unsigned long )self.length , (unsigned long )dataWithoutIV.length );
250254
251- CCCryptorStatus status = CCCrypt (kCCDecrypt , // basic operation kCCEncrypt or kCCDecrypt
252- kCCAlgorithmAES , // encryption algorithm
253- kCCOptionPKCS7Padding , // flags defining encryption
254- key.bytes , // Raw key material
255- kCCKeySizeAES256 , // Length of key material
256- IV.bytes , // Initialization vector for Cipher Block Chaining (CBC) mode (first 16 bytes)
257- dataWithoutIV.bytes , // Data to encrypt or decrypt
258- dataWithoutIV.length , // Length of data to encrypt or decrypt
259- decryptedData.mutableBytes , // Result is written here
260- decryptedData.length , // The size of the dataOut buffer in bytes
261- &copiedBytes); // On successful return, the number of bytes written to dataOut.
262-
255+ CCCryptorStatus status = CCCrypt (
256+ kCCDecrypt , // basic operation kCCEncrypt or kCCDecrypt
257+ kCCAlgorithmAES , // encryption algorithm
258+ kCCOptionPKCS7Padding , // flags defining encryption
259+ key.bytes , // Raw key material
260+ kCCKeySizeAES256 , // Length of key material
261+ IV.bytes , // Initialization vector for Cipher Block Chaining (CBC) mode (first 16 bytes)
262+ dataWithoutIV.bytes , // Data to encrypt or decrypt
263+ dataWithoutIV.length , // Length of data to encrypt or decrypt
264+ decryptedData.mutableBytes , // Result is written here
265+ decryptedData.length , // The size of the dataOut buffer in bytes
266+ &copiedBytes // On successful return, the number of bytes written to dataOut.
267+ );
268+
263269 if (status != kCCSuccess ) {
264270 ZMLogError (@" Error in decryption: %d " , status);
265271 return nil ;
0 commit comments