Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/evp/pbe_scrypt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <stdio.h>
12
#include <string.h>
13
#include <openssl/evp.h>
14
#include <openssl/err.h>
15
#include "internal/numbers.h"
16
17
#ifndef OPENSSL_NO_SCRYPT
18
19
0
#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
20
static void salsa208_word_specification(uint32_t inout[16])
21
0
{
22
0
    int i;
23
0
    uint32_t x[16];
24
0
    memcpy(x, inout, sizeof(x));
25
0
    for (i = 8; i > 0; i -= 2) {
26
0
        x[4] ^= R(x[0] + x[12], 7);
27
0
        x[8] ^= R(x[4] + x[0], 9);
28
0
        x[12] ^= R(x[8] + x[4], 13);
29
0
        x[0] ^= R(x[12] + x[8], 18);
30
0
        x[9] ^= R(x[5] + x[1], 7);
31
0
        x[13] ^= R(x[9] + x[5], 9);
32
0
        x[1] ^= R(x[13] + x[9], 13);
33
0
        x[5] ^= R(x[1] + x[13], 18);
34
0
        x[14] ^= R(x[10] + x[6], 7);
35
0
        x[2] ^= R(x[14] + x[10], 9);
36
0
        x[6] ^= R(x[2] + x[14], 13);
37
0
        x[10] ^= R(x[6] + x[2], 18);
38
0
        x[3] ^= R(x[15] + x[11], 7);
39
0
        x[7] ^= R(x[3] + x[15], 9);
40
0
        x[11] ^= R(x[7] + x[3], 13);
41
0
        x[15] ^= R(x[11] + x[7], 18);
42
0
        x[1] ^= R(x[0] + x[3], 7);
43
0
        x[2] ^= R(x[1] + x[0], 9);
44
0
        x[3] ^= R(x[2] + x[1], 13);
45
0
        x[0] ^= R(x[3] + x[2], 18);
46
0
        x[6] ^= R(x[5] + x[4], 7);
47
0
        x[7] ^= R(x[6] + x[5], 9);
48
0
        x[4] ^= R(x[7] + x[6], 13);
49
0
        x[5] ^= R(x[4] + x[7], 18);
50
0
        x[11] ^= R(x[10] + x[9], 7);
51
0
        x[8] ^= R(x[11] + x[10], 9);
52
0
        x[9] ^= R(x[8] + x[11], 13);
53
0
        x[10] ^= R(x[9] + x[8], 18);
54
0
        x[12] ^= R(x[15] + x[14], 7);
55
0
        x[13] ^= R(x[12] + x[15], 9);
56
0
        x[14] ^= R(x[13] + x[12], 13);
57
0
        x[15] ^= R(x[14] + x[13], 18);
58
0
    }
59
0
    for (i = 0; i < 16; ++i)
60
0
        inout[i] += x[i];
61
0
    OPENSSL_cleanse(x, sizeof(x));
62
0
}
63
64
static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r)
65
0
{
66
0
    uint64_t i, j;
67
0
    uint32_t X[16], *pB;
68
0
69
0
    memcpy(X, B + (r * 2 - 1) * 16, sizeof(X));
70
0
    pB = B;
71
0
    for (i = 0; i < r * 2; i++) {
72
0
        for (j = 0; j < 16; j++)
73
0
            X[j] ^= *pB++;
74
0
        salsa208_word_specification(X);
75
0
        memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X));
76
0
    }
77
0
    OPENSSL_cleanse(X, sizeof(X));
78
0
}
79
80
static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N,
81
                        uint32_t *X, uint32_t *T, uint32_t *V)
82
0
{
83
0
    unsigned char *pB;
84
0
    uint32_t *pV;
85
0
    uint64_t i, k;
86
0
87
0
    /* Convert from little endian input */
88
0
    for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) {
89
0
        *pV = *pB++;
90
0
        *pV |= *pB++ << 8;
91
0
        *pV |= *pB++ << 16;
92
0
        *pV |= (uint32_t)*pB++ << 24;
93
0
    }
94
0
95
0
    for (i = 1; i < N; i++, pV += 32 * r)
96
0
        scryptBlockMix(pV, pV - 32 * r, r);
97
0
98
0
    scryptBlockMix(X, V + (N - 1) * 32 * r, r);
99
0
100
0
    for (i = 0; i < N; i++) {
101
0
        uint32_t j;
102
0
        j = X[16 * (2 * r - 1)] % N;
103
0
        pV = V + 32 * r * j;
104
0
        for (k = 0; k < 32 * r; k++)
105
0
            T[k] = X[k] ^ *pV++;
106
0
        scryptBlockMix(X, T, r);
107
0
    }
108
0
    /* Convert output to little endian */
109
0
    for (i = 0, pB = B; i < 32 * r; i++) {
110
0
        uint32_t xtmp = X[i];
111
0
        *pB++ = xtmp & 0xff;
112
0
        *pB++ = (xtmp >> 8) & 0xff;
113
0
        *pB++ = (xtmp >> 16) & 0xff;
114
0
        *pB++ = (xtmp >> 24) & 0xff;
115
0
    }
116
0
}
117
118
#ifndef SIZE_MAX
119
# define SIZE_MAX    ((size_t)-1)
120
#endif
121
122
/*
123
 * Maximum power of two that will fit in uint64_t: this should work on
124
 * most (all?) platforms.
125
 */
