2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/evp.h>
11 #include <openssl/core_names.h>
12 #include <openssl/err.h>
13 #include "prov/provider_util.h"
15 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
17 EVP_CIPHER_free(pc->alloc_cipher);
18 pc->alloc_cipher = NULL;
23 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
25 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
27 dst->engine = src->engine;
28 dst->cipher = src->cipher;
29 dst->alloc_cipher = src->alloc_cipher;
33 static int load_common(const OSSL_PARAM params[], const char **propquery,
39 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
41 if (p->data_type != OSSL_PARAM_UTF8_STRING)
47 /* TODO legacy stuff, to be removed */
48 /* Inside the FIPS module, we don't support legacy ciphers */
49 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
50 p = OSSL_PARAM_locate_const(params, "engine");
52 if (p->data_type != OSSL_PARAM_UTF8_STRING)
54 ENGINE_finish(*engine);
55 *engine = ENGINE_by_id(p->data);
63 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
64 const OSSL_PARAM params[],
68 const char *propquery;
70 if (!load_common(params, &propquery, &pc->engine))
73 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
76 if (p->data_type != OSSL_PARAM_UTF8_STRING)
79 EVP_CIPHER_free(pc->alloc_cipher);
81 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
82 /* TODO legacy stuff, to be removed */
83 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
84 if (pc->cipher == NULL)
85 pc->cipher = EVP_get_cipherbyname(p->data);
87 if (pc->cipher != NULL)
90 ERR_clear_last_mark();
91 return pc->cipher != NULL;
94 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
99 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
104 void ossl_prov_digest_reset(PROV_DIGEST *pd)
106 EVP_MD_free(pd->alloc_md);
112 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
114 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
116 dst->engine = src->engine;
118 dst->alloc_md = src->alloc_md;
122 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
123 const OSSL_PARAM params[],
127 const char *propquery;
129 if (!load_common(params, &propquery, &pd->engine))
133 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
136 if (p->data_type != OSSL_PARAM_UTF8_STRING)
139 EVP_MD_free(pd->alloc_md);
141 pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
142 /* TODO legacy stuff, to be removed */
143 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
145 pd->md = EVP_get_digestbyname(p->data);
150 ERR_clear_last_mark();
151 return pd->md != NULL;
154 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
159 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
164 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
165 const OSSL_PARAM params[],
167 const char *ciphername,
172 OSSL_PARAM mac_params[5], *mp = mac_params;
173 const char *properties = NULL;
176 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
177 if (p->data_type != OSSL_PARAM_UTF8_STRING)
181 if ((p = OSSL_PARAM_locate_const(params,
182 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
183 if (p->data_type != OSSL_PARAM_UTF8_STRING)
185 properties = p->data;
188 /* If we got a new mac name, we make a new EVP_MAC_CTX */
189 if (macname != NULL) {
190 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
192 EVP_MAC_CTX_free(*macctx);
193 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
194 /* The context holds on to the MAC */
201 * If there is no MAC yet (and therefore, no MAC context), we ignore
202 * all other parameters.
207 if (mdname == NULL) {
208 if ((p = OSSL_PARAM_locate_const(params,
209 OSSL_ALG_PARAM_DIGEST)) != NULL) {
210 if (p->data_type != OSSL_PARAM_UTF8_STRING)
215 if (ciphername == NULL) {
216 if ((p = OSSL_PARAM_locate_const(params,
217 OSSL_ALG_PARAM_CIPHER)) != NULL) {
218 if (p->data_type != OSSL_PARAM_UTF8_STRING)
220 ciphername = p->data;
225 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
227 if (ciphername != NULL)
228 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
229 (char *)ciphername, 0);
230 if (properties != NULL)
231 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
232 (char *)properties, 0);
234 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
235 if ((p = OSSL_PARAM_locate_const(params, "engine")) != NULL) {
236 if (p->data_type != OSSL_PARAM_UTF8_STRING)
238 *mp++ = OSSL_PARAM_construct_utf8_string("engine",
239 p->data, p->data_size);
242 *mp = OSSL_PARAM_construct_end();
244 if (EVP_MAC_CTX_set_params(*macctx, mac_params))
247 EVP_MAC_CTX_free(*macctx);
252 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
257 if (out[0].algorithm_names == NULL) {
258 for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
259 if (in[i].capable == NULL || in[i].capable())
260 out[j++] = in[i].alg;
262 out[j++] = in[i].alg;