Coverage Report

Created: 2025-12-31 07:08

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