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