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 typedef struct omac_ctx {
17 #define MAX_GOST_OMAC_SIZE 16
19 static int omac_init(EVP_MD_CTX *ctx, int cipher_nid)
21 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
22 memset(c, 0, sizeof(OMAC_CTX));
23 c->cipher_nid = cipher_nid;
31 case NID_grasshopper_cbc:
39 static int magma_imit_init(EVP_MD_CTX *ctx)
41 return omac_init(ctx, NID_magma_cbc);
44 static int grasshopper_imit_init(EVP_MD_CTX *ctx)
46 return omac_init(ctx, NID_grasshopper_cbc);
49 static int omac_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
51 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
54 GOSTerr(GOST_F_OMAC_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
58 return CMAC_Update(c->cmac_ctx, data, count);
61 int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
63 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
64 unsigned char mac[MAX_GOST_OMAC_SIZE];
65 size_t mac_size = sizeof(mac);
68 GOSTerr(GOST_F_OMAC_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
72 CMAC_Final(c->cmac_ctx, mac, &mac_size);
74 memcpy(md, mac, c->dgst_size);
78 int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
80 OMAC_CTX *c_to = EVP_MD_CTX_md_data(to);
81 const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from);
84 c_to->dgst_size = c_from->dgst_size;
85 c_to->cipher_nid = c_from->cipher_nid;
86 c_to->key_set = c_from->key_set;
92 return CMAC_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
95 /* Clean up imit ctx */
96 int omac_imit_cleanup(EVP_MD_CTX *ctx)
98 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
102 CMAC_CTX_free(c->cmac_ctx);
103 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX));
108 static int omac_key(OMAC_CTX *c, const EVP_CIPHER *cipher, const unsigned char *key, size_t key_size)
112 c->cmac_ctx = CMAC_CTX_new();
113 if (c->cmac_ctx == NULL)
115 GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE);
119 ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL);
127 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
130 case EVP_MD_CTRL_KEY_LEN:
131 *((unsigned int *)(ptr)) = 32;
133 case EVP_MD_CTRL_SET_KEY:
135 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
136 const EVP_MD *md = EVP_MD_CTX_md(ctx);
137 const EVP_CIPHER *cipher = NULL;
139 if (c->cipher_nid == NID_undef)
141 switch (EVP_MD_nid(md))
144 c->cipher_nid = NID_magma_cbc;
147 case NID_grasshopper_mac:
148 c->cipher_nid = NID_grasshopper_cbc;
152 cipher = EVP_get_cipherbynid(c->cipher_nid);
156 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
159 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
160 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
163 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
167 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
172 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
173 return omac_key(c, cipher, key->key, 32);
175 } else if (arg == 32) {
176 return omac_key(c, cipher, ptr, 32);
178 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
181 case EVP_MD_CTRL_MAC_LEN:
183 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
184 switch (c->cipher_nid)
187 if (arg < 1 || arg > 8) {
188 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
193 case NID_grasshopper_cbc:
194 if (arg < 1 || arg > 16) {
195 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
211 static EVP_MD *_hidden_magma_mac_md = NULL;
213 EVP_MD *magma_omac(void)
215 if (_hidden_magma_mac_md == NULL) {
218 if ((md = EVP_MD_meth_new(NID_magma_mac, NID_undef)) == NULL
219 || !EVP_MD_meth_set_result_size(md, 4)
220 || !EVP_MD_meth_set_input_blocksize(md, 8)
221 || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
222 || !EVP_MD_meth_set_flags(md, 0)
223 || !EVP_MD_meth_set_init(md, magma_imit_init)
224 || !EVP_MD_meth_set_update(md, omac_imit_update)
225 || !EVP_MD_meth_set_final(md, omac_imit_final)
226 || !EVP_MD_meth_set_copy(md, omac_imit_copy)
227 || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
228 || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
229 EVP_MD_meth_free(md);
232 _hidden_magma_mac_md = md;
234 return _hidden_magma_mac_md;
237 void magma_omac_destroy(void)
239 EVP_MD_meth_free(_hidden_magma_mac_md);
240 _hidden_magma_mac_md = NULL;
243 static EVP_MD *_hidden_grasshopper_mac_md = NULL;
245 EVP_MD *grasshopper_omac(void)
247 if (_hidden_grasshopper_mac_md == NULL) {
250 if ((md = EVP_MD_meth_new(NID_grasshopper_mac, NID_undef)) == NULL
251 || !EVP_MD_meth_set_result_size(md, 4)
252 || !EVP_MD_meth_set_input_blocksize(md, 8)
253 || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
254 || !EVP_MD_meth_set_flags(md, 0)
255 || !EVP_MD_meth_set_init(md, grasshopper_imit_init)
256 || !EVP_MD_meth_set_update(md, omac_imit_update)
257 || !EVP_MD_meth_set_final(md, omac_imit_final)
258 || !EVP_MD_meth_set_copy(md, omac_imit_copy)
259 || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
260 || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
261 EVP_MD_meth_free(md);
264 _hidden_grasshopper_mac_md = md;
266 return _hidden_grasshopper_mac_md;
269 void grasshopper_omac_destroy(void)
271 EVP_MD_meth_free(_hidden_grasshopper_mac_md);
272 _hidden_grasshopper_mac_md = NULL;