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