xref: /freebsd/crypto/openssl/crypto/evp/evp_lib.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-2026 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 /*
11  * EVP _meth_ APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include "internal/cryptlib.h"
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/objects.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include <openssl/rsa.h>
25 #include <openssl/dh.h>
26 #include <openssl/ec.h>
27 #include "crypto/evp.h"
28 #include "crypto/cryptlib.h"
29 #include "internal/provider.h"
30 #include "evp_local.h"
31 
32 #if !defined(FIPS_MODULE)
33 #include "crypto/asn1.h"
34 
EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX * c,ASN1_TYPE * type)35 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
36 {
37     return evp_cipher_param_to_asn1_ex(c, type, NULL);
38 }
39 
EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX * c,ASN1_TYPE * type)40 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
41 {
42     return evp_cipher_asn1_to_param_ex(c, type, NULL);
43 }
44 
EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX * ctx,ASN1_TYPE * type)45 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
46 {
47     int i = 0;
48     unsigned int l;
49 
50     if (type != NULL) {
51         unsigned char iv[EVP_MAX_IV_LENGTH];
52 
53         l = EVP_CIPHER_CTX_get_iv_length(ctx);
54         if (!ossl_assert(l <= sizeof(iv)))
55             return -1;
56         i = ASN1_TYPE_get_octetstring(type, iv, l);
57         if (i != (int)l)
58             return -1;
59 
60         if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
61             return -1;
62     }
63     return i;
64 }
65 
EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX * c,ASN1_TYPE * type)66 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
67 {
68     int i = 0;
69     unsigned int j;
70     unsigned char *oiv = NULL;
71 
72     if (type != NULL) {
73         oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
74         j = EVP_CIPHER_CTX_get_iv_length(c);
75         OPENSSL_assert(j <= sizeof(c->iv));
76         i = ASN1_TYPE_set_octetstring(type, oiv, j);
77     }
78     return i;
79 }
80 
evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX * c,ASN1_TYPE * type,evp_cipher_aead_asn1_params * asn1_params)81 int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
82     evp_cipher_aead_asn1_params *asn1_params)
83 {
84     int ret = -1; /* Assume the worst */
85     const EVP_CIPHER *cipher;
86 
87     if (c == NULL || c->cipher == NULL)
88         goto err;
89 
90     cipher = c->cipher;
91     /*
92      * For legacy implementations, we detect custom AlgorithmIdentifier
93      * parameter handling by checking if the function pointer
94      * cipher->set_asn1_parameters is set.  We know that this pointer
95      * is NULL for provided implementations.
96      *
97      * Otherwise, for any implementation, we check the flag
98      * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
99      * default AI parameter extraction.
100      *
101      * Otherwise, for provided implementations, we convert |type| to
102      * a DER encoded blob and pass to the implementation in OSSL_PARAM
103      * form.
104      *
105      * If none of the above applies, this operation is unsupported.
106      */
107     if (cipher->set_asn1_parameters != NULL) {
108         ret = cipher->set_asn1_parameters(c, type);
109     } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
110         switch (EVP_CIPHER_get_mode(cipher)) {
111         case EVP_CIPH_WRAP_MODE:
112             if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
113                 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
114             ret = 1;
115             break;
116 
117         case EVP_CIPH_GCM_MODE:
118             ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
119             break;
120 
121         case EVP_CIPH_CCM_MODE:
122         case EVP_CIPH_XTS_MODE:
123         case EVP_CIPH_OCB_MODE:
124             ret = -2;
125             break;
126 
127         default:
128             ret = EVP_CIPHER_set_asn1_iv(c, type);
129         }
130     } else if (cipher->prov != NULL) {
131         /* We cheat, there's no need for an object ID for this use */
132         X509_ALGOR alg;
133 
134         alg.algorithm = NULL;
135         alg.parameter = type;
136 
137         ret = EVP_CIPHER_CTX_get_algor_params(c, &alg);
138     } else {
139         ret = -2;
140     }
141 
142 err:
143     if (ret == -2)
144         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
145     else if (ret <= 0)
146         ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
147     if (ret < -1)
148         ret = -1;
149     return ret;
150 }
151 
evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX * c,ASN1_TYPE * type,evp_cipher_aead_asn1_params * asn1_params)152 int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
153     evp_cipher_aead_asn1_params *asn1_params)
154 {
155     int ret = -1; /* Assume the worst */
156     const EVP_CIPHER *cipher;
157 
158     if (c == NULL || c->cipher == NULL)
159         goto err;
160 
161     cipher = c->cipher;
162     /*
163      * For legacy implementations, we detect custom AlgorithmIdentifier
164      * parameter handling by checking if there the function pointer
165      * cipher->get_asn1_parameters is set.  We know that this pointer
166      * is NULL for provided implementations.
167      *
168      * Otherwise, for any implementation, we check the flag
169      * EVP_CIPH_FLAG_CUSTOM_ASN1.  If it isn't set, we apply
170      * default AI parameter creation.
171      *
172      * Otherwise, for provided implementations, we get the AI parameter
173      * in DER encoded form from the implementation by requesting the
174      * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
175      *
176      * If none of the above applies, this operation is unsupported.
177      */
178     if (cipher->get_asn1_parameters != NULL) {
179         ret = cipher->get_asn1_parameters(c, type);
180     } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
181         switch (EVP_CIPHER_get_mode(cipher)) {
182         case EVP_CIPH_WRAP_MODE:
183             ret = 1;
184             break;
185 
186         case EVP_CIPH_GCM_MODE:
187             ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
188             break;
189 
190         case EVP_CIPH_CCM_MODE:
191         case EVP_CIPH_XTS_MODE:
192         case EVP_CIPH_OCB_MODE:
193             ret = -2;
194             break;
195 
196         default:
197             ret = EVP_CIPHER_get_asn1_iv(c, type) >= 0 ? 1 : -1;
198         }
199     } else if (cipher->prov != NULL) {
200         /* We cheat, there's no need for an object ID for this use */
201         X509_ALGOR alg;
202 
203         alg.algorithm = NULL;
204         alg.parameter = type;
205 
206         ret = EVP_CIPHER_CTX_set_algor_params(c, &alg);
207     } else {
208         ret = -2;
209     }
210 
211 err:
212     if (ret == -2)
213         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
214     else if (ret <= 0)
215         ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
216     if (ret < -1)
217         ret = -1;
218     return ret;
219 }
220 
evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX * c,ASN1_TYPE * type,evp_cipher_aead_asn1_params * asn1_params)221 int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
222     evp_cipher_aead_asn1_params *asn1_params)
223 {
224     int i = 0;
225     long tl;
226     unsigned char iv[EVP_MAX_IV_LENGTH];
227 
228     if (type == NULL || asn1_params == NULL)
229         return 0;
230 
231     i = ossl_asn1_type_get_octetstring_int(type, &tl, iv, EVP_MAX_IV_LENGTH);
232     if (i <= 0 || i > EVP_MAX_IV_LENGTH)
233         return -1;
234 
235     memcpy(asn1_params->iv, iv, i);
236     asn1_params->iv_len = i;
237 
238     return i;
239 }
240 
evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX * c,ASN1_TYPE * type,evp_cipher_aead_asn1_params * asn1_params)241 int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
242     evp_cipher_aead_asn1_params *asn1_params)
243 {
244     if (type == NULL || asn1_params == NULL)
245         return 0;
246 
247     return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
248         asn1_params->iv,
249         asn1_params->iv_len);
250 }
251 #endif /* !defined(FIPS_MODULE) */
252 
253 /* Convert the various cipher NIDs and dummies to a proper OID NID */
EVP_CIPHER_get_type(const EVP_CIPHER * cipher)254 int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
255 {
256     int nid;
257     nid = EVP_CIPHER_get_nid(cipher);
258 
259     switch (nid) {
260 
261     case NID_rc2_cbc:
262     case NID_rc2_64_cbc:
263     case NID_rc2_40_cbc:
264 
265         return NID_rc2_cbc;
266 
267     case NID_rc4:
268     case NID_rc4_40:
269 
270         return NID_rc4;
271 
272     case NID_aes_128_cfb128:
273     case NID_aes_128_cfb8:
274     case NID_aes_128_cfb1:
275 
276         return NID_aes_128_cfb128;
277 
278     case NID_aes_192_cfb128:
279     case NID_aes_192_cfb8:
280     case NID_aes_192_cfb1:
281 
282         return NID_aes_192_cfb128;
283 
284     case NID_aes_256_cfb128:
285     case NID_aes_256_cfb8:
286     case NID_aes_256_cfb1:
287 
288         return NID_aes_256_cfb128;
289 
290     case NID_des_cfb64:
291     case NID_des_cfb8:
292     case NID_des_cfb1:
293 
294         return NID_des_cfb64;
295 
296     case NID_des_ede3_cfb64:
297     case NID_des_ede3_cfb8:
298     case NID_des_ede3_cfb1:
299 
300         return NID_des_cfb64;
301 
302     default:
303 #ifdef FIPS_MODULE
304         return NID_undef;
305 #else
306     {
307         /* Check it has an OID and it is valid */
308         ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
309 
310         if (OBJ_get0_data(otmp) == NULL)
311             nid = NID_undef;
312         ASN1_OBJECT_free(otmp);
313         return nid;
314     }
315 #endif
316     }
317 }
318 
evp_cipher_cache_constants(EVP_CIPHER * cipher)319 int evp_cipher_cache_constants(EVP_CIPHER *cipher)
320 {
321     int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0;
322     size_t ivlen = 0;
323     size_t blksz = 0;
324     size_t keylen = 0;
325     unsigned int mode = 0;
326     OSSL_PARAM params[10];
327 
328     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
329     params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
330     params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
331     params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
332     params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
333     params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
334         &custom_iv);
335     params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
336     params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
337         &multiblock);
338     params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY,
339         &randkey);
340     params[9] = OSSL_PARAM_construct_end();
341     ok = evp_do_ciph_getparams(cipher, params) > 0;
342     if (ok) {
343         cipher->block_size = blksz;
344         cipher->iv_len = ivlen;
345         cipher->key_len = keylen;
346         cipher->flags = mode;
347         if (aead)
348             cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
349         if (custom_iv)
350             cipher->flags |= EVP_CIPH_CUSTOM_IV;
351         if (cts)
352             cipher->flags |= EVP_CIPH_FLAG_CTS;
353         if (multiblock)
354             cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
355         if (cipher->ccipher != NULL)
356             cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
357         if (randkey)
358             cipher->flags |= EVP_CIPH_RAND_KEY;
359         if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
360                 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
361             cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
362     }
363     return ok;
364 }
365 
EVP_CIPHER_get_block_size(const EVP_CIPHER * cipher)366 int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
367 {
368     return (cipher == NULL) ? 0 : cipher->block_size;
369 }
370 
EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX * ctx)371 int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
372 {
373     return (ctx == NULL) ? 0 : EVP_CIPHER_get_block_size(ctx->cipher);
374 }
375 
EVP_CIPHER_impl_ctx_size(const EVP_CIPHER * e)376 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
377 {
378     return e->ctx_size;
379 }
380 
EVP_Cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)381 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
382     const unsigned char *in, unsigned int inl)
383 {
384     if (ctx == NULL || ctx->cipher == NULL)
385         return 0;
386 
387     if (ctx->cipher->prov != NULL) {
388         /*
389          * If the provided implementation has a ccipher function, we use it,
390          * and translate its return value like this: 0 => -1, 1 => outlen
391          *
392          * Otherwise, we call the cupdate function if in != NULL, or cfinal
393          * if in == NULL.  Regardless of which, we return what we got.
394          */
395         int ret = -1;
396         size_t outl = 0;
397         size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
398 
399         if (blocksize == 0)
400             return 0;
401 
402         if (ctx->cipher->ccipher != NULL)
403             ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
404                       inl + (blocksize == 1 ? 0 : blocksize),
405                       in, (size_t)inl)
406                 ? (int)outl
407                 : -1;
408         else if (in != NULL)
409             ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
410                 inl + (blocksize == 1 ? 0 : blocksize),
411                 in, (size_t)inl);
412         else
413             ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
414                 blocksize == 1 ? 0 : blocksize);
415 
416         return ret;
417     }
418 
419     return ctx->cipher->do_cipher(ctx, out, in, inl);
420 }
421 
422 #ifndef OPENSSL_NO_DEPRECATED_3_0
EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX * ctx)423 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
424 {
425     if (ctx == NULL)
426         return NULL;
427     return ctx->cipher;
428 }
429 #endif
430 
EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX * ctx)431 const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
432 {
433     if (ctx == NULL)
434         return NULL;
435     return ctx->cipher;
436 }
437 
EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX * ctx)438 EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
439 {
440     EVP_CIPHER *cipher;
441 
442     if (ctx == NULL || ctx->cipher == NULL)
443         return NULL;
444     cipher = (EVP_CIPHER *)ctx->cipher;
445     if (!EVP_CIPHER_up_ref(cipher))
446         return NULL;
447     return cipher;
448 }
449 
EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX * ctx)450 int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
451 {
452     return ctx->encrypt;
453 }
454 
EVP_CIPHER_get_flags(const EVP_CIPHER * cipher)455 unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
456 {
457     return cipher == NULL ? 0 : cipher->flags;
458 }
459 
EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX * ctx)460 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
461 {
462     return ctx->app_data;
463 }
464 
EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX * ctx,void * data)465 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
466 {
467     ctx->app_data = data;
468 }
469 
EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX * ctx)470 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
471 {
472     return ctx->cipher_data;
473 }
474 
EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX * ctx,void * cipher_data)475 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
476 {
477     void *old_cipher_data;
478 
479     old_cipher_data = ctx->cipher_data;
480     ctx->cipher_data = cipher_data;
481 
482     return old_cipher_data;
483 }
484 
EVP_CIPHER_get_iv_length(const EVP_CIPHER * cipher)485 int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
486 {
487     return (cipher == NULL) ? 0 : cipher->iv_len;
488 }
489 
EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX * ctx)490 int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
491 {
492     if (ctx->cipher == NULL)
493         return 0;
494 
495     if (ctx->iv_len < 0) {
496         int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
497         size_t v = len;
498         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
499 
500         if (ctx->cipher->get_ctx_params != NULL) {
501             params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN,
502                 &v);
503             rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
504             if (rv > 0) {
505                 if (OSSL_PARAM_modified(params)
506                     && !OSSL_PARAM_get_int(params, &len))
507                     return -1;
508             } else if (rv != EVP_CTRL_RET_UNSUPPORTED) {
509                 return -1;
510             }
511         }
512         /* Code below to be removed when legacy support is dropped. */
513         else if ((EVP_CIPHER_get_flags(ctx->cipher)
514                      & EVP_CIPH_CUSTOM_IV_LENGTH)
515             != 0) {
516             rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
517                 0, &len);
518             if (rv <= 0)
519                 return -1;
520         }
521         /*-
522          * Casting away the const is annoying but required here.  We need to
523          * cache the result for performance reasons.
524          */
525         ((EVP_CIPHER_CTX *)ctx)->iv_len = len;
526     }
527     return ctx->iv_len;
528 }
529 
EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX * ctx)530 int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
531 {
532     int ret;
533     size_t v = 0;
534     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
535 
536     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
537     ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
538     return ret == 1 ? (int)v : 0;
539 }
540 
541 #ifndef OPENSSL_NO_DEPRECATED_3_0
EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX * ctx)542 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
543 {
544     int ok;
545     const unsigned char *v = ctx->oiv;
546     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
547 
548     params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
549         (void **)&v, sizeof(ctx->oiv));
550     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
551 
552     return ok != 0 ? v : NULL;
553 }
554 
555 /*
556  * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
557  */
EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX * ctx)558 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
559 {
560     int ok;
561     const unsigned char *v = ctx->iv;
562     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
563 
564     params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
565         (void **)&v, sizeof(ctx->iv));
566     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
567 
568     return ok != 0 ? v : NULL;
569 }
570 
EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX * ctx)571 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
572 {
573     int ok;
574     unsigned char *v = ctx->iv;
575     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
576 
577     params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
578         (void **)&v, sizeof(ctx->iv));
579     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
580 
581     return ok != 0 ? v : NULL;
582 }
583 #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
584 
EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX * ctx,void * buf,size_t len)585 int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
586 {
587     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
588 
589     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
590     return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
591 }
592 
EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX * ctx,void * buf,size_t len)593 int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
594 {
595     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
596 
597     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
598     return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params) > 0;
599 }
600 
EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX * ctx)601 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
602 {
603     return ctx->buf;
604 }
605 
EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX * ctx)606 int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
607 {
608     int ok;
609     unsigned int v = (unsigned int)ctx->num;
610     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
611 
612     params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
613     ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
614 
615     return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
616 }
617 
EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX * ctx,int num)618 int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
619 {
620     int ok;
621     unsigned int n = (unsigned int)num;
622     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
623 
624     params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
625     ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
626 
627     if (ok != 0)
628         ctx->num = (int)n;
629     return ok != 0;
630 }
631 
EVP_CIPHER_get_key_length(const EVP_CIPHER * cipher)632 int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
633 {
634     return cipher->key_len;
635 }
636 
EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX * ctx)637 int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
638 {
639     if (ctx->cipher == NULL)
640         return 0;
641 
642     if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) {
643         int ok;
644         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
645         size_t len;
646 
647         params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
648         ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
649         if (ok <= 0)
650             return EVP_CTRL_RET_UNSUPPORTED;
651 
652         /*-
653          * The if branch should never be taken since EVP_MAX_KEY_LENGTH is
654          * less than INT_MAX but best to be safe.
655          *
656          * Casting away the const is annoying but required here.  We need to
657          * cache the result for performance reasons.
658          */
659         if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len))
660             return -1;
661         ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len;
662     }
663     return ctx->key_len;
664 }
665 
EVP_CIPHER_get_nid(const EVP_CIPHER * cipher)666 int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
667 {
668     return (cipher == NULL) ? NID_undef : cipher->nid;
669 }
670 
EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX * ctx)671 int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
672 {
673     return EVP_CIPHER_get_nid(ctx->cipher);
674 }
675 
EVP_CIPHER_is_a(const EVP_CIPHER * cipher,const char * name)676 int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
677 {
678     if (cipher == NULL)
679         return 0;
680     if (cipher->prov != NULL)
681         return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
682     return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
683 }
684 
evp_cipher_get_number(const EVP_CIPHER * cipher)685 int evp_cipher_get_number(const EVP_CIPHER *cipher)
686 {
687     return cipher->name_id;
688 }
689 
EVP_CIPHER_get0_name(const EVP_CIPHER * cipher)690 const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
691 {
692     if (cipher->type_name != NULL)
693         return cipher->type_name;
694 #ifndef FIPS_MODULE
695     return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
696 #else
697     return NULL;
698 #endif
699 }
700 
EVP_CIPHER_get0_description(const EVP_CIPHER * cipher)701 const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
702 {
703     if (cipher->description != NULL)
704         return cipher->description;
705 #ifndef FIPS_MODULE
706     return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
707 #else
708     return NULL;
709 #endif
710 }
711 
EVP_CIPHER_names_do_all(const EVP_CIPHER * cipher,void (* fn)(const char * name,void * data),void * data)712 int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
713     void (*fn)(const char *name, void *data),
714     void *data)
715 {
716     if (cipher->prov != NULL)
717         return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
718 
719     return 1;
720 }
721 
EVP_CIPHER_get0_provider(const EVP_CIPHER * cipher)722 const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
723 {
724     return cipher->prov;
725 }
726 
EVP_CIPHER_get_mode(const EVP_CIPHER * cipher)727 int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
728 {
729     return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
730 }
731 
EVP_MD_is_a(const EVP_MD * md,const char * name)732 int EVP_MD_is_a(const EVP_MD *md, const char *name)
733 {
734     if (md == NULL)
735         return 0;
736     if (md->prov != NULL)
737         return evp_is_a(md->prov, md->name_id, NULL, name);
738     return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
739 }
740 
evp_md_get_number(const EVP_MD * md)741 int evp_md_get_number(const EVP_MD *md)
742 {
743     return md->name_id;
744 }
745 
EVP_MD_get0_description(const EVP_MD * md)746 const char *EVP_MD_get0_description(const EVP_MD *md)
747 {
748     if (md->description != NULL)
749         return md->description;
750 #ifndef FIPS_MODULE
751     return OBJ_nid2ln(EVP_MD_nid(md));
752 #else
753     return NULL;
754 #endif
755 }
756 
EVP_MD_get0_name(const EVP_MD * md)757 const char *EVP_MD_get0_name(const EVP_MD *md)
758 {
759     if (md == NULL)
760         return NULL;
761     if (md->type_name != NULL)
762         return md->type_name;
763 #ifndef FIPS_MODULE
764     return OBJ_nid2sn(EVP_MD_nid(md));
765 #else
766     return NULL;
767 #endif
768 }
769 
EVP_MD_names_do_all(const EVP_MD * md,void (* fn)(const char * name,void * data),void * data)770 int EVP_MD_names_do_all(const EVP_MD *md,
771     void (*fn)(const char *name, void *data),
772     void *data)
773 {
774     if (md->prov != NULL)
775         return evp_names_do_all(md->prov, md->name_id, fn, data);
776 
777     return 1;
778 }
779 
EVP_MD_get0_provider(const EVP_MD * md)780 const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
781 {
782     return md->prov;
783 }
784 
EVP_MD_get_type(const EVP_MD * md)785 int EVP_MD_get_type(const EVP_MD *md)
786 {
787     return md->type;
788 }
789 
EVP_MD_get_pkey_type(const EVP_MD * md)790 int EVP_MD_get_pkey_type(const EVP_MD *md)
791 {
792     return md->pkey_type;
793 }
794 
EVP_MD_get_block_size(const EVP_MD * md)795 int EVP_MD_get_block_size(const EVP_MD *md)
796 {
797     if (md == NULL) {
798         ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
799         return -1;
800     }
801     return md->block_size;
802 }
803 
EVP_MD_get_size(const EVP_MD * md)804 int EVP_MD_get_size(const EVP_MD *md)
805 {
806     if (md == NULL) {
807         ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
808         return -1;
809     }
810     return md->md_size;
811 }
812 
EVP_MD_xof(const EVP_MD * md)813 int EVP_MD_xof(const EVP_MD *md)
814 {
815     return md != NULL && ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0);
816 }
817 
EVP_MD_get_flags(const EVP_MD * md)818 unsigned long EVP_MD_get_flags(const EVP_MD *md)
819 {
820     return md->flags;
821 }
822 
EVP_MD_meth_new(int md_type,int pkey_type)823 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
824 {
825     EVP_MD *md = evp_md_new();
826 
827     if (md != NULL) {
828         md->type = md_type;
829         md->pkey_type = pkey_type;
830         md->origin = EVP_ORIG_METH;
831     }
832     return md;
833 }
834 
EVP_MD_meth_dup(const EVP_MD * md)835 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
836 {
837     EVP_MD *to = NULL;
838 
839     /*
840      * Non-legacy EVP_MDs can't be duplicated like this.
841      * Use EVP_MD_up_ref() instead.
842      */
843     if (md->prov != NULL)
844         return NULL;
845 
846     if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
847         CRYPTO_REF_COUNT refcnt = to->refcnt;
848 
849         memcpy(to, md, sizeof(*to));
850         to->refcnt = refcnt;
851         to->origin = EVP_ORIG_METH;
852     }
853     return to;
854 }
855 
evp_md_free_int(EVP_MD * md)856 void evp_md_free_int(EVP_MD *md)
857 {
858     OPENSSL_free(md->type_name);
859     ossl_provider_free(md->prov);
860     CRYPTO_FREE_REF(&md->refcnt);
861     OPENSSL_free(md);
862 }
863 
EVP_MD_meth_free(EVP_MD * md)864 void EVP_MD_meth_free(EVP_MD *md)
865 {
866     if (md == NULL || md->origin != EVP_ORIG_METH)
867         return;
868 
869     evp_md_free_int(md);
870 }
871 
EVP_MD_meth_set_input_blocksize(EVP_MD * md,int blocksize)872 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
873 {
874     if (md->block_size != 0)
875         return 0;
876 
877     md->block_size = blocksize;
878     return 1;
879 }
EVP_MD_meth_set_result_size(EVP_MD * md,int resultsize)880 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
881 {
882     if (md->md_size != 0)
883         return 0;
884 
885     md->md_size = resultsize;
886     return 1;
887 }
EVP_MD_meth_set_app_datasize(EVP_MD * md,int datasize)888 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
889 {
890     if (md->ctx_size != 0)
891         return 0;
892 
893     md->ctx_size = datasize;
894     return 1;
895 }
EVP_MD_meth_set_flags(EVP_MD * md,unsigned long flags)896 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
897 {
898     if (md->flags != 0)
899         return 0;
900 
901     md->flags = flags;
902     return 1;
903 }
EVP_MD_meth_set_init(EVP_MD * md,int (* init)(EVP_MD_CTX * ctx))904 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
905 {
906     if (md->init != NULL)
907         return 0;
908 
909     md->init = init;
910     return 1;
911 }
EVP_MD_meth_set_update(EVP_MD * md,int (* update)(EVP_MD_CTX * ctx,const void * data,size_t count))912 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count))
913 {
914     if (md->update != NULL)
915         return 0;
916 
917     md->update = update;
918     return 1;
919 }
EVP_MD_meth_set_final(EVP_MD * md,int (* final)(EVP_MD_CTX * ctx,unsigned char * md))920 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, unsigned char *md))
921 {
922     if (md->final != NULL)
923         return 0;
924 
925     md->final = final;
926     return 1;
927 }
EVP_MD_meth_set_copy(EVP_MD * md,int (* copy)(EVP_MD_CTX * to,const EVP_MD_CTX * from))928 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from))
929 {
930     if (md->copy != NULL)
931         return 0;
932 
933     md->copy = copy;
934     return 1;
935 }
EVP_MD_meth_set_cleanup(EVP_MD * md,int (* cleanup)(EVP_MD_CTX * ctx))936 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
937 {
938     if (md->cleanup != NULL)
939         return 0;
940 
941     md->cleanup = cleanup;
942     return 1;
943 }
EVP_MD_meth_set_ctrl(EVP_MD * md,int (* ctrl)(EVP_MD_CTX * ctx,int cmd,int p1,void * p2))944 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2))
945 {
946     if (md->md_ctrl != NULL)
947         return 0;
948 
949     md->md_ctrl = ctrl;
950     return 1;
951 }
952 
EVP_MD_meth_get_input_blocksize(const EVP_MD * md)953 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
954 {
955     return md->block_size;
956 }
EVP_MD_meth_get_result_size(const EVP_MD * md)957 int EVP_MD_meth_get_result_size(const EVP_MD *md)
958 {
959     return md->md_size;
960 }
EVP_MD_meth_get_app_datasize(const EVP_MD * md)961 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
962 {
963     return md->ctx_size;
964 }
EVP_MD_meth_get_flags(const EVP_MD * md)965 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
966 {
967     return md->flags;
968 }
EVP_MD_meth_get_init(const EVP_MD * md)969 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
970 {
971     return md->init;
972 }
EVP_MD_meth_get_update(const EVP_MD * md)973 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
974     const void *data,
975     size_t count)
976 {
977     return md->update;
978 }
EVP_MD_meth_get_final(const EVP_MD * md)979 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
980     unsigned char *md)
981 {
982     return md->final;
983 }
EVP_MD_meth_get_copy(const EVP_MD * md)984 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
985     const EVP_MD_CTX *from)
986 {
987     return md->copy;
988 }
EVP_MD_meth_get_cleanup(const EVP_MD * md)989 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
990 {
991     return md->cleanup;
992 }
EVP_MD_meth_get_ctrl(const EVP_MD * md)993 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
994     int p1, void *p2)
995 {
996     return md->md_ctrl;
997 }
998 
999 #ifndef OPENSSL_NO_DEPRECATED_3_0
EVP_MD_CTX_md(const EVP_MD_CTX * ctx)1000 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
1001 {
1002     if (ctx == NULL)
1003         return NULL;
1004     return ctx->reqdigest;
1005 }
1006 #endif
1007 
EVP_MD_CTX_get0_md(const EVP_MD_CTX * ctx)1008 const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
1009 {
1010     if (ctx == NULL)
1011         return NULL;
1012     return ctx->reqdigest;
1013 }
1014 
EVP_MD_CTX_get1_md(EVP_MD_CTX * ctx)1015 EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
1016 {
1017     EVP_MD *md;
1018 
1019     if (ctx == NULL)
1020         return NULL;
1021     md = (EVP_MD *)ctx->reqdigest;
1022     if (md == NULL || !EVP_MD_up_ref(md))
1023         return NULL;
1024     return md;
1025 }
1026 
EVP_MD_CTX_get_size_ex(const EVP_MD_CTX * ctx)1027 int EVP_MD_CTX_get_size_ex(const EVP_MD_CTX *ctx)
1028 {
1029     EVP_MD_CTX *c = (EVP_MD_CTX *)ctx;
1030     const OSSL_PARAM *gettables;
1031 
1032     gettables = EVP_MD_CTX_gettable_params(c);
1033     if (gettables != NULL
1034         && OSSL_PARAM_locate_const(gettables,
1035                OSSL_DIGEST_PARAM_SIZE)
1036             != NULL) {
1037         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1038         size_t sz = 0;
1039 
1040         /*
1041          * For XOF's EVP_MD_get_size() returns 0
1042          * So try to get the xoflen instead. This will return -1 if the
1043          * xof length has not been set.
1044          */
1045         params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &sz);
1046         if (EVP_MD_CTX_get_params(c, params) != 1
1047             || sz == SIZE_MAX
1048             || sz == 0)
1049             return -1;
1050         return sz;
1051     }
1052     /* Normal digests have a constant fixed size output */
1053     return EVP_MD_get_size(EVP_MD_CTX_get0_md(ctx));
1054 }
1055 
EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX * ctx)1056 EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
1057 {
1058     return ctx->pctx;
1059 }
1060 
1061 #if !defined(FIPS_MODULE)
EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX * ctx,EVP_PKEY_CTX * pctx)1062 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1063 {
1064     /*
1065      * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1066      * we have to deal with the cleanup job here.
1067      */
1068     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1069         EVP_PKEY_CTX_free(ctx->pctx);
1070 
1071     ctx->pctx = pctx;
1072 
1073     if (pctx != NULL) {
1074         /* make sure pctx is not freed when destroying EVP_MD_CTX */
1075         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1076     } else {
1077         EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1078     }
1079 }
1080 #endif /* !defined(FIPS_MODULE) */
1081 
EVP_MD_CTX_get0_md_data(const EVP_MD_CTX * ctx)1082 void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
1083 {
1084     return ctx->md_data;
1085 }
1086 
EVP_MD_CTX_update_fn(EVP_MD_CTX * ctx)1087 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1088     const void *data, size_t count)
1089 {
1090     return ctx->update;
1091 }
1092 
EVP_MD_CTX_set_update_fn(EVP_MD_CTX * ctx,int (* update)(EVP_MD_CTX * ctx,const void * data,size_t count))1093 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1094     int (*update)(EVP_MD_CTX *ctx,
1095         const void *data, size_t count))
1096 {
1097     ctx->update = update;
1098 }
1099 
EVP_MD_CTX_set_flags(EVP_MD_CTX * ctx,int flags)1100 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
1101 {
1102     ctx->flags |= flags;
1103 }
1104 
EVP_MD_CTX_clear_flags(EVP_MD_CTX * ctx,int flags)1105 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
1106 {
1107     ctx->flags &= ~flags;
1108 }
1109 
EVP_MD_CTX_test_flags(const EVP_MD_CTX * ctx,int flags)1110 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
1111 {
1112     return (ctx->flags & flags);
1113 }
1114 
evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX * ctx,unsigned int enable)1115 static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1116     unsigned int enable)
1117 {
1118     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1119 
1120     params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1121     return EVP_CIPHER_CTX_set_params(ctx, params);
1122 }
1123 
EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX * ctx,int flags)1124 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
1125 {
1126     int oldflags = ctx->flags;
1127 
1128     ctx->flags |= flags;
1129     if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1130         evp_cipher_ctx_enable_use_bits(ctx, 1);
1131 }
1132 
EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX * ctx,int flags)1133 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
1134 {
1135     int oldflags = ctx->flags;
1136 
1137     ctx->flags &= ~flags;
1138     if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1139         evp_cipher_ctx_enable_use_bits(ctx, 0);
1140 }
1141 
EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX * ctx,int flags)1142 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
1143 {
1144     return (ctx->flags & flags);
1145 }
1146 
1147 #if !defined(FIPS_MODULE)
1148 
EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX * ctx,const char * name)1149 int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1150 {
1151     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1152 
1153     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1154         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1155         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1156         return -2;
1157     }
1158 
1159     if (name == NULL)
1160         return -1;
1161 
1162     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1163         (char *)name, 0);
1164     return EVP_PKEY_CTX_set_params(ctx, params);
1165 }
1166 
EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX * ctx,char * name,size_t namelen)1167 int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1168 {
1169     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1170     OSSL_PARAM *p = params;
1171 
1172     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1173         /* There is no legacy support for this */
1174         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1175         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1176         return -2;
1177     }
1178 
1179     if (name == NULL)
1180         return -1;
1181 
1182     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1183         name, namelen);
1184     if (!EVP_PKEY_CTX_get_params(ctx, params))
1185         return -1;
1186     return 1;
1187 }
1188 #endif /* !FIPS_MODULE */
1189 
1190 /*
1191  * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1192  * while providing a generic way of generating a new asymmetric key pair
1193  * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1194  * The library context I<libctx> and property query I<propq>
1195  * are used when fetching algorithms from providers.
1196  * The I<params> specify algorithm-specific parameters
1197  * such as the RSA modulus size or the name of an EC curve.
1198  */
evp_pkey_keygen(OSSL_LIB_CTX * libctx,const char * name,const char * propq,const OSSL_PARAM * params)1199 static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
1200     const char *propq, const OSSL_PARAM *params)
1201 {
1202     EVP_PKEY *pkey = NULL;
1203     EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1204 
1205     if (ctx != NULL
1206         && EVP_PKEY_keygen_init(ctx) > 0
1207         && EVP_PKEY_CTX_set_params(ctx, params))
1208         (void)EVP_PKEY_generate(ctx, &pkey);
1209 
1210     EVP_PKEY_CTX_free(ctx);
1211     return pkey;
1212 }
1213 
EVP_PKEY_Q_keygen(OSSL_LIB_CTX * libctx,const char * propq,const char * type,...)1214 EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1215     const char *type, ...)
1216 {
1217     va_list args;
1218     size_t bits;
1219     char *name;
1220     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1221     EVP_PKEY *ret = NULL;
1222 
1223     va_start(args, type);
1224 
1225     if (OPENSSL_strcasecmp(type, "RSA") == 0) {
1226         bits = va_arg(args, size_t);
1227         params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1228     } else if (OPENSSL_strcasecmp(type, "EC") == 0) {
1229         name = va_arg(args, char *);
1230         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1231             name, 0);
1232     }
1233 
1234     ret = evp_pkey_keygen(libctx, type, propq, params);
1235 
1236     va_end(args);
1237     return ret;
1238 }
1239 
1240 #if !defined(FIPS_MODULE)
EVP_CIPHER_CTX_set_algor_params(EVP_CIPHER_CTX * ctx,const X509_ALGOR * alg)1241 int EVP_CIPHER_CTX_set_algor_params(EVP_CIPHER_CTX *ctx, const X509_ALGOR *alg)
1242 {
1243     int ret = -1; /* Assume the worst */
1244     unsigned char *der = NULL;
1245     int derl = -1;
1246 
1247     if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
1248         const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
1249         const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
1250         OSSL_PARAM params[3];
1251 
1252         /*
1253          * Passing the same data with both the old (deprecated) and the
1254          * new AlgID parameters OSSL_PARAM key.
1255          */
1256         params[0] = OSSL_PARAM_construct_octet_string(k_old, der, (size_t)derl);
1257         params[1] = OSSL_PARAM_construct_octet_string(k_new, der, (size_t)derl);
1258         params[2] = OSSL_PARAM_construct_end();
1259         ret = EVP_CIPHER_CTX_set_params(ctx, params);
1260     }
1261     OPENSSL_free(der);
1262     return ret;
1263 }
1264 
EVP_CIPHER_CTX_get_algor_params(EVP_CIPHER_CTX * ctx,X509_ALGOR * alg)1265 int EVP_CIPHER_CTX_get_algor_params(EVP_CIPHER_CTX *ctx, X509_ALGOR *alg)
1266 {
1267     int ret = -1; /* Assume the worst */
1268     unsigned char *der = NULL;
1269     size_t derl;
1270     ASN1_TYPE *type = NULL;
1271     int i = -1;
1272     const char *k_old = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD;
1273     const char *k_new = OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS;
1274     const char *derk;
1275     OSSL_PARAM params[3];
1276 
1277     /*
1278      * We make two passes, the first to get the appropriate buffer size,
1279      * and the second to get the actual value.
1280      * Also, using both the old (deprecated) and the new AlgID parameters
1281      * OSSL_PARAM key, and using whichever the provider responds to.
1282      * Should the provider respond on both, the new key takes priority.
1283      */
1284     params[0] = OSSL_PARAM_construct_octet_string(k_old, NULL, 0);
1285     params[1] = OSSL_PARAM_construct_octet_string(k_new, NULL, 0);
1286     params[2] = OSSL_PARAM_construct_end();
1287 
1288     if (!EVP_CIPHER_CTX_get_params(ctx, params))
1289         goto err;
1290 
1291     /* ... but, we should get a return size too! */
1292     if (OSSL_PARAM_modified(&params[0]) && params[0].return_size != 0)
1293         i = 0;
1294     if (OSSL_PARAM_modified(&params[1]) && params[1].return_size != 0)
1295         i = 1;
1296     if (i < 0)
1297         goto err;
1298 
1299     /*
1300      * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
1301      * below.  If it is NULL, the d2i_ASN1_TYPE() call will allocate new
1302      * space for it.  Either way, alg->parameter can be safely assigned
1303      * with type after the d2i_ASN1_TYPE() call, with the safety that it
1304      * will be ok.
1305      */
1306     type = alg->parameter;
1307 
1308     derk = params[i].key;
1309     derl = params[i].return_size;
1310     if ((der = OPENSSL_malloc(derl)) != NULL) {
1311         unsigned char *derp = der;
1312 
1313         params[i] = OSSL_PARAM_construct_octet_string(derk, der, derl);
1314         if (EVP_CIPHER_CTX_get_params(ctx, params)
1315             && OSSL_PARAM_modified(&params[i])
1316             && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
1317                    (int)derl)
1318                 != NULL) {
1319             /*
1320              * Don't free alg->parameter, see comment further up.
1321              * Worst case, alg->parameter gets assigned its own value.
1322              */
1323             alg->parameter = type;
1324             ret = 1;
1325         }
1326     }
1327 err:
1328     OPENSSL_free(der);
1329     return ret;
1330 }
1331 
EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX * ctx,X509_ALGOR ** alg)1332 int EVP_CIPHER_CTX_get_algor(EVP_CIPHER_CTX *ctx, X509_ALGOR **alg)
1333 {
1334     int ret = -1; /* Assume the worst */
1335     OSSL_PARAM params[2];
1336     size_t aid_len = 0;
1337     const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
1338 
1339     params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
1340     params[1] = OSSL_PARAM_construct_end();
1341 
1342     if (EVP_CIPHER_CTX_get_params(ctx, params) <= 0)
1343         goto err;
1344 
1345     if (OSSL_PARAM_modified(&params[0]))
1346         aid_len = params[0].return_size;
1347     if (aid_len == 0) {
1348         ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
1349         ret = -2;
1350         goto err;
1351     }
1352     if (alg != NULL) {
1353         unsigned char *aid = NULL;
1354         const unsigned char *pp = NULL;
1355 
1356         if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
1357             params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
1358             pp = aid;
1359             if (EVP_CIPHER_CTX_get_params(ctx, params)
1360                 && OSSL_PARAM_modified(&params[0])
1361                 && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
1362                 ret = 1;
1363         }
1364         OPENSSL_free(aid);
1365     }
1366 err:
1367     return ret;
1368 }
1369 
EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX * ctx,const X509_ALGOR * alg)1370 int EVP_PKEY_CTX_set_algor_params(EVP_PKEY_CTX *ctx, const X509_ALGOR *alg)
1371 {
1372     int ret = -1; /* Assume the worst */
1373     unsigned char *der = NULL;
1374     int derl = -1;
1375 
1376     if ((derl = i2d_ASN1_TYPE(alg->parameter, &der)) >= 0) {
1377         const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
1378         OSSL_PARAM params[2];
1379 
1380         /*
1381          * Passing the same data with both the old (deprecated) and the
1382          * new AlgID parameters OSSL_PARAM key.
1383          */
1384         params[0] = OSSL_PARAM_construct_octet_string(k, der, (size_t)derl);
1385         params[1] = OSSL_PARAM_construct_end();
1386         ret = EVP_PKEY_CTX_set_params(ctx, params);
1387     }
1388     OPENSSL_free(der);
1389     return ret;
1390 }
1391 
EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX * ctx,X509_ALGOR * alg)1392 int EVP_PKEY_CTX_get_algor_params(EVP_PKEY_CTX *ctx, X509_ALGOR *alg)
1393 {
1394     int ret = -1; /* Assume the worst */
1395     OSSL_PARAM params[2];
1396     unsigned char *der = NULL;
1397     size_t derl;
1398     ASN1_TYPE *type = NULL;
1399     const char *k = OSSL_PKEY_PARAM_ALGORITHM_ID_PARAMS;
1400 
1401     /*
1402      * We make two passes, the first to get the appropriate buffer size,
1403      * and the second to get the actual value.
1404      * Also, using both the old (deprecated) and the new AlgID parameters
1405      * OSSL_PARAM key, and using whichever the provider responds to.
1406      * Should the provider respond on both, the new key takes priority.
1407      */
1408     params[0] = OSSL_PARAM_construct_octet_string(k, NULL, 0);
1409     params[1] = OSSL_PARAM_construct_end();
1410 
1411     if (!EVP_PKEY_CTX_get_params(ctx, params))
1412         goto err;
1413 
1414     /*
1415      * If alg->parameter is non-NULL, it will be changed by d2i_ASN1_TYPE()
1416      * below.  If it is NULL, the d2i_ASN1_TYPE() call will allocate new
1417      * space for it.  Either way, alg->parameter can be safely assigned
1418      * with type after the d2i_ASN1_TYPE() call, with the safety that it
1419      * will be ok.
1420      */
1421     type = alg->parameter;
1422 
1423     derl = params[0].return_size;
1424     if (OSSL_PARAM_modified(&params[0])
1425         /* ... but, we should get a return size too! */
1426         && derl != 0
1427         && (der = OPENSSL_malloc(derl)) != NULL) {
1428         unsigned char *derp = der;
1429 
1430         params[0] = OSSL_PARAM_construct_octet_string(k, der, derl);
1431         if (EVP_PKEY_CTX_get_params(ctx, params)
1432             && OSSL_PARAM_modified(&params[0])
1433             && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
1434                    derl)
1435                 != NULL) {
1436             /*
1437              * Don't free alg->parameter, see comment further up.
1438              * Worst case, alg->parameter gets assigned its own value.
1439              */
1440             alg->parameter = type;
1441             ret = 1;
1442         }
1443     }
1444 err:
1445     OPENSSL_free(der);
1446     return ret;
1447 }
1448 
EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX * ctx,X509_ALGOR ** alg)1449 int EVP_PKEY_CTX_get_algor(EVP_PKEY_CTX *ctx, X509_ALGOR **alg)
1450 {
1451     int ret = -1; /* Assume the worst */
1452     OSSL_PARAM params[2];
1453     size_t aid_len = 0;
1454     const char *k_aid = OSSL_SIGNATURE_PARAM_ALGORITHM_ID;
1455 
1456     params[0] = OSSL_PARAM_construct_octet_string(k_aid, NULL, 0);
1457     params[1] = OSSL_PARAM_construct_end();
1458 
1459     if (EVP_PKEY_CTX_get_params(ctx, params) <= 0)
1460         goto err;
1461 
1462     if (OSSL_PARAM_modified(&params[0]))
1463         aid_len = params[0].return_size;
1464     if (aid_len == 0) {
1465         ERR_raise(ERR_LIB_EVP, EVP_R_GETTING_ALGORITHMIDENTIFIER_NOT_SUPPORTED);
1466         ret = -2;
1467         goto err;
1468     }
1469     if (alg != NULL) {
1470         unsigned char *aid = NULL;
1471         const unsigned char *pp = NULL;
1472 
1473         if ((aid = OPENSSL_malloc(aid_len)) != NULL) {
1474             params[0] = OSSL_PARAM_construct_octet_string(k_aid, aid, aid_len);
1475             pp = aid;
1476             if (EVP_PKEY_CTX_get_params(ctx, params)
1477                 && OSSL_PARAM_modified(&params[0])
1478                 && d2i_X509_ALGOR(alg, &pp, aid_len) != NULL)
1479                 ret = 1;
1480         }
1481         OPENSSL_free(aid);
1482     }
1483 err:
1484     return ret;
1485 }
1486 
1487 #endif /* !defined(FIPS_MODULE) */
1488