2 * Copyright (c) 2016 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
[32] = {
25 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c,
26 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37,
27 0xa8, 0x65, 0x48, 0xc9, 0x2c, 0xcc, 0x35, 0x48,
28 0x08, 0x05, 0x98, 0x7c, 0xb7, 0x0b, 0xe1, 0x7b
31 const char resultSize
= sizeof(expected
);
33 uint8_t actual
[resultSize
];
35 is(pbkdf2_hmac_sha256((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-1");
37 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-1");
41 const char *password
= "password";
42 const char *salt
= "salt";
43 const int iterations
= 2;
44 const uint8_t expected
[32] = {
45 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
46 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0,
47 0x2a, 0x30, 0x3f, 0x8e, 0xf3, 0xc2, 0x51, 0xdf,
48 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43
51 const char resultSize
= sizeof(expected
);
53 uint8_t actual
[resultSize
];
55 is(pbkdf2_hmac_sha256((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-2");
57 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-2");
61 const char *password
= "password";
62 const char *salt
= "salt";
63 const int iterations
= 4096;
64 const uint8_t expected
[32] = {
65 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41,
66 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d,
67 0x96, 0x28, 0x93, 0xa0, 0x01, 0xce, 0x4e, 0x11,
68 0xa4, 0x96, 0x38, 0x73, 0xaa, 0x98, 0x13, 0x4a
71 const char resultSize
= sizeof(expected
);
73 uint8_t actual
[resultSize
];
75 is(pbkdf2_hmac_sha256((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-4096");
77 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-4096");
81 skip("16777216 iterations is too slow", 1, 0);
83 const char *password
= "password";
84 const char *salt
= "salt";
85 const int iterations
= 16777216;
86 const uint8_t expected
[32] = {
87 0xcf, 0x81, 0xc6, 0x6f, 0xe8, 0xcf, 0xc0, 0x4d,
88 0x1f, 0x31, 0xec, 0xb6, 0x5d, 0xab, 0x40, 0x89,
89 0xf7, 0xf1, 0x79, 0xe8, 0x9b, 0x3b, 0x0b, 0xcb,
90 0x17, 0xad, 0x10, 0xe3, 0xac, 0x6e, 0xba, 0x46
93 const char resultSize
= sizeof(expected
);
95 uint8_t actual
[resultSize
];
97 is(pbkdf2_hmac_sha256((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-16777216");
99 is(memcmp(expected
, actual
, resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-16777216");
104 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
105 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
107 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
108 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
110 const int iterations
= 1;
111 const uint8_t expected
[32] = {
112 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c,
113 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37,
114 0xa8, 0x65, 0x48, 0xc9, 0x2c, 0xcc, 0x35, 0x48,
115 0x08, 0x05, 0x98, 0x7c, 0xb7, 0x0b, 0xe1, 0x7b
118 const char resultSize
= sizeof(expected
);
120 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
121 CFDataIncreaseLength(resultData
, resultSize
);
123 is(SecKeyFromPassphraseDataHMACSHA256(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-1");
125 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-1");
127 CFReleaseSafe(password
);
129 CFReleaseSafe(passwordData
);
130 CFReleaseSafe(saltData
);
131 CFReleaseSafe(resultData
);
135 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
136 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
138 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
139 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
141 const int iterations
= 2;
142 const uint8_t expected
[32] = {
143 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
144 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0,
145 0x2a, 0x30, 0x3f, 0x8e, 0xf3, 0xc2, 0x51, 0xdf,
146 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43
149 const char resultSize
= sizeof(expected
);
151 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
152 CFDataIncreaseLength(resultData
, resultSize
);
154 is(SecKeyFromPassphraseDataHMACSHA256(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-2");
156 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-2");
158 CFReleaseSafe(password
);
160 CFReleaseSafe(passwordData
);
161 CFReleaseSafe(saltData
);
162 CFReleaseSafe(resultData
);
166 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
167 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
169 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
170 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
172 const int iterations
= 4096;
173 const uint8_t expected
[32] = {
174 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41,
175 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d,
176 0x96, 0x28, 0x93, 0xa0, 0x01, 0xce, 0x4e, 0x11,
177 0xa4, 0x96, 0x38, 0x73, 0xaa, 0x98, 0x13, 0x4a
180 const char resultSize
= sizeof(expected
);
182 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
183 CFDataIncreaseLength(resultData
, resultSize
);
185 is(SecKeyFromPassphraseDataHMACSHA256(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
, "pbkdf-sha-256: Failed Key Derivation I-4096");
187 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-4096");
189 CFReleaseSafe(password
);
191 CFReleaseSafe(passwordData
);
192 CFReleaseSafe(saltData
);
193 CFReleaseSafe(resultData
);
197 skip("16777216 iterations is too slow", 2, 0);
199 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
200 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
202 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
203 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
205 const int iterations
= 16777216;
206 const uint8_t expected
[32] = {
207 0xcf, 0x81, 0xc6, 0x6f, 0xe8, 0xcf, 0xc0, 0x4d,
208 0x1f, 0x31, 0xec, 0xb6, 0x5d, 0xab, 0x40, 0x89,
209 0xf7, 0xf1, 0x79, 0xe8, 0x9b, 0x3b, 0x0b, 0xcb,
210 0x17, 0xad, 0x10, 0xe3, 0xac, 0x6e, 0xba, 0x46
214 const char resultSize
= sizeof(expected
);
216 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
217 CFDataIncreaseLength(resultData
, resultSize
);
219 is(SecKeyFromPassphraseDataHMACSHA256(passwordData
, saltData
, iterations
, resultData
), errSecSuccess
,
220 "pbkdf-sha-256: P-'password' S-'salt' I-16777216");
222 is(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
), 0, "pbkdf-sha-256: P-'password' S-'salt' I-16777216");
224 CFReleaseSafe(password
);
226 CFReleaseSafe(passwordData
);
227 CFReleaseSafe(saltData
);
228 CFReleaseSafe(resultData
);
233 int spbkdf_01_hmac_sha256(int argc
, char *const *argv
)
235 plan_tests(kTestTestCount
);