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