]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/crypto/spbkdf-00-hmac-sha1.c
Security-59306.101.1.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 static int kTestTestCount = 16;
17
18 static void tests(void)
19 {
20 {
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 };
29
30 const char resultSize = sizeof(expected);
31
32 uint8_t actual[resultSize];
33
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");
35
36 is(memcmp(expected, actual, resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-1");
37 }
38
39 {
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 };
48
49 const char resultSize = sizeof(expected);
50
51 uint8_t actual[resultSize];
52
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");
54
55 is(memcmp(expected, actual, resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-2");
56 }
57
58 {
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 };
67
68 const char resultSize = sizeof(expected);
69
70 uint8_t actual[resultSize];
71
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");
73
74 is(memcmp(expected, actual, resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-4096");
75 }
76
77 SKIP: {
78 skip("16777216 iterations is too slow", 1, 0);
79
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 };
88
89 const char resultSize = sizeof(expected);
90
91 uint8_t actual[resultSize];
92
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");
94
95 is(memcmp(expected, actual, resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-16777216");
96 }
97
98
99 {
100 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
101 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
102
103 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
104 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
105
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 };
112
113 const char resultSize = sizeof(expected);
114
115 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
116 CFDataIncreaseLength(resultData, resultSize);
117
118 is(SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData), errSecSuccess, "pbkdf-sha-1: Failed Key Derivation I-1");
119
120 is(memcmp(expected, CFDataGetBytePtr(resultData), resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-1");
121
122 CFReleaseSafe(password);
123 CFReleaseSafe(salt);
124 CFReleaseSafe(passwordData);
125 CFReleaseSafe(saltData);
126 CFReleaseSafe(resultData);
127 }
128
129 {
130 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
131 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
132
133 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
134 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
135
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 };
142
143 const char resultSize = sizeof(expected);
144
145 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
146 CFDataIncreaseLength(resultData, resultSize);
147
148 is(SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData), errSecSuccess, "pbkdf-sha-1: Failed Key Derivation I-2");
149
150 is(memcmp(expected, CFDataGetBytePtr(resultData), resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-2");
151
152 CFReleaseSafe(password);
153 CFReleaseSafe(salt);
154 CFReleaseSafe(passwordData);
155 CFReleaseSafe(saltData);
156 CFReleaseSafe(resultData);
157 }
158
159 {
160 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
161 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
162
163 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
164 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
165
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 };
172
173
174 const char resultSize = sizeof(expected);
175
176 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
177 CFDataIncreaseLength(resultData, resultSize);
178
179 is(SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData), errSecSuccess, "pbkdf-sha-1: Failed Key Derivation I-4096");
180
181 is(memcmp(expected, CFDataGetBytePtr(resultData), resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-4096");
182
183 CFReleaseSafe(password);
184 CFReleaseSafe(salt);
185 CFReleaseSafe(passwordData);
186 CFReleaseSafe(saltData);
187 CFReleaseSafe(resultData);
188 }
189
190 SKIP: {
191 skip("16777216 iterations is too slow", 2, 0);
192
193 CFStringRef password = CFStringCreateWithCString(NULL, "password", kCFStringEncodingUTF8);
194 CFStringRef salt = CFStringCreateWithCString(NULL, "salt", kCFStringEncodingUTF8);
195
196 CFDataRef passwordData = CFStringCreateExternalRepresentation(NULL, password, kCFStringEncodingUTF8, 0);
197 CFDataRef saltData = CFStringCreateExternalRepresentation(NULL, salt, kCFStringEncodingUTF8, 0);
198
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 };
205
206
207 const char resultSize = sizeof(expected);
208
209 CFMutableDataRef resultData = CFDataCreateMutable(NULL, resultSize);
210 CFDataIncreaseLength(resultData, resultSize);
211
212 is(SecKeyFromPassphraseDataHMACSHA1(passwordData, saltData, iterations, resultData), errSecSuccess, "pbkdf-sha-1: Failed Key Derivation I-16777216");
213
214 is(memcmp(expected, CFDataGetBytePtr(resultData), resultSize), 0, "pbkdf-sha-1: P-'password' S-'salt' I-16777216");
215
216 CFReleaseSafe(password);
217 CFReleaseSafe(salt);
218 CFReleaseSafe(passwordData);
219 CFReleaseSafe(saltData);
220 CFReleaseSafe(resultData);
221 }
222
223 }
224
225 int spbkdf_00_hmac_sha1(int argc, char *const *argv)
226 {
227 plan_tests(kTestTestCount);
228
229 tests();
230
231 return 0;
232 }