Skip to content

Commit

Permalink
Update Cocoapods.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesmchen-signal committed Aug 6, 2018
1 parent 320f479 commit 716524e
Show file tree
Hide file tree
Showing 17 changed files with 2,169 additions and 1,976 deletions.
8 changes: 6 additions & 2 deletions AxolotlKit/AxolotlKit/Classes/Crypto/AES-CBC.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface AES_CBC : NSObject

/**
Expand All @@ -20,7 +22,7 @@
* @return ciphertext
*/

+(NSData*)encryptCBCMode:(NSData*)data withKey:(NSData*)key withIV:(NSData*)iv;
+ (NSData *)encryptCBCMode:(NSData *)data withKey:(NSData *)key withIV:(NSData *)iv;

/**
* Decrypts with AES in CBC mode
Expand All @@ -32,6 +34,8 @@
* @return plaintext
*/

+(NSData*)decryptCBCMode:(NSData*)data withKey:(NSData*)key withIV:(NSData*)iv;
+ (NSData *)decryptCBCMode:(NSData *)data withKey:(NSData *)key withIV:(NSData *)iv;

@end

NS_ASSUME_NONNULL_END
131 changes: 85 additions & 46 deletions AxolotlKit/AxolotlKit/Classes/Crypto/AES-CBC.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,105 @@
// Copyright (c) 2014 Frederic Jacobs. All rights reserved.
//

#import "AES-CBC.h"
#import "AxolotlExceptions.h"
#import "MessageKeys.h"
#import "AES-CBC.h"
#import <Security/Security.h>
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonHMAC.h>
#import <Security/Security.h>

NS_ASSUME_NONNULL_BEGIN

@implementation AES_CBC

#pragma mark AESCBC Mode

+(NSData*)encryptCBCMode:(NSData*)data withKey:(NSData*)key withIV:(NSData*)iv{
NSAssert(data, @"Missing data to encrypt");
NSAssert([key length] == 32, @"AES key should be 256 bits");
NSAssert([iv length] == 16, @"AES-CBC IV should be 128 bits");

size_t bufferSize = [data length] + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);

size_t bytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
[key bytes], [key length],
[iv bytes],
[data bytes], [data length],
buffer, bufferSize,
&bytesEncrypted);

if (cryptStatus == kCCSuccess){
NSData *data = [NSData dataWithBytes:buffer length:bytesEncrypted];
free(buffer);

return data;
} else{
free(buffer);
@throw [NSException exceptionWithName:CipherException reason:@"We encountered an issue while encrypting." userInfo:nil];
+ (NSData *)encryptCBCMode:(NSData *)data withKey:(NSData *)key withIV:(NSData *)iv
{
if (!data) {
@throw [NSException exceptionWithName:CipherException reason:@"Missing data to encrypt." userInfo:nil];
}
if (data.length >= SIZE_MAX - kCCBlockSizeAES128) {
@throw [NSException exceptionWithName:CipherException reason:@"Oversize data." userInfo:nil];
}
if (key.length != 32) {
@throw [NSException exceptionWithName:CipherException reason:@"AES key should be 256 bits." userInfo:nil];
}
if (iv.length != 16) {
@throw [NSException exceptionWithName:CipherException reason:@"AES-CBC IV should be 128 bits." userInfo:nil];
}

size_t bufferSize = [data length] + kCCBlockSizeAES128;
NSMutableData *_Nullable bufferData = [NSMutableData dataWithLength:bufferSize];
if (!bufferData) {
@throw [NSException exceptionWithName:CipherException reason:@"Couldn't allocate buffer." userInfo:nil];
}
}

+(NSData*) decryptCBCMode:(NSData*)data withKey:(NSData*)key withIV:(NSData*)iv {
size_t bytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[key bytes],
[key length],
[iv bytes],
[data bytes],
[data length],
bufferData.mutableBytes,
bufferSize,
&bytesEncrypted);

size_t bufferSize = [data length] + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);

size_t bytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
[key bytes], [key length],
[iv bytes],
[data bytes], [data length],
buffer, bufferSize,
&bytesDecrypted);

