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 {
17 unsigned char key[32];
20 #define MAX_GOST_OMAC_SIZE 16
22 static int omac_init(EVP_MD_CTX *ctx, int cipher_nid)
24 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
25 memset(c, 0, sizeof(OMAC_CTX));
26 c->cipher_nid = cipher_nid;
34 case NID_grasshopper_cbc:
42 static int magma_imit_init(EVP_MD_CTX *ctx)
44 return omac_init(ctx, NID_magma_cbc);
47 static int grasshopper_imit_init(EVP_MD_CTX *ctx)
49 return omac_init(ctx, NID_grasshopper_cbc);
52 static int omac_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
54 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
56 GOSTerr(GOST_F_OMAC_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
60 return CMAC_Update(c->cmac_ctx, data, count);
63 int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
65 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
66 unsigned char mac[MAX_GOST_OMAC_SIZE];
67 size_t mac_size = sizeof(mac);
70 GOSTerr(GOST_F_OMAC_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
74 CMAC_Final(c->cmac_ctx, mac, &mac_size);
76 memcpy(md, mac, c->dgst_size);
80 int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
82 OMAC_CTX *c_to = EVP_MD_CTX_md_data(to);
83 const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from);
86 c_to->dgst_size = c_from->dgst_size;
87 c_to->cipher_nid = c_from->cipher_nid;
88 c_to->key_set = c_from->key_set;
89 memcpy(c_to->key, c_from->key, 32);
93 if (!c_from->cmac_ctx) {
95 CMAC_CTX_free(c_to->cmac_ctx);
96 c_to->cmac_ctx = NULL;
100 if (c_to->cmac_ctx == c_from->cmac_ctx) {
101 c_to->cmac_ctx = CMAC_CTX_new();
103 return CMAC_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
106 /* Clean up imit ctx */
107 int omac_imit_cleanup(EVP_MD_CTX *ctx)
109 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
112 CMAC_CTX_free(c->cmac_ctx);
113 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX));
118 static int omac_key(OMAC_CTX * c, const EVP_CIPHER *cipher,
119 const unsigned char *key, size_t key_size)
123 c->cmac_ctx = CMAC_CTX_new();
124 if (c->cmac_ctx == NULL) {
125 GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE);
129 ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL);
136 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
139 case EVP_MD_CTRL_KEY_LEN:
140 *((unsigned int *)(ptr)) = 32;
142 case EVP_MD_CTRL_SET_KEY:
144 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
145 const EVP_MD *md = EVP_MD_CTX_md(ctx);
146 const EVP_CIPHER *cipher = NULL;
149 if (c->cipher_nid == NID_undef) {
150 switch (EVP_MD_nid(md)) {
152 c->cipher_nid = NID_magma_cbc;
155 case NID_grasshopper_mac:
156 c->cipher_nid = NID_grasshopper_cbc;
160 cipher = EVP_get_cipherbynid(c->cipher_nid);
162 if (cipher == NULL) {
163 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
166 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
167 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
170 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
173 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
178 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
179 ret = omac_key(c, cipher, key->key, 32);
181 memcpy(c->key, key->key, 32);
183 } else if (arg == 32) {
184 ret = omac_key(c, cipher, ptr, 32);
186 memcpy(c->key, ptr, 32);
189 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
192 case EVP_MD_CTRL_MAC_LEN:
194 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
195 switch (c->cipher_nid) {
197 if (arg < 1 || arg > 8) {
198 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
203 case NID_grasshopper_cbc:
204 if (arg < 1 || arg > 16) {
205 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
215 #ifdef EVP_MD_CTRL_TLSTREE
216 case EVP_MD_CTRL_TLSTREE:
218 OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
221 unsigned char diversed_key[32];
222 return gost_tlstree(c->cipher_nid, c->key, diversed_key,
223 (const unsigned char *)ptr) ?
224 omac_key(c, EVP_get_cipherbynid(c->cipher_nid),
225 diversed_key, 32) : 0;
227 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
237 static EVP_MD *_hidden_magma_mac_md = NULL;
239 EVP_MD *magma_omac(void)
241 if (_hidden_magma_mac_md == NULL) {
244 if ((md = EVP_MD_meth_new(NID_magma_mac, NID_undef)) == NULL
245 || !EVP_MD_meth_set_result_size(md, 4)
246 || !EVP_MD_meth_set_input_blocksize(md, 8)
247 || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
248 || !EVP_MD_meth_set_flags(md, 0)
249 || !EVP_MD_meth_set_init(md, magma_imit_init)
250 || !EVP_MD_meth_set_update(md, omac_imit_update)
251 || !EVP_MD_meth_set_final(md, omac_imit_final)
252 || !EVP_MD_meth_set_copy(md, omac_imit_copy)
253 || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
254 || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
255 EVP_MD_meth_free(md);
258 _hidden_magma_mac_md = md;
260 return _hidden_magma_mac_md;
263 void magma_omac_destroy(void)
265 EVP_MD_meth_free(_hidden_magma_mac_md);
266 _hidden_magma_mac_md = NULL;
269 static EVP_MD *_hidden_grasshopper_mac_md = NULL;
271 EVP_MD *grasshopper_omac(void)
273 if (_hidden_grasshopper_mac_md == NULL) {
276 if ((md = EVP_MD_meth_new(NID_grasshopper_mac, NID_undef)) == NULL
277 || !EVP_MD_meth_set_result_size(md, 8)
278 || !EVP_MD_meth_set_input_blocksize(md, 8)
279 || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
280 || !EVP_MD_meth_set_flags(md, 0)
281 || !EVP_MD_meth_set_init(md, grasshopper_imit_init)
282 || !EVP_MD_meth_set_update(md, omac_imit_update)
283 || !EVP_MD_meth_set_final(md, omac_imit_final)
284 || !EVP_MD_meth_set_copy(md, omac_imit_copy)
285 || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
286 || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
287 EVP_MD_meth_free(md);
290 _hidden_grasshopper_mac_md = md;
292 return _hidden_grasshopper_mac_md;
295 void grasshopper_omac_destroy(void)
297 EVP_MD_meth_free(_hidden_grasshopper_mac_md);
298 _hidden_grasshopper_mac_md = NULL;