/src/wolfssl/wolfcrypt/src/pwdbased.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* pwdbased.c  | 
2  |  |  *  | 
3  |  |  * Copyright (C) 2006-2025 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  |  | #include <wolfssl/wolfcrypt/libwolfssl_sources.h>  | 
23  |  |  | 
24  |  | #ifndef NO_PWDBASED  | 
25  |  |  | 
26  |  | #if FIPS_VERSION3_GE(6,0,0)  | 
27  |  |     /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */  | 
28  |  |     #define FIPS_NO_WRAPPERS  | 
29  |  |  | 
30  |  |        #ifdef USE_WINDOWS_API  | 
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  |  |  | 
41  |  | #ifdef NO_INLINE  | 
42  |  |     #include <wolfssl/wolfcrypt/misc.h>  | 
43  |  | #else  | 
44  |  |     #define WOLFSSL_MISC_INCLUDED  | 
45  |  |     #include <wolfcrypt/src/misc.c>  | 
46  |  | #endif  | 
47  |  |  | 
48  |  | #if FIPS_VERSION3_GE(6,0,0)  | 
49  |  |     const unsigned int wolfCrypt_FIPS_pbkdf_ro_sanity[2] =  | 
50  |  |                                                      { 0x1a2b3c4d, 0x00000010 }; | 
51  |  |     int wolfCrypt_FIPS_PBKDF_sanity(void)  | 
52  |  |     { | 
53  |  |         return 0;  | 
54  |  |     }  | 
55  |  | #endif  | 
56  |  |  | 
57  |  | #ifdef HAVE_PBKDF1  | 
58  |  |  | 
59  |  | /* PKCS#5 v1.5 with non standard extension to optionally derive the extra data (IV) */  | 
60  |  | int wc_PBKDF1_ex(byte* key, int keyLen, byte* iv, int ivLen,  | 
61  |  |     const byte* passwd, int passwdLen, const byte* salt, int saltLen,  | 
62  |  |     int iterations, int hashType, void* heap)  | 
63  | 0  | { | 
64  | 0  |     int  err;  | 
65  | 0  |     int  keyLeft, ivLeft, i;  | 
66  | 0  |     int  store;  | 
67  | 0  |     int  keyOutput = 0;  | 
68  | 0  |     int  digestLen;  | 
69  | 0  |     byte digest[WC_MAX_DIGEST_SIZE];  | 
70  |  | #ifdef WOLFSSL_SMALL_STACK  | 
71  |  |     wc_HashAlg* hash = NULL;  | 
72  |  | #else  | 
73  | 0  |     wc_HashAlg  hash[1];  | 
74  | 0  | #endif  | 
75  | 0  |     enum wc_HashType hashT;  | 
76  |  | 
  | 
77  | 0  |     (void)heap;  | 
78  |  | 
  | 
79  | 0  |     if (key == NULL || keyLen < 0 || passwdLen < 0 || saltLen < 0 || ivLen < 0){ | 
80  | 0  |         return BAD_FUNC_ARG;  | 
81  | 0  |     }  | 
82  |  |  | 
83  | 0  |     if (iterations <= 0)  | 
84  | 0  |         iterations = 1;  | 
85  |  | 
  | 
86  | 0  |     hashT = wc_HashTypeConvert(hashType);  | 
87  | 0  |     err = wc_HashGetDigestSize(hashT);  | 
88  | 0  |     if (err < 0)  | 
89  | 0  |         return err;  | 
90  | 0  |     digestLen = err;  | 
91  |  |  | 
92  |  |     /* initialize hash */  | 
93  |  | #ifdef WOLFSSL_SMALL_STACK  | 
94  |  |     hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap,  | 
95  |  |                                 DYNAMIC_TYPE_HASHCTX);  | 
96  |  |     if (hash == NULL)  | 
97  |  |         return MEMORY_E;  | 
98  |  | #endif  | 
99  |  | 
  | 
100  | 0  |     err = wc_HashInit_ex(hash, hashT, heap, INVALID_DEVID);  | 
101  | 0  |     if (err != 0) { | 
102  |  |     #ifdef WOLFSSL_SMALL_STACK  | 
103  |  |         XFREE(hash, heap, DYNAMIC_TYPE_HASHCTX);  | 
104  |  |     #endif  | 
105  | 0  |         return err;  | 
106  | 0  |     }  | 
107  |  |  | 
108  | 0  |     keyLeft = keyLen;  | 
109  | 0  |     ivLeft  = ivLen;  | 
110  | 0  |     while (keyOutput < (keyLen + ivLen)) { | 
111  | 0  |         int digestLeft = digestLen;  | 
112  |  |         /* D_(i - 1) */  | 
113  | 0  |         if (keyOutput) { /* first time D_0 is empty */ | 
114  | 0  |             err = wc_HashUpdate(hash, hashT, digest, (word32)digestLen);  | 
115  | 0  |             if (err != 0) break;  | 
116  | 0  |         }  | 
117  |  |  | 
118  |  |         /* data */  | 
119  | 0  |         err = wc_HashUpdate(hash, hashT, passwd, (word32)passwdLen);  | 
120  | 0  |         if (err != 0) break;  | 
121  |  |  | 
122  |  |         /* salt */  | 
123  | 0  |         if (salt) { | 
124  | 0  |             err = wc_HashUpdate(hash, hashT, salt, (word32)saltLen);  | 
125  | 0  |             if (err != 0) break;  | 
126  | 0  |         }  | 
127  |  |  | 
128  | 0  |         err = wc_HashFinal(hash, hashT, digest);  | 
129  | 0  |         if (err != 0) break;  | 
130  |  |  | 
131  |  |         /* count */  | 
132  | 0  |         for (i = 1; i < iterations; i++) { | 
133  | 0  |             err = wc_HashUpdate(hash, hashT, digest, (word32)digestLen);  | 
134  | 0  |             if (err != 0) break;  | 
135  |  |  | 
136  | 0  |             err = wc_HashFinal(hash, hashT, digest);  | 
137  | 0  |             if (err != 0) break;  | 
138  | 0  |         }  | 
139  |  | 
  | 
140  | 0  |         if (err != 0) break;  | 
141  |  |  | 
142  | 0  |         if (keyLeft) { | 
143  | 0  |             store = (int)min((word32)keyLeft, (word32)digestLen);  | 
144  | 0  |             XMEMCPY(&key[keyLen - keyLeft], digest, (size_t)store);  | 
145  |  | 
  | 
146  | 0  |             keyOutput  += store;  | 
147  | 0  |             keyLeft    -= store;  | 
148  | 0  |             digestLeft -= store;  | 
149  | 0  |         }  | 
150  |  | 
  | 
151  | 0  |         if (ivLeft && digestLeft) { | 
152  | 0  |             store = (int)min((word32)ivLeft, (word32)digestLeft);  | 
153  | 0  |             if (iv != NULL)  | 
154  | 0  |                 XMEMCPY(&iv[ivLen - ivLeft],  | 
155  | 0  |                         &digest[digestLen - digestLeft], (size_t)store);  | 
156  | 0  |             keyOutput += store;  | 
157  | 0  |             ivLeft    -= store;  | 
158  | 0  |         }  | 
159  | 0  |     }  | 
160  |  | 
  | 