if (cryptStatus == kCCSuccess) {
NSData *plaintext = [NSData dataWithBytes:buffer length:bytesDecrypted];
free(buffer);
return [bufferData subdataWithRange:NSMakeRange(0, bytesEncrypted)];
} else {
@throw [NSException exceptionWithName:CipherException
reason:@"We encountered an issue while encrypting."
userInfo:nil];
}
}

+ (NSData *)decryptCBCMode:(NSData *)data withKey:(NSData *)key withIV:(NSData *)iv
{
if (!data) {
@throw [NSException exceptionWithName:CipherException reason:@"Missing data to decrypt." userInfo:nil];
}
if (data.length >= SIZE_MAX - kCCBlockSizeAES128) {
@throw [NSException exceptionWithName:CipherException reason:@"Oversize data." userInfo:nil];
}
if (key.length != 32) {
@throw [NSException exceptionWithName:CipherException reason:@"AES key should be 256 bits." userInfo:nil];
}
if (iv.length != 16) {
@throw [NSException exceptionWithName:CipherException reason:@"AES-CBC IV should be 128 bits." userInfo:nil];
}

size_t bufferSize = [data length] + kCCBlockSizeAES128;
NSMutableData *_Nullable bufferData = [NSMutableData dataWithLength:bufferSize];
if (!bufferData) {
@throw [NSException exceptionWithName:CipherException reason:@"Couldn't allocate buffer." userInfo:nil];
}

return plaintext;
} else{
free(buffer);
@throw [NSException exceptionWithName:CipherException reason:@"We encountered an issue while decrypting." userInfo:nil];
size_t bytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
[key bytes],
[key length],
[iv bytes],
[data bytes],
[data length],
bufferData.mutableBytes,
bufferSize,
&bytesDecrypted);

if (cryptStatus == kCCSuccess) {
return [bufferData subdataWithRange:NSMakeRange(0, bytesDecrypted)];
} else {
@throw [NSException exceptionWithName:CipherException
reason:@"We encountered an issue while decrypting."
userInfo:nil];
}
}

@end

NS_ASSUME_NONNULL_END
16 changes: 9 additions & 7 deletions AxolotlKit/AxolotlKit/Classes/Ratchet/ChainKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
#import "MessageKeys.h"
#import <Foundation/Foundation.h>

@interface ChainKey : NSObject <NSSecureCoding>
NS_ASSUME_NONNULL_BEGIN

-(instancetype)initWithData:(NSData*)chainKey index:(int)index;
@interface ChainKey : NSObject <NSSecureCoding>

-(instancetype)nextChainKey;
@property (nonatomic, readonly) int index;
@property (nonatomic, readonly) NSData *key;

-(MessageKeys*)messageKeys;
- (instancetype)initWithData:(NSData *)chainKey index:(int)index;

-(NSData*)baseMaterial:(NSData*)seed;
- (instancetype)nextChainKey;

@property (readonly) int index;
@property (readonly) NSData *key;
- (MessageKeys *)messageKeys;

@end

NS_ASSUME_NONNULL_END
85 changes: 58 additions & 27 deletions AxolotlKit/AxolotlKit/Classes/Ratchet/ChainKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,102 @@

#import "ChainKey.h"
#import "TSDerivedSecrets.h"
#import <Curve25519Kit/Curve25519.h>
#import <CommonCrypto/CommonCrypto.h>
#import <Curve25519Kit/Curve25519.h>

NS_ASSUME_NONNULL_BEGIN

@implementation ChainKey

static NSString* const kCoderKey = @"kCoderKey";
static NSString* const kCoderIndex = @"kCoderIndex";
static NSString *const kCoderKey = @"kCoderKey";
static NSString *const kCoderIndex = @"kCoderIndex";

#define kTSKeySeedLength 1

static uint8_t kMessageKeySeed[kTSKeySeedLength] = {01};
static uint8_t kChainKeySeed[kTSKeySeedLength] = {02};
static uint8_t kMessageKeySeed[kTSKeySeedLength] = { 01 };
static uint8_t kChainKeySeed[kTSKeySeedLength] = { 02 };

