2013-02-14 17:59:12 +04:00
|
|
|
/*
|
|
|
|
Password Encryptor
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-12-04 14:37:57 +04:00
|
|
|
Copyright 2013 Thincast Technologies GmbH, Author: Dorian Johnson
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
|
|
|
If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
|
|
http://mozilla.org/MPL/2.0/.
|
2013-02-14 17:59:12 +04:00
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* We try to use CommonCrypto as much as possible. PBKDF2 was added to CommonCrypto in iOS 5, so use
|
|
|
|
* OpenSSL only as a fallback to do PBKDF2 on pre iOS 5 systems. */
|
2013-02-14 17:59:12 +04:00
|
|
|
|
|
|
|
#import "Encryptor.h"
|
2019-11-06 17:24:51 +03:00
|
|
|
#import <CommonCrypto/CommonKeyDerivation.h>
|
|
|
|
#import <CommonCrypto/CommonCryptor.h>
|
|
|
|
#import <CommonCrypto/CommonDigest.h>
|
2013-02-14 17:59:12 +04:00
|
|
|
#import <openssl/evp.h> // For PBKDF2 on < 5.0
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
@interface Encryptor (Private)
|
2019-11-06 17:24:51 +03:00
|
|
|
- (NSData *)randomInitializationVector;
|
2013-02-14 17:59:12 +04:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Encryptor
|
2019-11-06 17:24:51 +03:00
|
|
|
@synthesize plaintextPassword = _plaintext_password;
|
2013-02-14 17:59:12 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (id)initWithPassword:(NSString *)plaintext_password
|
2013-02-14 17:59:12 +04:00
|
|
|
{
|
|
|
|
if (plaintext_password == nil)
|
|
|
|
return nil;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
_plaintext_password = [plaintext_password retain];
|
2019-11-06 17:24:51 +03:00
|
|
|
const char *plaintext_password_data =
|
|
|
|
[plaintext_password length] ? [plaintext_password UTF8String] : " ";
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
if (!plaintext_password_data || !strlen(plaintext_password_data))
|
2019-11-06 17:24:51 +03:00
|
|
|
[NSException raise:NSInternalInconsistencyException
|
|
|
|
format:@"%s: plaintext password data is zero length!", __func__];
|
|
|
|
|
|
|
|
uint8_t *derived_key = calloc(1, TSXEncryptorPBKDF2KeySize);
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
if (CCKeyDerivationPBKDF != NULL)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
int ret = CCKeyDerivationPBKDF(
|
|
|
|
kCCPBKDF2, plaintext_password_data, strlen(plaintext_password_data) - 1,
|
|
|
|
(const uint8_t *)TSXEncryptorPBKDF2Salt, TSXEncryptorPBKDF2SaltLen, kCCPRFHmacAlgSHA1,
|
|
|
|
TSXEncryptorPBKDF2Rounds, derived_key, TSXEncryptorPBKDF2KeySize);
|
|
|
|
// NSLog(@"CCKeyDerivationPBKDF ret = %d; key: %@", ret, [NSData
|
|
|
|
// dataWithBytesNoCopy:derived_key length:TWEncryptorPBKDF2KeySize freeWhenDone:NO]);
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
if (ret)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
NSLog(@"%s: CCKeyDerivationPBKDF ret == %d, indicating some sort of failure.", __func__,
|
|
|
|
ret);
|
|
|
|
free(derived_key);
|
2013-02-14 17:59:12 +04:00
|
|
|
[self autorelease];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// iOS 4.x or earlier -- use OpenSSL
|
2019-11-06 17:24:51 +03:00
|
|
|
unsigned long ret = PKCS5_PBKDF2_HMAC_SHA1(
|
|
|
|
plaintext_password_data, (int)strlen(plaintext_password_data) - 1,
|
|
|
|
(const unsigned char *)TSXEncryptorPBKDF2Salt, TSXEncryptorPBKDF2SaltLen,
|
|
|
|
TSXEncryptorPBKDF2Rounds, TSXEncryptorPBKDF2KeySize, derived_key);
|
|
|
|
// NSLog(@"PKCS5_PBKDF2_HMAC_SHA1 ret = %lu; key: %@", ret, [NSData
|
|
|
|
// dataWithBytesNoCopy:derived_key length:TWEncryptorPBKDF2KeySize freeWhenDone:NO]);
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
if (ret != 1)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
NSLog(@"%s: PKCS5_PBKDF2_HMAC_SHA1 ret == %lu, indicating some sort of failure.",
|
|
|
|
__func__, ret);
|
|
|
|
free(derived_key);
|
2013-02-14 17:59:12 +04:00
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_encryption_key = [[NSData alloc] initWithBytesNoCopy:derived_key
|
|
|
|
length:TSXEncryptorPBKDF2KeySize
|
|
|
|
freeWhenDone:YES];
|
2013-02-14 17:59:12 +04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Encrypting/Decrypting data
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (NSData *)encryptData:(NSData *)plaintext_data
|
2013-02-14 17:59:12 +04:00
|
|
|
{
|
|
|
|
if (![plaintext_data length])
|
|
|
|
return nil;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
NSData *iv = [self randomInitializationVector];
|
|
|
|
NSMutableData *encrypted_data = [NSMutableData
|
|
|
|
dataWithLength:[iv length] + [plaintext_data length] + TSXEncryptorBlockCipherBlockSize];
|
2013-02-14 17:59:12 +04:00
|
|
|
[encrypted_data replaceBytesInRange:NSMakeRange(0, [iv length]) withBytes:[iv bytes]];
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
size_t data_out_moved = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
int ret = CCCrypt(kCCEncrypt, TSXEncryptorBlockCipherAlgo, TSXEncryptorBlockCipherOptions,
|
|
|
|
[_encryption_key bytes], TSXEncryptorBlockCipherKeySize, [iv bytes],
|
|
|
|
[plaintext_data bytes], [plaintext_data length],
|
|
|
|
[encrypted_data mutableBytes] + [iv length],
|
|
|
|
[encrypted_data length] - [iv length], &data_out_moved);
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case kCCSuccess:
|
|
|
|
[encrypted_data setLength:[iv length] + data_out_moved];
|
|
|
|
return encrypted_data;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
default:
|
2019-11-06 17:24:51 +03:00
|
|
|
NSLog(
|
|
|
|
@"%s: uncaught error, ret CCCryptorStatus = %d (plaintext len = %lu; buffer size = "
|
|
|
|
@"%lu)",
|
|
|
|
__func__, ret, (unsigned long)[plaintext_data length],
|
|
|
|
(unsigned long)([encrypted_data length] - [iv length]));
|
2013-02-14 17:59:12 +04:00
|
|
|
return nil;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (NSData *)decryptData:(NSData *)encrypted_data
|
2013-02-14 17:59:12 +04:00
|
|
|
{
|
|
|
|
if ([encrypted_data length] <= TSXEncryptorBlockCipherBlockSize)
|
|
|
|
return nil;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
NSMutableData *plaintext_data =
|
|
|
|
[NSMutableData dataWithLength:[encrypted_data length] + TSXEncryptorBlockCipherBlockSize];
|
2013-02-14 17:59:12 +04:00
|
|
|
size_t data_out_moved = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
int ret =
|
|
|
|
CCCrypt(kCCDecrypt, TSXEncryptorBlockCipherAlgo, TSXEncryptorBlockCipherOptions,
|
|
|
|
[_encryption_key bytes], TSXEncryptorBlockCipherKeySize, [encrypted_data bytes],
|
|
|
|
[encrypted_data bytes] + TSXEncryptorBlockCipherBlockSize,
|
|
|
|
[encrypted_data length] - TSXEncryptorBlockCipherBlockSize,
|
|
|
|
[plaintext_data mutableBytes], [plaintext_data length], &data_out_moved);
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case kCCSuccess:
|
|
|
|
[plaintext_data setLength:data_out_moved];
|
|
|
|
return plaintext_data;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
case kCCBufferTooSmall: // Our output buffer is big enough to decrypt valid data. This
|
|
|
|
// return code indicates malformed data.
|
2013-02-14 17:59:12 +04:00
|
|
|
case kCCAlignmentError: // Shouldn't get this, since we're using padding.
|
2019-11-06 17:24:51 +03:00
|
|
|
case kCCDecodeError: // Wrong key.
|
2013-02-14 17:59:12 +04:00
|
|
|
return nil;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
default:
|
2019-11-06 17:24:51 +03:00
|
|
|
NSLog(@"%s: uncaught error, ret CCCryptorStatus = %d (encrypted data len = %lu; buffer "
|
|
|
|
@"size = %lu; dom = %lu)",
|
|
|
|
__func__, ret, (unsigned long)[encrypted_data length],
|
|
|
|
(unsigned long)[plaintext_data length], data_out_moved);
|
2013-02-14 17:59:12 +04:00
|
|
|
return nil;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (NSData *)encryptString:(NSString *)plaintext_string
|
2013-02-14 17:59:12 +04:00
|
|
|
{
|
|
|
|
return [self encryptData:[plaintext_string dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (NSString *)decryptString:(NSData *)encrypted_string
|
2013-02-14 17:59:12 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return [[[NSString alloc] initWithData:[self decryptData:encrypted_string]
|
|
|
|
encoding:NSUTF8StringEncoding] autorelease];
|
2013-02-14 17:59:12 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
- (NSData *)randomInitializationVector
|
2013-02-14 17:59:12 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
NSMutableData *iv = [NSMutableData dataWithLength:TSXEncryptorBlockCipherBlockSize];
|
2013-02-14 17:59:12 +04:00
|
|
|
int fd;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
2013-02-14 17:59:12 +04:00
|
|
|
return nil;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
NSInteger bytes_needed = [iv length];
|
2019-11-06 17:24:51 +03:00
|
|
|
char *p = [iv mutableBytes];
|
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
while (bytes_needed)
|
|
|
|
{
|
|
|
|
long bytes_read = read(fd, p, bytes_needed);
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
if (bytes_read < 0)
|
|
|
|
continue;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
p += bytes_read;
|
|
|
|
bytes_needed -= bytes_read;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2013-02-14 17:59:12 +04:00
|
|
|
close(fd);
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|