]>
Commit | Line | Data |
---|---|---|
2e5db6ad | 1 | /* |
8020d79b | 2 | * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. |
2e5db6ad RL |
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 | ||
a6d572e6 P |
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 | ||
23c48d94 | 16 | #include <openssl/core_dispatch.h> |
bad41b68 MC |
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> | |
2e5db6ad | 22 | |
af3e7e1b | 23 | #include "prov/implementations.h" |
ddd21319 RL |
24 | #include "prov/provider_ctx.h" |
25 | #include "prov/provider_util.h" | |
5b104a81 | 26 | #include "prov/providercommon.h" |
2e5db6ad RL |
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 | */ | |
363b1e5d DMSP |
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; | |
2e5db6ad RL |
43 | |
44 | /* local CMAC data */ | |
45 | ||
46 | struct cmac_data_st { | |
47 | void *provctx; | |
48 | CMAC_CTX *ctx; | |
1dcc7ee6 | 49 | PROV_CIPHER cipher; |
2e5db6ad RL |
50 | }; |
51 | ||
52 | static void *cmac_new(void *provctx) | |
53 | { | |
54 | struct cmac_data_st *macctx; | |
55 | ||
5b104a81 P |
56 | if (!ossl_prov_is_running()) |
57 | return NULL; | |
58 | ||
2e5db6ad RL |
59 | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
60 | || (macctx->ctx = CMAC_CTX_new()) == NULL) { | |
61 | OPENSSL_free(macctx); | |
62 | macctx = NULL; | |
d5f85429 RL |
63 | } else { |
64 | macctx->provctx = provctx; | |
2e5db6ad | 65 | } |
2e5db6ad RL |
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); | |
1dcc7ee6 | 76 | ossl_prov_cipher_reset(&macctx->cipher); |
2e5db6ad RL |
77 | OPENSSL_free(macctx); |
78 | } | |
79 | } | |
80 | ||
81 | static void *cmac_dup(void *vsrc) | |
82 | { | |
83 | struct cmac_data_st *src = vsrc; | |
5b104a81 P |
84 | struct cmac_data_st *dst; |
85 | ||
86 | if (!ossl_prov_is_running()) | |
87 | return NULL; | |
2e5db6ad | 88 | |
5b104a81 | 89 | dst = cmac_new(src->provctx); |
1dcc7ee6 P |
90 | if (!CMAC_CTX_copy(dst->ctx, src->ctx) |
91 | || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) { | |
2e5db6ad RL |
92 | cmac_free(dst); |
93 | return NULL; | |
94 | } | |
2e5db6ad RL |
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 | ||
005b1902 P |
105 | static int cmac_setkey(struct cmac_data_st *macctx, |
106 | const unsigned char *key, size_t keylen) | |
107 | { | |
108 | int rv = CMAC_Init(macctx->ctx, key, keylen, | |
109 | ossl_prov_cipher_cipher(&macctx->cipher), | |
110 | ossl_prov_cipher_engine(&macctx->cipher)); | |
111 | ossl_prov_cipher_reset(&macctx->cipher); | |
112 | return rv; | |
113 | } | |
114 | ||
115 | static int cmac_init(void *vmacctx, const unsigned char *key, | |
116 | size_t keylen, const OSSL_PARAM params[]) | |
2e5db6ad RL |
117 | { |
118 | struct cmac_data_st *macctx = vmacctx; | |
5b104a81 | 119 | |
005b1902 | 120 | if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params)) |
5b104a81 | 121 | return 0; |
005b1902 P |
122 | if (key != NULL) |
123 | return cmac_setkey(macctx, key, keylen); | |
124 | return 1; | |
2e5db6ad RL |
125 | } |
126 | ||
127 | static int cmac_update(void *vmacctx, const unsigned char *data, | |
128 | size_t datalen) | |
129 | { | |
130 | struct cmac_data_st *macctx = vmacctx; | |
131 | ||
132 | return CMAC_Update(macctx->ctx, data, datalen); | |
133 | } | |
134 | ||
135 | static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl, | |
136 | size_t outsize) | |
137 | { | |
138 | struct cmac_data_st *macctx = vmacctx; | |
139 | ||
5b104a81 P |
140 | if (!ossl_prov_is_running()) |
141 | return 0; | |
142 | ||
2e5db6ad RL |
143 | return CMAC_Final(macctx->ctx, out, outl); |
144 | } | |
145 | ||
146 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
703170d4 | 147 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
2e5db6ad RL |
148 | OSSL_PARAM_END |
149 | }; | |
eee323c3 P |
150 | static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx, |
151 | ossl_unused void *provctx) | |
2e5db6ad RL |
152 | { |
153 | return known_gettable_ctx_params; | |
154 | } | |
155 | ||
92d9d0ae | 156 | static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
2e5db6ad RL |
157 | { |
158 | OSSL_PARAM *p; | |
159 | ||
703170d4 | 160 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) |
2e5db6ad RL |
161 | return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)); |
162 | ||
163 | return 1; | |
164 | } | |
165 | ||
166 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
2e5db6ad | 167 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0), |
2e5db6ad RL |
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 | }; | |
eee323c3 P |
172 | static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx, |
173 | ossl_unused void *provctx) | |
2e5db6ad RL |
174 | { |
175 | return known_settable_ctx_params; | |
176 | } | |
177 | ||
178 | /* | |
179 | * ALL parameters should be set before init(). | |
180 | */ | |
92d9d0ae | 181 | static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
2e5db6ad RL |
182 | { |
183 | struct cmac_data_st *macctx = vmacctx; | |
a829b735 | 184 | OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx); |
2e5db6ad RL |
185 | const OSSL_PARAM *p; |
186 | ||
1dcc7ee6 P |
187 | if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx)) |
188 | return 0; | |
2e5db6ad | 189 | |
2e5db6ad RL |
190 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { |
191 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
192 | return 0; | |
005b1902 | 193 | return cmac_setkey(macctx, p->data, p->data_size); |
2e5db6ad RL |
194 | } |
195 | return 1; | |
196 | } | |
197 | ||
1be63951 | 198 | const OSSL_DISPATCH ossl_cmac_functions[] = { |
2e5db6ad RL |
199 | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new }, |
200 | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup }, | |
201 | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free }, | |
202 | { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init }, | |
203 | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update }, | |
204 | { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final }, | |
205 | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, | |
206 | (void (*)(void))cmac_gettable_ctx_params }, | |
92d9d0ae | 207 | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params }, |
2e5db6ad RL |
208 | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
209 | (void (*)(void))cmac_settable_ctx_params }, | |
92d9d0ae | 210 | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params }, |
2e5db6ad RL |
211 | { 0, NULL } |
212 | }; |