]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_omac.c
Update Copyright lines after registration rework
[openssl-gost/engine.git] / gost_omac.c
1 /*
2  * Copyright (c) 2019 Dmitry Belyavskiy <beldmit@gmail.com>
3  * Copyright (c) 2020 Vitaly Chikunov <vt@altlinux.org>
4  *
5  * Contents licensed under the terms of the OpenSSL license
6  * See https://www.openssl.org/source/license.html for details
7  */
8 #include <string.h>
9 #include <openssl/cmac.h>
10 #include <openssl/conf.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13
14 #include "e_gost_err.h"
15 #include "gost_lcl.h"
16
17 #define min(a,b) (((a) < (b)) ? (a) : (b))
18
19 typedef struct omac_ctx {
20     CMAC_CTX *cmac_ctx;
21     size_t dgst_size;
22     int cipher_nid;
23     int key_set;
24 /* 
25  * Here begins stuff related to TLSTREE processing
26  * We MUST store the original key to derive TLSTREE keys from it
27  * and TLS seq no.
28  * */
29     unsigned char key[32];
30 /*
31  * TODO
32  * TLSTREE intermediate values should be recalculated only when 
33  * C_i & (seq_no+1) != C_i & (seq_no)
34  * so somewhen we will store C_i & (seq_no) in this structure 
35  * to avoid redundant hash calculations.
36  * */
37 } OMAC_CTX;
38
39 #define MAX_GOST_OMAC_SIZE 16
40
41 static int omac_init(EVP_MD_CTX *ctx, int cipher_nid)
42 {
43     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
44     memset(c, 0, sizeof(OMAC_CTX));
45     c->cipher_nid = cipher_nid;
46     c->key_set = 0;
47
48     switch (cipher_nid) {
49     case NID_magma_cbc:
50         c->dgst_size = 8;
51         break;
52
53     case NID_grasshopper_cbc:
54         c->dgst_size = 16;
55         break;
56     }
57
58     return 1;
59 }
60
61 static int magma_imit_init(EVP_MD_CTX *ctx)
62 {
63     return omac_init(ctx, NID_magma_cbc);
64 }
65
66 static int grasshopper_imit_init(EVP_MD_CTX *ctx)
67 {
68     return omac_init(ctx, NID_grasshopper_cbc);
69 }
70
71 static int omac_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
72 {
73     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
74     if (!c->key_set) {
75         GOSTerr(GOST_F_OMAC_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
76         return 0;
77     }
78
79     return CMAC_Update(c->cmac_ctx, data, count);
80 }
81
82 int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
83 {
84     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
85     unsigned char mac[MAX_GOST_OMAC_SIZE];
86     size_t mac_size = sizeof(mac);
87
88     if (!c->key_set) {
89         GOSTerr(GOST_F_OMAC_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
90         return 0;
91     }
92
93     CMAC_Final(c->cmac_ctx, mac, &mac_size);
94
95     memcpy(md, mac, c->dgst_size);
96     return 1;
97 }
98
99 int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
100 {
101     OMAC_CTX *c_to = EVP_MD_CTX_md_data(to);
102     const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from);
103
104     if (c_from && c_to) {
105         c_to->dgst_size = c_from->dgst_size;
106         c_to->cipher_nid = c_from->cipher_nid;
107         c_to->key_set = c_from->key_set;
108         memcpy(c_to->key, c_from->key, 32);
109     } else {
110         return 0;
111     }
112     if (!c_from->cmac_ctx) {
113         if (c_to->cmac_ctx) {
114             CMAC_CTX_free(c_to->cmac_ctx);
115             c_to->cmac_ctx = NULL;
116         }
117         return 1;
118     }
119     if (c_to->cmac_ctx == c_from->cmac_ctx) {
120         c_to->cmac_ctx = CMAC_CTX_new();
121     }
122     return CMAC_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
123 }
124
125 /* Clean up imit ctx */
126 int omac_imit_cleanup(EVP_MD_CTX *ctx)
127 {
128     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
129
130     if (c) {
131         CMAC_CTX_free(c->cmac_ctx);
132         memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX));
133     }
134     return 1;
135 }
136
137 static int omac_key(OMAC_CTX * c, const EVP_CIPHER *cipher,
138                     const unsigned char *key, size_t key_size)
139 {
140     int ret = 0;
141
142     CMAC_CTX_free(c->cmac_ctx);
143     c->cmac_ctx = CMAC_CTX_new();
144     if (c->cmac_ctx == NULL) {
145         GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE);
146         return 0;
147     }
148
149     ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL);
150     if (ret > 0) {
151         c->key_set = 1;
152     }
153     return 1;
154 }
155
156 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
157 {
158     switch (type) {
159     case EVP_MD_CTRL_KEY_LEN:
160         *((unsigned int *)(ptr)) = 32;
161         return 1;
162     case EVP_MD_CTRL_SET_KEY:
163         {
164             OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
165             const EVP_MD *md = EVP_MD_CTX_md(ctx);
166             const EVP_CIPHER *cipher = NULL;
167             int ret = 0;
168
169             if (c->cipher_nid == NID_undef) {
170                 switch (EVP_MD_nid(md)) {
171                 case NID_magma_mac:
172                     c->cipher_nid = NID_magma_cbc;
173                     break;
174
175                 case NID_grasshopper_mac:
176                     c->cipher_nid = NID_grasshopper_cbc;
177                     break;
178                 }
179             }
180             cipher = EVP_get_cipherbynid(c->cipher_nid);
181
182             if (cipher == NULL) {
183                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
184             }
185
186             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
187                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
188                 return 0;
189             }
190             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
191
192             if (c->key_set) {
193                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
194                 return 0;
195             }
196
197             if (arg == 0) {
198                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
199                 ret = omac_key(c, cipher, key->key, 32);
200                 if (ret > 0)
201                     memcpy(c->key, key->key, 32);
202                 return ret;
203             } else if (arg == 32) {
204                 ret = omac_key(c, cipher, ptr, 32);
205                 if (ret > 0)
206                     memcpy(c->key, ptr, 32);
207                 return ret;
208             }
209             GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
210             return 0;
211         }
212     case EVP_MD_CTRL_XOF_LEN:   /* Supported in OpenSSL */
213         {
214             OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
215             switch (c->cipher_nid) {
216             case NID_magma_cbc:
217                 if (arg < 1 || arg > 8) {
218                     GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
219                     return 0;
220                 }
221                 c->dgst_size = arg;
222                 break;
223             case NID_grasshopper_cbc:
224                 if (arg < 1 || arg > 16) {
225                     GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
226                     return 0;
227                 }
228                 c->dgst_size = arg;
229                 break;
230             default:
231                 return 0;
232             }
233             return 1;
234         }
235 #ifdef EVP_MD_CTRL_TLSTREE
236     case EVP_MD_CTRL_TLSTREE:
237         {
238             OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
239             if (c->key_set) {
240                 unsigned char diversed_key[32];
241                 return gost_tlstree(c->cipher_nid, c->key, diversed_key,
242                                     (const unsigned char *)ptr) ?
243                     omac_key(c, EVP_get_cipherbynid(c->cipher_nid),
244                              diversed_key, 32) : 0;
245             }
246             GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
247             return 0;
248         }
249         return 0;
250 #endif
251     default:
252         return 0;
253     }
254 }
255
256 static GOST_digest omac_template_digest = {
257     .input_blocksize = 8,
258     .app_datasize = sizeof(OMAC_CTX),
259     .flags = EVP_MD_FLAG_XOF,
260     .update = omac_imit_update,
261     .final = omac_imit_final,
262     .copy = omac_imit_copy,
263     .cleanup = omac_imit_cleanup,
264     .ctrl = omac_imit_ctrl,
265 };
266
267 GOST_digest magma_mac_digest = {
268     .nid = NID_magma_mac,
269     .template = &omac_template_digest,
270     .result_size = 8,
271     .init = magma_imit_init,
272 };
273
274 GOST_digest grasshopper_mac_digest = {
275     .nid = NID_grasshopper_mac,
276     .template = &omac_template_digest,
277     .result_size = 16,
278     .init = grasshopper_imit_init,
279 };
280
281 EVP_MD *magma_omac(void)
282 {
283     return GOST_init_digest(&magma_mac_digest);
284 }
285
286 void magma_omac_destroy(void)
287 {
288     GOST_deinit_digest(&magma_mac_digest);
289 }
290
291 EVP_MD *grasshopper_omac(void)
292 {
293     return GOST_init_digest(&grasshopper_mac_digest);
294 }
295
296 void grasshopper_omac_destroy(void)
297 {
298     GOST_deinit_digest(&grasshopper_mac_digest);
299 }