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 "internal/provider_util.h"
14 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
16 EVP_CIPHER_free(pc->alloc_cipher);
17 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;
30 OPENSSL_strlcpy(dst->name, src->name, sizeof(dst->name));
34 static int load_common(const OSSL_PARAM params[], const char **propquery,
40 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
42 if (p->data_type != OSSL_PARAM_UTF8_STRING)
48 /* TODO legacy stuff, to be removed */
49 /* Inside the FIPS module, we don't support legacy ciphers */
50 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
51 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
53 if (p->data_type != OSSL_PARAM_UTF8_STRING)
55 ENGINE_finish(*engine);
56 *engine = ENGINE_by_id(p->data);
64 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
65 const OSSL_PARAM params[],
69 const char *propquery;
71 if (!load_common(params, &propquery, &pc->engine))
74 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
77 if (p->data_type != OSSL_PARAM_UTF8_STRING)
80 EVP_CIPHER_free(pc->alloc_cipher);
81 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
82 OPENSSL_strlcpy(pc->name, p->data, sizeof(pc->name));
83 /* TODO legacy stuff, to be removed */
84 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
85 if (pc->cipher == NULL)
86 pc->cipher = EVP_get_cipherbyname(p->data);
88 return pc->cipher != NULL;
91 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
96 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
101 const char *ossl_prov_cipher_name(const PROV_CIPHER *pc)
106 void ossl_prov_digest_reset(PROV_DIGEST *pd)
108 EVP_MD_free(pd->alloc_md);
115 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
117 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
119 dst->engine = src->engine;
121 dst->alloc_md = src->alloc_md;
122 OPENSSL_strlcpy(dst->name, src->name, sizeof(dst->name));
126 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
127 const OSSL_PARAM params[],
131 const char *propquery;
133 if (!load_common(params, &propquery, &pd->engine))
137 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
140 if (p->data_type != OSSL_PARAM_UTF8_STRING)
143 EVP_MD_free(pd->alloc_md);
144 pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
145 OPENSSL_strlcpy(pd->name, p->data, sizeof(pd->name));
146 /* TODO legacy stuff, to be removed */
147 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
149 pd->md = EVP_get_digestbyname(p->data);
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 const char *ossl_prov_digest_name(const PROV_DIGEST *pd)
169 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
170 const OSSL_PARAM params[],
172 const char *ciphername,
177 OSSL_PARAM mac_params[5], *mp = mac_params;
178 const char *properties = NULL;
181 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
182 if (p->data_type != OSSL_PARAM_UTF8_STRING)
186 if ((p = OSSL_PARAM_locate_const(params,
187 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
188 if (p->data_type != OSSL_PARAM_UTF8_STRING)
190 properties = p->data;
193 /* If we got a new mac name, we make a new EVP_MAC_CTX */
194 if (macname != NULL) {
195 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
197 EVP_MAC_CTX_free(*macctx);
198 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
199 /* The context holds on to the MAC */
206 * If there is no MAC yet (and therefore, no MAC context), we ignore
207 * all other parameters.
212 if (mdname == NULL) {
213 if ((p = OSSL_PARAM_locate_const(params,
214 OSSL_ALG_PARAM_DIGEST)) != NULL) {
215 if (p->data_type != OSSL_PARAM_UTF8_STRING)
220 if (ciphername == NULL) {
221 if ((p = OSSL_PARAM_locate_const(params,
222 OSSL_ALG_PARAM_CIPHER)) != NULL) {
223 if (p->data_type != OSSL_PARAM_UTF8_STRING)
225 ciphername = p->data;
230 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
232 if (ciphername != NULL)
233 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
234 (char *)ciphername, 0);
235 if (properties != NULL)
236 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
237 (char *)properties, 0);
239 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
240 if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE)) != NULL) {
241 if (p->data_type != OSSL_PARAM_UTF8_STRING)
243 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
244 p->data, p->data_size);
247 *mp = OSSL_PARAM_construct_end();
249 if (EVP_MAC_CTX_set_params(*macctx, mac_params))
252 EVP_MAC_CTX_free(*macctx);