Fix Solaris aes_hw_t4 compile issue
[openssl.git] / providers / common / provider_util.c
1 /*
2  * Copyright 2019 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 #include <openssl/evp.h>
11 #include <openssl/core_names.h>
12 #include "internal/provider_util.h"
13
14 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
15 {
16     EVP_CIPHER_free(pc->alloc_cipher);
17     pc->alloc_cipher = NULL;
18     pc->cipher = NULL;
19     pc->engine = NULL;
20 }
21
22 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
23 {
24     if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
25         return 0;
26     dst->engine = src->engine;
27     dst->cipher = src->cipher;
28     dst->alloc_cipher = src->alloc_cipher;
29     return 1;
30 }
31
32 static int load_common(const OSSL_PARAM params[], const char **propquery,
33                        ENGINE **engine)
34 {
35     const OSSL_PARAM *p;
36
37     *propquery = NULL;
38     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
39     if (p != NULL) {
40         if (p->data_type != OSSL_PARAM_UTF8_STRING)
41             return 0;
42         *propquery = p->data;
43     }
44
45     *engine = NULL;
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, OSSL_ALG_PARAM_ENGINE);
50     if (p != NULL) {
51         if (p->data_type != OSSL_PARAM_UTF8_STRING)
52             return 0;
53         ENGINE_finish(*engine);
54         *engine = ENGINE_by_id(p->data);
55         if (*engine == NULL)
56             return 0;
57     }
58 #endif
59     return 1;
60 }
61
62 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
63                                       const OSSL_PARAM params[],
64                                       OPENSSL_CTX *ctx)
65 {
66     const OSSL_PARAM *p;
67     const char *propquery;
68
69     if (!load_common(params, &propquery, &pc->engine))
70         return 0;
71
72     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
73     if (p == NULL)
74         return 1;
75     if (p->data_type != OSSL_PARAM_UTF8_STRING)
76         return 0;
77
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);
84 #endif
85     return pc->cipher != NULL;
86 }
87
88 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
89 {
90     return pc->cipher;
91 }
92
93 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
94 {
95     return pc->engine;
96 }
97
98 void ossl_prov_digest_reset(PROV_DIGEST *pd)
99 {
100     EVP_MD_free(pd->alloc_md);
101     pd->alloc_md = NULL;
102     pd->md = NULL;
103     pd->engine = NULL;
104 }
105
106 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
107 {
108     if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
109         return 0;
110     dst->engine = src->engine;
111     dst->md = src->md;
112     dst->alloc_md = src->alloc_md;
113     return 1;
114 }
115
116 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
117                                       const OSSL_PARAM params[],
118                                       OPENSSL_CTX *ctx)
119 {
120     const OSSL_PARAM *p;
121     const char *propquery;
122
123     if (!load_common(params, &propquery, &pd->engine))
124         return 0;
125
126
127     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
128     if (p == NULL)
129         return 1;
130     if (p->data_type != OSSL_PARAM_UTF8_STRING)
131         return 0;
132
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 */
137     if (pd->md == NULL)
138         pd->md = EVP_get_digestbyname(p->data);
139 #endif
140     return pd->md != NULL;
141 }
142
143 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
144 {
145     return pd->md;
146 }
147
148 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
149 {
150     return pd->engine;
151 }
152