Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/asn1/p5_scrypt.c

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