/src/wolfssl/wolfcrypt/src/pwdbased.c
Line | Count | Source |
1 | | /* pwdbased.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | #define WC_FIPS_LL_CRYPTO |
23 | | #define _WC_BUILDING_PWDBASED_C |
24 | | |
25 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
26 | | |
27 | | #ifndef NO_PWDBASED |
28 | | |
29 | | #if FIPS_VERSION3_GE(6,0,0) |
30 | | #if defined(USE_WINDOWS_API) && defined(_MSC_VER) |
31 | | #pragma code_seg(".fipsA$h") |
32 | | #pragma const_seg(".fipsB$h") |
33 | | #endif |
34 | | #endif |
35 | | |
36 | | #include <wolfssl/wolfcrypt/pwdbased.h> |
37 | | #include <wolfssl/wolfcrypt/hmac.h> |
38 | | #include <wolfssl/wolfcrypt/hash.h> |
39 | | #include <wolfssl/wolfcrypt/wolfmath.h> |
40 | | #ifdef WOLF_CRYPTO_CB |
41 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
42 | | #endif |
43 | | |
44 | | #ifdef NO_INLINE |
45 | | #include <wolfssl/wolfcrypt/misc.h> |
46 | | #else |
47 | | #define WOLFSSL_MISC_INCLUDED |
48 | | #include <wolfcrypt/src/misc.c> |
49 | | #endif |
50 | | |
51 | | #if FIPS_VERSION3_GE(6,0,0) |
52 | | const unsigned int wolfCrypt_FIPS_pbkdf_ro_sanity[2] = |
53 | | { 0x1a2b3c4d, 0x00000010 }; |
54 | | int wolfCrypt_FIPS_PBKDF_sanity(void) |
55 | | { |
56 | | return 0; |
57 | | } |
58 | | #endif |
59 | | |
60 | | static int current_wc_pbkdf_max_iterations = WC_PBKDF_DEFAULT_MAX_ITERATIONS; |
61 | | |
62 | | int wc_PBKDF_max_iterations_set(int iters) |
63 | 0 | { |
64 | 0 | if (iters <= 0) |
65 | 0 | return BAD_FUNC_ARG; |
66 | 0 | else { |
67 | 0 | int prev = current_wc_pbkdf_max_iterations; |
68 | 0 | current_wc_pbkdf_max_iterations = iters; |
69 | 0 | return prev; |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | int wc_PBKDF_max_iterations_get(void) |
74 | 0 | { |
75 | 0 | return current_wc_pbkdf_max_iterations; |
76 | 0 | } |
77 | | |
78 | | #ifdef HAVE_PBKDF1 |
79 | | |
80 | | /* PKCS#5 v1.5 with non standard extension to optionally derive the extra data (IV) */ |
81 | | int wc_PBKDF1_ex(byte* key, int keyLen, byte* iv, int ivLen, |
82 | | const byte* passwd, int passwdLen, const byte* salt, int saltLen, |
83 | | int iterations, int hashType, void* heap) |
84 | 0 | { |
85 | 0 | int err; |
86 | 0 | int freeErr; |
87 | 0 | int keyLeft, ivLeft, i; |
88 | 0 | int store; |
89 | 0 | int keyOutput = 0; |
90 | 0 | int digestLen; |
91 | 0 | byte digest[WC_MAX_DIGEST_SIZE]; |
92 | 0 | WC_DECLARE_VAR(hash, wc_HashAlg, 1, 0); |
93 | 0 | enum wc_HashType hashT; |
94 | |
|
95 | 0 | (void)heap; |
96 | |
|
97 | 0 | if (key == NULL || keyLen < 0 || passwdLen < 0 || saltLen < 0 || |
98 | 0 | ivLen < 0 || (salt == NULL && saltLen > 0)){ |
99 | 0 | return BAD_FUNC_ARG; |
100 | 0 | } |
101 | | |
102 | 0 | if (keyLen > INT_MAX - ivLen) |
103 | 0 | return BAD_FUNC_ARG; |
104 | | |
105 | 0 | if (iterations <= 0) |
106 | 0 | return BAD_FUNC_ARG; |
107 | | |
108 | 0 | if (iterations > current_wc_pbkdf_max_iterations) { |
109 | 0 | WOLFSSL_MSG("PBKDF1 iteration count exceeds current_wc_pbkdf_max_iterations"); |
110 | 0 | return BAD_FUNC_ARG; |
111 | 0 | } |
112 | | |
113 | 0 | hashT = wc_HashTypeConvert(hashType); |
114 | 0 | err = wc_HashGetDigestSize(hashT); |
115 | 0 | if (err < 0) |
116 | 0 | return err; |
117 | 0 | digestLen = err; |
118 | | |
119 | | /* initialize hash */ |
120 | 0 | WC_ALLOC_VAR_EX(hash, wc_HashAlg, 1, heap, DYNAMIC_TYPE_HASHCTX, |
121 | 0 | return MEMORY_E); |
122 | |
|
123 | 0 | err = wc_HashInit_ex(hash, hashT, heap, INVALID_DEVID); |
124 | 0 | if (err != 0) { |
125 | 0 | WC_FREE_VAR_EX(hash, heap, DYNAMIC_TYPE_HASHCTX); |
126 | 0 | return err; |
127 | 0 | } |
128 | | |
129 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
130 | | /* poison so a missed ForceZero on any path is caught by the check */ |
131 | | XMEMSET(digest, 0xff, sizeof(digest)); |
132 | | wc_MemZero_Add("wc_PBKDF1_ex digest", digest, sizeof(digest)); |
133 | | #endif |
134 | | |
135 | 0 | keyLeft = keyLen; |
136 | 0 | ivLeft = ivLen; |
137 | 0 | while (keyOutput < (keyLen + ivLen)) { |
138 | 0 | int digestLeft = digestLen; |
139 | | /* D_(i - 1) */ |
140 | 0 | if (keyOutput) { /* first time D_0 is empty */ |
141 | 0 | err = wc_HashUpdate(hash, hashT, digest, (word32)digestLen); |
142 | 0 | if (err != 0) break; |
143 | 0 | } |
144 | | |
145 | | /* data */ |
146 | 0 | err = wc_HashUpdate(hash, hashT, passwd, (word32)passwdLen); |
147 | 0 | if (err != 0) break; |
148 | | |
149 | | /* salt */ |
150 | 0 | if (salt) { |
151 | 0 | err = wc_HashUpdate(hash, hashT, salt, (word32)saltLen); |
152 | 0 | if (err != 0) break; |
153 | 0 | } |
154 | | |
155 | 0 | err = wc_HashFinal(hash, hashT, digest); |
156 | 0 | if (err != 0) break; |
157 | | |
158 | | /* count */ |
159 | 0 | for (i = 1; i < iterations; i++) { |
160 | 0 | err = wc_HashUpdate(hash, hashT, digest, (word32)digestLen); |
161 | 0 | if (err != 0) break; |
162 | | |
163 | 0 | err = wc_HashFinal(hash, hashT, digest); |
164 | 0 | if (err != 0) break; |
165 | 0 | } |
166 | |
|
167 | 0 | if (err != 0) break; |
168 | | |
169 | 0 | if (keyLeft) { |
170 | 0 | store = (int)min((word32)keyLeft, (word32)digestLen); |
171 | 0 | XMEMCPY(&key[keyLen - keyLeft], digest, (size_t)store); |
172 | |
|
173 | 0 | keyOutput += store; |
174 | 0 | keyLeft -= store; |
175 | 0 | digestLeft -= store; |
176 | 0 | } |
177 | |
|
178 | 0 | if (ivLeft && digestLeft) { |
179 | 0 | store = (int)min((word32)ivLeft, (word32)digestLeft); |
180 | 0 | if (iv != NULL) |
181 | 0 | XMEMCPY(&iv[ivLen - ivLeft], |
182 | 0 | &digest[digestLen - digestLeft], (size_t)store); |
183 | 0 | keyOutput += store; |
184 | 0 | ivLeft -= store; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | /* A failed wipe outranks an earlier error. */ |
189 | 0 | freeErr = wc_HashFree(hash, hashT); |
190 | 0 | if (freeErr != 0) |
191 | 0 | err = freeErr; |
192 | |
|
193 | 0 | WC_FREE_VAR_EX(hash, heap, DYNAMIC_TYPE_HASHCTX); |
194 | |
|
195 | 0 | ForceZero(digest, sizeof(digest)); |
196 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
197 | | wc_MemZero_Check(digest, sizeof(digest)); |
198 | | #endif |
199 | |
|
200 | 0 | if (err != 0) |
201 | 0 | return err; |
202 | | |
203 | 0 | if (keyOutput != (keyLen + ivLen)) |
204 | 0 | return BUFFER_E; |
205 | | |
206 | 0 | return err; |
207 | 0 | } |
208 | | |
209 | | /* PKCS#5 v1.5 */ |
210 | | int wc_PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt, |
211 | | int sLen, int iterations, int kLen, int hashType) |
212 | 0 | { |
213 | |
|
214 | 0 | return wc_PBKDF1_ex(output, kLen, NULL, 0, |
215 | 0 | passwd, pLen, salt, sLen, iterations, hashType, NULL); |
216 | 0 | } |
217 | | |
218 | | #endif /* HAVE_PKCS5 */ |
219 | | |
220 | | #if defined(HAVE_PBKDF2) && !defined(NO_HMAC) |
221 | | |
222 | | int wc_PBKDF2_ex(byte* output, const byte* passwd, int pLen, const byte* salt, |
223 | | int sLen, int iterations, int kLen, int hashType, void* heap, int devId) |
224 | 0 | { |
225 | 0 | int hLen; |
226 | 0 | int ret; |
227 | | #ifdef WOLFSSL_SMALL_STACK |
228 | | byte* buffer; |
229 | | Hmac* hmac; |
230 | | #else |
231 | 0 | byte buffer[WC_MAX_DIGEST_SIZE]; |
232 | 0 | Hmac hmac[1]; |
233 | 0 | #endif |
234 | 0 | enum wc_HashType hashT; |
235 | 0 | byte* outStart = output; |
236 | 0 | int outLen = kLen; |
237 | |
|
238 | 0 | if (output == NULL || pLen < 0 || sLen < 0 || kLen < 0) { |
239 | 0 | return BAD_FUNC_ARG; |
240 | 0 | } |
241 | | |
242 | | #if FIPS_VERSION3_GE(6,0,0) |
243 | | /* Per SP800-132 section 5 "The kLen value shall be at least 112 bits in |
244 | | * length", ensure the returned bits for the derived master key are at a |
245 | | * minimum 14-bytes or 112-bits after stretching and strengthening |
246 | | * (iterations) */ |
247 | | if (kLen < HMAC_FIPS_MIN_KEY) |
248 | | return BAD_LENGTH_E; |
249 | | #endif |
250 | | |
251 | | #if FIPS_VERSION3_GE(6,0,0) && defined(DEBUG_WOLFSSL) |
252 | | /* SP800-132 section 5.2 recommends an iteration count of 1000 but this is |
253 | | * not strictly enforceable and is listed in Appendix B Table 1 as a |
254 | | * non-testable requirement. wolfCrypt will log it when appropriate but |
255 | | * take no action */ |
256 | | if (iterations < 1000) { |
257 | | WOLFSSL_MSG("WARNING: Iteration < 1,000, see SP800-132 section 5.2"); |
258 | | } |
259 | | #endif |
260 | 0 | if (iterations <= 0) |
261 | 0 | return BAD_FUNC_ARG; |
262 | | |
263 | 0 | if (iterations > current_wc_pbkdf_max_iterations) { |
264 | 0 | WOLFSSL_MSG("PBKDF2 iteration count exceeds current_wc_pbkdf_max_iterations"); |
265 | 0 | return BAD_FUNC_ARG; |
266 | 0 | } |
267 | | |
268 | | #ifdef WOLF_CRYPTO_CB |
269 | | /* Validate the pointer/length pairs before dispatching so a registered |
270 | | * device sees the same contract the software path enforces later via |
271 | | * wc_HmacSetKey_ex()/wc_HmacUpdate(). NULL is allowed only for a |
272 | | * zero-length input. */ |
273 | | if (output == NULL || (passwd == NULL && pLen > 0) || |
274 | | (salt == NULL && sLen > 0) || pLen < 0 || sLen < 0 || kLen < 0) { |
275 | | return BAD_FUNC_ARG; |
276 | | } |
277 | | if (devId != INVALID_DEVID) { |
278 | | ret = wc_CryptoCb_Pbkdf2(output, passwd, pLen, salt, sLen, iterations, |
279 | | kLen, hashType, devId); |
280 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
281 | | return ret; |
282 | | /* fall-through when unavailable */ |
283 | | } |
284 | | #endif |
285 | | |
286 | 0 | hashT = wc_HashTypeConvert(hashType); |
287 | 0 | hLen = wc_HashGetDigestSize(hashT); |
288 | 0 | if (hLen < 0) |
289 | 0 | return BAD_FUNC_ARG; |
290 | | |
291 | | #ifdef WOLFSSL_SMALL_STACK |
292 | | buffer = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER); |
293 | | if (buffer == NULL) |
294 | | return MEMORY_E; |
295 | | hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap, DYNAMIC_TYPE_HMAC); |
296 | | if (hmac == NULL) { |
297 | | XFREE(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER); |
298 | | return MEMORY_E; |
299 | | } |
300 | | #endif |
301 | | |
302 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
303 | | /* poison so a missed ForceZero on any path is caught by the check */ |
304 | | XMEMSET(buffer, 0xff, (word32)hLen); |
305 | | wc_MemZero_Add("wc_PBKDF2_ex buffer", buffer, (word32)hLen); |
306 | | #endif |
307 | | |
308 | 0 | ret = wc_HmacInit(hmac, heap, devId); |
309 | 0 | if (ret == 0) { |
310 | 0 | word32 i = 1; |
311 | | /* use int hashType here, since HMAC FIPS uses the old unique value */ |
312 | | #if FIPS_VERSION3_GE(6,0,0) |
313 | | { |
314 | | /* Allow passwords that are less than 14-bytes for compatibility |
315 | | * / interoperability, only since module v6.0.0 */ |
316 | | int allowShortPasswd = 1; |
317 | | ret = wc_HmacSetKey_ex(hmac, hashType, passwd, (word32)pLen, |
318 | | allowShortPasswd); |
319 | | } |
320 | | #else |
321 | 0 | ret = wc_HmacSetKey(hmac, hashType, passwd, (word32)pLen); |
322 | 0 | #endif |
323 | |
|
324 | 0 | while (ret == 0 && kLen) { |
325 | 0 | int currentLen; |
326 | 0 | int j; |
327 | |
|
328 | 0 | ret = wc_HmacUpdate(hmac, salt, (word32)sLen); |
329 | 0 | if (ret != 0) |
330 | 0 | break; |
331 | | |
332 | | /* encode i */ |
333 | 0 | for (j = 0; j < 4; j++) { |
334 | 0 | byte b = (byte)(i >> ((3-j) * 8)); |
335 | |
|
336 | 0 | ret = wc_HmacUpdate(hmac, &b, 1); |
337 | 0 | if (ret != 0) |
338 | 0 | break; |
339 | 0 | } |
340 | | |
341 | | /* check ret from inside for loop */ |
342 | 0 | if (ret != 0) |
343 | 0 | break; |
344 | | |
345 | 0 | ret = wc_HmacFinal(hmac, buffer); |
346 | 0 | if (ret != 0) |
347 | 0 | break; |
348 | | |
349 | 0 | currentLen = (int)min((word32)kLen, (word32)hLen); |
350 | 0 | XMEMCPY(output, buffer, (size_t)currentLen); |
351 | |
|
352 | 0 | for (j = 1; j < iterations; j++) { |
353 | 0 | ret = wc_HmacUpdate(hmac, buffer, (word32)hLen); |
354 | 0 | if (ret != 0) |
355 | 0 | break; |
356 | 0 | ret = wc_HmacFinal(hmac, buffer); |
357 | 0 | if (ret != 0) |
358 | 0 | break; |
359 | 0 | xorbuf(output, buffer, (word32)currentLen); |
360 | 0 | } |
361 | | |
362 | | /* check ret from inside for loop */ |
363 | 0 | if (ret != 0) |
364 | 0 | break; |
365 | | |
366 | 0 | output += currentLen; |
367 | 0 | kLen -= currentLen; |
368 | 0 | i++; |
369 | 0 | } |
370 | 0 | wc_HmacFree(hmac); |
371 | 0 | } |
372 | 0 | if (ret != 0) { |
373 | | /* Partial output is not released (ISO/IEC 19790 7.9.7). */ |
374 | 0 | ForceZero(outStart, (word32)outLen); |
375 | 0 | } |
376 | |
|
377 | 0 | ForceZero(buffer, (word32)hLen); |
378 | | #if !defined(WOLFSSL_SMALL_STACK) && defined(WOLFSSL_CHECK_MEM_ZERO) |
379 | | wc_MemZero_Check(buffer, (word32)hLen); |
380 | | #endif |
381 | 0 | WC_FREE_VAR_EX(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER); |
382 | 0 | WC_FREE_VAR_EX(hmac, heap, DYNAMIC_TYPE_HMAC); |
383 | |
|
384 | 0 | return ret; |
385 | 0 | } |
386 | | |
387 | | int wc_PBKDF2(byte* output, const byte* passwd, int pLen, const byte* salt, |
388 | | int sLen, int iterations, int kLen, int hashType) |
389 | 0 | { |
390 | 0 | return wc_PBKDF2_ex(output, passwd, pLen, salt, sLen, iterations, kLen, |
391 | 0 | hashType, NULL, INVALID_DEVID); |
392 | 0 | } |
393 | | |
394 | | #endif /* HAVE_PBKDF2 && !NO_HMAC */ |
395 | | |
396 | | #ifdef HAVE_PKCS12 |
397 | | |
398 | | /* helper for PKCS12_PBKDF(), does hash operation. |
399 | | * buffer and Ai are guaranteed non-NULL by the caller: each is either a stack |
400 | | * array or an XMALLOC result whose failure returns MEMORY_E before the call. */ |
401 | | static int DoPKCS12Hash(enum wc_HashType hashT, byte* buffer, word32 totalLen, |
402 | | byte* Ai, word32 u, int iterations) |
403 | 0 | { |
404 | 0 | int i; |
405 | 0 | int ret = 0; |
406 | 0 | int freeRet; |
407 | 0 | WC_DECLARE_VAR(hash, wc_HashAlg, 1, 0); |
408 | | |
409 | | /* initialize hash */ |
410 | 0 | WC_ALLOC_VAR_EX(hash, wc_HashAlg, 1, NULL, DYNAMIC_TYPE_HASHCTX, |
411 | 0 | return MEMORY_E); |
412 | |
|
413 | 0 | ret = wc_HashInit(hash, hashT); |
414 | 0 | if (ret == 0) { |
415 | 0 | ret = wc_HashUpdate(hash, hashT, buffer, totalLen); |
416 | 0 | if (ret == 0) |
417 | 0 | ret = wc_HashFinal(hash, hashT, Ai); |
418 | |
|
419 | 0 | for (i = 1; i < iterations; i++) { |
420 | 0 | if (ret == 0) |
421 | 0 | ret = wc_HashUpdate(hash, hashT, Ai, u); |
422 | 0 | if (ret == 0) |
423 | 0 | ret = wc_HashFinal(hash, hashT, Ai); |
424 | 0 | } |
425 | |
|
426 | 0 | freeRet = wc_HashFree(hash, hashT); |
427 | 0 | if (freeRet != 0) |
428 | 0 | ret = freeRet; |
429 | 0 | } |
430 | |
|
431 | 0 | WC_FREE_VAR_EX(hash, NULL, DYNAMIC_TYPE_HASHCTX); |
432 | 0 | return ret; |
433 | 0 | } |
434 | | |
435 | | |
436 | | int wc_PKCS12_PBKDF(byte* output, const byte* passwd, int passLen, |
437 | | const byte* salt, int saltLen, int iterations, int kLen, int hashType, |
438 | | int id) |
439 | 0 | { |
440 | 0 | return wc_PKCS12_PBKDF_ex(output, passwd, passLen, salt, saltLen, |
441 | 0 | iterations, kLen, hashType, id, NULL); |
442 | 0 | } |
443 | | |
444 | | |
445 | | #ifdef WC_PKCS12_PBKDF_USING_MP_API |
446 | | /* extended API that allows a heap hint to be used */ |
447 | | int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen, |
448 | | const byte* salt, int saltLen, int iterations, int kLen, |
449 | | int hashType, int id, void* heap) |
450 | | { |
451 | | /* all in bytes instead of bits */ |
452 | | word32 u, v, dLen, pLen, iLen, sLen, totalLen; |
453 | | int dynamic = 0; |
454 | | int ret = 0; |
455 | | word32 i; |
456 | | byte *D, *S, *P, *I; |
457 | | #ifdef WOLFSSL_SMALL_STACK |
458 | | byte staticBuffer[1]; /* force dynamic usage */ |
459 | | #else |
460 | | byte staticBuffer[1024]; |
461 | | #endif |
462 | | byte* buffer = staticBuffer; |
463 | | |
464 | | #ifdef WOLFSSL_SMALL_STACK |
465 | | byte* Ai = NULL; |
466 | | byte* B = NULL; |
467 | | mp_int *B1 = NULL; |
468 | | mp_int *i1 = NULL; |
469 | | mp_int *res = NULL; |
470 | | #else |
471 | | byte Ai[WC_MAX_DIGEST_SIZE]; |
472 | | byte B[WC_MAX_BLOCK_SIZE]; |
473 | | mp_int B1[1]; |
474 | | mp_int i1[1]; |
475 | | mp_int res[1]; |
476 | | #endif |
477 | | enum wc_HashType hashT; |
478 | | |
479 | | (void)heap; |
480 | | |
481 | | if (output == NULL || passLen <= 0 || saltLen <= 0 || kLen < 0) { |
482 | | return BAD_FUNC_ARG; |
483 | | } |
484 | | |
485 | | if (iterations <= 0) |
486 | | return BAD_FUNC_ARG; |
487 | | |
488 | | if (iterations > current_wc_pbkdf_max_iterations) { |
489 | | WOLFSSL_MSG("PKCS12 PBKDF iteration count exceeds " |
490 | | "current_wc_pbkdf_max_iterations"); |
491 | | return BAD_FUNC_ARG; |
492 | | } |
493 | | |
494 | | hashT = wc_HashTypeConvert(hashType); |
495 | | ret = wc_HashGetDigestSize(hashT); |
496 | | if (ret < 0) |
497 | | return ret; |
498 | | if (ret == 0) |
499 | | return BAD_STATE_E; |
500 | | u = (word32)ret; |
501 | | |
502 | | ret = wc_HashGetBlockSize(hashT); |
503 | | if (ret < 0) |
504 | | return ret; |
505 | | if (ret == 0) |
506 | | return BAD_STATE_E; |
507 | | v = (word32)ret; |
508 | | /* the block size must not be mistaken for a result when kLen is 0 and the |
509 | | * derivation loop below never runs */ |
510 | | ret = 0; |
511 | | |
512 | | #ifdef WOLFSSL_SMALL_STACK |
513 | | Ai = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER); |
514 | | if (Ai == NULL) |
515 | | return MEMORY_E; |
516 | | |
517 | | B = (byte*)XMALLOC(WC_MAX_BLOCK_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER); |
518 | | if (B == NULL) { |
519 | | XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER); |
520 | | return MEMORY_E; |
521 | | } |
522 | | #endif |
523 | | |
524 | | XMEMSET(Ai, 0, WC_MAX_DIGEST_SIZE); |
525 | | XMEMSET(B, 0, WC_MAX_BLOCK_SIZE); |
526 | | |
527 | | dLen = v; |
528 | | sLen = v * (((word32)saltLen + v - 1) / v); |
529 | | |
530 | | /* with passLen checked at the top of the function for >= 0 then passLen |
531 | | * must be 1 or greater here and is always 'true' */ |
532 | | pLen = v * (((word32)passLen + v - 1) / v); |
533 | | |
534 | | if (! WC_SAFE_SUM_UNSIGNED(word32, sLen, pLen, iLen)) { |
535 | | WC_FREE_VAR_EX(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER); |
536 | | WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER); |
537 | | return BAD_FUNC_ARG; |
538 | | } |
539 | | |
540 | | /* the working buffer holds D || S || P, so totalLen is dLen + iLen */ |
541 | | if (! WC_SAFE_SUM_UNSIGNED(word32, dLen, iLen, totalLen)) { |
542 | | WC_FREE_VAR_EX(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER); |
543 | | WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER); |
544 | | return BAD_FUNC_ARG; |
545 | | } |
546 | | |
547 | | if (totalLen > sizeof(staticBuffer)) { |
548 | | buffer = (byte*)XMALLOC(totalLen, heap, DYNAMIC_TYPE_KEY); |
549 | | if (buffer == NULL) { |
550 | | WC_FREE_VAR_EX(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER); |
551 | | WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER); |
552 | | return MEMORY_E; |
553 | | } |
554 | | dynamic = 1; |
555 | | } |
556 | | |
557 | | D = buffer; |
558 | | S = D + dLen; |
559 | | P = S + sLen; |
560 | | I = S; |
561 | | |
562 | | XMEMSET(D, id, dLen); |
563 | | |
564 | | for (i = 0; i < sLen; i++) |
565 | | S[i] = salt[i % (word32)saltLen]; |
566 | | for (i = 0; i < pLen; i++) |
567 | | P[i] = passwd[i % (word32)passLen]; |
568 | | |
569 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
570 | | wc_MemZero_Add("wc_PKCS12_PBKDF_ex Ai", Ai, WC_MAX_DIGEST_SIZE); |
571 | | wc_MemZero_Add("wc_PKCS12_PBKDF_ex B", B, WC_MAX_BLOCK_SIZE); |
572 | | wc_MemZero_Add("wc_PKCS12_PBKDF_ex buffer", buffer, totalLen); |
573 | | #endif |
574 | | |
575 | | #ifdef WOLFSSL_SMALL_STACK |
576 | | if (((B1 = (mp_int *)XMALLOC(sizeof(*B1), heap, DYNAMIC_TYPE_TMP_BUFFER)) |
577 | | == NULL) || |
578 | | ((i1 = (mp_int *)XMALLOC(sizeof(*i1), heap, DYNAMIC_TYPE_TMP_BUFFER)) |
579 | | == NULL) || |
580 | | ((res = (mp_int *)XMALLOC(sizeof(*res), heap, DYNAMIC_TYPE_TMP_BUFFER)) |
581 | | == NULL)) { |
582 | | ret = MEMORY_E; |
583 | | goto out; |
584 | | } |
585 | | #endif |
586 | | |
587 | | while (kLen > 0) { |
588 | | word32 currentLen; |
589 | | |
590 | | ret = DoPKCS12Hash(hashT, buffer, totalLen, Ai, u, iterations); |
591 | | if (ret != 0) |
592 | | break; |
593 | | |
594 | | for (i = 0; i < v; i++) |
595 | | B[i] = Ai[(word32)i % u]; |
596 | | |
597 | | if (mp_init(B1) != MP_OKAY) |
598 | | ret = MP_INIT_E; |
599 | | else if (mp_read_unsigned_bin(B1, B, v) != MP_OKAY) |
600 | | ret = MP_READ_E; |
601 | | else if (mp_add_d(B1, (mp_digit)1, B1) != MP_OKAY) |
602 | | ret = MP_ADD_E; |
603 | | |
604 | | if (ret != 0) { |
605 | | mp_clear(B1); |
606 | | break; |
607 | | } |
608 | | |
609 | | for (i = 0; i < iLen; i += v) { |
610 | | int outSz; |
611 | | |
612 | | if (mp_init_multi(i1, res, NULL, NULL, NULL, NULL) != MP_OKAY) { |
613 | | ret = MP_INIT_E; |
614 | | break; |
615 | | } |
616 | | if (mp_read_unsigned_bin(i1, I + i, v) != MP_OKAY) |
617 | | ret = MP_READ_E; |
618 | | else if (mp_add(i1, B1, res) != MP_OKAY) |
619 | | ret = MP_ADD_E; |
620 | | else if ( (outSz = mp_unsigned_bin_size(res)) < 0) |
621 | | ret = MP_TO_E; |
622 | | else { |
623 | | if (outSz > (int)v) { |
624 | | /* take off MSB */ |
625 | | byte tmp[WC_MAX_BLOCK_SIZE + 1]; |
626 | | ret = mp_to_unsigned_bin(res, tmp); |
627 | | XMEMCPY(I + i, tmp + 1, v); |
628 | | } |
629 | | else if (outSz < (int)v) { |
630 | | XMEMSET(I + i, 0, v - (word32)outSz); |
631 | | ret = mp_to_unsigned_bin(res, I + i + v - (word32)outSz); |
632 | | } |
633 | | else |
634 | | ret = mp_to_unsigned_bin(res, I + i); |
635 | | } |
636 | | |
637 | | mp_clear(i1); |
638 | | mp_clear(res); |
639 | | if (ret < 0) break; |
640 | | } |
641 | | |
642 | | if (ret < 0) { |
643 | | mp_clear(B1); |
644 | | break; |
645 | | } |
646 | | |
647 | | currentLen = min((word32)kLen, u); |
648 | | XMEMCPY(output, Ai, currentLen); |
649 | | output += currentLen; |
650 | | kLen -= (int)currentLen; |
651 | | mp_clear(B1); |
652 | | } |
653 | | |
654 | | #ifdef WOLFSSL_SMALL_STACK |
655 | | out: |
656 | | |
657 | | ForceZero(Ai, WC_MAX_DIGEST_SIZE); |
658 | | XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER); |
659 | | ForceZero(B, WC_MAX_BLOCK_SIZE); |
660 | | XFREE(B, heap, DYNAMIC_TYPE_TMP_BUFFER); |
661 | | XFREE(B1, heap, DYNAMIC_TYPE_TMP_BUFFER); |
662 | | XFREE(i1, heap, DYNAMIC_TYPE_TMP_BUFFER); |
663 | | XFREE(res, heap, DYNAMIC_TYPE_TMP_BUFFER); |
664 | | #else |
665 | | ForceZero(Ai, WC_MAX_DIGEST_SIZE); |
666 | | ForceZero(B, WC_MAX_BLOCK_SIZE); |
667 | | #if defined(WOLFSSL_CHECK_MEM_ZERO) |
668 | | wc_MemZero_Check(Ai, WC_MAX_DIGEST_SIZE); |
669 | | wc_MemZero_Check(B, WC_MAX_BLOCK_SIZE); |
670 | | #endif |
671 | | #endif |
672 | | |
673 | | ForceZero(buffer, totalLen); |
674 | | #if defined(WOLFSSL_CHECK_MEM_ZERO) |
675 | | if (!dynamic) |
676 | | wc_MemZero_Check(buffer, totalLen); |
677 | | #endif |
678 | | if (dynamic) |
679 | | XFREE(buffer, heap, DYNAMIC_TYPE_KEY); |
680 | | |
681 | | return ret; |
682 | | } |
683 | | #else |
684 | | |
685 | | #if defined(WC_64BIT_CPU) && defined(HAVE___UINT128_T) && \ |
686 | | !defined(NO_INT128) |
687 | 0 | #define PKCS12_DWORD word128 |
688 | 0 | #define PKCS12_WORD word64 |
689 | 0 | #define PKCS12_ByteReverseWords ByteReverseWords64 |
690 | | #elif defined(WC_32BIT_CPU) || defined(WC_64BIT_CPU) |
691 | | #define PKCS12_DWORD word64 |
692 | | #define PKCS12_WORD word32 |
693 | | #define PKCS12_ByteReverseWords ByteReverseWords |
694 | | #else |
695 | | #define PKCS12_DWORD word16 |
696 | | #define PKCS12_WORD word8 |
697 | | /* No need to byte reverse when handling 1 byte at a time. */ |
698 | | #define PKCS12_ByteReverseWords(r, a, n) WC_DO_NOTHING |
699 | | #endif |
700 | | |
701 | | /* extended API that allows a heap hint to be used */ |
702 | | int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen, |
703 | | const byte* salt, int saltLen, int iterations, int kLen, |
704 | | int hashType, int id, void* heap) |
705 | 0 | { |
706 | 0 | word32 u, v, pLen, sLen; |
707 | 0 | word32 iLen = 0; |
708 | 0 | word32 totalLen = 0; |
709 | | /* nwc: v / sizeof(PKCS12_WORD) - words per v-byte block |
710 | | * (v is always a multiple of sizeof(PKCS12_WORD)) |
711 | | * nBlocks: iLen / v - number of v-byte blocks in I */ |
712 | 0 | word32 nwc, nBlocks; |
713 | 0 | int ret = 0; |
714 | 0 | word32 i, k, blk; |
715 | 0 | byte* I; |
716 | 0 | PKCS12_WORD* Bw; |
717 | | #ifdef WOLFSSL_SMALL_STACK |
718 | | byte staticBuffer[1]; /* force dynamic usage */ |
719 | | byte* B = NULL; |
720 | | #else |
721 | 0 | ALIGN8 byte staticBuffer[1024]; |
722 | 0 | ALIGN8 byte B[WC_MAX_BLOCK_SIZE]; |
723 | 0 | #endif |
724 | 0 | byte* buffer = staticBuffer; |
725 | 0 | enum wc_HashType hashT; |
726 | |
|
727 | 0 | (void)heap; |
728 | |
|
729 | 0 | if ((output == NULL) || (passLen <= 0) || (saltLen <= 0) || (kLen < 0)) { |
730 | 0 | return BAD_FUNC_ARG; |
731 | 0 | } |
732 | | |
733 | 0 | if (iterations <= 0) { |
734 | 0 | return BAD_FUNC_ARG; |
735 | 0 | } |
736 | | |
737 | 0 | if (iterations > current_wc_pbkdf_max_iterations) { |
738 | 0 | WOLFSSL_MSG("PKCS12 PBKDF iteration count exceeds " |
739 | 0 | "current_wc_pbkdf_max_iterations"); |
740 | 0 | return BAD_FUNC_ARG; |
741 | 0 | } |
742 | | |
743 | | /* u = hash output size. */ |
744 | 0 | hashT = wc_HashTypeConvert(hashType); |
745 | 0 | ret = wc_HashGetDigestSize(hashT); |
746 | 0 | if (ret < 0) |
747 | 0 | return ret; |
748 | 0 | if (ret == 0) |
749 | 0 | return BAD_STATE_E; |
750 | 0 | u = (word32)ret; |
751 | | |
752 | | /* v = hash block size. */ |
753 | 0 | ret = wc_HashGetBlockSize(hashT); |
754 | 0 | if (ret < 0) |
755 | 0 | return ret; |
756 | 0 | if (ret == 0) |
757 | 0 | return BAD_STATE_E; |
758 | 0 | v = (word32)ret; |
759 | | |
760 | | /* RFC 7292 B.2 step 2: S = salt repeated to ceil(saltLen/v)*v bytes */ |
761 | 0 | sLen = v * (((word32)saltLen + v - 1) / v); |
762 | | /* RFC 7292 B.2 step 3: P = password repeated to ceil(passLen/v)*v bytes */ |
763 | 0 | pLen = v * (((word32)passLen + v - 1) / v); |
764 | | |
765 | | /* RFC 7292 B.2 step 4: I = S || P */ |
766 | 0 | if (! WC_SAFE_SUM_UNSIGNED(word32, sLen, pLen, iLen)) { |
767 | 0 | return BAD_FUNC_ARG; |
768 | 0 | } |
769 | | |
770 | 0 | if (! WC_SAFE_SUM_UNSIGNED(word32, v, iLen, totalLen)) { |
771 | 0 | return BAD_FUNC_ARG; |
772 | 0 | } |
773 | | |
774 | 0 | nwc = v / (word32)sizeof(PKCS12_WORD); |
775 | 0 | nBlocks = iLen / v; |
776 | |
|
777 | | #ifdef WOLFSSL_SMALL_STACK |
778 | | B = (byte*)XMALLOC(WC_MAX_BLOCK_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER); |
779 | | if (B == NULL) |
780 | | return MEMORY_E; |
781 | | #endif |
782 | 0 | Bw = (PKCS12_WORD*)B; |
783 | |
|
784 | 0 | if (totalLen > sizeof(staticBuffer)) { |
785 | 0 | buffer = (byte*)XMALLOC(totalLen, heap, DYNAMIC_TYPE_KEY); |
786 | 0 | if (buffer == NULL) { |
787 | 0 | WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER); |
788 | 0 | return MEMORY_E; |
789 | 0 | } |
790 | 0 | } |
791 | | |
792 | | /* RFC 7292 B.2 step 1: D = v bytes each set to ID */ |
793 | | /* RFC 7292 B.2 step 4: I = S || P; buffer = D || I */ |
794 | 0 | I = buffer + v; |
795 | 0 | XMEMSET(buffer, id, v); |
796 | 0 | for (i = 0; i < sLen; i++) |
797 | 0 | I[i] = salt[i % (word32)saltLen]; |
798 | 0 | for (i = 0; i < pLen; i++) |
799 | 0 | I[sLen + i] = passwd[i % (word32)passLen]; |
800 | |
|
801 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
802 | | wc_MemZero_Add("wc_PKCS12_PBKDF_ex buffer", buffer, totalLen); |
803 | | #endif |
804 | |
|
805 | 0 | ret = 0; |
806 | 0 | while ((ret == 0) && (kLen > 0)) { |
807 | | /* RFC 7292 B.2 step 6a: A_i = H^r(D || I) */ |
808 | 0 | ret = DoPKCS12Hash(hashT, buffer, totalLen, B, u, iterations); |
809 | 0 | if (ret != 0) |
810 | 0 | break; |
811 | | |
812 | | /* RFC 7292 B.2 step 7: output A_i bytes (up to kLen) */ |
813 | 0 | i = min((word32)kLen, u); |
814 | 0 | XMEMCPY(output, B, i); |
815 | 0 | output += i; |
816 | 0 | kLen -= (int)i; |
817 | 0 | if (kLen == 0) |
818 | 0 | break; |
819 | | |
820 | | /* RFC 7292 B.2 step 6b: B = A_i repeated to length v */ |
821 | 0 | for (i = u; i < v; i++) |
822 | 0 | B[i] = B[i % u]; |
823 | | |
824 | | /* RFC 7292 B.2 step 6c: I_j = (I_j + B + 1) mod 2^(8v). */ |
825 | 0 | #ifndef BIG_ENDIAN_ORDER |
826 | 0 | PKCS12_ByteReverseWords(Bw, Bw, v); |
827 | 0 | #endif |
828 | | /* Increment B by 1. */ |
829 | 0 | for (k = nwc; k > 0; ) { |
830 | 0 | --k; |
831 | 0 | ++Bw[k]; |
832 | 0 | if (Bw[k] != 0) |
833 | 0 | break; |
834 | 0 | } |
835 | |
|
836 | 0 | #ifndef BIG_ENDIAN_ORDER |
837 | 0 | PKCS12_ByteReverseWords((PKCS12_WORD*)I, (PKCS12_WORD*)I, nBlocks * v); |
838 | 0 | #endif |
839 | | /* Add B+1 to each I_j block. */ |
840 | 0 | for (blk = 0; blk < nBlocks; blk++) { |
841 | 0 | PKCS12_DWORD c = 0; |
842 | 0 | PKCS12_WORD* Iw = (PKCS12_WORD*)(I + blk * v); |
843 | 0 | for (k = nwc; k-- > 0; ) { |
844 | 0 | c += (PKCS12_DWORD)Iw[k]; |
845 | 0 | c += (PKCS12_DWORD)Bw[k]; |
846 | 0 | Iw[k] = (PKCS12_WORD)c; |
847 | 0 | c >>= 8 * sizeof(PKCS12_WORD); |
848 | 0 | } |
849 | 0 | } |
850 | 0 | #ifndef BIG_ENDIAN_ORDER |
851 | 0 | PKCS12_ByteReverseWords((PKCS12_WORD*)I, (PKCS12_WORD*)I, nBlocks * v); |
852 | 0 | #endif |
853 | 0 | } |
854 | |
|
855 | 0 | ForceZero(B, WC_MAX_BLOCK_SIZE); |
856 | 0 | WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER); |
857 | 0 | ForceZero(buffer, totalLen); |
858 | | #if defined(WOLFSSL_CHECK_MEM_ZERO) |
859 | | if (buffer == staticBuffer) |
860 | | wc_MemZero_Check(buffer, totalLen); |
861 | | #endif |
862 | 0 | if (buffer != staticBuffer) { |
863 | 0 | XFREE(buffer, heap, DYNAMIC_TYPE_KEY); |
864 | 0 | } |
865 | |
|
866 | 0 | return ret; |
867 | 0 | } |
868 | | |
869 | | #undef PKCS12_DWORD |
870 | | #undef PKCS12_WORD |
871 | | #undef PKCS12_ByteReverseWords |
872 | | |
873 | | #endif |
874 | | |
875 | | #endif /* HAVE_PKCS12 */ |
876 | | |
877 | | #ifdef HAVE_SCRYPT |
878 | | #ifdef NO_HMAC |
879 | | #error scrypt requires HMAC |
880 | | #endif |
881 | | |
882 | | /* Rotate the 32-bit value a by b bits to the left. |
883 | | * |
884 | | * a 32-bit value. |
885 | | * b Number of bits to rotate. |
886 | | * returns rotated value. |
887 | | */ |
888 | | #define R(a, b) rotlFixed(a, b) |
889 | | |
890 | | /* (2^32 - 1) */ |
891 | | #define SCRYPT_WORD32_MAX 4294967295U |
892 | | |
893 | | /* One round of Salsa20/8. |
894 | | * Code taken from RFC 7914: scrypt PBKDF. |
895 | | * |
896 | | * out Output buffer. |
897 | | * in Input data to hash. |
898 | | */ |
899 | | static void scryptSalsa(word32* out, word32* in) |
900 | | { |
901 | | int i; |
902 | | word32 x[16]; |
903 | | |
904 | | #ifdef LITTLE_ENDIAN_ORDER |
905 | | XMEMCPY(x, in, sizeof(x)); |
906 | | #else |
907 | | for (i = 0; i < 16; i++) |
908 | | x[i] = ByteReverseWord32(in[i]); |
909 | | #endif |
910 | | for (i = 8; i > 0; i -= 2) { |
911 | | x[ 4] ^= R(x[ 0] + x[12], 7); x[ 8] ^= R(x[ 4] + x[ 0], 9); |
912 | | x[12] ^= R(x[ 8] + x[ 4], 13); x[ 0] ^= R(x[12] + x[ 8], 18); |
913 | | x[ 9] ^= R(x[ 5] + x[ 1], 7); x[13] ^= R(x[ 9] + x[ 5], 9); |
914 | | x[ 1] ^= R(x[13] + x[ 9], 13); x[ 5] ^= R(x[ 1] + x[13], 18); |
915 | | x[14] ^= R(x[10] + x[ 6], 7); x[ 2] ^= R(x[14] + x[10], 9); |
916 | | x[ 6] ^= R(x[ 2] + x[14], 13); x[10] ^= R(x[ 6] + x[ 2], 18); |
917 | | x[ 3] ^= R(x[15] + x[11], 7); x[ 7] ^= R(x[ 3] + x[15], 9); |
918 | | x[11] ^= R(x[ 7] + x[ 3], 13); x[15] ^= R(x[11] + x[ 7], 18); |
919 | | x[ 1] ^= R(x[ 0] + x[ 3], 7); x[ 2] ^= R(x[ 1] + x[ 0], 9); |
920 | | x[ 3] ^= R(x[ 2] + x[ 1], 13); x[ 0] ^= R(x[ 3] + x[ 2], 18); |
921 | | x[ 6] ^= R(x[ 5] + x[ 4], 7); x[ 7] ^= R(x[ 6] + x[ 5], 9); |
922 | | x[ 4] ^= R(x[ 7] + x[ 6], 13); x[ 5] ^= R(x[ 4] + x[ 7], 18); |
923 | | x[11] ^= R(x[10] + x[ 9], 7); x[ 8] ^= R(x[11] + x[10], 9); |
924 | | x[ 9] ^= R(x[ 8] + x[11], 13); x[10] ^= R(x[ 9] + x[ 8], 18); |
925 | | x[12] ^= R(x[15] + x[14], 7); x[13] ^= R(x[12] + x[15], 9); |
926 | | x[14] ^= R(x[13] + x[12], 13); x[15] ^= R(x[14] + x[13], 18); |
927 | | } |
928 | | #ifdef LITTLE_ENDIAN_ORDER |
929 | | for (i = 0; i < 16; ++i) |
930 | | out[i] = in[i] + x[i]; |
931 | | #else |
932 | | for (i = 0; i < 16; i++) |
933 | | out[i] = ByteReverseWord32(ByteReverseWord32(in[i]) + x[i]); |
934 | | #endif |
935 | | } |
936 | | |
937 | | /* Mix a block using Salsa20/8. |
938 | | * Based on RFC 7914: scrypt PBKDF. |
939 | | * |
940 | | * b Blocks to mix. |
941 | | * y Temporary storage. |
942 | | * r Size of the block. |
943 | | */ |
944 | | static void scryptBlockMix(byte* b, byte* y, int r) |
945 | | { |
946 | | #ifdef WORD64_AVAILABLE |
947 | | word64 x[8]; |
948 | | word32 x32[16]; |
949 | | word64* b64 = (word64*)b; |
950 | | word64* y64 = (word64*)y; |
951 | | #else |
952 | | word32 x[16]; |
953 | | word32* b32 = (word32*)b; |
954 | | word32* y32 = (word32*)y; |
955 | | #endif |
956 | | int i; |
957 | | int j; |
958 | | |
959 | | /* Step 1. */ |
960 | | XMEMCPY(x, b + (2 * r - 1) * 64, sizeof(x)); |
961 | | /* Step 2. */ |
962 | | for (i = 0; i < 2 * r; i++) |
963 | | { |
964 | | #ifdef WORD64_AVAILABLE |
965 | | for (j = 0; j < 8; j++) |
966 | | x[j] ^= b64[i * 8 + j]; |
967 | | /* C11 6.5p7: x is word64, so run the word32 Salsa on a word32 copy. */ |
968 | | XMEMCPY(x32, x, sizeof(x32)); |
969 | | scryptSalsa(x32, x32); |
970 | | XMEMCPY(x, x32, sizeof(x)); |
971 | | #else |
972 | | for (j = 0; j < 16; j++) |
973 | | x[j] ^= b32[i * 16 + j]; |
974 | | scryptSalsa(x, x); |
975 | | #endif |
976 | | XMEMCPY(y + i * 64, x, sizeof(x)); |
977 | | } |
978 | | /* Step 3. */ |
979 | | for (i = 0; i < r; i++) { |
980 | | #ifdef WORD64_AVAILABLE |
981 | | for (j = 0; j < 8; j++) { |
982 | | b64[i * 8 + j] = y64[2 * i * 8 + j]; |
983 | | b64[(r + i) * 8 + j] = y64[(2 * i + 1) * 8 + j]; |
984 | | } |
985 | | #else |
986 | | for (j = 0; j < 16; j++) { |
987 | | b32[i * 16 + j] = y32[2 * i * 16 + j]; |
988 | | b32[(r + i) * 16 + j] = y32[(2 * i + 1) * 16 + j]; |
989 | | } |
990 | | #endif |
991 | | } |
992 | | } |
993 | | |
994 | | /* Random oracles mix. |
995 | | * Based on RFC 7914: scrypt PBKDF. |
996 | | * |
997 | | * x Data to mix. |
998 | | * v Temporary buffer. |
999 | | * y Temporary buffer for the block mix. |
1000 | | * r Block size parameter. |
1001 | | * n CPU/Memory cost parameter. |
1002 | | */ |
1003 | | static void scryptROMix(byte* x, byte* v, byte* y, int r, word32 n) |
1004 | | { |
1005 | | word32 i; |
1006 | | word32 j; |
1007 | | word32 k; |
1008 | | word32 bSz = (word32)(128 * r); |
1009 | | #ifdef WORD64_AVAILABLE |
1010 | | word64* x64 = (word64*)x; |
1011 | | word64* v64 = (word64*)v; |
1012 | | #else |
1013 | | word32* x32 = (word32*)x; |
1014 | | word32* v32 = (word32*)v; |
1015 | | #endif |
1016 | | |
1017 | | /* Step 1. X = B (B not needed therefore not implemented) */ |
1018 | | /* Step 2. */ |
1019 | | for (i = 0; i < n; i++) |
1020 | | { |
1021 | | XMEMCPY(v + i * bSz, x, bSz); |
1022 | | scryptBlockMix(x, y, r); |
1023 | | } |
1024 | | |
1025 | | /* Step 3. */ |
1026 | | for (i = 0; i < n; i++) |
1027 | | { |
1028 | | #ifdef LITTLE_ENDIAN_ORDER |
1029 | | /* x is an allocator byte array; the big-endian path below already |
1030 | | * assembles this byte-wise. */ |
1031 | | #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS) |
1032 | | j = (word32)(readUnalignedWord64(x + (2*r - 1) * 64) & (n-1)); |
1033 | | #else |
1034 | | j = readUnalignedWord32(x + (2*r - 1) * 64) & (n-1); |
1035 | | #endif |
1036 | | #else |
1037 | | byte* t = x + (2*r - 1) * 64; |
1038 | | j = ((word32)t[0] | ((word32)t[1] << 8) | ((word32)t[2] << 16) | |
1039 | | ((word32)t[3] << 24)) & (n-1); |
1040 | | #endif |
1041 | | #ifdef WORD64_AVAILABLE |
1042 | | for (k = 0; k < bSz / 8; k++) |
1043 | | x64[k] ^= v64[j * bSz / 8 + k]; |
1044 | | #else |
1045 | | for (k = 0; k < bSz / 4; k++) |
1046 | | x32[k] ^= v32[j * bSz / 4 + k]; |
1047 | | #endif |
1048 | | scryptBlockMix(x, y, r); |
1049 | | } |
1050 | | /* Step 4. B' = X (B = X = B' so not needed, therefore not implemented) */ |
1051 | | } |
1052 | | |
1053 | | /* Generates an key derived from a password and salt using a memory hard |
1054 | | * algorithm. |
1055 | | * Implements RFC 7914: scrypt PBKDF. |
1056 | | * |
1057 | | * output The derived key. |
1058 | | * passwd The password to derive key from. |
1059 | | * passLen The length of the password. |
1060 | | * salt The key specific data. |
1061 | | * saltLen The length of the salt data. |
1062 | | * cost The CPU/memory cost parameter. Range: 1..(128*r/8-1) |
1063 | | * (Iterations = 2^cost) |
1064 | | * blockSize The number of 128 byte octets in a working block. |
1065 | | * parallel The number of parallel mix operations to perform. |
1066 | | * (Note: this implementation does not use threads.) |
1067 | | * dkLen The length of the derived key in bytes. |
1068 | | * returns BAD_FUNC_ARG when: blockSize is too large for cost. |
1069 | | */ |
1070 | | int wc_scrypt(byte* output, const byte* passwd, int passLen, |
1071 | | const byte* salt, int saltLen, int cost, int blockSize, |
1072 | | int parallel, int dkLen) |
1073 | | { |
1074 | | int ret = 0; |
1075 | | int i; |
1076 | | byte* v = NULL; |
1077 | | byte* y = NULL; |
1078 | | byte* blocks = NULL; |
1079 | | word32 blocksSz; |
1080 | | word32 bSz; |
1081 | | |
1082 | | if (blockSize > 8) |
1083 | | return BAD_FUNC_ARG; |
1084 | | |
1085 | | if (cost < 1 || cost >= 128 * blockSize / 8 || parallel < 1 || dkLen < 1) |
1086 | | return BAD_FUNC_ARG; |
1087 | | |
1088 | | /* The following comparison used to be: |
1089 | | * ((word32)parallel > (SCRYPT_MAX / (128 * blockSize))) |
1090 | | * where SCRYPT_MAX is (2^32 - 1) * 32. For some compilers, the RHS of |
1091 | | * the comparison is greater than parallel's type. It wouldn't promote |
1092 | | * both sides to word64. What follows is just arithmetic simplification. |
1093 | | */ |
1094 | | if (parallel > (int)((SCRYPT_WORD32_MAX / 4) / (word32)blockSize)) |
1095 | | return BAD_FUNC_ARG; |
1096 | | |
1097 | | bSz = 128 * (word32)blockSize; |
1098 | | if (parallel > (int)(SCRYPT_WORD32_MAX / bSz)) |
1099 | | return BAD_FUNC_ARG; |
1100 | | blocksSz = bSz * (word32)parallel; |
1101 | | blocks = (byte*)XMALLOC((size_t)blocksSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1102 | | if (blocks == NULL) { |
1103 | | ret = MEMORY_E; |
1104 | | goto end; |
1105 | | } |
1106 | | |
1107 | | /* Check that (1 << cost) * bSz won't overflow or exceed allowed max */ |
1108 | | if (((size_t)1 << cost) * (size_t)bSz > SCRYPT_WORD32_MAX) { |
1109 | | ret = BAD_FUNC_ARG; |
1110 | | goto end; |
1111 | | } |
1112 | | |
1113 | | /* Temporary for scryptROMix. */ |
1114 | | v = (byte*)XMALLOC(((size_t)1 << cost) * (size_t)bSz, NULL, |
1115 | | DYNAMIC_TYPE_TMP_BUFFER); |
1116 | | if (v == NULL) { |
1117 | | ret = MEMORY_E; |
1118 | | goto end; |
1119 | | } |
1120 | | /* Temporary for scryptBlockMix. */ |
1121 | | y = (byte*)XMALLOC((size_t)(blockSize * 128), NULL, |
1122 | | DYNAMIC_TYPE_TMP_BUFFER); |
1123 | | if (y == NULL) { |
1124 | | ret = MEMORY_E; |
1125 | | goto end; |
1126 | | } |
1127 | | |
1128 | | XMEMSET(y, 0, (size_t)(blockSize * 128)); |
1129 | | |
1130 | | /* Step 1. */ |
1131 | | ret = wc_PBKDF2(blocks, passwd, passLen, salt, saltLen, 1, (int)blocksSz, |
1132 | | WC_SHA256); |
1133 | | if (ret != 0) |
1134 | | goto end; |
1135 | | |
1136 | | /* Step 2. */ |
1137 | | for (i = 0; i < parallel; i++) |
1138 | | scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize, |
1139 | | (word32)((size_t)1 << cost)); |
1140 | | |
1141 | | /* Step 3. */ |
1142 | | ret = wc_PBKDF2(output, passwd, passLen, blocks, (int)blocksSz, 1, dkLen, |
1143 | | WC_SHA256); |
1144 | | end: |
1145 | | if (blocks != NULL) { |
1146 | | ForceZero(blocks, blocksSz); |
1147 | | } |
1148 | | if (v != NULL) { |
1149 | | ForceZero(v, ((size_t)1 << cost) * (size_t)bSz); |
1150 | | } |
1151 | | if (y != NULL) { |
1152 | | ForceZero(y, (size_t)blockSize * 128); |
1153 | | } |
1154 | | |
1155 | | XFREE(blocks, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1156 | | XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1157 | | XFREE(y, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1158 | | |
1159 | | return ret; |
1160 | | } |
1161 | | |
1162 | | /* Generates an key derived from a password and salt using a memory hard |
1163 | | * algorithm. |
1164 | | * Implements RFC 7914: scrypt PBKDF. |
1165 | | * |
1166 | | * output Derived key. |
1167 | | * passwd Password to derive key from. |
1168 | | * passLen Length of the password. |
1169 | | * salt Key specific data. |
1170 | | * saltLen Length of the salt data. |
1171 | | * iterations Number of iterations to perform. Range: 1 << (1..(128*r/8-1)) |
1172 | | * blockSize Number of 128 byte octets in a working block. |
1173 | | * parallel Number of parallel mix operations to perform. |
1174 | | * (Note: this implementation does not use threads.) |
1175 | | * dkLen Length of the derived key in bytes. |
1176 | | * returns BAD_FUNC_ARG when: iterations is not a power of 2 or blockSize is too |
1177 | | * large for iterations. |
1178 | | */ |
1179 | | int wc_scrypt_ex(byte* output, const byte* passwd, int passLen, |
1180 | | const byte* salt, int saltLen, word32 iterations, |
1181 | | int blockSize, int parallel, int dkLen) |
1182 | | { |
1183 | | int cost; |
1184 | | |
1185 | | /* Iterations must be a power of 2. */ |
1186 | | if ((iterations & (iterations - 1)) != 0) |
1187 | | return BAD_FUNC_ARG; |
1188 | | |
1189 | | for (cost = -1; iterations != 0; cost++) { |
1190 | | iterations >>= 1; |
1191 | | } |
1192 | | |
1193 | | return wc_scrypt(output, passwd, passLen, salt, saltLen, cost, blockSize, |
1194 | | parallel, dkLen); |
1195 | | } |
1196 | | #endif /* HAVE_SCRYPT */ |
1197 | | |
1198 | | #endif /* NO_PWDBASED */ |