Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/p5_scrypt.c
Line
Count
Source
1
/*
2
 * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/core_names.h>
14
#include <openssl/err.h>
15
#include <openssl/evp.h>
16
#include <openssl/x509.h>
17
#include <openssl/rand.h>
18
#include "crypto/evp.h"
19
20
#include <crypto/asn1.h>
21
22
#ifndef OPENSSL_NO_SCRYPT
23
/* PKCS#5 scrypt password based encryption structures */
24
25
ASN1_SEQUENCE(SCRYPT_PARAMS) = {
26
    ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
27
    ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
28
    ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
29
    ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
30
    ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
31
0
} ASN1_SEQUENCE_END(SCRYPT_PARAMS)
32
0
33
0
IMPLEMENT_ASN1_FUNCTIONS(SCRYPT_PARAMS)
34
0
35
0
static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, int saltlen,
36
0
    size_t keylen, uint64_t N, uint64_t r,
37
0
    uint64_t p);
38
0
39
0
/*
40
0
 * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
41
0
 */
42
0
43
0
X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
44
0
    const unsigned char *salt, int saltlen,
45
0
    unsigned char *aiv, uint64_t N, uint64_t r,
46
0
    uint64_t p)
47
0
{
48
0
    X509_ALGOR *scheme = NULL, *ret = NULL;
49
0
    int alg_nid;
50
0
    size_t keylen = 0;
51
0
    EVP_CIPHER_CTX *ctx = NULL;
52
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
53
0
    PBE2PARAM *pbe2 = NULL;
54
55
0
    if (!cipher) {
56
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
57
0
        goto err;
58
0
    }
59
60
0
    if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
61
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SCRYPT_PARAMETERS);
62
0
        goto err;
63
0
    }
64
65
0
    alg_nid = EVP_CIPHER_get_type(cipher);
66
0
    if (alg_nid == NID_undef) {
67
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
68
0
        goto err;
69
0
    }
70
71
0
    pbe2 = PBE2PARAM_new();
72
0
    if (pbe2 == NULL) {
73
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
74
0
        goto err;
75
0
    }
76
77
    /* Setup the AlgorithmIdentifier for the encryption scheme */
78
0
    scheme = pbe2->encryption;
79
80
0
    scheme->algorithm = OBJ_nid2obj(alg_nid);
81
0
    scheme->parameter = ASN1_TYPE_new();
82
0
    if (scheme->parameter == NULL) {
83
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
84
0
        goto err;
85
0
    }
86
87
    /* Create random IV */
88
0
    if (EVP_CIPHER_get_iv_length(cipher)) {
89
0
        if (aiv)
90
0
            memcpy(iv, aiv, EVP_CIPHER_get_iv_length(cipher));
91
0
        else if (RAND_bytes(iv, EVP_CIPHER_get_iv_length(cipher)) <= 0)
92
0
            goto err;
93
0
    }
94
95
0
    ctx = EVP_CIPHER_CTX_new();
96
0
    if (ctx == NULL) {
97
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
98
0
        goto err;
99
0
    }
100
101
    /* Dummy cipherinit to just setup the IV */
102
0
    if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
103
0
        goto err;
104
0
    if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
105
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
106
0
        goto err;
107
0
    }
108
0
    EVP_CIPHER_CTX_free(ctx);
109
0
    ctx = NULL;
110
111
    /* If its RC2 then we'd better setup the key length */
112
113
0
    if (alg_nid == NID_rc2_cbc)
114
0
        keylen = EVP_CIPHER_get_key_length(cipher);
115
116
    /* Setup keyfunc */
117
118
0
    X509_ALGOR_free(pbe2->keyfunc);
119
120
0
    pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
121
122
0
    if (pbe2->keyfunc == NULL) {
123
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
124
0
        goto err;
125
0
    }
126
127
    /* Now set up top level AlgorithmIdentifier */
128
129
0
    ret = X509_ALGOR_new();
130
0
    if (ret == NULL) {
131
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
132
0
        goto err;
133
0
    }
134
135
0
    ret->algorithm = OBJ_nid2obj(NID_pbes2);
136
137
    /* Encode PBE2PARAM into parameter */
138
139
0
    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
140
0
            &ret->parameter)
141
0
        == NULL) {
142
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
143
0
        goto err;
144
0
    }
145
146
0
    PBE2PARAM_free(pbe2);
147
0
    pbe2 = NULL;
148
149
0
    return ret;
150
151
0
err:
152
0
    PBE2PARAM_free(pbe2);
153
0
    X509_ALGOR_free(ret);
154
0
    EVP_CIPHER_CTX_free(ctx);
155
156
0
    return NULL;
