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