@ -363,6 +363,19 @@ const NSUInteger kAES256_KeyByteLength = 32;
}
}
}
}
+ ( unsigned long ) paddedSize : ( unsigned long ) unpaddedSize
{
/ / Don ' t enable this until clients are sufficiently rolled out .
BOOL shouldPad = NO ;
if ( shouldPad ) {
/ / Note : This just rounds up to the nearsest power of two ,
/ / but the actual padding scheme is TBD
return pow ( 2 , ceil ( log2 ( unpaddedSize ) ) ) ;
} else {
return unpaddedSize ;
}
}
+ ( NSData * ) encryptAttachmentData : ( NSData * ) attachmentData
+ ( NSData * ) encryptAttachmentData : ( NSData * ) attachmentData
outKey : ( NSData * _Nonnull * _Nullable ) outKey
outKey : ( NSData * _Nonnull * _Nullable ) outKey
outDigest : ( NSData * _Nonnull * _Nullable ) outDigest
outDigest : ( NSData * _Nonnull * _Nullable ) outDigest
@ -377,8 +390,13 @@ const NSUInteger kAES256_KeyByteLength = 32;
[ attachmentKey appendData : hmacKey ] ;
[ attachmentKey appendData : hmacKey ] ;
* outKey = [ attachmentKey copy ] ;
* outKey = [ attachmentKey copy ] ;
/ / Apply any padding
unsigned long desiredSize = [ self paddedSize : attachmentData . length ] ;
NSMutableData * paddedAttachmentData = [ attachmentData mutableCopy ] ;
paddedAttachmentData . length = desiredSize ;
/ / Encrypt
/ / Encrypt
size_t bufferSize = [ attachmentData length ] + kCCBlockSizeAES128 ;
size_t bufferSize = [ p addedA ttachmentData length ] + kCCBlockSizeAES128 ;
void * buffer = malloc ( bufferSize ) ;
void * buffer = malloc ( bufferSize ) ;
if ( buffer == NULL ) {
if ( buffer == NULL ) {
@ -393,8 +411,8 @@ const NSUInteger kAES256_KeyByteLength = 32;
[ encryptionKey bytes ] ,
[ encryptionKey bytes ] ,
[ encryptionKey length ] ,
[ encryptionKey length ] ,
[ iv bytes ] ,
[ iv bytes ] ,
[ attachmentData bytes ] ,
[ p addedA ttachmentData bytes ] ,
[ attachmentData length ] ,
[ p addedA ttachmentData length ] ,
buffer ,
buffer ,
bufferSize ,
bufferSize ,
& bytesEncrypted ) ;
& bytesEncrypted ) ;
@ -407,22 +425,22 @@ const NSUInteger kAES256_KeyByteLength = 32;
NSData * cipherText = [ NSData dataWithBytesNoCopy : buffer length : bytesEncrypted freeWhenDone : YES ] ;
NSData * cipherText = [ NSData dataWithBytesNoCopy : buffer length : bytesEncrypted freeWhenDone : YES ] ;
NSMutableData * encrypted Attachment Data = [ NSMutableData data ] ;
NSMutableData * encrypted Padded Data = [ NSMutableData data ] ;
[ encrypted Attachment Data appendData : iv ] ;
[ encrypted Padded Data appendData : iv ] ;
[ encrypted Attachment Data appendData : cipherText ] ;
[ encrypted Padded Data appendData : cipherText ] ;
/ / compute hmac of : iv || encrypted data
/ / compute hmac of : iv || encrypted data
NSData * hmac =
NSData * hmac =
[ Cryptography truncatedSHA256HMAC : encrypted Attachment Data withHMACKey : hmacKey truncation : HMAC256_OUTPUT_LENGTH ] ;
[ Cryptography truncatedSHA256HMAC : encrypted Padded Data withHMACKey : hmacKey truncation : HMAC256_OUTPUT_LENGTH ] ;
DDLogVerbose ( @ "%@ computed hmac: %@", self.tag, hmac);
DDLogVerbose ( @ "%@ computed hmac: %@", self.tag, hmac);
[ encrypted Attachment Data appendData : hmac ] ;
[ encrypted Padded Data appendData : hmac ] ;
/ / compute digest of : iv || encrypted data || hmac
/ / compute digest of : iv || encrypted data || hmac
* outDigest = [ self computeSHA256Digest : encrypted Attachment Data] ;
* outDigest = [ self computeSHA256Digest : encrypted Padded Data] ;
DDLogVerbose ( @ "%@ computed digest: %@", self.tag, *outDigest);
DDLogVerbose ( @ "%@ computed digest: %@", self.tag, *outDigest);
return [ encrypted Attachment Data copy ] ;
return [ encrypted Padded Data copy ] ;
}
}
+ ( nullable NSData * ) encryptAESGCMWithData : ( NSData * ) plaintext key : ( OWSAES256Key * ) key
+ ( nullable NSData * ) encryptAESGCMWithData : ( NSData * ) plaintext key : ( OWSAES256Key * ) key