2014-05-06 19:41:08 +02:00
|
|
|
#import "CryptoTools.h"
|
|
|
|
|
|
|
|
#import "Constraints.h"
|
|
|
|
#import "Conversions.h"
|
|
|
|
#import "EvpMessageDigest.h"
|
|
|
|
#import "EvpSymetricUtil.h"
|
|
|
|
#import "Util.h"
|
|
|
|
|
|
|
|
@implementation CryptoTools
|
|
|
|
|
|
|
|
+(NSData*)generateSecureRandomData:(NSUInteger)length {
|
|
|
|
NSMutableData* d = [NSMutableData dataWithLength:length];
|
2015-07-10 08:53:07 +02:00
|
|
|
int err = SecRandomCopyBytes(kSecRandomDefault, length, [d mutableBytes]);
|
|
|
|
if (err != 0) {
|
|
|
|
[SecurityFailure raise:@"SecRandomCopyBytes failed"];
|
|
|
|
}
|
2014-05-06 19:41:08 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(uint16_t)generateSecureRandomUInt16 {
|
|
|
|
return [[self generateSecureRandomData:sizeof(uint16_t)] bigEndianUInt16At:0];
|
|
|
|
}
|
|
|
|
|
2014-09-07 07:58:14 +02:00
|
|
|
+(uint32_t)generateSecureRandomUInt32 {
|
|
|
|
return [[self generateSecureRandomData:sizeof(uint32_t)] bigEndianUInt32At:0];
|
|
|
|
}
|
|
|
|
|
2014-05-06 19:41:08 +02:00
|
|
|
+(NSString*) computeOtpWithPassword:(NSString*)password andCounter:(int64_t)counter {
|
|
|
|
require(password != nil);
|
|
|
|
|
2014-08-13 02:02:29 +02:00
|
|
|
NSData* d = [[@(counter) stringValue] encodedAsUtf8];
|
2014-09-07 20:43:53 +02:00
|
|
|
NSData* h = [d hmacWithSha1WithKey:password.encodedAsUtf8];
|
|
|
|
return h.encodedAsBase64;
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-07-30 02:33:38 +02:00
|
|
|
@implementation NSData (CryptoTools)
|
2014-05-06 19:41:08 +02:00
|
|
|
|
|
|
|
-(NSData*)hmacWithSha1WithKey:(NSData*)key {
|
|
|
|
return [EvpMessageDigest hmacUsingSha1Data:self withKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSData*)hmacWithSha256WithKey:(NSData*)key {
|
|
|
|
return [EvpMessageDigest hmacUsingSha256Data:self withKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSData*)encryptWithAesInCipherFeedbackModeWithKey:(NSData*)key andIv:(NSData*)iv {
|
|
|
|
return [EvpSymetricUtil encryptMessage:self usingAes128WithCfbAndKey:key andIv:iv];
|
|
|
|
}
|
|
|
|
-(NSData*)encryptWithAesInCipherBlockChainingModeWithPkcs7PaddingWithKey:(NSData*)key andIv:(NSData*)iv {
|
|
|
|
return [EvpSymetricUtil encryptMessage:self usingAes128WithCbcAndPaddingAndKey:key andIv:iv];
|
|
|
|
}
|
|
|
|
-(NSData*)encryptWithAesInCounterModeWithKey:(NSData*)key andIv:(NSData*)iv {
|
|
|
|
return [EvpSymetricUtil encryptMessage:self usingAes128InCounterModeAndKey:key andIv:iv];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSData*)decryptWithAesInCipherFeedbackModeWithKey:(NSData*)key andIv:(NSData*)iv {
|
|
|
|
return [EvpSymetricUtil decryptMessage:self usingAes128WithCfbAndKey:key andIv:iv];
|
|
|
|
}
|
|
|
|
-(NSData*)decryptWithAesInCipherBlockChainingModeWithPkcs7PaddingWithKey:(NSData*)key andIv:(NSData*)iv {
|
|
|
|
return [EvpSymetricUtil decryptMessage:self usingAes128WithCbcAndPaddingAndKey:key andIv:iv];
|
|
|
|
}
|
|
|
|
-(NSData*)decryptWithAesInCounterModeWithKey:(NSData*)key andIv:(NSData*)iv {
|
|
|
|
return [EvpSymetricUtil decryptMessage:self usingAes128InCounterModeAndKey:key andIv:iv];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSData*)hashWithSha256 {
|
|
|
|
return [EvpMessageDigest hashWithSha256:self];
|
|
|
|
}
|
|
|
|
-(bool)isEqualToData_TimingSafe:(NSData*)other {
|
|
|
|
if (other == nil) return false;
|
2014-08-14 03:13:24 +02:00
|
|
|
NSUInteger n = self.length;
|
|
|
|
if (other.length != n) return false;
|
2014-05-06 19:41:08 +02:00
|
|
|
bool equal = true;
|
|
|
|
for (NSUInteger i = 0; i < n; i++)
|
|
|
|
equal &= [self uint8At:i] == [other uint8At:i];
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
@end
|