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 "prov/provider_util.h"
14 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
16 EVP_CIPHER_free(pc->alloc_cipher);
17 pc->alloc_cipher = NULL;
22 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
24 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
26 dst->engine = src->engine;
27 dst->cipher = src->cipher;
28 dst->alloc_cipher = src->alloc_cipher;
32 static int load_common(const OSSL_PARAM params[], const char **propquery,
38 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
40 if (p->data_type != OSSL_PARAM_UTF8_STRING)
46 /* TODO legacy stuff, to be removed */
47 /* Inside the FIPS module, we don't support legacy ciphers */
48 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
49 p = OSSL_PARAM_locate_const(params, "engine");
51 if (p->data_type != OSSL_PARAM_UTF8_STRING)
53 ENGINE_finish(*engine);
54 *engine = ENGINE_by_id(p->data);
62 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
63 const OSSL_PARAM params[],
67 const char *propquery;
69 if (!load_common(params, &propquery, &pc->engine))
72 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
75 if (p->data_type != OSSL_PARAM_UTF8_STRING)
78 EVP_CIPHER_free(pc->alloc_cipher);
79 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
80 /* TODO legacy stuff, to be removed */
81 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
82 if (pc->cipher == NULL)
83 pc->cipher = EVP_get_cipherbyname(p->data);
85 return pc->cipher != NULL;
88 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
93 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
98 void ossl_prov_digest_reset(PROV_DIGEST *pd)
100 EVP_MD_free(pd->alloc_md);
106 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
108 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
110 dst->engine = src->engine;
112 dst->alloc_md = src->alloc_md;
116 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
117 const OSSL_PARAM params[],
121 const char *propquery;
123 if (!load_common(params, &propquery, &pd->engine))
127 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
130 if (p->data_type != OSSL_PARAM_UTF8_STRING)
133 EVP_MD_free(pd->alloc_md);
134 pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
135 /* TODO legacy stuff, to be removed */
136 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
138 pd->md = EVP_get_digestbyname(p->data);
140 return pd->md != NULL;
143 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
148 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
153 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
154 const OSSL_PARAM params[],
156 const char *ciphername,
161 OSSL_PARAM mac_params[5], *mp = mac_params;
162 const char *properties = NULL;
165 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
166 if (p->data_type != OSSL_PARAM_UTF8_STRING)
170 if ((p = OSSL_PARAM_locate_const(params,
171 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
172 if (p->data_type != OSSL_PARAM_UTF8_STRING)
174 properties = p->data;
177 /* If we got a new mac name, we make a new EVP_MAC_CTX */
178 if (macname != NULL) {
179 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
181 EVP_MAC_CTX_free(*macctx);
182 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
183 /* The context holds on to the MAC */
190 * If there is no MAC yet (and therefore, no MAC context), we ignore
191 * all other parameters.
196 if (mdname == NULL) {
197 if ((p = OSSL_PARAM_locate_const(params,
198 OSSL_ALG_PARAM_DIGEST)) != NULL) {
199 if (p->data_type != OSSL_PARAM_UTF8_STRING)
204 if (ciphername == NULL) {
205 if ((p = OSSL_PARAM_locate_const(params,
206 OSSL_ALG_PARAM_CIPHER)) != NULL) {
207 if (p->data_type != OSSL_PARAM_UTF8_STRING)
209 ciphername = p->data;
214 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
216 if (ciphername != NULL)
217 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
218 (char *)ciphername, 0);
219 if (properties != NULL)
220 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
221 (char *)properties, 0);
223 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
224 if ((p = OSSL_PARAM_locate_const(params, "engine")) != NULL) {
225 if (p->data_type != OSSL_PARAM_UTF8_STRING)
227 *mp++ = OSSL_PARAM_construct_utf8_string("engine",
228 p->data, p->data_size);
231 *mp = OSSL_PARAM_construct_end();
233 if (EVP_MAC_CTX_set_params(*macctx, mac_params))
236 EVP_MAC_CTX_free(*macctx);