Skip to content

Commit

Permalink
Password vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
rnapier committed Dec 22, 2013
1 parent fa1df71 commit e14402a
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 44 deletions.
18 changes: 18 additions & 0 deletions RNCryptor/RNEncryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
HMACKey:(NSData *)HMACKey
handler:(RNCryptorHandler)handler;


- (RNEncryptor *)initWithSettings:(RNCryptorSettings)settings
password:(NSString *)password
handler:(RNCryptorHandler)handler;
Expand All @@ -44,6 +45,15 @@
IV:(NSData *)anIV
handler:(RNCryptorHandler)aHandler;

// This form with manual IV and salts is generally only used for testing
- (RNEncryptor *)initWithSettings:(RNCryptorSettings)settings
password:(NSString *)password
IV:(NSData *)anIV
encryptionSalt:(NSData *)anEncryptionSalt
HMACSalt:(NSData *)anHMACSalt
handler:(RNCryptorHandler)handler;


+ (NSData *)encryptData:(NSData *)data withSettings:(RNCryptorSettings)settings password:(NSString *)password error:(NSError **)error;
+ (NSData *)encryptData:(NSData *)data withSettings:(RNCryptorSettings)settings encryptionKey:(NSData *)encryptionKey HMACKey:(NSData *)HMACKey error:(NSError **)error;

Expand All @@ -55,5 +65,13 @@
IV:(NSData *)anIV
error:(NSError **)anError;

// This form with manual IV and salts is generally only used for testing
+ (NSData *)encryptData:(NSData *)data
withSettings:(RNCryptorSettings)settings
password:(NSString *)password
IV:(NSData *)anIV
encryptionSalt:(NSData *)anEncryptionSalt
HMACSalt:(NSData *)anHMACSalt
error:(NSError **)error;

@end
51 changes: 42 additions & 9 deletions RNCryptor/RNEncryptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ + (NSData *)encryptData:(NSData *)thePlaintext withSettings:(RNCryptorSettings)t
return [self synchronousResultForCryptor:cryptor data:thePlaintext error:anError];
}

+ (NSData *)encryptData:(NSData *)thePlaintext
withSettings:(RNCryptorSettings)theSettings
password:(NSString *)aPassword
IV:(NSData *)anIV
encryptionSalt:(NSData *)anEncryptionSalt
HMACSalt:(NSData *)anHMACSalt
error:(NSError **)anError
{
RNEncryptor *cryptor = [[self alloc] initWithSettings:theSettings
password:aPassword
IV:anIV
encryptionSalt:anEncryptionSalt
HMACSalt:anHMACSalt
handler:^(RNCryptor *c, NSData *d) {}];
return [self synchronousResultForCryptor:cryptor data:thePlaintext error:anError];
}

