2 #include <openssl/cmac.h>
3 #include <openssl/conf.h>
4 #include <openssl/err.h>
5 #include <openssl/evp.h>
7 #include "e_gost_err.h"
10 #define min(a,b) (((a) < (b)) ? (a) : (b))
12 typedef struct omac_ctx {
18 * Here begins stuff related to TLSTREE processing
19 * We MUST store the original key to derive TLSTREE keys from it
22 unsigned char key[32];
25 * TLSTREE intermediate values should be recalculated only when
26 * C_i & (seq_no+1) != C_i & (seq_no)
27 * so somewhen we will store C_i & (seq_no) in this structure
28 * to avoid redundant hash calculations.
32 #define MAX_GOST_OMAC_SIZE 16
34 static int omac_init(EVP_MD_CTX *ctx, int cipher_nid)
36 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
37 memset(c, 0, sizeof(OMAC_CTX));
38 c->cipher_nid = cipher_nid;
46 case NID_grasshopper_cbc:
54 static int magma_imit_init(EVP_MD_CTX *ctx)
56 return omac_init(ctx, NID_magma_cbc);
59 static int grasshopper_imit_init(EVP_MD_CTX *ctx)
61 return omac_init(ctx, NID_grasshopper_cbc);
64 static int omac_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
66 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
68 GOSTerr(GOST_F_OMAC_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
72 return CMAC_Update(c->cmac_ctx, data, count);
75 int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
77 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
78 unsigned char mac[MAX_GOST_OMAC_SIZE];
79 size_t mac_size = sizeof(mac);
82 GOSTerr(GOST_F_OMAC_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
86 CMAC_Final(c->cmac_ctx, mac, &mac_size);
88 memcpy(md, mac, c->dgst_size);
92 int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
94 OMAC_CTX *c_to = EVP_MD_CTX_md_data(to);
95 const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from);
98 c_to->dgst_size = c_from->dgst_size;
99 c_to->cipher_nid = c_from->cipher_nid;
100 c_to->key_set = c_from->key_set;
101 memcpy(c_to->key, c_from->key, 32);
105 if (!c_from->cmac_ctx) {
106 if (c_to->cmac_ctx) {
107 CMAC_CTX_free(c_to->cmac_ctx);
108 c_to->cmac_ctx = NULL;
112 if (c_to->cmac_ctx == c_from->cmac_ctx) {
113 c_to->cmac_ctx = CMAC_CTX_new();
115 return CMAC_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
118 /* Clean up imit ctx */
119 int omac_imit_cleanup(EVP_MD_CTX *ctx)
121 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
124 CMAC_CTX_free(c->cmac_ctx);
125 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX));
130 static int omac_key(OMAC_CTX * c, const EVP_CIPHER *cipher,
131 const unsigned char *key, size_t key_size)
135 c->cmac_ctx = CMAC_CTX_new();
136 if (c->cmac_ctx == NULL) {
137 GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE);
141 ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL);
148 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
151 case EVP_MD_CTRL_KEY_LEN:
152 *((unsigned int *)(ptr)) = 32;
154 case EVP_MD_CTRL_SET_KEY:
156 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
157 const EVP_MD *md = EVP_MD_CTX_md(ctx);
158 const EVP_CIPHER *cipher = NULL;
161 if (c->cipher_nid == NID_undef) {
162 switch (EVP_MD_nid(md)) {
164 c->cipher_nid = NID_magma_cbc;
167 case NID_grasshopper_mac:
168 c->cipher_nid = NID_grasshopper_cbc;
172 cipher = EVP_get_cipherbynid(c->cipher_nid);
174 if (cipher == NULL) {
175 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
178 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
179 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
182 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
185 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
190 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
191 ret = omac_key(c, cipher, key->key, 32);
193 memcpy(c->key, key->key, 32);
195 } else if (arg == 32) {
196 ret = omac_key(c, cipher, ptr, 32);
198 memcpy(c->key, ptr, 32);
201 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
204 case EVP_MD_CTRL_MAC_LEN:
206 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
207 switch (c->cipher_nid) {
209 if (arg < 1 || arg > 8) {
210 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
215 case NID_grasshopper_cbc:
216 if (arg < 1 || arg > 16) {
217 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
227 #ifdef EVP_MD_CTRL_TLSTREE
228 case EVP_MD_CTRL_TLSTREE:
230 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
232 unsigned char diversed_key[32];
233 return gost_tlstree(c->cipher_nid, c->key, diversed_key,
234 (const unsigned char *)ptr) ?
235 omac_key(c, EVP_get_cipherbynid(c->cipher_nid),
236 diversed_key, 32) : 0;
238 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
248 static EVP_MD *_hidden_magma_mac_md = NULL;
250 EVP_MD *magma_omac(void)
252 if (_hidden_magma_mac_md == NULL) {
255 if ((md = EVP_MD_meth_new(NID_magma_mac, NID_undef)) == NULL
256 || !EVP_MD_meth_set_result_size(md, 8)
257 || !EVP_MD_meth_set_input_blocksize(md, 8)
258 || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
259 || !EVP_MD_meth_set_flags(md, 0)
260 || !EVP_MD_meth_set_init(md, magma_imit_init)
261 || !EVP_MD_meth_set_update(md, omac_imit_update)
262 || !EVP_MD_meth_set_final(md, omac_imit_final)
263 || !EVP_MD_meth_set_copy(md, omac_imit_copy)
264 || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
265 || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
266 EVP_MD_meth_free(md);
269 _hidden_magma_mac_md = md;
271 return _hidden_magma_mac_md;
274 void magma_omac_destroy(void)
276 EVP_MD_meth_free(_hidden_magma_mac_md);
277 _hidden_magma_mac_md = NULL;
280 static EVP_MD *_hidden_grasshopper_mac_md = NULL;
282 EVP_MD *grasshopper_omac(void)
284 if (_hidden_grasshopper_mac_md == NULL) {
287 if ((md = EVP_MD_meth_new(NID_grasshopper_mac, NID_undef)) == NULL
288 || !EVP_MD_meth_set_result_size(md, 16)
289 || !EVP_MD_meth_set_input_blocksize(md, 8)
290 || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
291 || !EVP_MD_meth_set_flags(md, 0)
292 || !EVP_MD_meth_set_init(md, grasshopper_imit_init)
293 || !EVP_MD_meth_set_update(md, omac_imit_update)
294 || !EVP_MD_meth_set_final(md, omac_imit_final)
295 || !EVP_MD_meth_set_copy(md, omac_imit_copy)
296 || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
297 || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
298 EVP_MD_meth_free(md);
301 _hidden_grasshopper_mac_md = md;
303 return _hidden_grasshopper_mac_md;
306 void grasshopper_omac_destroy(void)
308 EVP_MD_meth_free(_hidden_grasshopper_mac_md);
309 _hidden_grasshopper_mac_md = NULL;