]>
Commit | Line | Data |
---|---|---|
2e5db6ad RL |
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 | ||
bad41b68 MC |
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> | |
2e5db6ad | 16 | |
bad41b68 MC |
17 | #include "internal/provider_algs.h" |
18 | #include "internal/provider_ctx.h" | |
1dcc7ee6 | 19 | #include "internal/provider_util.h" |
2e5db6ad RL |
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; | |
92d9d0ae | 30 | static OSSL_OP_mac_get_ctx_params_fn cmac_get_ctx_params; |
2e5db6ad | 31 | static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params; |
92d9d0ae | 32 | static OSSL_OP_mac_set_ctx_params_fn cmac_set_ctx_params; |
2e5db6ad RL |
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; | |
1dcc7ee6 | 42 | PROV_CIPHER cipher; |
2e5db6ad RL |
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; | |
d5f85429 RL |
53 | } else { |
54 | macctx->provctx = provctx; | |
2e5db6ad | 55 | } |
2e5db6ad RL |
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); | |
1dcc7ee6 | 66 | ossl_prov_cipher_reset(&macctx->cipher); |
2e5db6ad RL |
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 | ||
1dcc7ee6 P |
76 | if (!CMAC_CTX_copy(dst->ctx, src->ctx) |
77 | || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) { | |
2e5db6ad RL |
78 | cmac_free(dst); |
79 | return NULL; | |
80 | } | |
2e5db6ad RL |
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; | |
1dcc7ee6 P |
94 | int rv = CMAC_Init(macctx->ctx, NULL, 0, |
95 | ossl_prov_cipher_cipher(&macctx->cipher), | |
96 | ossl_prov_cipher_engine(&macctx->cipher)); | |
2e5db6ad | 97 | |
1dcc7ee6 | 98 | ossl_prov_cipher_reset(&macctx->cipher); |
2e5db6ad RL |
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[] = { | |
703170d4 | 119 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
2e5db6ad RL |
120 | OSSL_PARAM_END |
121 | }; | |
122 | static const OSSL_PARAM *cmac_gettable_ctx_params(void) | |
123 | { | |
124 | return known_gettable_ctx_params; | |
125 | } | |
126 | ||
92d9d0ae | 127 | static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
2e5db6ad RL |
128 | { |
129 | OSSL_PARAM *p; | |
130 | ||
703170d4 | 131 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) |
2e5db6ad RL |
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[] = { | |
2e5db6ad RL |
138 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0), |
139 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0), | |
140 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), | |
141 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), | |
142 | OSSL_PARAM_END | |
143 | }; | |
144 | static const OSSL_PARAM *cmac_settable_ctx_params(void) | |
145 | { | |
146 | return known_settable_ctx_params; | |
147 | } | |
148 | ||
149 | /* | |
150 | * ALL parameters should be set before init(). | |
151 | */ | |
92d9d0ae | 152 | static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
2e5db6ad RL |
153 | { |
154 | struct cmac_data_st *macctx = vmacctx; | |
1dcc7ee6 | 155 | OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx); |
2e5db6ad RL |
156 | const OSSL_PARAM *p; |
157 | ||
1dcc7ee6 P |
158 | if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx)) |
159 | return 0; | |
2e5db6ad | 160 | |
2e5db6ad RL |
161 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { |
162 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
163 | return 0; | |
164 | ||
165 | if (!CMAC_Init(macctx->ctx, p->data, p->data_size, | |
1dcc7ee6 P |
166 | ossl_prov_cipher_cipher(&macctx->cipher), |
167 | ossl_prov_cipher_engine(&macctx->cipher))) | |
2e5db6ad RL |
168 | return 0; |
169 | ||
1dcc7ee6 | 170 | ossl_prov_cipher_reset(&macctx->cipher); |
2e5db6ad RL |
171 | } |
172 | return 1; | |
173 | } | |
174 | ||
175 | const OSSL_DISPATCH cmac_functions[] = { | |
176 | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new }, | |
177 | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup }, | |
178 | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free }, | |
179 | { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init }, | |
180 | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update }, | |
181 | { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final }, | |
182 | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, | |
183 | (void (*)(void))cmac_gettable_ctx_params }, | |
92d9d0ae | 184 | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params }, |
2e5db6ad RL |
185 | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
186 | (void (*)(void))cmac_settable_ctx_params }, | |
92d9d0ae | 187 | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params }, |
2e5db6ad RL |
188 | { 0, NULL } |
189 | }; |