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