Re-implement EVP_MD_name() and EVP_CIPHER_name() as functions
[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, v = cipher->block_size;
222     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
223
224     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_BLOCK_SIZE, &v);
225     ok = evp_do_ciph_getparams(cipher, params);
226
227     return ok != 0 ? v : -1;
228 }
229
230 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
231 {
232     return EVP_CIPHER_block_size(ctx->cipher);
233 }
234
235 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
236 {
237     return e->ctx_size;
238 }
239
240 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
241                const unsigned char *in, unsigned int inl)
242 {
243     if (ctx->cipher->prov != NULL) {
244         size_t outl = 0;         /* ignored */
245         int blocksize = EVP_CIPHER_CTX_block_size(ctx);
246
247         if (ctx->cipher->ccipher != NULL)
248             return
249                 ctx->cipher->ccipher(ctx->provctx, out, &outl,
250                                      inl + (blocksize == 1 ? 0 : blocksize),
251                                      in, (size_t)inl);
252         return 0;
253     }
254
255     return ctx->cipher->do_cipher(ctx, out, in, inl);
256 }
257
258 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
259 {
260     return ctx->cipher;
261 }
262
263 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
264 {
265     return ctx->encrypt;
266 }
267
268 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
269 {
270     int ok;
271     unsigned long v = cipher->flags;
272     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
273
274     params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
275     ok = evp_do_ciph_getparams(cipher, params);
276
277     return ok != 0 ? v : 0;
278 }
279
280 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
281 {
282     return ctx->app_data;
283 }
284
285 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
286 {
287     ctx->app_data = data;
288 }
289
290 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
291 {
292     return ctx->cipher_data;
293 }
294
295 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
296 {
297     void *old_cipher_data;
298
299     old_cipher_data = ctx->cipher_data;
300     ctx->cipher_data = cipher_data;
301
302     return old_cipher_data;
303 }
304
305 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
306 {
307     int ok, v = cipher->iv_len;
308     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
309
310     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_IVLEN, &v);
311     ok = evp_do_ciph_getparams(cipher, params);
312
313     return ok != 0 ? v : -1;
314 }
315
316 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
317 {
318     return EVP_CIPHER_iv_length(ctx->cipher);
319 }
320
321 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
322 {
323     return ctx->oiv;
324 }
325
326 /*
327  * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
328  */
329 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
330 {
331     int ok;
332     const unsigned char *v = ctx->iv;
333     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
334
335     params[0] =
336         OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
337                                        sizeof(ctx->iv));
338     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
339
340     return ok != 0 ? v : NULL;
341 }
342
343 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
344 {
345     int ok;
346     unsigned char *v = ctx->iv;
347     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
348
349     params[0] =
350         OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
351                                        sizeof(ctx->iv));
352     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
353
354     return ok != 0 ? v : NULL;
355 }
356
357 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
358 {
359     return ctx->buf;
360 }
361
362 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
363 {
364     int ok, v = ctx->num;
365     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
366
367     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &v);
368     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
369
370     return ok != 0 ? v : -1;
371 }
372
373 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
374 {
375     int ok;
376     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
377
378     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &num);
379     ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
380
381     if (ok != 0)
382         ctx->num = num;
383     return ok != 0;
384 }
385
386 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
387 {
388     int ok, v = cipher->key_len;
389     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
390
391     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v);
392     ok = evp_do_ciph_getparams(cipher, params);
393
394     return ok != 0 ? v : -1;
395 }
396
397 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
398 {
399     int ok, v = ctx->key_len;
400     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
401
402     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v);
403     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
404
405     return ok != 0 ? v : -1;
406 }
407
408 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
409 {
410     return cipher->nid;
411 }
412
413 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
414 {
415     return ctx->cipher->nid;
416 }
417
418 const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
419 {
420     if (cipher->prov != NULL)
421         return cipher->name;
422 #ifndef FIPS_MODE
423     return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
424 #else
425     return NULL;
426 #endif
427 }
428
429 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
430 {
431     int ok, v = EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
432     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
433
434     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_MODE, &v);
435     ok = evp_do_ciph_getparams(cipher, params);
436
437     return ok != 0 ? v : 0;
438 }
439
440 const char *EVP_MD_name(const EVP_MD *md)
441 {
442     if (md->prov != NULL)
443         return md->name;
444 #ifndef FIPS_MODE
445     return OBJ_nid2sn(EVP_MD_nid(md));
446 #else
447     return NULL;
448 #endif
449 }
450
451 int EVP_MD_block_size(const EVP_MD *md)
452 {
453     if (md == NULL) {
454         EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
455         return -1;
456     }
457
458     if (md->prov != NULL && md->dblock_size != NULL)
459         return (int)md->dblock_size();
460
461     return md->block_size;
462 }
463
464 int EVP_MD_type(const EVP_MD *md)
465 {
466     return md->type;
467 }
468
469 int EVP_MD_pkey_type(const EVP_MD *md)
470 {
471     return md->pkey_type;
472 }
473
474 int EVP_MD_size(const EVP_MD *md)
475 {
476     if (!md) {
477         EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
478         return -1;
479     }
480
481     if (md->prov != NULL && md->size != NULL)
482         return (int)md->size();
483
484     return md->md_size;
485 }
486
487 unsigned long EVP_MD_flags(const EVP_MD *md)
488 {
489     return md->flags;
490 }
491
492 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
493 {
494     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
495
496     if (md != NULL) {
497         md->type = md_type;
498         md->pkey_type = pkey_type;
499         md->lock = CRYPTO_THREAD_lock_new();
500         if (md->lock == NULL) {
501             OPENSSL_free(md);
502             return NULL;
503         }
504         md->refcnt = 1;
505     }
506     return md;
507 }
508
509 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
510 {
511     EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
512
513     if (to != NULL) {
514         CRYPTO_RWLOCK *lock = to->lock;
515         memcpy(to, md, sizeof(*to));
516         to->lock = lock;
517     }
518     return to;
519 }
520
521 int EVP_MD_up_ref(EVP_MD *md)
522 {
523     int ref = 0;
524
525     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
526     return 1;
527 }
528
529 void EVP_MD_meth_free(EVP_MD *md)
530 {
531     if (md != NULL) {
532         int i;
533
534         CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
535         if (i > 0)
536             return;
537         ossl_provider_free(md->prov);
538         OPENSSL_free(md->name);
539         CRYPTO_THREAD_lock_free(md->lock);
540         OPENSSL_free(md);
541     }
542 }
543 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
544 {
545     md->block_size = blocksize;
546     return 1;
547 }
548 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
549 {
550     md->md_size = resultsize;
551     return 1;
552 }
553 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
554 {
555     md->ctx_size = datasize;
556     return 1;
557 }
558 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
559 {
560     md->flags = flags;
561     return 1;
562 }
563 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
564 {
565     md->init = init;
566     return 1;
567 }
568 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
569                                                      const void *data,
570                                                      size_t count))
571 {
572     md->update = update;
573     return 1;
574 }
575 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
576                                                    unsigned char *md))
577 {
578     md->final = final;
579     return 1;
580 }
581 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
582                                                  const EVP_MD_CTX *from))
583 {
584     md->copy = copy;
585     return 1;
586 }
587 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
588 {
589     md->cleanup = cleanup;
590     return 1;
591 }
592 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
593                                                  int p1, void *p2))
594 {
595     md->md_ctrl = ctrl;
596     return 1;
597 }
598
599 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
600 {
601     return md->block_size;
602 }
603 int EVP_MD_meth_get_result_size(const EVP_MD *md)
604 {
605     return md->md_size;
606 }
607 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
608 {
609     return md->ctx_size;
610 }
611 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
612 {
613     return md->flags;
614 }
615 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
616 {
617     return md->init;
618 }
619 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
620                                                 const void *data,
621                                                 size_t count)
622 {
623     return md->update;
624 }
625 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
626                                                unsigned char *md)
627 {
628     return md->final;
629 }
630 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
631                                               const EVP_MD_CTX *from)
632 {
633     return md->copy;
634 }
635 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
636 {
637     return md->cleanup;
638 }
639 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
640                                               int p1, void *p2)
641 {
642     return md->md_ctrl;
643 }
644
645 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
646 {
647     if (ctx == NULL)
648         return NULL;
649     return ctx->reqdigest;
650 }
651
652 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
653 {
654     return ctx->pctx;
655 }
656
657 #if !defined(FIPS_MODE)
658 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
659 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
660 {
661     /*
662      * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
663      * we have to deal with the cleanup job here.
664      */
665     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
666         EVP_PKEY_CTX_free(ctx->pctx);
667
668     ctx->pctx = pctx;
669
670     if (pctx != NULL) {
671         /* make sure pctx is not freed when destroying EVP_MD_CTX */
672         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
673     } else {
674         EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
675     }
676 }
677 #endif /* !defined(FIPS_MODE) */
678
679 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
680 {
681     return ctx->md_data;
682 }
683
684 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
685                                              const void *data, size_t count)
686 {
687     return ctx->update;
688 }
689
690 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
691                               int (*update) (EVP_MD_CTX *ctx,
692                                              const void *data, size_t count))
693 {
694     ctx->update = update;
695 }
696
697 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
698 {
699     ctx->flags |= flags;
700 }
701
702 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
703 {
704     ctx->flags &= ~flags;
705 }
706
707 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
708 {
709     return (ctx->flags & flags);
710 }
711
712 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
713 {
714     ctx->flags |= flags;
715 }
716
717 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
718 {
719     ctx->flags &= ~flags;
720 }
721
722 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
723 {
724     return (ctx->flags & flags);
725 }
726
727 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
728                  void *ctx, int cmd, const char *value)
729 {
730     size_t len;
731
732     len = strlen(value);
733     if (len > INT_MAX)
734         return -1;
735     return cb(ctx, cmd, (void *)value, len);
736 }
737
738 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
739                  void *ctx, int cmd, const char *hex)
740 {
741     unsigned char *bin;
742     long binlen;
743     int rv = -1;
744
745     bin = OPENSSL_hexstr2buf(hex, &binlen);
746     if (bin == NULL)
747         return 0;
748     if (binlen <= INT_MAX)
749         rv = cb(ctx, cmd, bin, binlen);
750     OPENSSL_free(bin);
751     return rv;
752 }
753
754 #ifndef FIPS_MODE
755 # ifndef OPENSSL_NO_DH
756 /*
757  * TODO(3.0): Temporarily unavailable in FIPS mode. This will need to be added
758  * in later.
759  */
760
761 #  define MAX_PARAMS 10
762 typedef struct {
763     /* Number of the current param */
764     size_t curr;
765     struct {
766         /* Key for the current param */
767         const char *key;
768         /* Value for the current param */
769         const BIGNUM *bnparam;
770         /* Size of the buffer required for the BN */
771         size_t bufsz;
772     } params[MAX_PARAMS];
773     /* Running count of the total size required */
774     size_t totsz;
775     int ispublic;
776 } PARAMS_TEMPLATE;
777
778 static int push_param_bn(PARAMS_TEMPLATE *tmpl, const char *key,
779                          const BIGNUM *bn)
780 {
781     int sz;
782
783     sz = BN_num_bytes(bn);
784     if (sz <= 0)
785         return 0;
786     tmpl->params[tmpl->curr].key = key;
787     tmpl->params[tmpl->curr].bnparam = bn;
788     tmpl->params[tmpl->curr++].bufsz = (size_t)sz;
789     tmpl->totsz += sizeof(OSSL_PARAM) + (size_t)sz;
790
791     return 1;
792 }
793
794 static OSSL_PARAM *param_template_to_param(PARAMS_TEMPLATE *tmpl, size_t *sz)
795 {
796     size_t i;
797     void *buf;
798     OSSL_PARAM *param = NULL;
799     unsigned char *currbuf = NULL;
800
801     if (tmpl->totsz == 0)
802         return NULL;
803
804     /* Add some space for the end of OSSL_PARAM marker */
805     tmpl->totsz += sizeof(*param);
806
807     if (tmpl->ispublic)
808         buf = OPENSSL_zalloc(tmpl->totsz);
809     else
810         buf = OPENSSL_secure_zalloc(tmpl->totsz);
811     if (buf == NULL)
812         return NULL;
813     param = buf;
814
815     currbuf = (unsigned char *)buf + (sizeof(*param) * (tmpl->curr + 1));
816
817     for (i = 0; i < tmpl->curr; i++) {
818         if (!ossl_assert((currbuf - (unsigned char *)buf )
819                          + tmpl->params[i].bufsz <= tmpl->totsz))
820             goto err;
821         if (BN_bn2nativepad(tmpl->params[i].bnparam, currbuf,
822                             tmpl->params[i].bufsz) < 0)
823             goto err;
824         param[i] = OSSL_PARAM_construct_BN(tmpl->params[i].key, currbuf,
825                                            tmpl->params[i].bufsz);
826         currbuf += tmpl->params[i].bufsz;
827     }
828     param[i] = OSSL_PARAM_construct_end();
829
830     if (sz != NULL)
831         *sz = tmpl->totsz;
832     return param;
833
834  err:
835     if (tmpl->ispublic)
836         OPENSSL_free(param);
837     else
838         OPENSSL_clear_free(param, tmpl->totsz);
839     return NULL;
840 }
841
842 static OSSL_PARAM *evp_pkey_dh_to_param(EVP_PKEY *pkey, size_t *sz)
843 {
844     DH *dh = pkey->pkey.dh;
845     PARAMS_TEMPLATE tmpl = {0};
846     const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh);
847     const BIGNUM *pub_key = DH_get0_pub_key(dh);
848     const BIGNUM *priv_key = DH_get0_priv_key(dh);
849
850     if (p == NULL || g == NULL || pub_key == NULL)
851         return NULL;
852
853     if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_P, p)
854             || !push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_G, g)
855             || !push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_key))
856         return NULL;
857
858     if (q != NULL) {
859         if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_Q, q))
860             return NULL;
861     }
862
863     if (priv_key != NULL) {
864         if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv_key))
865             return NULL;
866     } else {
867         tmpl.ispublic = 1;
868     }
869
870     return param_template_to_param(&tmpl, sz);
871 }
872 # endif /* OPENSSL_NO_DH */
873
874 OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz)
875 {
876     switch (pkey->type) {
877 # ifndef OPENSSL_NO_DH
878     case EVP_PKEY_DH:
879         return evp_pkey_dh_to_param(pkey, sz);
880 # endif
881     default:
882         return NULL;
883     }
884 }
885
886 #endif /* FIPS_MODE */