+ (NSData *)encryptData:(NSData *)thePlaintext withSettings:(RNCryptorSettings)theSettings encryptionKey:(NSData *)anEncryptionKey HMACKey:(NSData *)anHMACKey error:(NSError **)anError {
RNEncryptor *cryptor = [[self alloc] initWithSettings:theSettings
encryptionKey:anEncryptionKey
Expand Down Expand Up @@ -120,24 +137,40 @@ - (RNEncryptor *)initWithSettings:(RNCryptorSettings)theSettings
return self;
}

- (RNEncryptor *)initWithSettings:(RNCryptorSettings)theSettings password:(NSString *)aPassword handler:(RNCryptorHandler)aHandler
{
NSParameterAssert(aPassword != nil);
- (RNEncryptor *)initWithSettings:(RNCryptorSettings)theSettings password:(NSString *)aPassword handler:(RNCryptorHandler)aHandler {
return [self initWithSettings:theSettings
password:aPassword
IV:[[self class] randomDataOfLength:theSettings.IVSize]
encryptionSalt:[[self class] randomDataOfLength:theSettings.keySettings.saltSize]
HMACSalt:[[self class] randomDataOfLength:theSettings.HMACKeySettings.saltSize]
handler:aHandler];
}

NSData *encryptionSalt = [[self class] randomDataOfLength:theSettings.keySettings.saltSize];
NSData *encryptionKey = [[self class] keyForPassword:aPassword salt:encryptionSalt settings:theSettings.keySettings];

NSData *HMACSalt = [[self class] randomDataOfLength:theSettings.HMACKeySettings.saltSize];
NSData *HMACKey = [[self class] keyForPassword:aPassword salt:HMACSalt settings:theSettings.HMACKeySettings];
- (RNEncryptor *)initWithSettings:(RNCryptorSettings)theSettings
password:(NSString *)aPassword
IV:(NSData *)anIV
encryptionSalt:(NSData *)anEncryptionSalt
HMACSalt:(NSData *)anHMACSalt
handler:(RNCryptorHandler)aHandler;
{
NSParameterAssert(aPassword);
NSParameterAssert(anIV);
NSParameterAssert(anEncryptionSalt);
NSParameterAssert(anHMACSalt);

NSData *encryptionKey = [[self class] keyForPassword:aPassword salt:anEncryptionSalt settings:theSettings.keySettings];
NSData *HMACKey = [[self class] keyForPassword:aPassword salt:anHMACSalt settings:theSettings.HMACKeySettings];

self = [self initWithSettings:theSettings
encryptionKey:encryptionKey
HMACKey:HMACKey
IV:anIV
handler:aHandler];
if (self) {
self.options |= kRNCryptorOptionHasPassword;
self.encryptionSalt = encryptionSalt;
self.HMACSalt = HMACSalt;
self.encryptionSalt = anEncryptionSalt;
self.HMACSalt = anHMACSalt;
}
return self;
}
Expand Down
54 changes: 53 additions & 1 deletion RNCryptorVectors/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void VerifyKeyVector(NSDictionary *vector) {
if (! cipherText || ! [cipherText isEqual:GetDataForHex(vector[@"ciphertext"])]) {
printf("Failed encrypting test (v%d): %s\n", [vector[@"version"] intValue], [vector[@"title"] UTF8String]);
printf("Error: %s\n", [[error description] UTF8String]);
printf("Expected: %s\n", [vector[@"plaintext"] UTF8String]);
printf("Expected: %s\n", [vector[@"ciphertext"] UTF8String]);
printf("Found: %s\n", [[cipherText description] UTF8String]);
abort();
}
Expand All @@ -164,6 +164,55 @@ void VerifyKeyVectors(NSArray *vectors) {
}
}

void VerifyPasswordVector(NSDictionary *vector) {
NSCParameterAssert(vector[@"title"]);
NSCParameterAssert(vector[@"version"]);
NSCParameterAssert(vector[@"password"]);
NSCParameterAssert(vector[@"iv"]);
NSCParameterAssert(vector[@"enc_salt"]);
NSCParameterAssert(vector[@"hmac_salt"]);
NSCParameterAssert(vector[@"plaintext"]);
NSCParameterAssert(vector[@"ciphertext"]);

NSError *error;

if ([vector[@"version"] intValue] == kRNCryptorFileVersion) {
NSData *cipherText = [RNEncryptor encryptData:GetDataForHex(vector[@"plaintext"])
withSettings:kRNCryptorAES256Settings
password:vector[@"password"]
IV:GetDataForHex(vector[@"iv"])
encryptionSalt:GetDataForHex(vector[@"enc_salt"])
HMACSalt:GetDataForHex(vector[@"hmac_salt"])
error:&error];
if (! cipherText || ! [cipherText isEqual:GetDataForHex(vector[@"ciphertext"])]) {
printf("Failed encrypting test (v%d): %s\n", [vector[@"version"] intValue], [vector[@"title"] UTF8String]);
printf("Error: %s\n", [[error description] UTF8String]);
printf("Expected: %s\n", [vector[@"ciphertext"] UTF8String]);
printf("Found: %s\n", [[cipherText description] UTF8String]);
abort();
}
}

NSData *plaintext = [RNDecryptor decryptData:GetDataForHex(vector[@"ciphertext"])
withPassword:vector[@"password"]
error:&error];

if (! plaintext || ! [plaintext isEqual:GetDataForHex(vector[@"plaintext"])]) {
printf("Failed decrypting test: (v%d) %s \n", [vector[@"version"] intValue], [vector[@"title"] UTF8String]);
printf("Error: %s\n", [[error description] UTF8String]);
printf("Expected: %s\n", [vector[@"plaintext"] UTF8String]);
printf("Found: %s\n", [[plaintext description] UTF8String]);
abort();
}
}


void VerifyPasswordVectors(NSArray *vectors) {
for (NSDictionary *vector in vectors) {
VerifyPasswordVector(vector);
}
}

int main(int argc, const char * argv[])
{
@autoreleasepool {
Expand All @@ -179,6 +228,9 @@ int main(int argc, const char * argv[])
GetVectorsFromPath([vectorPath stringByAppendingPathComponent:@"key"])
);

VerifyPasswordVectors(GetVectorsFromPath([vectorPath stringByAppendingPathComponent:@"password"]));


}
return 0;
}
Expand Down
54 changes: 27 additions & 27 deletions vectors/key
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@
# Version 3 vectors
#

title: All fields empty or zero
version: 3
enc_key: 00000000000000000000000000000000
hmac_key: 00000000000000000000000000000000
iv: 00000000000000000000000000000000
title: All fields empty or zero
version: 3
enc_key: 00000000000000000000000000000000
hmac_key: 00000000000000000000000000000000
iv: 00000000000000000000000000000000
plaintext:
ciphertext: 03000000 00000000 00000000 00000000 00000143 db63ee66 b0cdff9f 69917680 151e0e67 e6f5aea8 30ced4af ef779fe7 e5b3767e b06ea81a 0bb8a7a0 bf62c6b0 0405
ciphertext: 03000000 00000000 00000000 00000000 00000143 db63ee66 b0cdff9f 69917680 151e0e67 e6f5aea8 30ced4af ef779fe7 e5b3767e b06ea81a 0bb8a7a0 bf62c6b0 0405

title: One byte
version: 3
enc_key: 000102030405060708090a0b0c0d0e0f
hmac_key: 0102030405060708090a0b0c0d0e0f00
iv: 02030405060708090a0b0c0d0e0f0001
plaintext: 01
ciphertext: 03000203 04050607 08090a0b 0c0d0e0f 000198dc 7e36e7cc cb0cb7e8 2b048c46 0825ecd5 4ad9b093 3b236b74 8a1ce455 ee1ec4e9 3043f60b e2ed50dc cfb3c4b2 383c
title: One byte
version: 3
enc_key: 000102030405060708090a0b0c0d0e0f
hmac_key: 0102030405060708090a0b0c0d0e0f00
iv: 02030405060708090a0b0c0d0e0f0001
plaintext: 01
ciphertext: 03000203 04050607 08090a0b 0c0d0e0f 000198dc 7e36e7cc cb0cb7e8 2b048c46 0825ecd5 4ad9b093 3b236b74 8a1ce455 ee1ec4e9 3043f60b e2ed50dc cfb3c4b2 383c

title: Exactly one block
version: 3
enc_key: 0102030405060708090a0b0c0d0e0f00
hmac_key: 02030405060708090a0b0c0d0e0f0001
iv: 030405060708090a0b0c0d0e0f000102
plaintext: 000102030405060708090a0b0c0d0e0f
ciphertext: 03000304 05060708 090a0b0c 0d0e0f00 01029228 f6538960 defc04a2 be30eee6 665ea738 f6c2f3fa 2b73c2ed bbe3a0d5 7f59d197 45313f9e a7ede5bb 6b1bd56f 2ff331dd d22f25dc 99bc11f3 d7ebbf49 14bc
title: Exactly one block
version: 3
enc_key: 0102030405060708090a0b0c0d0e0f00
hmac_key: 02030405060708090a0b0c0d0e0f0001
iv: 030405060708090a0b0c0d0e0f000102
plaintext: 000102030405060708090a0b0c0d0e0f
ciphertext: 03000304 05060708 090a0b0c 0d0e0f00 01029228 f6538960 defc04a2 be30eee6 665ea738 f6c2f3fa 2b73c2ed bbe3a0d5 7f59d197 45313f9e a7ede5bb 6b1bd56f 2ff331dd d22f25dc 99bc11f3 d7ebbf49 14bc

title: More than one block
version: 3
enc_key: 02030405060708090a0b0c0d0e0f0001
hmac_key: 030405060708090a0b0c0d0e0f000102
iv: 0405060708090a0b0c0d0e0f00010203
plaintext: 000102030405060708090a0b0c0d0e0f 000102030405060708
ciphertext: 03000405 06070809 0a0b0c0d 0e0f0001 0203a7c3 b4598b47 45fb62fb 266a54ee c7dcddc9 73d5ecb8 93586198 5407d656 2314d01f d9cddf52 859611d6 e917b6e2 40f82aa5 a508ddd8 8960df8b ceea3aeb e9de
title: More than one block
version: 3
enc_key: 02030405060708090a0b0c0d0e0f0001
hmac_key: 030405060708090a0b0c0d0e0f000102
iv: 0405060708090a0b0c0d0e0f00010203
plaintext: 000102030405060708090a0b0c0d0e0f 000102030405060708
ciphertext: 03000405 06070809 0a0b0c0d 0e0f0001 0203a7c3 b4598b47 45fb62fb 266a54ee c7dcddc9 73d5ecb8 93586198 5407d656 2314d01f d9cddf52 859611d6 e917b6e2 40f82aa5 a508ddd8 8960df8b ceea3aeb e9de
61 changes: 54 additions & 7 deletions vectors/password
Original file line number Diff line number Diff line change
@@ -1,16 +1,63 @@
# Test vectors for password-based encryption
# title: trimmed-string
# version: integer (version of format)
# password: hex-string (any length)
# enc_salt: hex-string (length=keySettings.saltSize; usually 8)
# hmac_salt: hex-string (length=HMACKeySettings.saltSize; usually 8)
# iv: hex-string (length=16 bytes)
# enc_salt: hex-string (length=keySettings.saltSize)
# hmac_salt: hex-string (length=HMACKeySettings.saltSize)
# plaintext: hex-string (any length)
# ciphertext: hex-string (any length)

title: All fields empty or zero
title: All fields empty or zero
version: 3
password:
iv: 00000000000000000000000000000000
enc_salt: 00000000000000000000000000000000
hmac_salt: 00000000000000000000000000000000
enc_salt: 0000000000000000
hmac_salt: 0000000000000000
iv: 00000000000000000000000000000000
plaintext:
ciphertext:
ciphertext: 03010000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000e73c 3a20a905 8bbd1622 2b2d52f3 94ebe0f2 90c2290c 3b4ce61e e9b7b73f 593ef584 c5053156 1822bf00 567c2838 f546

title: One byte
version: 3
password: thepassword
enc_salt: 0001020304050607
hmac_salt: 0102030405060708
iv: 02030405060708090a0b0c0d0e0f0001
plaintext: a
ciphertext: 03010001 02030405 06070102 03040506 07080203 04050607 08090a0b 0c0d0e0f 0001de17 cb07d089 c061385c 20fd3d47 74c717ba fac9d70f ce79f56a 6f65c1a7 cd790b15 6b8aef33 6227a442 3ce79ae5 abce

title: Exactly one block
version: 3
password: thepassword
enc_salt: 0102030405060700
hmac_salt: 0203040506070801
iv: 030405060708090a0b0c0d0e0f000102
plaintext: 0123456789abcdef
ciphertext: 03010102 03040506 07000203 04050607 08010304 05060708 090a0b0c 0d0e0f00 01020e43 7fe80930 9c03fd53 a475131e 9a1978b8 eaef576f 60adb8ce 2320849b a32d7429 00438ba8 97d22210 c76c35c8 49df

title: More than one block
version: 3
password: thepassword
enc_salt: 0203040506070001
hmac_salt: 0304050607080102
iv: 0405060708090a0b0c0d0e0f00010203
plaintext: 0123456789abcdef 01234567
ciphertext: 03010203 04050607 00010304 05060708 01020405 06070809 0a0b0c0d 0e0f0001 0203e01b bda5df2c a8adace3 8f6c588d 291e03f9 51b78d34 17bc2816 581dc6b7 67f1a2e5 7597512b 18e1638f 21235fa5 928c

title: Multibyte password and text
version: 3
password: 中文密码
enc_salt: 0304050607000102
hmac_salt: 0405060708010203
iv: 05060708090a0b0c0d0e0f0001020304
plaintext: 一点中文。
ciphertext: 03010304 05060700 01020405 06070801 02030506 0708090a 0b0c0d0e 0f000102 030407f1 a50eef3f 48ad4638 56d79751 e604d107 e6e3cf01 01f29287 67083c95 16d94982 d9fdd5a8 1b2b369c da9d781a 43a3

title: Longer text and password
version: 3
password: It was the best of times, it was the worst of times; it was the age of wisdom, it was the age of foolishness;
enc_salt: 0405060700010203
hmac_salt: 0506070801020304
iv: 060708090a0b0c0d0e0f000102030405
plaintext: it was the epoch of belief, it was the epoch of incredulity; it was the season of Light, it was the season of Darkness; it was the spring of hope, it was the winter of despair; we had everything before us, we had nothing before us; we were all going directly to Heaven, we were all going the other way.
ciphertext: 03010405 06070001 02030506 07080102 03040607 08090a0b 0c0d0e0f 00010203 0405566a 6bfc6076 2ab72296 5a6a3d33 d1bb40e7 2728040d 057dc4c1 98a068de 6708e7ad 8c500232 1539a0eb a2c8fd96 aef950ae ccc647cf e54a82f9 1ad67014 94f6bc5d 701e3db6 92b04fec 47b792f0 9b76e8f5 8ba94f81 b44d54ba b3b30a00 4924af0b 651f72ef 083a3efd 768d7f34 a106a4a5 cfc7417a f1f37524 3162e90f 3f43df76 c9b9e784 c0126cea f4dcc60c c28bacce 1bbd9658 69ff334a 3f60e4a0 5924

0 comments on commit e14402a

Please sign in to comment.