161  | 0  |     wc_HashFree(hash, hashT);  | 
162  |  | 
  | 
163  |  | #ifdef WOLFSSL_SMALL_STACK  | 
164  |  |     XFREE(hash, heap, DYNAMIC_TYPE_HASHCTX);  | 
165  |  | #endif  | 
166  |  | 
  | 
167  | 0  |     if (err != 0)  | 
168  | 0  |         return err;  | 
169  |  |  | 
170  | 0  |     if (keyOutput != (keyLen + ivLen))  | 
171  | 0  |         return BUFFER_E;  | 
172  |  |  | 
173  | 0  |     return err;  | 
174  | 0  | }  | 
175  |  |  | 
176  |  | /* PKCS#5 v1.5 */  | 
177  |  | int wc_PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt,  | 
178  |  |            int sLen, int iterations, int kLen, int hashType)  | 
179  | 0  | { | 
180  |  | 
  | 
181  | 0  |     return wc_PBKDF1_ex(output, kLen, NULL, 0,  | 
182  | 0  |         passwd, pLen, salt, sLen, iterations, hashType, NULL);  | 
183  | 0  | }  | 
184  |  |  | 
185  |  | #endif /* HAVE_PKCS5 */  | 
186  |  |  | 
187  |  | #if defined(HAVE_PBKDF2) && !defined(NO_HMAC)  | 
188  |  |  | 
189  |  | int wc_PBKDF2_ex(byte* output, const byte* passwd, int pLen, const byte* salt,  | 
190  |  |            int sLen, int iterations, int kLen, int hashType, void* heap, int devId)  | 
191  | 0  | { | 
192  | 0  |     int    hLen;  | 
193  | 0  |     int    ret;  | 
194  |  | #ifdef WOLFSSL_SMALL_STACK  | 
195  |  |     byte*  buffer;  | 
196  |  |     Hmac*  hmac;  | 
197  |  | #else  | 
198  | 0  |     byte   buffer[WC_MAX_DIGEST_SIZE];  | 
199  | 0  |     Hmac   hmac[1];  | 
200  | 0  | #endif  | 
201  | 0  |     enum wc_HashType hashT;  | 
202  |  | 
  | 
203  | 0  |     if (output == NULL || pLen < 0 || sLen < 0 || kLen < 0) { | 
204  | 0  |         return BAD_FUNC_ARG;  | 
205  | 0  |     }  | 
206  |  |  | 
207  |  | #if FIPS_VERSION3_GE(6,0,0)  | 
208  |  |     /* Per SP800-132 section 5 "The kLen value shall be at least 112 bits in  | 
209  |  |      * length", ensure the returned bits for the derived master key are at a  | 
210  |  |      * minimum 14-bytes or 112-bits after stretching and strengthening  | 
211  |  |      * (iterations) */  | 
212  |  |     if (kLen < HMAC_FIPS_MIN_KEY)  | 
213  |  |         return BAD_LENGTH_E;  | 
214  |  | #endif  | 
215  |  |  | 
216  |  | #if FIPS_VERSION3_GE(6,0,0) && defined(DEBUG_WOLFSSL)  | 
217  |  |     /* SP800-132 section 5.2 recommends an iteration count of 1000 but this is  | 
218  |  |      * not strictly enforceable and is listed in Appendix B Table 1 as a  | 
219  |  |      * non-testable requirement. wolfCrypt will log it when appropriate but  | 
220  |  |      * take no action */  | 
221  |  |     if (iterations < 1000) { | 
222  |  |         WOLFSSL_MSG("WARNING: Iteration < 1,000, see SP800-132 section 5.2"); | 
223  |  |     }  | 
224  |  | #endif  | 
225  | 0  |     if (iterations <= 0)  | 
226  | 0  |         iterations = 1;  | 
227  |  | 
  | 
228  | 0  |     hashT = wc_HashTypeConvert(hashType);  | 
229  | 0  |     hLen = wc_HashGetDigestSize(hashT);  | 
230  | 0  |     if (hLen < 0)  | 
231  | 0  |         return BAD_FUNC_ARG;  | 
232  |  |  | 
233  |  | #ifdef WOLFSSL_SMALL_STACK  | 
234  |  |     buffer = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
235  |  |     if (buffer == NULL)  | 
236  |  |         return MEMORY_E;  | 
237  |  |     hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap, DYNAMIC_TYPE_HMAC);  | 
238  |  |     if (hmac == NULL) { | 
239  |  |         XFREE(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
240  |  |         return MEMORY_E;  | 
241  |  |     }  | 
242  |  | #endif  | 
243  |  |  | 
244  | 0  |     ret = wc_HmacInit(hmac, heap, devId);  | 
245  | 0  |     if (ret == 0) { | 
246  | 0  |         word32 i = 1;  | 
247  |  |         /* use int hashType here, since HMAC FIPS uses the old unique value */  | 
248  |  |     #if FIPS_VERSION3_GE(6,0,0)  | 
249  |  |         { | 
250  |  |             /* Allow passwords that are less than 14-bytes for compatibility  | 
251  |  |              * / interoperability, only since module v6.0.0 */  | 
252  |  |             int allowShortPasswd = 1;  | 
253  |  |             ret = wc_HmacSetKey_ex(hmac, hashType, passwd, (word32)pLen,  | 
254  |  |                                    allowShortPasswd);  | 
255  |  |         }  | 
256  |  |     #else  | 
257  | 0  |         ret = wc_HmacSetKey(hmac, hashType, passwd, (word32)pLen);  | 
258  | 0  |     #endif  | 
259  |  | 
  | 
260  | 0  |         while (ret == 0 && kLen) { | 
261  | 0  |             int currentLen;  | 
262  | 0  |             int j;  | 
263  |  | 
  | 
264  | 0  |             ret = wc_HmacUpdate(hmac, salt, (word32)sLen);  | 
265  | 0  |             if (ret != 0)  | 
266  | 0  |                 break;  | 
267  |  |  | 
268  |  |             /* encode i */  | 
269  | 0  |             for (j = 0; j < 4; j++) { | 
270  | 0  |                 byte b = (byte)(i >> ((3-j) * 8));  | 
271  |  | 
  | 
272  | 0  |                 ret = wc_HmacUpdate(hmac, &b, 1);  | 
273  | 0  |                 if (ret != 0)  | 
274  | 0  |                     break;  | 
275  | 0  |             }  | 
276  |  |  | 
277  |  |             /* check ret from inside for loop */  | 
278  | 0  |             if (ret != 0)  | 
279  | 0  |                 break;  | 
280  |  |  | 
281  | 0  |             ret = wc_HmacFinal(hmac, buffer);  | 
282  | 0  |             if (ret != 0)  | 
283  | 0  |                 break;  | 
284  |  |  | 
285  | 0  |             currentLen = (int)min((word32)kLen, (word32)hLen);  | 
286  | 0  |             XMEMCPY(output, buffer, (size_t)currentLen);  | 
287  |  | 
  | 
288  | 0  |             for (j = 1; j < iterations; j++) { | 
289  | 0  |                 ret = wc_HmacUpdate(hmac, buffer, (word32)hLen);  | 
290  | 0  |                 if (ret != 0)  | 
291  | 0  |                     break;  | 
292  | 0  |                 ret = wc_HmacFinal(hmac, buffer);  | 
293  | 0  |                 if (ret != 0)  | 
294  | 0  |                     break;  | 
295  | 0  |                 xorbuf(output, buffer, (word32)currentLen);  | 
296  | 0  |             }  | 
297  |  |  | 
298  |  |             /* check ret from inside for loop */  | 
299  | 0  |             if (ret != 0)  | 
300  | 0  |                 break;  | 
301  |  |  | 
302  | 0  |             output += currentLen;  | 
303  | 0  |             kLen   -= currentLen;  | 
304  | 0  |             i++;  | 
305  | 0  |         }  | 
306  | 0  |         wc_HmacFree(hmac);  | 
307  | 0  |     }  | 
308  |  | 
  | 
309  |  | #ifdef WOLFSSL_SMALL_STACK  | 
310  |  |     XFREE(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
311  |  |     XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);  | 
312  |  | #endif  | 
313  |  | 
  | 
314  | 0  |     return ret;  | 
315  | 0  | }  | 
316  |  |  | 
317  |  | int wc_PBKDF2(byte* output, const byte* passwd, int pLen, const byte* salt,  | 
318  |  |            int sLen, int iterations, int kLen, int hashType)  | 
319  | 0  | { | 
320  | 0  |     return wc_PBKDF2_ex(output, passwd, pLen, salt, sLen, iterations, kLen,  | 
321  | 0  |         hashType, NULL, INVALID_DEVID);  | 
322  | 0  | }  | 
323  |  |  | 
324  |  | #endif /* HAVE_PBKDF2 && !NO_HMAC */  | 
325  |  |  | 
326  |  | #ifdef HAVE_PKCS12  | 
327  |  |  | 
328  |  | /* helper for PKCS12_PBKDF(), does hash operation */  | 
329  |  | static int DoPKCS12Hash(int hashType, byte* buffer, word32 totalLen,  | 
330  |  |                  byte* Ai, word32 u, int iterations)  | 
331  | 0  | { | 
332  | 0  |     int i;  | 
333  | 0  |     int ret = 0;  | 
334  |  | #ifdef WOLFSSL_SMALL_STACK  | 
335  |  |     wc_HashAlg* hash = NULL;  | 
336  |  | #else  | 
337  | 0  |     wc_HashAlg  hash[1];  | 
338  | 0  | #endif  | 
339  | 0  |     enum wc_HashType hashT;  | 
340  |  | 
  | 
341  | 0  |     if (buffer == NULL || Ai == NULL) { | 
342  | 0  |         return BAD_FUNC_ARG;  | 
343  | 0  |     }  | 
344  |  |  | 
345  | 0  |     hashT = wc_HashTypeConvert(hashType);  | 
346  |  |  | 
347  |  |     /* initialize hash */  | 
348  |  | #ifdef WOLFSSL_SMALL_STACK  | 
349  |  |     hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), NULL,  | 
350  |  |                                 DYNAMIC_TYPE_HASHCTX);  | 
351  |  |     if (hash == NULL)  | 
352  |  |         return MEMORY_E;  | 
353  |  | #endif  | 
354  |  | 
  | 
