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"
18 printComparison(const uint8_t*left
, const uint8_t* right
, int length
)
21 for(i
= 0; i
< length
; ++i
)
23 fprintf(stderr
, "# Values :: 0x%02x :: 0x%02x\n", left
[i
], right
[i
]);
28 static int kTestTestCount
= 8;
30 static void tests(void)
33 const char *password
= "password";
34 const char *salt
= "salt";
35 const int iterations
= 1;
36 const uint8_t expected
[20] = { 0x0c, 0x60, 0xc8, 0x0f,
37 0x96, 0x1f, 0x0e, 0x71,
38 0xf3, 0xa9, 0xb5, 0x24,
39 0xaf, 0x60, 0x12, 0x06,
40 0x2f, 0xe0, 0x37, 0xa6 };
42 const char resultSize
= sizeof(expected
);
44 uint8_t actual
[resultSize
];
46 pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
);
48 ok(memcmp(expected
, actual
, resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
52 const char *password
= "password";
53 const char *salt
= "salt";
54 const int iterations
= 2;
55 const uint8_t expected
[20] = { 0xea, 0x6c, 0x01, 0x4d,
56 0xc7, 0x2d, 0x6f, 0x8c,
57 0xcd, 0x1e, 0xd9, 0x2a,
58 0xce, 0x1d, 0x41, 0xf0,
59 0xd8, 0xde, 0x89, 0x57 };
61 const char resultSize
= sizeof(expected
);
63 uint8_t actual
[resultSize
];
65 pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
);
67 ok(memcmp(expected
, actual
, resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-2");
71 const char *password
= "password";
72 const char *salt
= "salt";
73 const int iterations
= 4096;
74 const uint8_t expected
[20] = { 0x4b, 0x00, 0x79, 0x01,
75 0xb7, 0x65, 0x48, 0x9a,
76 0xbe, 0xad, 0x49, 0xd9,
77 0x26, 0xf7, 0x21, 0xd0,
78 0x65, 0xa4, 0x29, 0xc1 };
80 const char resultSize
= sizeof(expected
);
82 uint8_t actual
[resultSize
];
84 pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
);
86 ok(memcmp(expected
, actual
, resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-4096");
90 skip("16777216 iterations is too slow", 1, 0);
92 const char *password
= "password";
93 const char *salt
= "salt";
94 const int iterations
= 16777216;
95 const uint8_t expected
[20] = { 0xee, 0xfe, 0x3d, 0x61,
96 0xcd, 0x4d, 0xa4, 0xe4,
97 0xe9, 0x94, 0x5b, 0x3d,
98 0x6b, 0xa2, 0x15, 0x8c,
99 0x26, 0x34, 0xe9, 0x84 };
101 const char resultSize
= sizeof(expected
);
103 uint8_t actual
[resultSize
];
105 pbkdf2_hmac_sha1((const uint8_t*) password
, strlen(password
), (const uint8_t*) salt
, strlen(salt
), iterations
, actual
, resultSize
);
107 ok(memcmp(expected
, actual
, resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-16777216");
112 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
113 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
115 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
116 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
118 const int iterations
= 1;
119 const uint8_t expected
[20] = { 0x0c, 0x60, 0xc8, 0x0f,
120 0x96, 0x1f, 0x0e, 0x71,
121 0xf3, 0xa9, 0xb5, 0x24,
122 0xaf, 0x60, 0x12, 0x06,
123 0x2f, 0xe0, 0x37, 0xa6 };
125 const char resultSize
= sizeof(expected
);
127 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
128 CFDataIncreaseLength(resultData
, resultSize
);
130 SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
);
132 ok(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
134 CFReleaseSafe(password
);
136 CFReleaseSafe(passwordData
);
137 CFReleaseSafe(saltData
);
138 CFReleaseSafe(resultData
);
142 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
143 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
145 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
146 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
148 const int iterations
= 2;
149 const uint8_t expected
[20] = { 0xea, 0x6c, 0x01, 0x4d,
150 0xc7, 0x2d, 0x6f, 0x8c,
151 0xcd, 0x1e, 0xd9, 0x2a,
152 0xce, 0x1d, 0x41, 0xf0,
153 0xd8, 0xde, 0x89, 0x57 };
155 const char resultSize
= sizeof(expected
);
157 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
158 CFDataIncreaseLength(resultData
, resultSize
);
160 SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
);
162 ok(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
164 CFReleaseSafe(password
);
166 CFReleaseSafe(passwordData
);
167 CFReleaseSafe(saltData
);
168 CFReleaseSafe(resultData
);
172 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
173 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
175 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
176 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
178 const int iterations
= 4096;
179 const uint8_t expected
[20] = { 0x4b, 0x00, 0x79, 0x01,
180 0xb7, 0x65, 0x48, 0x9a,
181 0xbe, 0xad, 0x49, 0xd9,
182 0x26, 0xf7, 0x21, 0xd0,
183 0x65, 0xa4, 0x29, 0xc1 };
186 const char resultSize
= sizeof(expected
);
188 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
189 CFDataIncreaseLength(resultData
, resultSize
);
191 SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
);
193 ok(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
195 CFReleaseSafe(password
);
197 CFReleaseSafe(passwordData
);
198 CFReleaseSafe(saltData
);
199 CFReleaseSafe(resultData
);
203 skip("16777216 iterations is too slow", 1, 0);
205 CFStringRef password
= CFStringCreateWithCString(NULL
, "password", kCFStringEncodingUTF8
);
206 CFStringRef salt
= CFStringCreateWithCString(NULL
, "salt", kCFStringEncodingUTF8
);
208 CFDataRef passwordData
= CFStringCreateExternalRepresentation(NULL
, password
, kCFStringEncodingUTF8
, 0);
209 CFDataRef saltData
= CFStringCreateExternalRepresentation(NULL
, salt
, kCFStringEncodingUTF8
, 0);
211 const int iterations
= 16777216;
212 const uint8_t expected
[20] = { 0xee, 0xfe, 0x3d, 0x61,
213 0xcd, 0x4d, 0xa4, 0xe4,
214 0xe9, 0x94, 0x5b, 0x3d,
215 0x6b, 0xa2, 0x15, 0x8c,
216 0x26, 0x34, 0xe9, 0x84 };
219 const char resultSize
= sizeof(expected
);
221 CFMutableDataRef resultData
= CFDataCreateMutable(NULL
, resultSize
);
222 CFDataIncreaseLength(resultData
, resultSize
);
224 SecKeyFromPassphraseDataHMACSHA1(passwordData
, saltData
, iterations
, resultData
);
226 ok(memcmp(expected
, CFDataGetBytePtr(resultData
), resultSize
) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
228 CFReleaseSafe(password
);
230 CFReleaseSafe(passwordData
);
231 CFReleaseSafe(saltData
);
232 CFReleaseSafe(resultData
);
237 int spbkdf_00_hmac_sha1(int argc
, char *const *argv
)
239 plan_tests(kTestTestCount
);