61b58e01779bcb232ad54212438c8e642b8e4b97
[openssl.git] / providers / implementations / macs / cmac_prov.c
1 /*
2  * Copyright 2018-2020 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 /*
11  * CMAC low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_dispatch.h>
17 #include <openssl/core_names.h>
18 #include <openssl/params.h>
19 #include <openssl/engine.h>
20 #include <openssl/evp.h>
21 #include <openssl/cmac.h>
22
23 #include "prov/implementations.h"
24 #include "prov/provider_ctx.h"
25 #include "prov/provider_util.h"
26 #include "prov/providercommon.h"
27
28 /*
29  * Forward declaration of everything implemented here.  This is not strictly
30  * necessary for the compiler, but provides an assurance that the signatures
31  * of the functions in the dispatch table are correct.
32  */
33 static OSSL_FUNC_mac_newctx_fn cmac_new;
34 static OSSL_FUNC_mac_dupctx_fn cmac_dup;
35 static OSSL_FUNC_mac_freectx_fn cmac_free;
36 static OSSL_FUNC_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
37 static OSSL_FUNC_mac_get_ctx_params_fn cmac_get_ctx_params;
38 static OSSL_FUNC_mac_settable_ctx_params_fn cmac_settable_ctx_params;
39 static OSSL_FUNC_mac_set_ctx_params_fn cmac_set_ctx_params;
40 static OSSL_FUNC_mac_init_fn cmac_init;
41 static OSSL_FUNC_mac_update_fn cmac_update;
42 static OSSL_FUNC_mac_final_fn cmac_final;
43
44 /* local CMAC data */
45
46 struct cmac_data_st {
47     void *provctx;
48     CMAC_CTX *ctx;
49     PROV_CIPHER cipher;
50 };
51
52 static void *cmac_new(void *provctx)
53 {
54     struct cmac_data_st *macctx;
55
56     if (!ossl_prov_is_running())
57         return NULL;
58
59     if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
60         || (macctx->ctx = CMAC_CTX_new()) == NULL) {
61         OPENSSL_free(macctx);
62         macctx = NULL;
63     } else {
64         macctx->provctx = provctx;
65     }
66
67     return macctx;
68 }
69
70 static void cmac_free(void *vmacctx)
71 {
72     struct cmac_data_st *macctx = vmacctx;
73
74     if (macctx != NULL) {
75         CMAC_CTX_free(macctx->ctx);
76         ossl_prov_cipher_reset(&macctx->cipher);
77         OPENSSL_free(macctx);
78     }
79 }
80
81 static void *cmac_dup(void *vsrc)
82 {
83     struct cmac_data_st *src = vsrc;
84     struct cmac_data_st *dst;
85
86     if (!ossl_prov_is_running())
87         return NULL;
88
89     dst = cmac_new(src->provctx);
90     if (!CMAC_CTX_copy(dst->ctx, src->ctx)
91         || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
92         cmac_free(dst);
93         return NULL;
94     }
95     return dst;
96 }
97
98 static size_t cmac_size(void *vmacctx)
99 {
100     struct cmac_data_st *macctx = vmacctx;
101
102     return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
103 }
104
105 static int cmac_init(void *vmacctx)
106 {
107     struct cmac_data_st *macctx = vmacctx;
108     int rv;
109
110     if (!ossl_prov_is_running())
111         return 0;
112
113     rv = CMAC_Init(macctx->ctx, NULL, 0,
114                    ossl_prov_cipher_cipher(&macctx->cipher),
115                    ossl_prov_cipher_engine(&macctx->cipher));
116
117     ossl_prov_cipher_reset(&macctx->cipher);
118     return rv;
119 }
120
121 static int cmac_update(void *vmacctx, const unsigned char *data,
122                        size_t datalen)
123 {
124     struct cmac_data_st *macctx = vmacctx;
125
126     return CMAC_Update(macctx->ctx, data, datalen);
127 }
128
129 static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
130                       size_t outsize)
131 {
132     struct cmac_data_st *macctx = vmacctx;
133
134     if (!ossl_prov_is_running())
135         return 0;
136
137     return CMAC_Final(macctx->ctx, out, outl);
138 }
139
140 static const OSSL_PARAM known_gettable_ctx_params[] = {
141     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
142     OSSL_PARAM_END
143 };
144 static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *provctx)
145 {
146     return known_gettable_ctx_params;
147 }
148
149 static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
150 {
151     OSSL_PARAM *p;
152
153     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
154         return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
155
156     return 1;
157 }
158
159 static const OSSL_PARAM known_settable_ctx_params[] = {
160     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
161     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
162     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
163     OSSL_PARAM_END
164 };
165 static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *provctx)
166 {
167     return known_settable_ctx_params;
168 }
169
170 /*
171  * ALL parameters should be set before init().
172  */
173 static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
174 {
175     struct cmac_data_st *macctx = vmacctx;
176     OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
177     const OSSL_PARAM *p;
178
179     if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
180         return 0;
181
182     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
183         if (p->data_type != OSSL_PARAM_OCTET_STRING)
184             return 0;
185
186         if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
187                        ossl_prov_cipher_cipher(&macctx->cipher),
188                        ossl_prov_cipher_engine(&macctx->cipher)))
189             return 0;
190
191         ossl_prov_cipher_reset(&macctx->cipher);
192     }
193     return 1;
194 }
195
196 const OSSL_DISPATCH cmac_functions[] = {
197     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
198     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
199     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
200     { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
201     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
202     { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
203     { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
204       (void (*)(void))cmac_gettable_ctx_params },
205     { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
206     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
207       (void (*)(void))cmac_settable_ctx_params },
208     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
209     { 0, NULL }
210 };