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