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