]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_lib.c
Add a mechnism to save the name of fetched methods
[thirdparty/openssl.git] / crypto / evp / evp_lib.c
CommitLineData
62867571 1/*
fd38836b 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
58964a49 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
58964a49
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/evp.h>
13#include <openssl/objects.h>
718b133a
MC
14#include <openssl/params.h>
15#include <openssl/core_names.h>
ff64702b 16#include <openssl/dh.h>
2db6bf6f 17#include "internal/evp_int.h"
3653d0c2 18#include "internal/provider.h"
7638370c 19#include "evp_locl.h"
58964a49 20
319e518a 21#if !defined(FIPS_MODE)
6b691a5c 22int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e
MC
23{
24 int ret;
718b133a 25 const EVP_CIPHER *cipher = c->cipher;
0f113f3e 26
718b133a
MC
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)) {
2acdef5e 45 case EVP_CIPH_WRAP_MODE:
718b133a 46 if (EVP_CIPHER_nid(cipher) == NID_id_smime_alg_CMS3DESwrap)
4ec36aff 47 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
0f113f3e 48 ret = 1;
2acdef5e
DSH
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:
49c9c1b3 55 ret = -2;
2acdef5e
DSH
56 break;
57
58 default:
0f113f3e 59 ret = EVP_CIPHER_set_asn1_iv(c, type);
2acdef5e 60 }
0f113f3e
MC
61 } else
62 ret = -1;
49c9c1b3
DO
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;
26a7d938 69 return ret;
0f113f3e 70}
58964a49 71
6b691a5c 72int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e
MC
73{
74 int ret;
718b133a
MC
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 }
0f113f3e 86
718b133a
MC
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)) {
2acdef5e
DSH
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:
49c9c1b3 100 ret = -2;
2acdef5e
DSH
101 break;
102
103 default:
104 ret = EVP_CIPHER_get_asn1_iv(c, type);
105 break;
106 }
0f113f3e
MC
107 } else
108 ret = -1;
49c9c1b3
DO
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;
26a7d938 115 return ret;
0f113f3e 116}
58964a49 117
718b133a 118int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
0f113f3e
MC
119{
120 int i = 0;
121 unsigned int l;
122
123 if (type != NULL) {
718b133a
MC
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);
0f113f3e 130 if (i != (int)l)
26a7d938 131 return -1;
718b133a
MC
132
133 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
134 return -1;
0f113f3e 135 }
26a7d938 136 return i;
0f113f3e 137}
58964a49 138
6b691a5c 139int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e
MC
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 }
26a7d938 149 return i;
0f113f3e 150}
319e518a 151#endif /* !defined(FIPS_MODE) */
884e8ec6
DSH
152
153/* Convert the various cipher NIDs and dummies to a proper OID NID */
84fa704c 154int EVP_CIPHER_type(const EVP_CIPHER *ctx)
884e8ec6 155{
0f113f3e 156 int nid;
0f113f3e 157 nid = EVP_CIPHER_nid(ctx);
884e8ec6 158
0f113f3e 159 switch (nid) {
884e8ec6 160
0f113f3e
MC
161 case NID_rc2_cbc:
162 case NID_rc2_64_cbc:
163 case NID_rc2_40_cbc:
884e8ec6 164
0f113f3e 165 return NID_rc2_cbc;
884e8ec6 166
0f113f3e
MC
167 case NID_rc4:
168 case NID_rc4_40:
884e8ec6 169
0f113f3e 170 return NID_rc4;
884e8ec6 171
0f113f3e
MC
172 case NID_aes_128_cfb128:
173 case NID_aes_128_cfb8:
174 case NID_aes_128_cfb1:
8d1ebe0b 175
0f113f3e 176 return NID_aes_128_cfb128;
8d1ebe0b 177
0f113f3e
MC
178 case NID_aes_192_cfb128:
179 case NID_aes_192_cfb8:
180 case NID_aes_192_cfb1:
8d1ebe0b 181
0f113f3e 182 return NID_aes_192_cfb128;
8d1ebe0b 183
0f113f3e
MC
184 case NID_aes_256_cfb128:
185 case NID_aes_256_cfb8:
186 case NID_aes_256_cfb1:
8d1ebe0b 187
0f113f3e 188 return NID_aes_256_cfb128;
8d1ebe0b 189
0f113f3e
MC
190 case NID_des_cfb64:
191 case NID_des_cfb8:
192 case NID_des_cfb1:
8d1ebe0b 193
0f113f3e 194 return NID_des_cfb64;
8d1ebe0b 195
0f113f3e
MC
196 case NID_des_ede3_cfb64:
197 case NID_des_ede3_cfb8:
198 case NID_des_ede3_cfb1:
7e765bf2 199
0f113f3e 200 return NID_des_cfb64;
7e765bf2 201
0f113f3e 202 default:
319e518a
MC
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
0f113f3e 216 }
884e8ec6
DSH
217}
218
718b133a 219int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
0f113f3e 220{
459b15d4
SL
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);
13273237
RL
226
227 return ok != 0 ? v : -1;
0f113f3e 228}
7806f3dd 229
6343829a 230int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
0f113f3e 231{
718b133a 232 return EVP_CIPHER_block_size(ctx->cipher);
0f113f3e 233}
7806f3dd 234
e79f8773
RL
235int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
236{
237 return e->ctx_size;
238}
239
0f113f3e
MC
240int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
241 const unsigned char *in, unsigned int inl)
242{
718b133a 243 if (ctx->cipher->prov != NULL) {
f79858ac
RL
244 size_t outl = 0; /* ignored */
245 int blocksize = EVP_CIPHER_CTX_block_size(ctx);
246
718b133a 247 if (ctx->cipher->ccipher != NULL)
f79858ac
RL
248 return
249 ctx->cipher->ccipher(ctx->provctx, out, &outl,
250 inl + (blocksize == 1 ? 0 : blocksize),
251 in, (size_t)inl);
718b133a
MC
252 return 0;
253 }
254
0f113f3e
MC
255 return ctx->cipher->do_cipher(ctx, out, in, inl);
256}
7806f3dd
NL
257
258const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
259{
260 return ctx->cipher;
261}
7806f3dd 262
83b06347
RL
263int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
264{
265 return ctx->encrypt;
266}
267
7806f3dd 268unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
0f113f3e 269{
459b15d4 270 int ok;
13273237 271 unsigned long v = cipher->flags;
459b15d4
SL
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);
13273237
RL
276
277 return ok != 0 ? v : 0;
0f113f3e 278}
7806f3dd 279
7806f3dd 280void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
281{
282 return ctx->app_data;
283}
7806f3dd
NL
284
285void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
0f113f3e
MC
286{
287 ctx->app_data = data;
288}
7806f3dd 289
44ab2dfd 290void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
83b06347
RL
291{
292 return ctx->cipher_data;
293}
294
98ee7543
MC
295void *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
6343829a 305int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
0f113f3e 306{
459b15d4
SL
307 int ok, v = cipher->iv_len;
308 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
718b133a 309
459b15d4
SL
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;
0f113f3e 314}
7806f3dd 315
6343829a 316int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 317{
718b133a 318 return EVP_CIPHER_iv_length(ctx->cipher);
0f113f3e 319}
7806f3dd 320
83b06347
RL
321const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
322{
323 return ctx->oiv;
324}
325
13273237
RL
326/*
327 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
328 */
83b06347
RL
329const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
330{
459b15d4 331 int ok;
13273237 332 const unsigned char *v = ctx->iv;
459b15d4 333 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 334
459b15d4
SL
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;
83b06347
RL
341}
342
343unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
344{
459b15d4 345 int ok;
13273237 346 unsigned char *v = ctx->iv;
459b15d4
SL
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);
13273237 353
459b15d4 354 return ok != 0 ? v : NULL;
83b06347
RL
355}
356
357unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
358{
359 return ctx->buf;
360}
361
362int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
363{
459b15d4
SL
364 int ok, v = ctx->num;
365 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 366
459b15d4
SL
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;
83b06347
RL
371}
372
13273237 373int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
83b06347 374{
459b15d4
SL
375 int ok;
376 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 377
459b15d4
SL
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;
13273237 383 return ok != 0;
83b06347
RL
384}
385
6343829a 386int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
0f113f3e 387{
459b15d4
SL
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);
df05f2ce 393
459b15d4 394 return ok != 0 ? v : -1;
0f113f3e 395}
7806f3dd 396
6343829a 397int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 398{
459b15d4
SL
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);
13273237 404
459b15d4 405 return ok != 0 ? v : -1;
0f113f3e 406}
7806f3dd
NL
407
408int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
0f113f3e
MC
409{
410 return cipher->nid;
411}
7806f3dd
NL
412
413int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
414{
415 return ctx->cipher->nid;
416}
7806f3dd 417
718b133a
MC
418int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
419{
459b15d4
SL
420 int ok, v = EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
421 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
718b133a 422
459b15d4
SL
423 params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_MODE, &v);
424 ok = evp_do_ciph_getparams(cipher, params);
718b133a 425
459b15d4
SL
426 return ok != 0 ? v : 0;
427}
718b133a 428
0f113f3e
MC
429int EVP_MD_block_size(const EVP_MD *md)
430{
7556b9df
MC
431 if (md == NULL) {
432 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
433 return -1;
434 }
435
436 if (md->prov != NULL && md->dblock_size != NULL)
437 return (int)md->dblock_size();
438
0f113f3e
MC
439 return md->block_size;
440}
7806f3dd
NL
441
442int EVP_MD_type(const EVP_MD *md)
0f113f3e
MC
443{
444 return md->type;
445}
7806f3dd
NL
446
447int EVP_MD_pkey_type(const EVP_MD *md)
0f113f3e
MC
448{
449 return md->pkey_type;
450}
7806f3dd 451
6343829a 452int EVP_MD_size(const EVP_MD *md)
0f113f3e
MC
453{
454 if (!md) {
455 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
456 return -1;
457 }
8c8cf0d9
MC
458
459 if (md->prov != NULL && md->size != NULL)
460 return (int)md->size();
461
0f113f3e
MC
462 return md->md_size;
463}
7806f3dd 464
e5fa864f 465unsigned long EVP_MD_flags(const EVP_MD *md)
0f113f3e
MC
466{
467 return md->flags;
468}
e5fa864f 469
2db6bf6f
RL
470EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
471{
43ecb9c3
RS
472 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
473
2db6bf6f
RL
474 if (md != NULL) {
475 md->type = md_type;
476 md->pkey_type = pkey_type;
3653d0c2
MC
477 md->lock = CRYPTO_THREAD_lock_new();
478 if (md->lock == NULL) {
479 OPENSSL_free(md);
480 return NULL;
481 }
482 md->refcnt = 1;
2db6bf6f
RL
483 }
484 return md;
485}
df05f2ce 486
2db6bf6f
RL
487EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
488{
489 EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
43ecb9c3 490
df05f2ce
MC
491 if (to != NULL) {
492 CRYPTO_RWLOCK *lock = to->lock;
2db6bf6f 493 memcpy(to, md, sizeof(*to));
df05f2ce
MC
494 to->lock = lock;
495 }
2db6bf6f
RL
496 return to;
497}
3653d0c2 498
70c35fd1 499int EVP_MD_up_ref(EVP_MD *md)
3653d0c2
MC
500{
501 int ref = 0;
502
503 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
504 return 1;
505}
506
2db6bf6f
RL
507void EVP_MD_meth_free(EVP_MD *md)
508{
3653d0c2
MC
509 if (md != NULL) {
510 int i;
511
512 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
513 if (i > 0)
514 return;
515 ossl_provider_free(md->prov);
6b9e3724 516 OPENSSL_free(md->name);
3653d0c2
MC
517 CRYPTO_THREAD_lock_free(md->lock);
518 OPENSSL_free(md);
519 }
2db6bf6f
RL
520}
521int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
522{
523 md->block_size = blocksize;
524 return 1;
525}
526int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
527{
528 md->md_size = resultsize;
529 return 1;
530}
531int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
532{
533 md->ctx_size = datasize;
534 return 1;
535}
536int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
537{
538 md->flags = flags;
539 return 1;
540}
541int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
542{
543 md->init = init;
544 return 1;
545}
546int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
547 const void *data,
548 size_t count))
549{
550 md->update = update;
551 return 1;
552}
553int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
554 unsigned char *md))
555{
556 md->final = final;
557 return 1;
558}
559int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
560 const EVP_MD_CTX *from))
561{
562 md->copy = copy;
563 return 1;
564}
565int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
566{
567 md->cleanup = cleanup;
568 return 1;
569}
570int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
571 int p1, void *p2))
572{
573 md->md_ctrl = ctrl;
574 return 1;
575}
576
577int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
578{
579 return md->block_size;
580}
581int EVP_MD_meth_get_result_size(const EVP_MD *md)
582{
583 return md->md_size;
584}
585int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
586{
587 return md->ctx_size;
588}
589unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
590{
8bfa99f0 591 return md->flags;
2db6bf6f
RL
592}
593int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
594{
595 return md->init;
596}
597int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
598 const void *data,
599 size_t count)
600{
601 return md->update;
602}
603int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
604 unsigned char *md)
605{
606 return md->final;
607}
608int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
609 const EVP_MD_CTX *from)
610{
611 return md->copy;
612}
613int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
614{
615 return md->cleanup;
616}
617int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
618 int p1, void *p2)
619{
620 return md->md_ctrl;
621}
622
7806f3dd 623const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
0f113f3e 624{
b7c913c8 625 if (ctx == NULL)
0f113f3e 626 return NULL;
b7c913c8 627 return ctx->reqdigest;
0f113f3e 628}
7806f3dd 629
7638370c
RL
630EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
631{
632 return ctx->pctx;
633}
634
319e518a
MC
635#if !defined(FIPS_MODE)
636/* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
00902d94
PY
637void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
638{
81c79453
PY
639 /*
640 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
641 * we have to deal with the cleanup job here.
642 */
643 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
644 EVP_PKEY_CTX_free(ctx->pctx);
645
00902d94 646 ctx->pctx = pctx;
81c79453
PY
647
648 if (pctx != NULL) {
649 /* make sure pctx is not freed when destroying EVP_MD_CTX */
650 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
651 } else {
652 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
653 }
00902d94 654}
319e518a 655#endif /* !defined(FIPS_MODE) */
00902d94 656
7638370c
RL
657void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
658{
659 return ctx->md_data;
660}
661
662int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
663 const void *data, size_t count)
664{
665 return ctx->update;
666}
667
668void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
669 int (*update) (EVP_MD_CTX *ctx,
670 const void *data, size_t count))
671{
672 ctx->update = update;
673}
674
7806f3dd 675void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
676{
677 ctx->flags |= flags;
678}
7806f3dd
NL
679
680void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
681{
682 ctx->flags &= ~flags;
683}
7806f3dd
NL
684
685int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
686{
687 return (ctx->flags & flags);
688}
e92f9f45
DSH
689
690void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
691{
692 ctx->flags |= flags;
693}
e92f9f45
DSH
694
695void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
696{
697 ctx->flags &= ~flags;
698}
e92f9f45
DSH
699
700int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
701{
702 return (ctx->flags & flags);
703}
f842b6b2
RL
704
705int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
706 void *ctx, int cmd, const char *value)
707{
708 size_t len;
709
710 len = strlen(value);
711 if (len > INT_MAX)
712 return -1;
713 return cb(ctx, cmd, (void *)value, len);
714}
715
716int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
717 void *ctx, int cmd, const char *hex)
718{
719 unsigned char *bin;
720 long binlen;
721 int rv = -1;
722
723 bin = OPENSSL_hexstr2buf(hex, &binlen);
724 if (bin == NULL)
725 return 0;
726 if (binlen <= INT_MAX)
727 rv = cb(ctx, cmd, bin, binlen);
728 OPENSSL_free(bin);
729 return rv;
730}
ff64702b
MC
731
732#ifndef FIPS_MODE
76ca35e7 733# ifndef OPENSSL_NO_DH
ff64702b
MC
734/*
735 * TODO(3.0): Temporarily unavailable in FIPS mode. This will need to be added
736 * in later.
737 */
738
76ca35e7 739# define MAX_PARAMS 10
ff64702b
MC
740typedef struct {
741 /* Number of the current param */
742 size_t curr;
743 struct {
744 /* Key for the current param */
745 const char *key;
746 /* Value for the current param */
747 const BIGNUM *bnparam;
748 /* Size of the buffer required for the BN */
749 size_t bufsz;
750 } params[MAX_PARAMS];
751 /* Running count of the total size required */
752 size_t totsz;
753 int ispublic;
754} PARAMS_TEMPLATE;
755
756static int push_param_bn(PARAMS_TEMPLATE *tmpl, const char *key,
757 const BIGNUM *bn)
758{
759 int sz;
760
761 sz = BN_num_bytes(bn);
762 if (sz <= 0)
763 return 0;
764 tmpl->params[tmpl->curr].key = key;
765 tmpl->params[tmpl->curr].bnparam = bn;
766 tmpl->params[tmpl->curr++].bufsz = (size_t)sz;
767 tmpl->totsz += sizeof(OSSL_PARAM) + (size_t)sz;
768
769 return 1;
770}
771
772static OSSL_PARAM *param_template_to_param(PARAMS_TEMPLATE *tmpl, size_t *sz)
773{
774 size_t i;
775 void *buf;
776 OSSL_PARAM *param = NULL;
777 unsigned char *currbuf = NULL;
778
779 if (tmpl->totsz == 0)
780 return NULL;
781
782 /* Add some space for the end of OSSL_PARAM marker */
783 tmpl->totsz += sizeof(*param);
784
785 if (tmpl->ispublic)
786 buf = OPENSSL_zalloc(tmpl->totsz);
787 else
788 buf = OPENSSL_secure_zalloc(tmpl->totsz);
789 if (buf == NULL)
790 return NULL;
791 param = buf;
792
793 currbuf = (unsigned char *)buf + (sizeof(*param) * (tmpl->curr + 1));
794
795 for (i = 0; i < tmpl->curr; i++) {
796 if (!ossl_assert((currbuf - (unsigned char *)buf )
797 + tmpl->params[i].bufsz <= tmpl->totsz))
798 goto err;
799 if (BN_bn2nativepad(tmpl->params[i].bnparam, currbuf,
800 tmpl->params[i].bufsz) < 0)
801 goto err;
802 param[i] = OSSL_PARAM_construct_BN(tmpl->params[i].key, currbuf,
803 tmpl->params[i].bufsz);
804 currbuf += tmpl->params[i].bufsz;
805 }
806 param[i] = OSSL_PARAM_construct_end();
807
808 if (sz != NULL)
809 *sz = tmpl->totsz;
810 return param;
811
812 err:
813 if (tmpl->ispublic)
814 OPENSSL_free(param);
815 else
816 OPENSSL_clear_free(param, tmpl->totsz);
817 return NULL;
818}
819
820static OSSL_PARAM *evp_pkey_dh_to_param(EVP_PKEY *pkey, size_t *sz)
821{
822 DH *dh = pkey->pkey.dh;
823 PARAMS_TEMPLATE tmpl = {0};
824 const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh);
825 const BIGNUM *pub_key = DH_get0_pub_key(dh);
826 const BIGNUM *priv_key = DH_get0_priv_key(dh);
827
828 if (p == NULL || g == NULL || pub_key == NULL)
829 return NULL;
830
831 if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_P, p)
832 || !push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_G, g)
833 || !push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_key))
834 return NULL;
835
836 if (q != NULL) {
837 if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_Q, q))
838 return NULL;
839 }
840
841 if (priv_key != NULL) {
842 if (!push_param_bn(&tmpl, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv_key))
843 return NULL;
844 } else {
845 tmpl.ispublic = 1;
846 }
847
848 return param_template_to_param(&tmpl, sz);
849}
76ca35e7 850# endif /* OPENSSL_NO_DH */
ff64702b
MC
851
852OSSL_PARAM *evp_pkey_to_param(EVP_PKEY *pkey, size_t *sz)
853{
854 switch (pkey->type) {
76ca35e7 855# ifndef OPENSSL_NO_DH
ff64702b
MC
856 case EVP_PKEY_DH:
857 return evp_pkey_dh_to_param(pkey, sz);
76ca35e7 858# endif
ff64702b
MC
859 default:
860 return NULL;
861 }
862}
863
864#endif /* FIPS_MODE */