]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_lib.c
params: handle the modified sentinel.
[thirdparty/openssl.git] / crypto / evp / evp_lib.c
CommitLineData
62867571 1/*
33388b44 2 * Copyright 1995-2020 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>
25f2138b 17#include "crypto/evp.h"
3653d0c2 18#include "internal/provider.h"
706457b7 19#include "evp_local.h"
58964a49 20
319e518a 21#if !defined(FIPS_MODE)
6b691a5c 22int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e 23{
c96399e2 24 int ret = -1; /* Assume the worst */
718b133a 25 const EVP_CIPHER *cipher = c->cipher;
0f113f3e 26
c96399e2
RL
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) {
718b133a 44 ret = cipher->set_asn1_parameters(c, type);
c96399e2 45 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
718b133a 46 switch (EVP_CIPHER_mode(cipher)) {
2acdef5e 47 case EVP_CIPH_WRAP_MODE:
c96399e2 48 if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
4ec36aff 49 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
0f113f3e 50 ret = 1;
2acdef5e
DSH
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:
49c9c1b3 57 ret = -2;
2acdef5e
DSH
58 break;
59
60 default:
0f113f3e 61 ret = EVP_CIPHER_set_asn1_iv(c, type);
2acdef5e 62 }
c96399e2
RL
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:
51ba9ebd
RS
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);
49c9c1b3
DO
101 if (ret < -1)
102 ret = -1;
26a7d938 103 return ret;
0f113f3e 104}
58964a49 105
6b691a5c 106int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e 107{
c96399e2 108 int ret = -1; /* Assume the worst */
718b133a
MC
109 const EVP_CIPHER *cipher = c->cipher;
110
c96399e2
RL
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) {
718b133a 128 ret = cipher->get_asn1_parameters(c, type);
c96399e2 129 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
718b133a 130 switch (EVP_CIPHER_mode(cipher)) {
2acdef5e
DSH
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:
49c9c1b3 139 ret = -2;
2acdef5e
DSH
140 break;
141
142 default:
143 ret = EVP_CIPHER_get_asn1_iv(c, type);
2acdef5e 144 }
c96399e2
RL
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
51ba9ebd
RS
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);
49c9c1b3
DO
167 if (ret < -1)
168 ret = -1;
26a7d938 169 return ret;
0f113f3e 170}
58964a49 171
718b133a 172int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
0f113f3e
MC
173{
174 int i = 0;
175 unsigned int l;
176
177 if (type != NULL) {
718b133a
MC
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);
0f113f3e 184 if (i != (int)l)
26a7d938 185 return -1;
718b133a
MC
186
187 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
188 return -1;
0f113f3e 189 }
26a7d938 190 return i;
0f113f3e 191}
58964a49 192
6b691a5c 193int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
0f113f3e
MC
194{
195 int i = 0;
196 unsigned int j;
089cb623 197 unsigned char *oiv = NULL;
0f113f3e
MC
198
199 if (type != NULL) {
089cb623 200 oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
0f113f3e
MC
201 j = EVP_CIPHER_CTX_iv_length(c);
202 OPENSSL_assert(j <= sizeof(c->iv));
089cb623 203 i = ASN1_TYPE_set_octetstring(type, oiv, j);
0f113f3e 204 }
26a7d938 205 return i;
0f113f3e 206}
319e518a 207#endif /* !defined(FIPS_MODE) */
884e8ec6
DSH
208
209/* Convert the various cipher NIDs and dummies to a proper OID NID */
84fa704c 210int EVP_CIPHER_type(const EVP_CIPHER *ctx)
884e8ec6 211{
0f113f3e 212 int nid;
0f113f3e 213 nid = EVP_CIPHER_nid(ctx);
884e8ec6 214
0f113f3e 215 switch (nid) {
884e8ec6 216
0f113f3e
MC
217 case NID_rc2_cbc:
218 case NID_rc2_64_cbc:
219 case NID_rc2_40_cbc:
884e8ec6 220
0f113f3e 221 return NID_rc2_cbc;
884e8ec6 222
0f113f3e
MC
223 case NID_rc4:
224 case NID_rc4_40:
884e8ec6 225
0f113f3e 226 return NID_rc4;
884e8ec6 227
0f113f3e
MC
228 case NID_aes_128_cfb128:
229 case NID_aes_128_cfb8:
230 case NID_aes_128_cfb1:
8d1ebe0b 231
0f113f3e 232 return NID_aes_128_cfb128;
8d1ebe0b 233
0f113f3e
MC
234 case NID_aes_192_cfb128:
235 case NID_aes_192_cfb8:
236 case NID_aes_192_cfb1:
8d1ebe0b 237
0f113f3e 238 return NID_aes_192_cfb128;
8d1ebe0b 239
0f113f3e
MC
240 case NID_aes_256_cfb128:
241 case NID_aes_256_cfb8:
242 case NID_aes_256_cfb1:
8d1ebe0b 243
0f113f3e 244 return NID_aes_256_cfb128;
8d1ebe0b 245
0f113f3e
MC
246 case NID_des_cfb64:
247 case NID_des_cfb8:
248 case NID_des_cfb1:
8d1ebe0b 249
0f113f3e 250 return NID_des_cfb64;
8d1ebe0b 251
0f113f3e
MC
252 case NID_des_ede3_cfb64:
253 case NID_des_ede3_cfb8:
254 case NID_des_ede3_cfb1:
7e765bf2 255
0f113f3e 256 return NID_des_cfb64;
7e765bf2 257
0f113f3e 258 default:
319e518a
MC
259#ifdef FIPS_MODE
260 return NID_undef;
261#else
262 {
263 /* Check it has an OID and it is valid */
264 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
265
266 if (OBJ_get0_data(otmp) == NULL)
267 nid = NID_undef;
268 ASN1_OBJECT_free(otmp);
269 return nid;
270 }
271#endif
0f113f3e 272 }
884e8ec6
DSH
273}
274
3c957bcd 275int evp_cipher_cache_constants(EVP_CIPHER *cipher)
0f113f3e 276{
1c3ace68 277 int ok;
3c957bcd
SL
278 size_t ivlen = 0;
279 size_t blksz = 0;
280 size_t keylen = 0;
281 unsigned int mode = 0;
282 unsigned long flags = 0;
283 OSSL_PARAM params[6];
284
285 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
286 params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
287 params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
288 params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
289 params[4] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &flags);
290 params[5] = OSSL_PARAM_construct_end();
459b15d4 291 ok = evp_do_ciph_getparams(cipher, params);
3c957bcd
SL
292 if (ok) {
293 /* Provided implementations may have a custom cipher_cipher */
294 if (cipher->prov != NULL && cipher->ccipher != NULL)
295 flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
296 cipher->block_size = blksz;
297 cipher->iv_len = ivlen;
298 cipher->key_len = keylen;
299 cipher->flags = flags | mode;
300 }
301 return ok;
302}
13273237 303
3c957bcd
SL
304int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
305{
306 return cipher->block_size;
0f113f3e 307}
7806f3dd 308
6343829a 309int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
0f113f3e 310{
718b133a 311 return EVP_CIPHER_block_size(ctx->cipher);
0f113f3e 312}
7806f3dd 313
e79f8773
RL
314int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
315{
316 return e->ctx_size;
317}
318
0f113f3e
MC
319int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
320 const unsigned char *in, unsigned int inl)
321{
718b133a 322 if (ctx->cipher->prov != NULL) {
f7397f0d
RL
323 /*
324 * If the provided implementation has a ccipher function, we use it,
325 * and translate its return value like this: 0 => -1, 1 => outlen
326 *
327 * Otherwise, we call the cupdate function if in != NULL, or cfinal
328 * if in == NULL. Regardless of which, we return what we got.
329 */
330 int ret = -1;
331 size_t outl = 0;
332 size_t blocksize = EVP_CIPHER_CTX_block_size(ctx);
f79858ac 333
718b133a 334 if (ctx->cipher->ccipher != NULL)
f7397f0d
RL
335 ret = ctx->cipher->ccipher(ctx->provctx, out, &outl,
336 inl + (blocksize == 1 ? 0 : blocksize),
337 in, (size_t)inl)
338 ? (int)outl : -1;
339 else if (in != NULL)
340 ret = ctx->cipher->cupdate(ctx->provctx, out, &outl,
341 inl + (blocksize == 1 ? 0 : blocksize),
342 in, (size_t)inl);
343 else
344 ret = ctx->cipher->cfinal(ctx->provctx, out, &outl,
345 blocksize == 1 ? 0 : blocksize);
346
347 return ret;
718b133a
MC
348 }
349
0f113f3e
MC
350 return ctx->cipher->do_cipher(ctx, out, in, inl);
351}
7806f3dd
NL
352
353const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
354{
355 return ctx->cipher;
356}
7806f3dd 357
83b06347
RL
358int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
359{
360 return ctx->encrypt;
361}
362
7806f3dd 363unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
0f113f3e 364{
3c957bcd 365 return cipher->flags;
0f113f3e 366}
7806f3dd 367
7806f3dd 368void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
369{
370 return ctx->app_data;
371}
7806f3dd
NL
372
373void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
0f113f3e
MC
374{
375 ctx->app_data = data;
376}
7806f3dd 377
44ab2dfd 378void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
83b06347
RL
379{
380 return ctx->cipher_data;
381}
382
98ee7543
MC
383void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
384{
385 void *old_cipher_data;
386
387 old_cipher_data = ctx->cipher_data;
388 ctx->cipher_data = cipher_data;
389
390 return old_cipher_data;
391}
392
6343829a 393int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
0f113f3e 394{
3c957bcd 395 return cipher->iv_len;
0f113f3e 396}
7806f3dd 397
6343829a 398int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 399{
88d87082
SL
400 int rv, len = EVP_CIPHER_iv_length(ctx->cipher);
401 size_t v = len;
a672a02a
SL
402 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
403
1c3ace68 404 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
7dddf2fc
SL
405 rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
406 if (rv == EVP_CTRL_RET_UNSUPPORTED)
407 goto legacy;
1c3ace68 408 return rv != 0 ? (int)v : -1;
7dddf2fc
SL
409 /* TODO (3.0) Remove legacy support */
410legacy:
411 if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
412 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
413 0, &len);
88d87082 414 return (rv == 1) ? len : -1;
7dddf2fc 415 }
88d87082 416 return len;
0f113f3e 417}
7806f3dd 418
dc64dc2e
SL
419int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX *ctx)
420{
421 int ret;
422 size_t v = 0;
423 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
424
425 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
426 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
427 return ret == 1 ? (int)v : 0;
428}
429
83b06347
RL
430const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
431{
089cb623
SL
432 int ok;
433 const unsigned char *v = ctx->oiv;
434 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
435
436 params[0] =
437 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
438 (void **)&v, sizeof(ctx->oiv));
439 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
440
441 return ok != 0 ? v : NULL;
83b06347
RL
442}
443
13273237
RL
444/*
445 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
446 */
83b06347
RL
447const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
448{
459b15d4 449 int ok;
13273237 450 const unsigned char *v = ctx->iv;
459b15d4 451 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 452
459b15d4
SL
453 params[0] =
454 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
455 sizeof(ctx->iv));
456 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
457
458 return ok != 0 ? v : NULL;
83b06347
RL
459}
460
461unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
462{
459b15d4 463 int ok;
13273237 464 unsigned char *v = ctx->iv;
459b15d4
SL
465 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
466
467 params[0] =
468 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v,
469 sizeof(ctx->iv));
470 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
13273237 471
459b15d4 472 return ok != 0 ? v : NULL;
83b06347
RL
473}
474
475unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
476{
477 return ctx->buf;
478}
479
480int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
481{
1c3ace68
SL
482 int ok;
483 unsigned int v = (unsigned int)ctx->num;
459b15d4 484 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 485
1c3ace68 486 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
459b15d4
SL
487 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
488
1c3ace68 489 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
83b06347
RL
490}
491
13273237 492int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
83b06347 493{
459b15d4 494 int ok;
1c3ace68 495 unsigned int n = (unsigned int)num;
459b15d4 496 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 497
1c3ace68 498 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
459b15d4
SL
499 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
500
501 if (ok != 0)
1c3ace68 502 ctx->num = (int)n;
13273237 503 return ok != 0;
83b06347
RL
504}
505
6343829a 506int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
0f113f3e 507{
3c957bcd 508 return cipher->key_len;
0f113f3e 509}
7806f3dd 510
6343829a 511int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 512{
1c3ace68
SL
513 int ok;
514 size_t v = ctx->key_len;
459b15d4
SL
515 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
516
1c3ace68 517 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
459b15d4 518 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
13273237 519
1c3ace68 520 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
0f113f3e 521}
7806f3dd
NL
522
523int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
0f113f3e
MC
524{
525 return cipher->nid;
526}
7806f3dd
NL
527
528int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
529{
530 return ctx->cipher->nid;
531}
7806f3dd 532
7cfa1717
RL
533int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
534{
e4a1d023
RL
535 if (cipher->prov != NULL)
536 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
537 return evp_is_a(NULL, 0, EVP_CIPHER_name(cipher), name);
7cfa1717
RL
538}
539
506cb0f6
RL
540int EVP_CIPHER_number(const EVP_CIPHER *cipher)
541{
542 return cipher->name_id;
543}
544
c750bc08
RL
545const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
546{
547 if (cipher->prov != NULL)
f7c16d48 548 return evp_first_name(cipher->prov, cipher->name_id);
c750bc08
RL
549#ifndef FIPS_MODE
550 return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
551#else
552 return NULL;
553#endif
554}
555
f651c727
RL
556void EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
557 void (*fn)(const char *name, void *data),
558 void *data)
559{
560 if (cipher->prov != NULL)
561 evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
562}
563
1d2622d4
RL
564const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
565{
566 return cipher->prov;
567}
568
718b133a
MC
569int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
570{
3c957bcd 571 return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
459b15d4 572}
718b133a 573
251e610c
RL
574int EVP_MD_is_a(const EVP_MD *md, const char *name)
575{
e4a1d023
RL
576 if (md->prov != NULL)
577 return evp_is_a(md->prov, md->name_id, NULL, name);
578 return evp_is_a(NULL, 0, EVP_MD_name(md), name);
251e610c
RL
579}
580
506cb0f6
RL
581int EVP_MD_number(const EVP_MD *md)
582{
583 return md->name_id;
584}
585
c750bc08
RL
586const char *EVP_MD_name(const EVP_MD *md)
587{
588 if (md->prov != NULL)
f7c16d48 589 return evp_first_name(md->prov, md->name_id);
c750bc08
RL
590#ifndef FIPS_MODE
591 return OBJ_nid2sn(EVP_MD_nid(md));
592#else
593 return NULL;
594#endif
595}
596
f651c727
RL
597void EVP_MD_names_do_all(const EVP_MD *md,
598 void (*fn)(const char *name, void *data),
599 void *data)
600{
601 if (md->prov != NULL)
602 evp_names_do_all(md->prov, md->name_id, fn, data);
603}
604
1d2622d4
RL
605const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
606{
607 return md->prov;
608}
609
0f113f3e
MC
610int EVP_MD_block_size(const EVP_MD *md)
611{
1c3ace68
SL
612 int ok;
613 size_t v = md->block_size;
6a3b7c68
RL
614 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
615
7556b9df
MC
616 if (md == NULL) {
617 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
618 return -1;
619 }
620
1c3ace68 621 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &v);
6a3b7c68 622 ok = evp_do_md_getparams(md, params);
7556b9df 623
1c3ace68 624 return ok != 0 ? (int)v : -1;
0f113f3e 625}
7806f3dd
NL
626
627int EVP_MD_type(const EVP_MD *md)
0f113f3e
MC
628{
629 return md->type;
630}
7806f3dd
NL
631
632int EVP_MD_pkey_type(const EVP_MD *md)
0f113f3e
MC
633{
634 return md->pkey_type;
635}
7806f3dd 636
6343829a 637int EVP_MD_size(const EVP_MD *md)
0f113f3e 638{
1c3ace68
SL
639 int ok;
640 size_t v = md->md_size;
6a3b7c68
RL
641 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
642
643 if (md == NULL) {
0f113f3e
MC
644 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
645 return -1;
646 }
8c8cf0d9 647
1c3ace68 648 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &v);
6a3b7c68 649 ok = evp_do_md_getparams(md, params);
8c8cf0d9 650
1c3ace68 651 return ok != 0 ? (int)v : -1;
0f113f3e 652}
7806f3dd 653
e5fa864f 654unsigned long EVP_MD_flags(const EVP_MD *md)
0f113f3e 655{
6a3b7c68
RL
656 int ok;
657 unsigned long v = md->flags;
658 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
659
660 params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
661 ok = evp_do_md_getparams(md, params);
662
663 return ok != 0 ? v : 0;
0f113f3e 664}
e5fa864f 665
2db6bf6f
RL
666EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
667{
3fd70262 668 EVP_MD *md = evp_md_new();
43ecb9c3 669
2db6bf6f
RL
670 if (md != NULL) {
671 md->type = md_type;
672 md->pkey_type = pkey_type;
673 }
674 return md;
675}
df05f2ce 676
2db6bf6f
RL
677EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
678{
3fd70262
RL
679 EVP_MD *to = NULL;
680
681 /*
682 * Non-legacy EVP_MDs can't be duplicated like this.
683 * Use EVP_MD_up_ref() instead.
684 */
685 if (md->prov != NULL)
686 return NULL;
43ecb9c3 687
3fd70262 688 if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
df05f2ce 689 CRYPTO_RWLOCK *lock = to->lock;
3fd70262 690
2db6bf6f 691 memcpy(to, md, sizeof(*to));
df05f2ce
MC
692 to->lock = lock;
693 }
2db6bf6f
RL
694 return to;
695}
3653d0c2 696
2db6bf6f
RL
697void EVP_MD_meth_free(EVP_MD *md)
698{
3fd70262 699 EVP_MD_free(md);
2db6bf6f
RL
700}
701int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
702{
8bbc7f22
DB
703 if (md->block_size != 0)
704 return 0;
705
2db6bf6f
RL
706 md->block_size = blocksize;
707 return 1;
708}
709int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
710{
8bbc7f22
DB
711 if (md->md_size != 0)
712 return 0;
713
2db6bf6f
RL
714 md->md_size = resultsize;
715 return 1;
716}
717int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
718{
8bbc7f22
DB
719 if (md->ctx_size != 0)
720 return 0;
721
2db6bf6f
RL
722 md->ctx_size = datasize;
723 return 1;
724}
725int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
726{
8bbc7f22
DB
727 if (md->flags != 0)
728 return 0;
729
2db6bf6f
RL
730 md->flags = flags;
731 return 1;
732}
733int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
734{
8bbc7f22
DB
735 if (md->init != NULL)
736 return 0;
737
2db6bf6f
RL
738 md->init = init;
739 return 1;
740}
741int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
742 const void *data,
743 size_t count))
744{
8bbc7f22
DB
745 if (md->update != NULL)
746 return 0;
747
2db6bf6f
RL
748 md->update = update;
749 return 1;
750}
751int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
752 unsigned char *md))
753{
8bbc7f22
DB
754 if (md->final != NULL)
755 return 0;
756
2db6bf6f
RL
757 md->final = final;
758 return 1;
759}
760int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
761 const EVP_MD_CTX *from))
762{
8bbc7f22
DB
763 if (md->copy != NULL)
764 return 0;
765
2db6bf6f
RL
766 md->copy = copy;
767 return 1;
768}
769int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
770{
8bbc7f22
DB
771 if (md->cleanup != NULL)
772 return 0;
773
2db6bf6f
RL
774 md->cleanup = cleanup;
775 return 1;
776}
777int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
778 int p1, void *p2))
779{
8bbc7f22
DB
780 if (md->md_ctrl != NULL)
781 return 0;
782
2db6bf6f
RL
783 md->md_ctrl = ctrl;
784 return 1;
785}
786
787int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
788{
789 return md->block_size;
790}
791int EVP_MD_meth_get_result_size(const EVP_MD *md)
792{
793 return md->md_size;
794}
795int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
796{
797 return md->ctx_size;
798}
799unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
800{
8bfa99f0 801 return md->flags;
2db6bf6f
RL
802}
803int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
804{
805 return md->init;
806}
807int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
808 const void *data,
809 size_t count)
810{
811 return md->update;
812}
813int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
814 unsigned char *md)
815{
816 return md->final;
817}
818int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
819 const EVP_MD_CTX *from)
820{
821 return md->copy;
822}
823int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
824{
825 return md->cleanup;
826}
827int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
828 int p1, void *p2)
829{
830 return md->md_ctrl;
831}
832
7806f3dd 833const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
0f113f3e 834{
b7c913c8 835 if (ctx == NULL)
0f113f3e 836 return NULL;
b7c913c8 837 return ctx->reqdigest;
0f113f3e 838}
7806f3dd 839
7638370c
RL
840EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
841{
842 return ctx->pctx;
843}
844
319e518a
MC
845#if !defined(FIPS_MODE)
846/* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
00902d94
PY
847void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
848{
81c79453
PY
849 /*
850 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
851 * we have to deal with the cleanup job here.
852 */
853 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
854 EVP_PKEY_CTX_free(ctx->pctx);
855
00902d94 856 ctx->pctx = pctx;
81c79453
PY
857
858 if (pctx != NULL) {
859 /* make sure pctx is not freed when destroying EVP_MD_CTX */
860 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
861 } else {
862 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
863 }
00902d94 864}
319e518a 865#endif /* !defined(FIPS_MODE) */
00902d94 866
7638370c
RL
867void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
868{
869 return ctx->md_data;
870}
871
872int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
873 const void *data, size_t count)
874{
875 return ctx->update;
876}
877
878void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
879 int (*update) (EVP_MD_CTX *ctx,
880 const void *data, size_t count))
881{
882 ctx->update = update;
883}
884
7806f3dd 885void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
886{
887 ctx->flags |= flags;
888}
7806f3dd
NL
889
890void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
891{
892 ctx->flags &= ~flags;
893}
7806f3dd
NL
894
895int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
896{
897 return (ctx->flags & flags);
898}
e92f9f45
DSH
899
900void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
901{
902 ctx->flags |= flags;
903}
e92f9f45
DSH
904
905void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
906{
907 ctx->flags &= ~flags;
908}
e92f9f45
DSH
909
910int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
911{
912 return (ctx->flags & flags);
913}
f842b6b2
RL
914
915int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
916 void *ctx, int cmd, const char *value)
917{
918 size_t len;
919
920 len = strlen(value);
921 if (len > INT_MAX)
922 return -1;
923 return cb(ctx, cmd, (void *)value, len);
924}
925
926int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
927 void *ctx, int cmd, const char *hex)
928{
929 unsigned char *bin;
930 long binlen;
931 int rv = -1;
932
933 bin = OPENSSL_hexstr2buf(hex, &binlen);
934 if (bin == NULL)
935 return 0;
936 if (binlen <= INT_MAX)
937 rv = cb(ctx, cmd, bin, binlen);
938 OPENSSL_free(bin);
939 return rv;
940}