+ (BOOL)supportsSecureCoding{
+ (BOOL)supportsSecureCoding
{
return YES;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super init];

- (nullable id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];

if (self) {
_key = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderKey];
_key = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderKey];
_index = [aDecoder decodeIntForKey:kCoderIndex];
}

return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_key forKey:kCoderKey];
[aCoder encodeInt:_index forKey:kCoderIndex];
[aCoder encodeInt:_index forKey:kCoderIndex];
}

-(instancetype)initWithData:(NSData *)chainKey index:(int)index{
- (instancetype)initWithData:(NSData *)chainKey index:(int)index
{
SPKAssert(chainKey.length == ECCKeyLength);

self = [super init];

if (self) {
_key = chainKey;
_key = chainKey;
_index = index;
}

return self;
}

- (instancetype) nextChainKey{
NSData* nextCK = [self baseMaterial:[NSData dataWithBytes:kChainKeySeed length:kTSKeySeedLength]];

return [[ChainKey alloc] initWithData:nextCK index:self.index+1];
- (instancetype)nextChainKey
{
NSData *nextCK = [self baseMaterial:[NSData dataWithBytes:kChainKeySeed length:kTSKeySeedLength]];

return [[ChainKey alloc] initWithData:nextCK index:self.index + 1];
}

- (MessageKeys*)messageKeys{
- (MessageKeys *)messageKeys
{
NSData *inputKeyMaterial = [self baseMaterial:[NSData dataWithBytes:kMessageKeySeed length:kTSKeySeedLength]];
TSDerivedSecrets *derivedSecrets = [TSDerivedSecrets derivedMessageKeysWithData:inputKeyMaterial];
return [[MessageKeys alloc] initWithCipherKey:derivedSecrets.cipherKey macKey:derivedSecrets.macKey iv:derivedSecrets.iv index:self.index];
return [[MessageKeys alloc] initWithCipherKey:derivedSecrets.cipherKey
macKey:derivedSecrets.macKey
iv:derivedSecrets.iv
index:self.index];
}

- (NSData*)baseMaterial:(NSData*)seed{
uint8_t result[CC_SHA256_DIGEST_LENGTH] = {0};
- (NSData *)baseMaterial:(NSData *)seed
{
if (!self.key) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Missing key." userInfo:nil];
}
if (self.key.length >= SIZE_MAX) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Oversize key." userInfo:nil];
}
if (!seed) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Missing seed." userInfo:nil];
}
if (seed.length >= SIZE_MAX) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Oversize seed." userInfo:nil];
}

NSMutableData *_Nullable bufferData = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
if (!bufferData) {
@throw [NSException exceptionWithName:NSGenericException reason:@"Couldn't allocate buffer." userInfo:nil];
}

CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA256, [self.key bytes], [self.key length]);
CCHmacUpdate(&ctx, [seed bytes], [seed length]);
CCHmacFinal(&ctx, result);
return [NSData dataWithBytes:result length:sizeof(result)];
CCHmacFinal(&ctx, bufferData.mutableBytes);
return [bufferData copy];
}

@end

NS_ASSUME_NONNULL_END
10 changes: 9 additions & 1 deletion AxolotlKit/AxolotlKit/Classes/Utility/SerializationUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

#define MAC_LENGTH 8

@interface SerializationUtilities : NSObject
Expand All @@ -14,6 +16,12 @@

+ (Byte)intsToByteHigh:(int)highValue low:(int)lowValue;

+ (NSData*)macWithVersion:(int)version identityKey:(NSData*)senderIdentityKey receiverIdentityKey:(NSData*)receiverIdentityKey macKey:(NSData*)macKey serialized:(NSData*)serialized;
+ (NSData *)macWithVersion:(int)version
identityKey:(NSData *)senderIdentityKey
receiverIdentityKey:(NSData *)receiverIdentityKey
macKey:(NSData *)macKey
serialized:(NSData *)serialized;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 716524e

Please sign in to comment.