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