355  | 0  |     ret = wc_HashInit(hash, hashT);  | 
356  | 0  |     if (ret != 0) { | 
357  |  |     #ifdef WOLFSSL_SMALL_STACK  | 
358  |  |         XFREE(hash, NULL, DYNAMIC_TYPE_HASHCTX);  | 
359  |  |     #endif  | 
360  | 0  |         return ret;  | 
361  | 0  |     }  | 
362  |  |  | 
363  | 0  |     ret = wc_HashUpdate(hash, hashT, buffer, totalLen);  | 
364  |  | 
  | 
365  | 0  |     if (ret == 0)  | 
366  | 0  |         ret = wc_HashFinal(hash, hashT, Ai);  | 
367  |  | 
  | 
368  | 0  |     for (i = 1; i < iterations; i++) { | 
369  | 0  |         if (ret == 0)  | 
370  | 0  |             ret = wc_HashUpdate(hash, hashT, Ai, u);  | 
371  | 0  |         if (ret == 0)  | 
372  | 0  |             ret = wc_HashFinal(hash, hashT, Ai);  | 
373  | 0  |     }  | 
374  |  | 
  | 
375  | 0  |     wc_HashFree(hash, hashT);  | 
376  |  | 
  | 
377  |  | #ifdef WOLFSSL_SMALL_STACK  | 
378  |  |     XFREE(hash, NULL, DYNAMIC_TYPE_HASHCTX);  | 
379  |  | #endif  | 
380  |  | 
  | 
