2 * Copyright (c) 2010,2012 Apple Inc. All Rights Reserved.
5 #include <CoreFoundation/CoreFoundation.h>
6 #include <Security/SecInternal.h>
7 #include <Security/SecItem.h>
8 #include <Security/SecBase.h>
9 #include <CommonCrypto/CommonHMAC.h>
12 #include <Security/SecPBKDF.h>
14 #include "Security_regressions.h"
16 static int kTestTestCount
= 16;
18 static void tests(void)
21 const char *password
= "password";
22 const char *salt
= "salt";
23 const int iterations
= 1;
24 const uint8_t expected
[20] = { 0x0c, 0x60, 0xc8, 0x0f,
25 0x96, 0x1f, 0x0e, 0x71,
26 0xf3, 0xa9, 0xb5, 0x24,
27 0xaf, 0x60, 0x12, 0x06,
28 0x2f, 0xe0, 0x37, 0xa6 };
30 const char resultSize
= sizeof(expected
);
32 uint8_t actual
[resultSize
];
34 is(pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-1");
36 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-1");
40 const char *password
= "password";
41 const char *salt
= "salt";
42 const int iterations
= 2;
43 const uint8_t expected
[20] = { 0xea, 0x6c, 0x01, 0x4d,
44 0xc7, 0x2d, 0x6f, 0x8c,
45 0xcd, 0x1e, 0xd9, 0x2a,
46 0xce, 0x1d, 0x41, 0xf0,
47 0xd8, 0xde, 0x89, 0x57 };
49 const char resultSize
= sizeof(expected
);
51 uint8_t actual
[resultSize
];
53 is(pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-2");
55 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-2");
59 const char *password
= "password";
60 const char *salt
= "salt";
61 const int iterations
= 4096;
62 const uint8_t expected
[20] = { 0x4b, 0x00, 0x79, 0x01,
63 0xb7, 0x65, 0x48, 0x9a,
64 0xbe, 0xad, 0x49, 0xd9,
65 0x26, 0xf7, 0x21, 0xd0,
66 0x65, 0xa4, 0x29, 0xc1 };
68 const char resultSize
= sizeof(expected
);
70 uint8_t actual
[resultSize
];
72 is(pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-4096");
74 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-4096");
78 skip("16777216 iterations is too slow", 1, 0);
80 const char *password
= "password";
81 const char *salt
= "salt";
82 const int iterations
= 16777216;
83 const uint8_t expected
[20] = { 0xee, 0xfe, 0x3d, 0x61,
84 0xcd, 0x4d, 0xa4, 0xe4,
85 0xe9, 0x94, 0x5b, 0x3d,
86 0x6b, 0xa2, 0x15, 0x8c,
87 0x26, 0x34, 0xe9, 0x84 };
89 const char resultSize
= sizeof(expected
);
91 uint8_t actual
[resultSize
];
93 is(pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-16777216");
95 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-16777216");
100 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
101 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
103 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
104 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
106 const int iterations
= 1;
107 const uint8_t expected
[20] = { 0x0c, 0x60, 0xc8, 0x0f,
108 0x96, 0x1f, 0x0e, 0x71,
109 0xf3, 0xa9, 0xb5, 0x24,
110 0xaf, 0x60, 0x12, 0x06,
111 0x2f, 0xe0, 0x37, 0xa6 };
113 const char resultSize
= sizeof(expected
);
115 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
116 CFDataIncreaseLength(resultData
, resultSize
);
118 is(SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-1");
120 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-1");
122 CFReleaseSafe(password
);
124 CFReleaseSafe(passwordData
);
125 CFReleaseSafe(saltData
);
126 CFReleaseSafe(resultData
);
130 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
131 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
133 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
134 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
136 const int iterations
= 2;
137 const uint8_t expected
[20] = { 0xea, 0x6c, 0x01, 0x4d,
138 0xc7, 0x2d, 0x6f, 0x8c,
139 0xcd, 0x1e, 0xd9, 0x2a,
140 0xce, 0x1d, 0x41, 0xf0,
141 0xd8, 0xde, 0x89, 0x57 };
143 const char resultSize
= sizeof(expected
);
145 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
146 CFDataIncreaseLength(resultData
, resultSize
);
148 is(SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-2");
150 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-2");
152 CFReleaseSafe(password
);
154 CFReleaseSafe(passwordData
);
155 CFReleaseSafe(saltData
);
156 CFReleaseSafe(resultData
);
160 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
161 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
163 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
164 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
166 const int iterations
= 4096;
167 const uint8_t expected
[20] = { 0x4b, 0x00, 0x79, 0x01,
168 0xb7, 0x65, 0x48, 0x9a,
169 0xbe, 0xad, 0x49, 0xd9,
170 0x26, 0xf7, 0x21, 0xd0,
171 0x65, 0xa4, 0x29, 0xc1 };
174 const char resultSize
= sizeof(expected
);
176 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
177 CFDataIncreaseLength(resultData
, resultSize
);
179 is(SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-4096");
181 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-4096");
183 CFReleaseSafe(password
);
185 CFReleaseSafe(passwordData
);
186 CFReleaseSafe(saltData
);
187 CFReleaseSafe(resultData
);
191 skip("16777216 iterations is too slow", 2, 0);
193 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
194 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
196 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
197 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
199 const int iterations
= 16777216;
200 const uint8_t expected
[20] = { 0xee, 0xfe, 0x3d, 0x61,
201 0xcd, 0x4d, 0xa4, 0xe4,
202 0xe9, 0x94, 0x5b, 0x3d,
203 0x6b, 0xa2, 0x15, 0x8c,
204 0x26, 0x34, 0xe9, 0x84 };
207 const char resultSize
= sizeof(expected
);
209 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
210 CFDataIncreaseLength(resultData
, resultSize
);
212 is(SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-1: Failed Key Derivation I-16777216");
214 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-1: P-'password' S-'salt' I-16777216");
216 CFReleaseSafe(password
);
218 CFReleaseSafe(passwordData
);
219 CFReleaseSafe(saltData
);
220 CFReleaseSafe(resultData
);
225 int spbkdf_00_hmac_sha1(int argc
, char *const *argv
)
227 plan_tests(kTestTestCount
);