Get rid of the diversity of names for MAC parameters
[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_SIZE, NULL),
144     OSSL_PARAM_END
145 };
146 static const OSSL_PARAM *cmac_gettable_ctx_params(void)
147 {
148     return known_gettable_ctx_params;
149 }
150
151 static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
152 {
153     OSSL_PARAM *p;
154
155     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
156         return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
157
158     return 1;
159 }
160
161 static const OSSL_PARAM known_settable_ctx_params[] = {
162     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
163     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
164     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
165     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
166     OSSL_PARAM_END
167 };
168 static const OSSL_PARAM *cmac_settable_ctx_params(void)
169 {
170     return known_settable_ctx_params;
171 }
172
173 /*
174  * ALL parameters should be set before init().
175  */
176 static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
177 {
178     struct cmac_data_st *macctx = vmacctx;
179     const OSSL_PARAM *p;
180
181     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
182         if (p->data_type != OSSL_PARAM_UTF8_STRING)
183             return 0;
184
185         {
186             const char *algoname = p->data;
187             const char *propquery = NULL;
188
189 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */
190             ENGINE_finish(macctx->tmpengine);
191             macctx->tmpengine = NULL;
192
193             if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
194                 != NULL) {
195                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
196                     return 0;
197
198                 macctx->tmpengine = ENGINE_by_id(p->data);
199                 if (macctx->tmpengine == NULL)
200                     return 0;
201             }
202 #endif
203             if ((p = OSSL_PARAM_locate_const(params,
204                                              OSSL_MAC_PARAM_PROPERTIES))
205                 != NULL) {
206                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
207                     return 0;
208
209                 propquery = p->data;
210             }
211
212             EVP_CIPHER_meth_free(macctx->alloc_cipher);
213
214             macctx->tmpcipher = macctx->alloc_cipher =
215                 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
216                                  algoname, propquery);
217
218 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
219             /* TODO(3.0) BEGIN legacy stuff, to be removed */
220             if (macctx->tmpcipher == NULL)
221                 macctx->tmpcipher = EVP_get_cipherbyname(algoname);
222             /* TODO(3.0) END of legacy stuff */
223 #endif
224
225             if (macctx->tmpcipher == NULL)
226                 return 0;
227         }
228     }
229     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
230         if (p->data_type != OSSL_PARAM_OCTET_STRING)
231             return 0;
232
233         if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
234                        macctx->tmpcipher, macctx->tmpengine))
235             return 0;
236
237         macctx->tmpcipher = NULL;
238         macctx->tmpengine = NULL;
239     }
240     return 1;
241 }
242
243 const OSSL_DISPATCH cmac_functions[] = {
244     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
245     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
246     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
247     { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
248     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
249     { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
250     { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
251       (void (*)(void))cmac_gettable_ctx_params },
252     { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
253     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
254       (void (*)(void))cmac_settable_ctx_params },
255     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
256     { 0, NULL }
257 };
258
259 #endif