]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/crypto/spbkdf-00-hmac-sha1.c
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / crypto / spbkdf-00-hmac-sha1.c
1 /*
2 * Copyright (c) 2010,2012 Apple Inc. All Rights Reserved.
3 */
4
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>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <Security/SecPBKDF.h>
13
14 #include "Security_regressions.h"
15
16 #if 0
17 static void
18 printComparison(const uint8_t*left, const uint8_t* right, int length)
19 {
20 int i;
21 for(i = 0; i < length; ++i)
22 {
23 fprintf(stderr, "# Values :: 0x%02x :: 0x%02x\n", left[i], right[i]);
24 }
25 }
26 #endif
27
28 static int kTestTestCount = 8;
29
30 static void tests(void)
31 {
32 {
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 };
41
42 const char resultSize = sizeof(expected);
43
44 uint8_t actual[resultSize];
45
46 pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize);
47
48 ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
49 }
50
51 {
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 };
60
61 const char resultSize = sizeof(expected);
62
63 uint8_t actual[resultSize];
64
65 pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize);
66
67 ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-2");
68 }
69
70 {
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 };
79
80 const char resultSize = sizeof(expected);
81
82 uint8_t actual[resultSize];
83
84 pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize);
85
86 ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-4096");
87 }
88
89 SKIP: {
90 skip("16777216 iterations is too slow", 1, 0);
91
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 };
100
101 const char resultSize = sizeof(expected);
102
103 uint8_t actual[resultSize];
104
105 pbkdf2_hmac_sha1((const uint8_t*) password, strlen(password), (const uint8_t*) salt, strlen(salt), iterations, actual, resultSize);
106
107 ok(memcmp(expected, actual, resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-16777216");
108 }
109
110
111 {
112 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
113 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
114
115 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
116 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
117
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 };
124
125 const char resultSize = sizeof(expected);
126
127 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
128 CFDataIncreaseLength(resultData, resultSize);
129
130 SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData);
131
132 ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
133
134 CFReleaseSafe(password);
135 CFReleaseSafe(salt);
136 CFReleaseSafe(passwordData);
137 CFReleaseSafe(saltData);
138 CFReleaseSafe(resultData);
139 }
140
141 {
142 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
143 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
144
145 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
146 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
147
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 };
154
155 const char resultSize = sizeof(expected);
156
157 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
158 CFDataIncreaseLength(resultData, resultSize);
159
160 SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData);
161
162 ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
163
164 CFReleaseSafe(password);
165 CFReleaseSafe(salt);
166 CFReleaseSafe(passwordData);
167 CFReleaseSafe(saltData);
168 CFReleaseSafe(resultData);
169 }
170
171 {
172 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
173 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
174
175 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
176 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
177
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 };
184
185
186 const char resultSize = sizeof(expected);
187
188 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
189 CFDataIncreaseLength(resultData, resultSize);
190
191 SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData);
192
193 ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
194
195 CFReleaseSafe(password);
196 CFReleaseSafe(salt);
197 CFReleaseSafe(passwordData);
198 CFReleaseSafe(saltData);
199 CFReleaseSafe(resultData);
200 }
201
202 SKIP: {
203 skip("16777216 iterations is too slow", 1, 0);
204
205 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
206 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
207
208 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
209 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
210
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 };
217
218
219 const char resultSize = sizeof(expected);
220
221 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
222 CFDataIncreaseLength(resultData, resultSize);
223
224 SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData);
225
226 ok(memcmp(expected, CFDataGetBytePtr(resultData), resultSize) == 0, "pbkdf-sha-1: P-'password' S-'Salt' I-1");
227
228 CFReleaseSafe(password);
229 CFReleaseSafe(salt);
230 CFReleaseSafe(passwordData);
231 CFReleaseSafe(saltData);
232 CFReleaseSafe(resultData);
233 }
234
235 }
236
237 int spbkdf_00_hmac_sha1(int argc, char *const *argv)
238 {
239 plan_tests(kTestTestCount);
240
241 tests();
242
243 return 0;
244 }