157
0
}
158
159
static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, int saltlen,
160
    size_t keylen, uint64_t N, uint64_t r,
161
    uint64_t p)
162
0
{
163
0
    X509_ALGOR *keyfunc = NULL;
164
0
    SCRYPT_PARAMS *sparam = SCRYPT_PARAMS_new();
165
166
0
    if (sparam == NULL) {
167
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
168
0
        goto err;
169
0
    }
170
171
0
    if (!saltlen)
172
0
        saltlen = PKCS5_DEFAULT_PBE2_SALT_LEN;
173
174
    /* This will either copy salt or grow the buffer */
175
0
    if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0) {
176
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
177
0
        goto err;
178
0
    }
179
180
0
    if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
181
0
        goto err;
182
183
0
    if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0) {
184
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
185
0
        goto err;
186
0
    }
187
188
0
    if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0) {
189
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
190
0
        goto err;
191
0
    }
192
193
0
    if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0) {
194
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
195
0
        goto err;
196
0
    }
197
198
    /* If have a key len set it up */
199
200
0
    if (keylen > 0) {
201
0
        sparam->keyLength = ASN1_INTEGER_new();
202
0
        if (sparam->keyLength == NULL) {
203
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
204
0
            goto err;
205
0
        }
206
0
        if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0) {
207
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
208
0
            goto err;
209
0
        }
210
0
    }
211
212
    /* Finally setup the keyfunc structure */
213
214
0
    keyfunc = X509_ALGOR_new();
215
0
    if (keyfunc == NULL) {
216
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
217
0
        goto err;
218
0
    }
219
220
0
    keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
221
222
    /* Encode SCRYPT_PARAMS into parameter of pbe2 */
223
224
0
    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
225
0
            &keyfunc->parameter)
226
0
        == NULL) {
227
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
228
0
        goto err;
229
0
    }
230
231
0
    SCRYPT_PARAMS_free(sparam);
232
0
    return keyfunc;
233
234
0
err:
235
0
    SCRYPT_PARAMS_free(sparam);
236
0
    X509_ALGOR_free(keyfunc);
237
0
    return NULL;
238
0
}
239
240
int PKCS5_v2_scrypt_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
241
    int passlen, ASN1_TYPE *param,
242
    const EVP_CIPHER *c, const EVP_MD *md, int en_de,
243
    OSSL_LIB_CTX *libctx, const char *propq)
244
0
{
245
0
    unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
246
0
    uint64_t p, r, N;
247
0
    size_t saltlen;
248
0
    size_t keylen = 0;
249
0
    int t, rv = 0;
250
0
    SCRYPT_PARAMS *sparam = NULL;
251
252
0
    if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) {
253
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
254
0
        goto err;
255
0
    }
256
257
    /* Decode parameter */
258
259
0
    sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
260
261
0
    if (sparam == NULL) {
262
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
263
0
        goto err;
264
0
    }
265
266
0
    t = EVP_CIPHER_CTX_get_key_length(ctx);
267
0
    if (t < 0) {
268
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
269
0
        goto err;
270
0
    }
271
0
    keylen = t;
272
273
    /* Now check the parameters of sparam */
274
275
0
    if (sparam->keyLength) {
276
0
        uint64_t spkeylen;
277
0
        if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
278
0
            || (spkeylen != keylen)) {
279
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
280
0
            goto err;
281
0
        }
282
0
    }
283
    /* Check all parameters fit in uint64_t and are acceptable to scrypt */
284
0
    if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
285
0
        || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
286
0
        || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
287
0
        || EVP_PBE_scrypt_ex(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0,
288
0
               libctx, propq)
289
0
            == 0) {
290
0
        ERR_raise(ERR_LIB_EVP, EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
291
0
        goto err;
292
0
    }
293
294
    /* it seems that its all OK */
295
296
0
    salt = sparam->salt->data;
297
0
    saltlen = sparam->salt->length;
298
0
    if (EVP_PBE_scrypt_ex(pass, passlen, salt, saltlen, N, r, p, 0, key,
299
0
            keylen, libctx, propq)
300
0
        == 0)
301
0
        goto err;
302
0
    rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
303
0
err:
304
0
    if (keylen)
305
0
        OPENSSL_cleanse(key, keylen);
306
0
    SCRYPT_PARAMS_free(sparam);
307
0
    return rv;
308
0
}
309
310
int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
311
    int passlen, ASN1_TYPE *param,
312
    const EVP_CIPHER *c, const EVP_MD *md, int en_de)
313
0
{
314
0
    return PKCS5_v2_scrypt_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL);
315
0
}
316
317
#endif /* OPENSSL_NO_SCRYPT */