]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_lib.c
Don't hold a lock when calling a callback in ossl_namemap_doall_names
[thirdparty/openssl.git] / crypto / evp / evp_lib.c
1 /*
2 * Copyright 1995-2021 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 /*
11 * EVP _meth_ APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include <openssl/objects.h>
20 #include <openssl/params.h>
21 #include <openssl/core_names.h>
22 #include <openssl/dh.h>
23 #include <openssl/ec.h>
24 #include "crypto/evp.h"
25 #include "crypto/asn1.h"
26 #include "internal/provider.h"
27 #include "evp_local.h"
28
29 #if !defined(FIPS_MODULE)
30 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
31 {
32 return evp_cipher_param_to_asn1_ex(c, type, NULL);
33 }
34
35 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
36 {
37 return evp_cipher_asn1_to_param_ex(c, type, NULL);
38 }
39
40 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
41 {
42 int i = 0;
43 unsigned int l;
44
45 if (type != NULL) {
46 unsigned char iv[EVP_MAX_IV_LENGTH];
47
48 l = EVP_CIPHER_CTX_iv_length(ctx);
49 if (!ossl_assert(l <= sizeof(iv)))
50 return -1;
51 i = ASN1_TYPE_get_octetstring(type, iv, l);
52 if (i != (int)l)
53 return -1;
54
55 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
56 return -1;
57 }
58 return i;
59 }
60
61 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
62 {
63 int i = 0;
64 unsigned int j;
65 unsigned char *oiv = NULL;
66
67 if (type != NULL) {
68 oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
69 j = EVP_CIPHER_CTX_iv_length(c);
70 OPENSSL_assert(j <= sizeof(c->iv));
71 i = ASN1_TYPE_set_octetstring(type, oiv, j);
72 }
73 return i;
74 }
75
76 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
77 evp_cipher_aead_asn1_params *asn1_params)
78 {
79 int ret = -1; /* Assume the worst */
80 const EVP_CIPHER *cipher = c->cipher;
81
82 /*
83 * For legacy implementations, we detect custom AlgorithmIdentifier
84 * parameter handling by checking if the function pointer
85 * cipher->set_asn1_parameters is set. We know that this pointer
86 * is NULL for provided implementations.
87 *
88 * Otherwise, for any implementation, we check the flag
89 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
90 * default AI parameter extraction.
91 *
92 * Otherwise, for provided implementations, we convert |type| to
93 * a DER encoded blob and pass to the implementation in OSSL_PARAM
94 * form.
95 *
96 * If none of the above applies, this operation is unsupported.
97 */
98 if (cipher->set_asn1_parameters != NULL) {
99 ret = cipher->set_asn1_parameters(c, type);
100 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
101 switch (EVP_CIPHER_mode(cipher)) {
102 case EVP_CIPH_WRAP_MODE:
103 if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
104 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
105 ret = 1;
106 break;
107
108 case EVP_CIPH_GCM_MODE:
109 ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
110 break;
111
112 case EVP_CIPH_CCM_MODE:
113 case EVP_CIPH_XTS_MODE:
114 case EVP_CIPH_OCB_MODE:
115 ret = -2;
116 break;
117
118 default:
119 ret = EVP_CIPHER_set_asn1_iv(c, type);
120 }
121 } else if (cipher->prov != NULL) {
122 OSSL_PARAM params[3], *p = params;
123 unsigned char *der = NULL, *derp;
124
125 /*
126 * We make two passes, the first to get the appropriate buffer size,
127 * and the second to get the actual value.
128 */
129 *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
130 NULL, 0);
131 *p = OSSL_PARAM_construct_end();
132
133 if (!EVP_CIPHER_CTX_get_params(c, params))
134 goto err;
135
136 /* ... but, we should get a return size too! */
137 if (OSSL_PARAM_modified(params)
138 && params[0].return_size != 0
139 && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
140 params[0].data = der;
141 params[0].data_size = params[0].return_size;
142 OSSL_PARAM_set_all_unmodified(params);
143 derp = der;
144 if (EVP_CIPHER_CTX_get_params(c, params)
145 && OSSL_PARAM_modified(params)
146 && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
147 params[0].return_size) != NULL) {
148 ret = 1;
149 }
150 OPENSSL_free(der);
151 }
152 } else {
153 ret = -2;
154 }
155
156 err:
157 if (ret == -2)
158 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
159 else if (ret <= 0)
160 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
161 if (ret < -1)
162 ret = -1;
163 return ret;
164 }
165
166 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
167 evp_cipher_aead_asn1_params *asn1_params)
168 {
169 int ret = -1; /* Assume the worst */
170 const EVP_CIPHER *cipher = c->cipher;
171
172 /*
173 * For legacy implementations, we detect custom AlgorithmIdentifier
174 * parameter handling by checking if there the function pointer
175 * cipher->get_asn1_parameters is set. We know that this pointer
176 * is NULL for provided implementations.
177 *
178 * Otherwise, for any implementation, we check the flag
179 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
180 * default AI parameter creation.
181 *
182 * Otherwise, for provided implementations, we get the AI parameter
183 * in DER encoded form from the implementation by requesting the
184 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
185 *
186 * If none of the above applies, this operation is unsupported.
187 */
188 if (cipher->get_asn1_parameters != NULL) {
189 ret = cipher->get_asn1_parameters(c, type);
190 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
191 switch (EVP_CIPHER_mode(cipher)) {
192 case EVP_CIPH_WRAP_MODE:
193 ret = 1;
194 break;
195
196 case EVP_CIPH_GCM_MODE:
197 ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
198 break;
199
200 case EVP_CIPH_CCM_MODE:
201 case EVP_CIPH_XTS_MODE:
202 case EVP_CIPH_OCB_MODE:
203 ret = -2;
204 break;
205
206 default:
207 ret = EVP_CIPHER_get_asn1_iv(c, type);
208 }
209 } else if (cipher->prov != NULL) {
210 OSSL_PARAM params[3], *p = params;
211 unsigned char *der = NULL;
212 int derl = -1;
213
214 if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
215 *p++ =
216 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
217 der, (size_t)derl);
218 *p = OSSL_PARAM_construct_end();
219 if (EVP_CIPHER_CTX_set_params(c, params))
220 ret = 1;
221 OPENSSL_free(der);
222 }
223 } else {
224 ret = -2;
225 }
226
227 if (ret == -2)
228 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
229 else if (ret <= 0)
230 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
231 if (ret < -1)
232 ret = -1;
233 return ret;
234 }
235
236 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
237 evp_cipher_aead_asn1_params *asn1_params)
238 {
239 int i = 0;
240 long tl;
241 unsigned char iv[EVP_MAX_IV_LENGTH];
242
243 if (type == NULL || asn1_params == NULL)
244 return 0;
245
246 i = asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
247 if (i <= 0)
248 return -1;
249 asn1_type_get_octetstring_int(type, &tl, iv, i);
250
251 memcpy(asn1_params->iv, iv, i);
252 asn1_params->iv_len = i;
253
254 return i;
255 }
256
257 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
258 evp_cipher_aead_asn1_params *asn1_params)
259 {
260 if (type == NULL || asn1_params == NULL)
261 return 0;
262
263 return asn1_type_set_octetstring_int(type, asn1_params->tag_len,
264 asn1_params->iv, asn1_params->iv_len);
265 }
266 #endif /* !defined(FIPS_MODULE) */
267
268 /* Convert the various cipher NIDs and dummies to a proper OID NID */
269 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
270 {
271 int nid;
272 nid = EVP_CIPHER_nid(ctx);
273
274 switch (nid) {
275
276 case NID_rc2_cbc:
277 case NID_rc2_64_cbc:
278 case NID_rc2_40_cbc:
279
280 return NID_rc2_cbc;
281
282 case NID_rc4:
283 case NID_rc4_40:
284
285 return NID_rc4;
286
287 case NID_aes_128_cfb128:
288 case NID_aes_128_cfb8:
289 case NID_aes_128_cfb1:
290
291 return NID_aes_128_cfb128;
292
293 case NID_aes_192_cfb128:
294 case NID_aes_192_cfb8:
295 case NID_aes_192_cfb1:
296
297 return NID_aes_192_cfb128;
298
299 case NID_aes_256_cfb128:
300 case NID_aes_256_cfb8:
301 case NID_aes_256_cfb1:
302
303 return NID_aes_256_cfb128;
304
305 case NID_des_cfb64:
306 case NID_des_cfb8:
307 case NID_des_cfb1:
308
309 return NID_des_cfb64;
310
311 case NID_des_ede3_cfb64:
312 case NID_des_ede3_cfb8:
313 case NID_des_ede3_cfb1:
314
315 return NID_des_cfb64;
316
317 default:
318 #ifdef FIPS_MODULE
319 return NID_undef;
320 #else
321 {
322 /* Check it has an OID and it is valid */
323 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
324
325 if (OBJ_get0_data(otmp) == NULL)
326 nid = NID_undef;
327 ASN1_OBJECT_free(otmp);
328 return nid;
329 }
330 #endif
331 }
332 }
333
334 int evp_cipher_cache_constants(EVP_CIPHER *cipher)
335 {
336 int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0;
337 size_t ivlen = 0;
338 size_t blksz = 0;
339 size_t keylen = 0;
340 unsigned int mode = 0;
341 OSSL_PARAM params[9];
342
343 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
344 params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
345 params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
346 params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
347 params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
348 params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
349 &custom_iv);
350 params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
351 params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
352 &multiblock);
353 params[8] = OSSL_PARAM_construct_end();
354 ok = evp_do_ciph_getparams(cipher, params);
355 if (ok) {
356 cipher->block_size = blksz;
357 cipher->iv_len = ivlen;
358 cipher->key_len = keylen;
359 cipher->flags = mode;
360 if (aead)
361 cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
362 if (custom_iv)
363 cipher->flags |= EVP_CIPH_CUSTOM_IV;
364 if (cts)
365 cipher->flags |= EVP_CIPH_FLAG_CTS;
366 if (multiblock)
367 cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
368 /* Provided implementations may have a custom cipher_cipher */
369 if (cipher->prov != NULL && cipher->ccipher != NULL)
370 cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
371 }
372 return ok;
373 }
374
375 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
376 {
377 return cipher->block_size;
378 }
379
380 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
381 {
382 return EVP_CIPHER_block_size(ctx->cipher);
383 }
384
385 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
386 {
387 return e->ctx_size;
388 }
389
390 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
391 const unsigned char *in, unsigned int inl)
392 {
393 if (ctx->cipher->prov != NULL) {
394 /*
395 * If the provided implementation has a ccipher function, we use it,
396 * and translate its return value like this: 0 => -1, 1 => outlen
397 *
398 * Otherwise, we call the cupdate function if in != NULL, or cfinal
399 * if in == NULL. Regardless of which, we return what we got.
400 */
401 int ret = -1;
402 size_t outl = 0;
403 size_t blocksize = EVP_CIPHER_CTX_block_size(ctx);
404
405 if (ctx->cipher->ccipher != NULL)
406 ret = ctx->cipher->ccipher(ctx->provctx, out, &outl,
407 inl + (blocksize == 1 ? 0 : blocksize),
408 in, (size_t)inl)
409 ? (int)outl : -1;
410 else if (in != NULL)
411 ret = ctx->cipher->cupdate(ctx->provctx, out, &outl,
412 inl + (blocksize == 1 ? 0 : blocksize),
413 in, (size_t)inl);
414 else
415 ret = ctx->cipher->cfinal(ctx->provctx, out, &outl,
416 blocksize == 1 ? 0 : blocksize);
417
418 return ret;
419 }
420
421 return ctx->cipher->do_cipher(ctx, out, in, inl);
422 }
423
424 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
425 {
426 return ctx->cipher;
427 }
428
429 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
430 {
431 return ctx->encrypt;
432 }
433
434 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
435 {
436 return cipher->flags;
437 }
438
439 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
440 {
441 return ctx->app_data;
442 }
443
444 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
445 {
446 ctx->app_data = data;
447 }
448
449 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
450 {
451 return ctx->cipher_data;
452 }
453
454 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
455 {
456 void *old_cipher_data;
457
458 old_cipher_data = ctx->cipher_data;
459 ctx->cipher_data = cipher_data;
460
461 return old_cipher_data;
462 }
463
464 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
465 {
466 return cipher->iv_len;
467 }
468
469 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
470 {
471 int rv, len = EVP_CIPHER_iv_length(ctx->cipher);
472 size_t v = len;
473 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
474
475 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
476 rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
477 if (rv == EVP_CTRL_RET_UNSUPPORTED)
478 goto legacy;
479 return rv != 0 ? (int)v : -1;
480 /* TODO (3.0) Remove legacy support */
481 legacy:
482 if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
483 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
484 0, &len);
485 return (rv == 1) ? len : -1;
486 }
487 return len;
488 }
489
490 int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX *ctx)
491 {
492 int ret;
493 size_t v = 0;
494 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
495
496 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
497 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
498 return ret == 1 ? (int)v : 0;
499 }
500
501 #ifndef OPENSSL_NO_DEPRECATED_3_0
502 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
503 {
504 int ok;
505 const unsigned char *v = ctx->oiv;
506 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
507
508 params[0] =
509 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
510 (void **)&v, sizeof(ctx->oiv));
511 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
512
513 return ok != 0 ? v : NULL;
514 }
515
516 /*
517 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
518 */
519 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
520 {
521 int ok;
522 const unsigned char *v = ctx->iv;
523 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
524
525 params[0] =
526 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
527 (void **)&v, sizeof(ctx->iv));
528 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
529
530 return ok != 0 ? v : NULL;
531 }
532
533 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
534 {
535 int ok;
536 unsigned char *v = ctx->iv;
537 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
538
539 params[0] =
540 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
541 (void **)&v, sizeof(ctx->iv));
542 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
543
544 return ok != 0 ? v : NULL;
545 }
546 #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
547
548 int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
549 {
550 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
551
552 params[0] =
553 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
554 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
555 }
556
557 int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
558 {
559 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
560
561 params[0] =
562 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
563 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
564 }
565
566 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
567 {
568 return ctx->buf;
569 }
570
571 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
572 {
573 int ok;
574 unsigned int v = (unsigned int)ctx->num;
575 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
576
577 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
578 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
579
580 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
581 }
582
583 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
584 {
585 int ok;
586 unsigned int n = (unsigned int)num;
587 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
588
589 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
590 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
591
592 if (ok != 0)
593 ctx->num = (int)n;
594 return ok != 0;
595 }
596
597 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
598 {
599 return cipher->key_len;
600 }
601
602 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
603 {
604 int ok;
605 size_t v = ctx->key_len;
606 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
607
608 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
609 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
610
611 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
612 }
613
614 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
615 {
616 return cipher->nid;
617 }
618
619 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
620 {
621 return ctx->cipher->nid;
622 }
623
624 int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
625 {
626 if (cipher->prov != NULL)
627 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
628 return evp_is_a(NULL, 0, EVP_CIPHER_name(cipher), name);
629 }
630
631 int EVP_CIPHER_number(const EVP_CIPHER *cipher)
632 {
633 return cipher->name_id;
634 }
635
636 const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
637 {
638 if (cipher->prov != NULL)
639 return evp_first_name(cipher->prov, cipher->name_id);
640 #ifndef FIPS_MODULE
641 return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
642 #else
643 return NULL;
644 #endif
645 }
646
647 int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
648 void (*fn)(const char *name, void *data),
649 void *data)
650 {
651 if (cipher->prov != NULL)
652 return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
653
654 return 1;
655 }
656
657 const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
658 {
659 return cipher->prov;
660 }
661
662 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
663 {
664 return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
665 }
666
667 int EVP_MD_is_a(const EVP_MD *md, const char *name)
668 {
669 if (md->prov != NULL)
670 return evp_is_a(md->prov, md->name_id, NULL, name);
671 return evp_is_a(NULL, 0, EVP_MD_name(md), name);
672 }
673
674 int EVP_MD_number(const EVP_MD *md)
675 {
676 return md->name_id;
677 }
678
679 const char *EVP_MD_name(const EVP_MD *md)
680 {
681 if (md->prov != NULL)
682 return evp_first_name(md->prov, md->name_id);
683 #ifndef FIPS_MODULE
684 return OBJ_nid2sn(EVP_MD_nid(md));
685 #else
686 return NULL;
687 #endif
688 }
689
690 int EVP_MD_names_do_all(const EVP_MD *md,
691 void (*fn)(const char *name, void *data),
692 void *data)
693 {
694 if (md->prov != NULL)
695 return evp_names_do_all(md->prov, md->name_id, fn, data);
696
697 return 1;
698 }
699
700 const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
701 {
702 return md->prov;
703 }
704
705 int EVP_MD_type(const EVP_MD *md)
706 {
707 return md->type;
708 }
709
710 int EVP_MD_pkey_type(const EVP_MD *md)
711 {
712 return md->pkey_type;
713 }
714
715 int EVP_MD_block_size(const EVP_MD *md)
716 {
717 if (md == NULL) {
718 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
719 return -1;
720 }
721 return md->block_size;
722 }
723
724 int EVP_MD_size(const EVP_MD *md)
725 {
726 if (md == NULL) {
727 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
728 return -1;
729 }
730 return md->md_size;
731 }
732
733 unsigned long EVP_MD_flags(const EVP_MD *md)
734 {
735 return md->flags;
736 }
737
738 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
739 {
740 EVP_MD *md = evp_md_new();
741
742 if (md != NULL) {
743 md->type = md_type;
744 md->pkey_type = pkey_type;
745 }
746 return md;
747 }
748
749 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
750 {
751 EVP_MD *to = NULL;
752
753 /*
754 * Non-legacy EVP_MDs can't be duplicated like this.
755 * Use EVP_MD_up_ref() instead.
756 */
757 if (md->prov != NULL)
758 return NULL;
759
760 if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
761 CRYPTO_RWLOCK *lock = to->lock;
762
763 memcpy(to, md, sizeof(*to));
764 to->lock = lock;
765 }
766 return to;
767 }
768
769 void EVP_MD_meth_free(EVP_MD *md)
770 {
771 EVP_MD_free(md);
772 }
773 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
774 {
775 if (md->block_size != 0)
776 return 0;
777
778 md->block_size = blocksize;
779 return 1;
780 }
781 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
782 {
783 if (md->md_size != 0)
784 return 0;
785
786 md->md_size = resultsize;
787 return 1;
788 }
789 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
790 {
791 if (md->ctx_size != 0)
792 return 0;
793
794 md->ctx_size = datasize;
795 return 1;
796 }
797 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
798 {
799 if (md->flags != 0)
800 return 0;
801
802 md->flags = flags;
803 return 1;
804 }
805 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
806 {
807 if (md->init != NULL)
808 return 0;
809
810 md->init = init;
811 return 1;
812 }
813 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
814 const void *data,
815 size_t count))
816 {
817 if (md->update != NULL)
818 return 0;
819
820 md->update = update;
821 return 1;
822 }
823 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
824 unsigned char *md))
825 {
826 if (md->final != NULL)
827 return 0;
828
829 md->final = final;
830 return 1;
831 }
832 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
833 const EVP_MD_CTX *from))
834 {
835 if (md->copy != NULL)
836 return 0;
837
838 md->copy = copy;
839 return 1;
840 }
841 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
842 {
843 if (md->cleanup != NULL)
844 return 0;
845
846 md->cleanup = cleanup;
847 return 1;
848 }
849 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
850 int p1, void *p2))
851 {
852 if (md->md_ctrl != NULL)
853 return 0;
854
855 md->md_ctrl = ctrl;
856 return 1;
857 }
858
859 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
860 {
861 return md->block_size;
862 }
863 int EVP_MD_meth_get_result_size(const EVP_MD *md)
864 {
865 return md->md_size;
866 }
867 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
868 {
869 return md->ctx_size;
870 }
871 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
872 {
873 return md->flags;
874 }
875 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
876 {
877 return md->init;
878 }
879 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
880 const void *data,
881 size_t count)
882 {
883 return md->update;
884 }
885 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
886 unsigned char *md)
887 {
888 return md->final;
889 }
890 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
891 const EVP_MD_CTX *from)
892 {
893 return md->copy;
894 }
895 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
896 {
897 return md->cleanup;
898 }
899 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
900 int p1, void *p2)
901 {
902 return md->md_ctrl;
903 }
904
905 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
906 {
907 if (ctx == NULL)
908 return NULL;
909 return ctx->reqdigest;
910 }
911
912 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
913 {
914 return ctx->pctx;
915 }
916
917 #if !defined(FIPS_MODULE)
918 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
919 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
920 {
921 /*
922 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
923 * we have to deal with the cleanup job here.
924 */
925 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
926 EVP_PKEY_CTX_free(ctx->pctx);
927
928 ctx->pctx = pctx;
929
930 if (pctx != NULL) {
931 /* make sure pctx is not freed when destroying EVP_MD_CTX */
932 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
933 } else {
934 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
935 }
936 }
937 #endif /* !defined(FIPS_MODULE) */
938
939 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
940 {
941 return ctx->md_data;
942 }
943
944 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
945 const void *data, size_t count)
946 {
947 return ctx->update;
948 }
949
950 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
951 int (*update) (EVP_MD_CTX *ctx,
952 const void *data, size_t count))
953 {
954 ctx->update = update;
955 }
956
957 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
958 {
959 ctx->flags |= flags;
960 }
961
962 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
963 {
964 ctx->flags &= ~flags;
965 }
966
967 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
968 {
969 return (ctx->flags & flags);
970 }
971
972 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
973 {
974 ctx->flags |= flags;
975 }
976
977 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
978 {
979 ctx->flags &= ~flags;
980 }
981
982 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
983 {
984 return (ctx->flags & flags);
985 }
986
987 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
988 {
989 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
990
991 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
992 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
993 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
994 return -2;
995 }
996
997 if (name == NULL)
998 return -1;
999
1000 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1001 (char *)name, 0);
1002 return EVP_PKEY_CTX_set_params(ctx, params);
1003 }
1004
1005 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1006 {
1007 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1008 OSSL_PARAM *p = params;
1009
1010 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1011 /* There is no legacy support for this */
1012 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1013 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1014 return -2;
1015 }
1016
1017 if (name == NULL)
1018 return -1;
1019
1020 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1021 name, namelen);
1022 if (!EVP_PKEY_CTX_get_params(ctx, params))
1023 return -1;
1024 return 1;
1025 }