381  | 0  |     return ret;  | 
382  | 0  | }  | 
383  |  |  | 
384  |  |  | 
385  |  | int wc_PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,  | 
386  |  |     const byte* salt, int saltLen, int iterations, int kLen, int hashType,  | 
387  |  |     int id)  | 
388  | 0  | { | 
389  | 0  |     return wc_PKCS12_PBKDF_ex(output, passwd, passLen, salt, saltLen,  | 
390  | 0  |                               iterations, kLen, hashType, id, NULL);  | 
391  | 0  | }  | 
392  |  |  | 
393  |  |  | 
394  |  | /* extended API that allows a heap hint to be used */  | 
395  |  | int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen,  | 
396  |  |                        const byte* salt, int saltLen, int iterations, int kLen,  | 
397  |  |                        int hashType, int id, void* heap)  | 
398  | 0  | { | 
399  |  |     /* all in bytes instead of bits */  | 
400  | 0  |     word32 u, v, dLen, pLen, iLen, sLen, totalLen;  | 
401  | 0  |     int    dynamic = 0;  | 
402  | 0  |     int    ret = 0;  | 
403  | 0  |     word32 i;  | 
404  | 0  |     byte   *D, *S, *P, *I;  | 
405  |  | #ifdef WOLFSSL_SMALL_STACK  | 
406  |  |     byte   staticBuffer[1]; /* force dynamic usage */  | 
407  |  | #else  | 
408  | 0  |     byte   staticBuffer[1024];  | 
409  | 0  | #endif  | 
410  | 0  |     byte*  buffer = staticBuffer;  | 
411  |  | 
  | 
412  |  | #ifdef WOLFSSL_SMALL_STACK  | 
413  |  |     byte*  Ai = NULL;  | 
414  |  |     byte*  B = NULL;  | 
415  |  |     mp_int *B1 = NULL;  | 
416  |  |     mp_int *i1 = NULL;  | 
417  |  |     mp_int *res = NULL;  | 
418  |  | #else  | 
419  | 0  |     byte   Ai[WC_MAX_DIGEST_SIZE];  | 
420  | 0  |     byte   B[WC_MAX_BLOCK_SIZE];  | 
421  | 0  |     mp_int B1[1];  | 
422  | 0  |     mp_int i1[1];  | 
423  | 0  |     mp_int res[1];  | 
424  | 0  | #endif  | 
425  | 0  |     enum wc_HashType hashT;  | 
426  |  | 
  | 
427  | 0  |     (void)heap;  | 
428  |  | 
  | 
429  | 0  |     if (output == NULL || passLen <= 0 || saltLen <= 0 || kLen < 0) { | 
430  | 0  |         return BAD_FUNC_ARG;  | 
431  | 0  |     }  | 
432  |  |  | 
433  | 0  |     if (iterations <= 0)  | 
434  | 0  |         iterations = 1;  | 
435  |  | 
  | 
436  | 0  |     hashT = wc_HashTypeConvert(hashType);  | 
437  | 0  |     ret = wc_HashGetDigestSize(hashT);  | 
438  | 0  |     if (ret < 0)  | 
439  | 0  |         return ret;  | 
440  | 0  |     if (ret == 0)  | 
441  | 0  |         return BAD_STATE_E;  | 
442  | 0  |     u = (word32)ret;  | 
443  |  | 
  | 
444  | 0  |     ret = wc_HashGetBlockSize(hashT);  | 
445  | 0  |     if (ret < 0)  | 
446  | 0  |         return ret;  | 
447  | 0  |     if (ret == 0)  | 
448  | 0  |         return BAD_STATE_E;  | 
449  | 0  |     v = (word32)ret;  | 
450  |  | 
  | 
451  |  | #ifdef WOLFSSL_SMALL_STACK  | 
452  |  |     Ai = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
453  |  |     if (Ai == NULL)  | 
454  |  |         return MEMORY_E;  | 
455  |  |  | 
456  |  |     B = (byte*)XMALLOC(WC_MAX_BLOCK_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
457  |  |     if (B == NULL) { | 
458  |  |         XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
459  |  |         return MEMORY_E;  | 
460  |  |     }  | 
461  |  | #endif  | 
462  |  | 
  | 
463  | 0  |     XMEMSET(Ai, 0, WC_MAX_DIGEST_SIZE);  | 
464  | 0  |     XMEMSET(B,  0, WC_MAX_BLOCK_SIZE);  | 
465  |  | 
  | 
466  | 0  |     dLen = v;  | 
467  | 0  |     sLen = v * (((word32)saltLen + v - 1) / v);  | 
468  |  |  | 
469  |  |     /* with passLen checked at the top of the function for >= 0 then passLen  | 
470  |  |      * must be 1 or greater here and is always 'true' */  | 
471  | 0  |     pLen = v * (((word32)passLen + v - 1) / v);  | 
472  | 0  |     iLen = sLen + pLen;  | 
473  |  | 
  | 
474  | 0  |     totalLen = dLen + sLen + pLen;  | 
475  |  | 
  | 
476  | 0  |     if (totalLen > sizeof(staticBuffer)) { | 
477  | 0  |         buffer = (byte*)XMALLOC(totalLen, heap, DYNAMIC_TYPE_KEY);  | 
478  | 0  |         if (buffer == NULL) { | 
479  |  | #ifdef WOLFSSL_SMALL_STACK  | 
480  |  |             XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
481  |  |             XFREE(B,  heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
482  |  | #endif  | 
483  | 0  |             return MEMORY_E;  | 
484  | 0  |         }  | 
485  | 0  |         dynamic = 1;  | 
486  | 0  |     }  | 
487  |  |  | 
488  | 0  |     D = buffer;  | 
489  | 0  |     S = D + dLen;  | 
490  | 0  |     P = S + sLen;  | 
491  | 0  |     I = S;  | 
492  |  | 
  | 
493  | 0  |     XMEMSET(D, id, dLen);  | 
494  |  | 
  | 
495  | 0  |     for (i = 0; i < sLen; i++)  | 
496  | 0  |         S[i] = salt[i % (word32)saltLen];  | 
497  | 0  |     for (i = 0; i < pLen; i++)  | 
498  | 0  |         P[i] = passwd[i % (word32)passLen];  | 
499  |  | 
  | 
500  |  | #ifdef WOLFSSL_SMALL_STACK  | 
501  |  |     if (((B1 = (mp_int *)XMALLOC(sizeof(*B1), heap, DYNAMIC_TYPE_TMP_BUFFER))  | 
502  |  |          == NULL) ||  | 
503  |  |         ((i1 = (mp_int *)XMALLOC(sizeof(*i1), heap, DYNAMIC_TYPE_TMP_BUFFER))  | 
504  |  |          == NULL) ||  | 
505  |  |         ((res = (mp_int *)XMALLOC(sizeof(*res), heap, DYNAMIC_TYPE_TMP_BUFFER))  | 
506  |  |          == NULL)) { | 
507  |  |         ret = MEMORY_E;  | 
508  |  |         goto out;  | 
509  |  |     }  | 
510  |  | #endif  | 
511  |  | 
  | 
512  | 0  |     while (kLen > 0) { | 
513  | 0  |         word32 currentLen;  | 
514  |  | 
  | 
515  | 0  |         ret = DoPKCS12Hash(hashType, buffer, totalLen, Ai, u, iterations);  | 
516  | 0  |         if (ret < 0)  | 
517  | 0  |             break;  | 
518  |  |  | 
519  | 0  |         for (i = 0; i < v; i++)  | 
520  | 0  |             B[i] = Ai[(word32)i % u];  | 
521  |  | 
  | 
522  | 0  |         if (mp_init(B1) != MP_OKAY)  | 
523  | 0  |             ret = MP_INIT_E;  | 
524  | 0  |         else if (mp_read_unsigned_bin(B1, B, v) != MP_OKAY)  | 
525  | 0  |             ret = MP_READ_E;  | 
526  | 0  |         else if (mp_add_d(B1, (mp_digit)1, B1) != MP_OKAY)  | 
527  | 0  |             ret = MP_ADD_E;  | 
528  |  | 
  | 
529  | 0  |         if (ret != 0) { | 
530  | 0  |             mp_clear(B1);  | 
531  | 0  |             break;  | 
532  | 0  |         }  | 
533  |  |  | 
534  | 0  |         for (i = 0; i < iLen; i += v) { | 
535  | 0  |             int    outSz;  | 
536  |  | 
  | 
537  | 0  |             if (mp_init_multi(i1, res, NULL, NULL, NULL, NULL) != MP_OKAY) { | 
538  | 0  |                 ret = MP_INIT_E;  | 
539  | 0  |                 break;  | 
540  | 0  |             }  | 
541  | 0  |             if (mp_read_unsigned_bin(i1, I + i, v) != MP_OKAY)  | 
542  | 0  |                 ret = MP_READ_E;  | 
543  | 0  |             else if (mp_add(i1, B1, res) != MP_OKAY)  | 
544  | 0  |                 ret = MP_ADD_E;  | 
545  | 0  |             else if ( (outSz = mp_unsigned_bin_size(res)) < 0)  | 
546  | 0  |                 ret = MP_TO_E;  | 
547  | 0  |             else { | 
548  | 0  |                 if (outSz > (int)v) { | 
549  |  |                     /* take off MSB */  | 
550  | 0  |                     byte  tmp[WC_MAX_BLOCK_SIZE + 1];  | 
551  | 0  |                     ret = mp_to_unsigned_bin(res, tmp);  | 
552  | 0  |                     XMEMCPY(I + i, tmp + 1, v);  | 
553  | 0  |                 }  | 
554  | 0  |                 else if (outSz < (int)v) { | 
555  | 0  |                     XMEMSET(I + i, 0, v - (word32)outSz);  | 
556  | 0  |                     ret = mp_to_unsigned_bin(res, I + i + v - (word32)outSz);  | 
557  | 0  |                 }  | 
558  | 0  |                 else  | 
559  | 0  |                     ret = mp_to_unsigned_bin(res, I + i);  | 
560  | 0  |             }  | 
561  |  | 
  | 
562  | 0  |             mp_clear(i1);  | 
563  | 0  |             mp_clear(res);  | 
564  | 0  |             if (ret < 0) break;  | 
565  | 0  |         }  | 
566  |  | 
  | 
567  | 0  |         if (ret < 0) { | 
568  | 0  |             mp_clear(B1);  | 
569  | 0  |             break;  | 
570  | 0  |         }  | 
571  |  |  | 
572  | 0  |         currentLen = min((word32)kLen, u);  | 
573  | 0  |         XMEMCPY(output, Ai, currentLen);  | 
574  | 0  |         output += currentLen;  | 
575  | 0  |         kLen   -= (int)currentLen;  | 
576  | 0  |         mp_clear(B1);  | 
577  | 0  |     }  | 
578  |  | 
  | 
579  |  | #ifdef WOLFSSL_SMALL_STACK  | 
580  |  |   out:  | 
581  |  |  | 
582  |  |     XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
583  |  |     XFREE(B, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
584  |  |     XFREE(B1, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
585  |  |     XFREE(i1, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
586  |  |     XFREE(res, heap, DYNAMIC_TYPE_TMP_BUFFER);  | 
587  |  | #endif  | 
588  |  | 
  | 
589  | 0  |     if (dynamic)  | 
590  | 0  |         XFREE(buffer, heap, DYNAMIC_TYPE_KEY);  | 
591  |  | 
  | 
592  | 0  |     return ret;  | 
593  | 0  | }  | 
594  |  |  | 
595  |  | #endif /* HAVE_PKCS12 */  | 
596  |  |  | 
597  |  | #ifdef HAVE_SCRYPT  | 
598  |  | #ifdef NO_HMAC  | 
599  |  |    #error scrypt requires HMAC  | 
600  |  | #endif  | 
601  |  |  | 
602  |  | /* Rotate the 32-bit value a by b bits to the left.  | 
603  |  |  *  | 
604  |  |  * a  32-bit value.  | 
605  |  |  * b  Number of bits to rotate.  | 
606  |  |  * returns rotated value.  | 
607  |  |  */  | 
608  |  | #define R(a, b) rotlFixed(a, b)  | 
609  |  |  | 
610  |  | /* (2^32 - 1) */  | 
611  |  | #define SCRYPT_WORD32_MAX 4294967295U  | 
612  |  |  | 
613  |  | /* One round of Salsa20/8.  | 
614  |  |  * Code taken from RFC 7914: scrypt PBKDF.  | 
615  |  |  *  | 
616  |  |  * out  Output buffer.  | 
617  |  |  * in   Input data to hash.  | 
618  |  |  */  | 
619  |  | static void scryptSalsa(word32* out, word32* in)  | 
620  |  | { | 
621  |  |     int    i;  | 
622  |  |     word32 x[16];  | 
623  |  |  | 
624  |  | #ifdef LITTLE_ENDIAN_ORDER  | 
625  |  |     XMEMCPY(x, in, sizeof(x));  | 
626  |  | #else  | 
627  |  |     for (i = 0; i < 16; i++)  | 
628  |  |         x[i] = ByteReverseWord32(in[i]);  | 
629  |  | #endif  | 
630  |  |     for (i = 8; i > 0; i -= 2) { | 
631  |  |         x[ 4] ^= R(x[ 0] + x[12],  7);  x[ 8] ^= R(x[ 4] + x[ 0],  9);  | 
632  |  |         x[12] ^= R(x[ 8] + x[ 4], 13);  x[ 0] ^= R(x[12] + x[ 8], 18);  | 
633  |  |         x[ 9] ^= R(x[ 5] + x[ 1],  7);  x[13] ^= R(x[ 9] + x[ 5],  9);  | 
634  |  |         x[ 1] ^= R(x[13] + x[ 9], 13);  x[ 5] ^= R(x[ 1] + x[13], 18);  | 
635  |  |         x[14] ^= R(x[10] + x[ 6],  7);  x[ 2] ^= R(x[14] + x[10],  9);  | 
636  |  |         x[ 6] ^= R(x[ 2] + x[14], 13);  x[10] ^= R(x[ 6] + x[ 2], 18);  | 
637  |  |         x[ 3] ^= R(x[15] + x[11],  7);  x[ 7] ^= R(x[ 3] + x[15],  9);  | 
638  |  |         x[11] ^= R(x[ 7] + x[ 3], 13);  x[15] ^= R(x[11] + x[ 7], 18);  | 
639  |  |         x[ 1] ^= R(x[ 0] + x[ 3],  7);  x[ 2] ^= R(x[ 1] + x[ 0],  9);  | 
640  |  |         x[ 3] ^= R(x[ 2] + x[ 1], 13);  x[ 0] ^= R(x[ 3] + x[ 2], 18);  | 
641  |  |         x[ 6] ^= R(x[ 5] + x[ 4],  7);  x[ 7] ^= R(x[ 6] + x[ 5],  9);  | 
642  |  |         x[ 4] ^= R(x[ 7] + x[ 6], 13);  x[ 5] ^= R(x[ 4] + x[ 7], 18);  | 
643  |  |         x[11] ^= R(x[10] + x[ 9],  7);  x[ 8] ^= R(x[11] + x[10],  9);  | 
644  |  |         x[ 9] ^= R(x[ 8] + x[11], 13);  x[10] ^= R(x[ 9] + x[ 8], 18);  | 
645  |  |         x[12] ^= R(x[15] + x[14],  7);  x[13] ^= R(x[12] + x[15],  9);  | 
646  |  |         x[14] ^= R(x[13] + x[12], 13);  x[15] ^= R(x[14] + x[13], 18);  | 
647  |  |     }  | 
648  |  | #ifdef LITTLE_ENDIAN_ORDER  | 
649  |  |     for (i = 0; i < 16; ++i)  | 
650  |  |         out[i] = in[i] + x[i];  | 
651  |  | #else  | 
652  |  |     for (i = 0; i < 16; i++)  | 
653  |  |         out[i] = ByteReverseWord32(ByteReverseWord32(in[i]) + x[i]);  | 
654  |  | #endif  | 
655  |  | }  | 
656  |  |  | 
657  |  | /* Mix a block using Salsa20/8.  | 
658  |  |  * Based on RFC 7914: scrypt PBKDF.  | 
659  |  |  *  | 
660  |  |  * b  Blocks to mix.  | 
661  |  |  * y  Temporary storage.  | 
662  |  |  * r  Size of the block.  | 
663  |  |  */  | 
664  |  | static void scryptBlockMix(byte* b, byte* y, int r)  | 
665  |  | { | 
666  |  | #ifdef WORD64_AVAILABLE  | 
667  |  |     word64  x[8];  | 
668  |  |     word64* b64 = (word64*)b;  | 
669  |  |     word64* y64 = (word64*)y;  | 
670  |  | #else  | 
671  |  |     word32  x[16];  | 
672  |  |     word32* b32 = (word32*)b;  | 
673  |  |     word32* y32 = (word32*)y;  | 
674  |  | #endif  | 
675  |  |     int  i;  | 
676  |  |     int  j;  | 
677  |  |  | 
678  |  |     /* Step 1. */  | 
679  |  |     XMEMCPY(x, b + (2 * r - 1) * 64, sizeof(x));  | 
680  |  |     /* Step 2. */  | 
681  |  |     for (i = 0; i < 2 * r; i++)  | 
682  |  |     { | 
683  |  | #ifdef WORD64_AVAILABLE  | 
684  |  |         for (j = 0; j < 8; j++)  | 
685  |  |             x[j] ^= b64[i * 8 + j];  | 
686  |  |  | 
687  |  | #else  | 
688  |  |         for (j = 0; j < 16; j++)  | 
689  |  |             x[j] ^= b32[i * 16 + j];  | 
690  |  | #endif  | 
691  |  |         scryptSalsa((word32*)x, (word32*)x);  | 
692  |  |         XMEMCPY(y + i * 64, x, sizeof(x));  | 
693  |  |     }  | 
694  |  |     /* Step 3. */  | 
695  |  |     for (i = 0; i < r; i++) { | 
696  |  | #ifdef WORD64_AVAILABLE  | 
697  |  |         for (j = 0; j < 8; j++) { | 
698  |  |             b64[i * 8 + j] = y64[2 * i * 8 + j];  | 
699  |  |             b64[(r + i) * 8 + j] = y64[(2 * i + 1) * 8 + j];  | 
700  |  |         }  | 
701  |  | #else  | 
702  |  |         for (j = 0; j < 16; j++) { | 
703  |  |             b32[i * 16 + j] = y32[2 * i * 16 + j];  | 
704  |  |             b32[(r + i) * 16 + j] = y32[(2 * i + 1) * 16 + j];  | 
705  |  |         }  | 
706  |  | #endif  | 
707  |  |     }  | 
708  |  | }  | 
709  |  |  | 
710  |  | /* Random oracles mix.  | 
711  |  |  * Based on RFC 7914: scrypt PBKDF.  | 
712  |  |  *  | 
713  |  |  * x  Data to mix.  | 
714  |  |  * v  Temporary buffer.  | 
715  |  |  * y  Temporary buffer for the block mix.  | 
716  |  |  * r  Block size parameter.  | 
717  |  |  * n  CPU/Memory cost parameter.  | 
718  |  |  */  | 
719  |  | static void scryptROMix(byte* x, byte* v, byte* y, int r, word32 n)  | 
720  |  | { | 
721  |  |     word32 i;  | 
722  |  |     word32 j;  | 
723  |  |     word32 k;  | 
724  |  |     word32 bSz = (word32)(128 * r);  | 
725  |  | #ifdef WORD64_AVAILABLE  | 
726  |  |     word64* x64 = (word64*)x;  | 
727  |  |     word64* v64 = (word64*)v;  | 
728  |  | #else  | 
729  |  |     word32* x32 = (word32*)x;  | 
730  |  |     word32* v32 = (word32*)v;  | 
731  |  | #endif  | 
732  |  |  | 
733  |  |     /* Step 1. X = B (B not needed therefore not implemented) */  | 
734  |  |     /* Step 2. */  | 
735  |  |     for (i = 0; i < n; i++)  | 
736  |  |     { | 
737  |  |         XMEMCPY(v + i * bSz, x, bSz);  | 
738  |  |         scryptBlockMix(x, y, r);  | 
739  |  |     }  | 
740  |  |  | 
741  |  |     /* Step 3. */  | 
742  |  |     for (i = 0; i < n; i++)  | 
743  |  |     { | 
744  |  | #ifdef LITTLE_ENDIAN_ORDER  | 
745  |  | #ifdef WORD64_AVAILABLE  | 
746  |  |         j = (word32)(*(word64*)(x + (2*r - 1) * 64) & (n-1));  | 
747  |  | #else  | 
748  |  |         j = *(word32*)(x + (2*r - 1) * 64) & (n-1);  | 
749  |  | #endif  | 
750  |  | #else  | 
751  |  |         byte* t = x + (2*r - 1) * 64;  | 
752  |  |         j = (t[0] | (t[1] << 8) | (t[2] << 16) | ((word32)t[3] << 24)) & (n-1);  | 
753  |  | #endif  | 
754  |  | #ifdef WORD64_AVAILABLE  | 
755  |  |         for (k = 0; k < bSz / 8; k++)  | 
756  |  |             x64[k] ^= v64[j * bSz / 8 + k];  | 
757  |  | #else  | 
758  |  |         for (k = 0; k < bSz / 4; k++)  | 
759  |  |             x32[k] ^= v32[j * bSz / 4 + k];  | 
760  |  | #endif  | 
761  |  |         scryptBlockMix(x, y, r);  | 
762  |  |     }  | 
763  |  |     /* Step 4. B' = X (B = X = B' so not needed, therefore not implemented) */  | 
764  |  | }  | 
765  |  |  | 
766  |  | /* Generates an key derived from a password and salt using a memory hard  | 
767  |  |  * algorithm.  | 
768  |  |  * Implements RFC 7914: scrypt PBKDF.  | 
769  |  |  *  | 
770  |  |  * output     The derived key.  | 
771  |  |  * passwd     The password to derive key from.  | 
772  |  |  * passLen    The length of the password.  | 
773  |  |  * salt       The key specific data.  | 
774  |  |  * saltLen    The length of the salt data.  | 
775  |  |  * cost       The CPU/memory cost parameter. Range: 1..(128*r/8-1)  | 
776  |  |  *            (Iterations = 2^cost)  | 
777  |  |  * blockSize  The number of 128 byte octets in a working block.  | 
778  |  |  * parallel   The number of parallel mix operations to perform.  | 
779  |  |  *            (Note: this implementation does not use threads.)  | 
780  |  |  * dkLen      The length of the derived key in bytes.  | 
781  |  |  * returns BAD_FUNC_ARG when: blockSize is too large for cost.  | 
782  |  |  */  | 
783  |  | int wc_scrypt(byte* output, const byte* passwd, int passLen,  | 
784  |  |               const byte* salt, int saltLen, int cost, int blockSize,  | 
785  |  |               int parallel, int dkLen)  | 
786  |  | { | 
787  |  |     int    ret = 0;  | 
788  |  |     int    i;  | 
789  |  |     byte*  v = NULL;  | 
790  |  |     byte*  y = NULL;  | 
791  |  |     byte*  blocks = NULL;  | 
792  |  |     word32 blocksSz;  | 
793  |  |     word32 bSz;  | 
794  |  |  | 
795  |  |     if (blockSize > 8)  | 
796  |  |         return BAD_FUNC_ARG;  | 
797  |  |  | 
798  |  |     if (cost < 1 || cost >= 128 * blockSize / 8 || parallel < 1 || dkLen < 1)  | 
799  |  |         return BAD_FUNC_ARG;  | 
800  |  |  | 
801  |  |     /* The following comparison used to be:  | 
802  |  |      *    ((word32)parallel > (SCRYPT_MAX / (128 * blockSize)))  | 
803  |  |      * where SCRYPT_MAX is (2^32 - 1) * 32. For some compilers, the RHS of  | 
804  |  |      * the comparison is greater than parallel's type. It wouldn't promote  | 
805  |  |      * both sides to word64. What follows is just arithmetic simplification.  | 
806  |  |      */  | 
807  |  |     if (parallel > (int)((SCRYPT_WORD32_MAX / 4) / (word32)blockSize))  | 
808  |  |         return BAD_FUNC_ARG;  | 
809  |  |  | 
810  |  |     bSz = 128 * (word32)blockSize;  | 
811  |  |     if (parallel > (int)(SCRYPT_WORD32_MAX / bSz))  | 
812  |  |         return BAD_FUNC_ARG;  | 
813  |  |     blocksSz = bSz * (word32)parallel;  | 
814  |  |     blocks = (byte*)XMALLOC((size_t)blocksSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);  | 
815  |  |     if (blocks == NULL) { | 
816  |  |         ret = MEMORY_E;  | 
817  |  |         goto end;  | 
818  |  |     }  | 
819  |  |     /* Temporary for scryptROMix. */  | 
820  |  |     v = (byte*)XMALLOC((size_t)((1U << cost) * bSz), NULL,  | 
821  |  |                        DYNAMIC_TYPE_TMP_BUFFER);  | 
822  |  |     if (v == NULL) { | 
823  |  |         ret = MEMORY_E;  | 
824  |  |         goto end;  | 
825  |  |     }  | 
826  |  |     /* Temporary for scryptBlockMix. */  | 
827  |  |     y = (byte*)XMALLOC((size_t)(blockSize * 128), NULL,  | 
828  |  |                        DYNAMIC_TYPE_TMP_BUFFER);  | 
829  |  |     if (y == NULL) { | 
830  |  |         ret = MEMORY_E;  | 
831  |  |         goto end;  | 
832  |  |     }  | 
833  |  |  | 
834  |  |     XMEMSET(y, 0, (size_t)(blockSize * 128));  | 
835  |  |  | 
836  |  |     /* Step 1. */  | 
837  |  |     ret = wc_PBKDF2(blocks, passwd, passLen, salt, saltLen, 1, (int)blocksSz,  | 
838  |  |                     WC_SHA256);  | 
839  |  |     if (ret != 0)  | 
840  |  |         goto end;  | 
841  |  |  | 
842  |  |     /* Step 2. */  | 
843  |  |     for (i = 0; i < parallel; i++)  | 
844  |  |         scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize, 1U << cost);  | 
845  |  |  | 
846  |  |     /* Step 3. */  | 
847  |  |     ret = wc_PBKDF2(output, passwd, passLen, blocks, (int)blocksSz, 1, dkLen,  | 
848  |  |                     WC_SHA256);  | 
849  |  | end:  | 
850  |  |     XFREE(blocks, NULL, DYNAMIC_TYPE_TMP_BUFFER);  | 
851  |  |     XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);  | 
852  |  |     XFREE(y, NULL, DYNAMIC_TYPE_TMP_BUFFER);  | 
853  |  |  | 
854  |  |     return ret;  | 
855  |  | }  | 
856  |  |  | 
857  |  | /* Generates an key derived from a password and salt using a memory hard  | 
858  |  |  * algorithm.  | 
859  |  |  * Implements RFC 7914: scrypt PBKDF.  | 
860  |  |  *  | 
861  |  |  * output      Derived key.  | 
862  |  |  * passwd      Password to derive key from.  | 
863  |  |  * passLen     Length of the password.  | 
864  |  |  * salt        Key specific data.  | 
865  |  |  * saltLen     Length of the salt data.  | 
866  |  |  * iterations  Number of iterations to perform. Range: 1 << (1..(128*r/8-1))  | 
867  |  |  * blockSize   Number of 128 byte octets in a working block.  | 
868  |  |  * parallel    Number of parallel mix operations to perform.  | 
869  |  |  *             (Note: this implementation does not use threads.)  | 
870  |  |  * dkLen       Length of the derived key in bytes.  | 
871  |  |  * returns BAD_FUNC_ARG when: iterations is not a power of 2 or blockSize is too  | 
872  |  |  *                            large for iterations.  | 
873  |  |  */  | 
874  |  | int wc_scrypt_ex(byte* output, const byte* passwd, int passLen,  | 
875  |  |                  const byte* salt, int saltLen, word32 iterations,  | 
876  |  |                  int blockSize, int parallel, int dkLen)  | 
877  |  | { | 
878  |  |     int cost;  | 
879  |  |  | 
880  |  |     /* Iterations must be a power of 2. */  | 
881  |  |     if ((iterations & (iterations - 1)) != 0)  | 
882  |  |         return BAD_FUNC_ARG;  | 
883  |  |  | 
884  |  |     for (cost = -1; iterations != 0; cost++) { | 
885  |  |         iterations >>= 1;  | 
886  |  |     }  | 
887  |  |  | 
888  |  |     return wc_scrypt(output, passwd, passLen, salt, saltLen, cost, blockSize,  | 
889  |  |                      parallel, dkLen);  | 
890  |  | }  | 
891  |  | #endif /* HAVE_SCRYPT */  | 
892  |  |  | 
893  |  | #endif /* NO_PWDBASED */  |