126
127
0
#define LOG2_UINT64_MAX         (sizeof(uint64_t) * 8 - 1)
128
129
/*
130
 * Maximum value of p * r:
131
 * p <= ((2^32-1) * hLen) / MFLen =>
132
 * p <= ((2^32-1) * 32) / (128 * r) =>
133
 * p * r <= (2^30-1)
134
 *
135
 */
136
137
0
#define SCRYPT_PR_MAX   ((1 << 30) - 1)
138
139
/*
140
 * Maximum permitted memory allow this to be overridden with Configuration
141
 * option: e.g. -DSCRYPT_MAX_MEM=0 for maximum possible.
142
 */
143
144
#ifdef SCRYPT_MAX_MEM
145
# if SCRYPT_MAX_MEM == 0
146
#  undef SCRYPT_MAX_MEM
147
/*
148
 * Although we could theoretically allocate SIZE_MAX memory that would leave
149
 * no memory available for anything else so set limit as half that.
150
 */
151
#  define SCRYPT_MAX_MEM (SIZE_MAX/2)
152
# endif
153
#else
154
/* Default memory limit: 32 MB */
155
0
# define SCRYPT_MAX_MEM  (1024 * 1024 * 32)
156
#endif
157
158
int EVP_PBE_scrypt(const char *pass, size_t passlen,
159
                   const unsigned char *salt, size_t saltlen,
160
                   uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
161
                   unsigned char *key, size_t keylen)
162
0
{
163
0
    int rv = 0;
164
0
    unsigned char *B;
165
0
    uint32_t *X, *V, *T;
166
0
    uint64_t i, Blen, Vlen;
167
0
168
0
    /* Sanity check parameters */
169
0
    /* initial check, r,p must be non zero, N >= 2 and a power of 2 */
170
0
    if (r == 0 || p == 0 || N < 2 || (N & (N - 1)))
171
0
        return 0;
172
0
    /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
173
0
    if (p > SCRYPT_PR_MAX / r) {
174
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
175
0
        return 0;
176
0
    }
177
0
178
0
    /*
179
0
     * Need to check N: if 2^(128 * r / 8) overflows limit this is
180
0
     * automatically satisfied since N <= UINT64_MAX.
181
0
     */
182
0
183
0
    if (16 * r <= LOG2_UINT64_MAX) {
184
0
        if (N >= (((uint64_t)1) << (16 * r))) {
185
0
            EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
186
0
            return 0;
187
0
        }
188
0
    }
189
0
190
0
    /* Memory checks: check total allocated buffer size fits in uint64_t */
191
0
192
0
    /*
193
0
     * B size in section 5 step 1.S
194
0
     * Note: we know p * 128 * r < UINT64_MAX because we already checked
195
0
     * p * r < SCRYPT_PR_MAX
196
0
     */
197
0
    Blen = p * 128 * r;
198
0
    /*
199
0
     * Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
200
0
     * have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
201
0
     */
202
0
    if (Blen > INT_MAX) {
203
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
204
0
        return 0;
205
0
    }
206
0
207
0
    /*
208
0
     * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
209
0
     * This is combined size V, X and T (section 4)
210
0
     */
211
0
    i = UINT64_MAX / (32 * sizeof(uint32_t));
212
0
    if (N + 2 > i / r) {
213
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
214
0
        return 0;
215
0
    }
216
0
    Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
217
0
218
0
    /* check total allocated size fits in uint64_t */
219
0
    if (Blen > UINT64_MAX - Vlen) {
220
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
221
0
        return 0;
222
0
    }
223
0
224
0
    if (maxmem == 0)
225
0
        maxmem = SCRYPT_MAX_MEM;
226
0
227
0
    /* Check that the maximum memory doesn't exceed a size_t limits */
228
0
    if (maxmem > SIZE_MAX)
229
0
        maxmem = SIZE_MAX;
230
0
231
0
    if (Blen + Vlen > maxmem) {
232
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
233
0
        return 0;
234
0
    }
235
0
236
0
    /* If no key return to indicate parameters are OK */
237
0
    if (key == NULL)
238
0
        return 1;
239
0
240
0
    B = OPENSSL_malloc((size_t)(Blen + Vlen));
241
0
    if (B == NULL) {
242
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, ERR_R_MALLOC_FAILURE);
243
0
        return 0;
244
0
    }
245
0
    X = (uint32_t *)(B + Blen);
246
0
    T = X + 32 * r;
247
0
    V = T + 32 * r;
248
0
    if (PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, 1, EVP_sha256(),
249
0
                          (int)Blen, B) == 0)
250
0
        goto err;
251
0
252
0
    for (i = 0; i < p; i++)
253
0
        scryptROMix(B + 128 * r * i, r, N, X, T, V);
254
0
255
0
    if (PKCS5_PBKDF2_HMAC(pass, passlen, B, (int)Blen, 1, EVP_sha256(),
256
0
                          keylen, key) == 0)
257
0
        goto err;
258
0
    rv = 1;
259
0
 err:
260
0
    if (rv == 0)
261
0
        EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_PBKDF2_ERROR);
262
0
263
0
    OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
264
0
    return rv;
265
0
}
266
#endif