Rename ctx_{get,set}_params to {get,set}_ctx_params
[openssl.git] / providers / common / macs / cmac_prov.c
1 /*
2  * Copyright 2018 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/opensslconf.h>
11 #ifndef OPENSSL_NO_CMAC
12
13 # include <openssl/core_numbers.h>
14 # include <openssl/core_names.h>
15 # include <openssl/params.h>
16 # include <openssl/engine.h>
17 # include <openssl/evp.h>
18 # include <openssl/cmac.h>
19
20 # include "internal/provider_algs.h"
21 # include "internal/provider_ctx.h"
22
23 /*
24  * Forward declaration of everything implemented here.  This is not strictly
25  * necessary for the compiler, but provides an assurance that the signatures
26  * of the functions in the dispatch table are correct.
27  */
28 static OSSL_OP_mac_newctx_fn cmac_new;
29 static OSSL_OP_mac_dupctx_fn cmac_dup;
30 static OSSL_OP_mac_freectx_fn cmac_free;
31 static OSSL_OP_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
32 static OSSL_OP_mac_get_ctx_params_fn cmac_get_ctx_params;
33 static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params;
34 static OSSL_OP_mac_set_ctx_params_fn cmac_set_ctx_params;
35 static OSSL_OP_mac_init_fn cmac_init;
36 static OSSL_OP_mac_update_fn cmac_update;
37 static OSSL_OP_mac_final_fn cmac_final;
38
39 /* local CMAC data */
40
41 struct cmac_data_st {
42     void *provctx;
43     CMAC_CTX *ctx;
44
45     /*
46      * References to the underlying cipher implementation.  tmpcipher
47      * caches the cipher, always.  alloc_cipher only holds a reference
48      * to an explicitly fetched cipher.
49      * tmpcipher is cleared after a CMAC_Init call.
50      */
51     const EVP_CIPHER *tmpcipher; /* cached CMAC cipher */
52     EVP_CIPHER *alloc_cipher;    /* fetched CMAC cipher */
53
54     /*
55      * Conditions for legacy EVP_CIPHER uses.
56      * tmpengine is cleared after a CMAC_Init call.
57      */
58     ENGINE *tmpengine;           /* CMAC cipher engine (legacy) */
59 };
60
61 static void *cmac_new(void *provctx)
62 {
63     struct cmac_data_st *macctx;
64
65     if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
66         || (macctx->ctx = CMAC_CTX_new()) == NULL) {
67         OPENSSL_free(macctx);
68         macctx = NULL;
69     }
70     macctx->provctx = provctx;
71
72     return macctx;
73 }
74
75 static void cmac_free(void *vmacctx)
76 {
77     struct cmac_data_st *macctx = vmacctx;
78
79     if (macctx != NULL) {
80         CMAC_CTX_free(macctx->ctx);
81         EVP_CIPHER_meth_free(macctx->alloc_cipher);
82         OPENSSL_free(macctx);
83     }
84 }
85
86 static void *cmac_dup(void *vsrc)
87 {
88     struct cmac_data_st *src = vsrc;
89     struct cmac_data_st *dst = cmac_new(src->provctx);
90
91     if (!CMAC_CTX_copy(dst->ctx, src->ctx)) {
92         cmac_free(dst);
93         return NULL;
94     }
95
96     if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher)) {
97         cmac_free(dst);
98         return NULL;
99     }
100
101     dst->tmpengine = src->tmpengine;
102     dst->tmpcipher = src->tmpcipher;
103     dst->alloc_cipher = src->alloc_cipher;
104     return dst;
105 }
106
107 static size_t cmac_size(void *vmacctx)
108 {
109     struct cmac_data_st *macctx = vmacctx;
110
111     return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
112 }
113
114 static int cmac_init(void *vmacctx)
115 {
116     struct cmac_data_st *macctx = vmacctx;
117     int rv = CMAC_Init(macctx->ctx, NULL, 0, macctx->tmpcipher,
118                        (ENGINE *)macctx->tmpengine);
119
120     macctx->tmpcipher = NULL;
121     macctx->tmpengine = NULL;
122
123     return rv;
124 }
125
126 static int cmac_update(void *vmacctx, const unsigned char *data,
127                        size_t datalen)
128 {
129     struct cmac_data_st *macctx = vmacctx;
130
131     return CMAC_Update(macctx->ctx, data, datalen);
132 }
133
134 static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
135                       size_t outsize)
136 {
137     struct cmac_data_st *macctx = vmacctx;
138
139     return CMAC_Final(macctx->ctx, out, outl);
140 }
141
142 static const OSSL_PARAM known_gettable_ctx_params[] = {
143     OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL),
144     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */
145     OSSL_PARAM_END
146 };
147 static const OSSL_PARAM *cmac_gettable_ctx_params(void)
148 {
149     return known_gettable_ctx_params;
150 }
151
152 static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
153 {
154     OSSL_PARAM *p;
155
156     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL
157         || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
158         return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
159
160     return 1;
161 }
162
163 static const OSSL_PARAM known_settable_ctx_params[] = {
164     /* "algorithm" and "cipher" are the same parameter */
165     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ALGORITHM, NULL, 0),
166     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
167     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
168     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
169     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
170     OSSL_PARAM_END
171 };
172 static const OSSL_PARAM *cmac_settable_ctx_params(void)
173 {
174     return known_settable_ctx_params;
175 }
176
177 /*
178  * ALL parameters should be set before init().
179  */
180 static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
181 {
182     struct cmac_data_st *macctx = vmacctx;
183     const OSSL_PARAM *p;
184
185     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL
186         || ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ALGORITHM))
187             != NULL)) {
188         if (p->data_type != OSSL_PARAM_UTF8_STRING)
189             return 0;
190
191         {
192             const char *algoname = p->data;
193             const char *propquery = NULL;
194
195 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */
196             ENGINE_finish(macctx->tmpengine);
197             macctx->tmpengine = NULL;
198
199             if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
200                 != NULL) {
201                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
202                     return 0;
203
204                 macctx->tmpengine = ENGINE_by_id(p->data);
205                 if (macctx->tmpengine == NULL)
206                     return 0;
207             }
208 #endif
209             if ((p = OSSL_PARAM_locate_const(params,
210                                              OSSL_MAC_PARAM_PROPERTIES))
211                 != NULL) {
212                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
213                     return 0;
214
215                 propquery = p->data;
216             }
217
218             EVP_CIPHER_meth_free(macctx->alloc_cipher);
219
220             macctx->tmpcipher = macctx->alloc_cipher =
221                 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
222                                  algoname, propquery);
223
224 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
225             /* TODO(3.0) BEGIN legacy stuff, to be removed */
226             if (macctx->tmpcipher == NULL)
227                 macctx->tmpcipher = EVP_get_cipherbyname(algoname);
228             /* TODO(3.0) END of legacy stuff */
229 #endif
230
231             if (macctx->tmpcipher == NULL)
232                 return 0;
233         }
234     }
235     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
236         if (p->data_type != OSSL_PARAM_OCTET_STRING)
237             return 0;
238
239         if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
240                        macctx->tmpcipher, macctx->tmpengine))
241             return 0;
242
243         macctx->tmpcipher = NULL;
244         macctx->tmpengine = NULL;
245     }
246     return 1;
247 }
248
249 const OSSL_DISPATCH cmac_functions[] = {
250     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
251     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
252     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
253     { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
254     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
255     { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
256     { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
257       (void (*)(void))cmac_gettable_ctx_params },
258     { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
259     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
260       (void (*)(void))cmac_settable_ctx_params },
261     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
262     { 0, NULL }
263 };
264
265 #endif