]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_omac.c
Fix possible overflow of digest result writing
[openssl-gost/engine.git] / gost_omac.c
1 #include <string.h>
2 #include <openssl/cmac.h>
3 #include <openssl/conf.h>
4 #include <openssl/err.h>
5 #include <openssl/evp.h>
6
7 #include "e_gost_err.h"
8 #include "gost_lcl.h"
9
10 #define min(a,b) (((a) < (b)) ? (a) : (b))
11
12 typedef struct omac_ctx {
13         CMAC_CTX *cmac_ctx;
14         size_t   dgst_size;
15         int      cipher_nid;
16         int      key_set;
17 } OMAC_CTX;
18
19 #define MAX_GOST_OMAC_SIZE 16
20
21 static int omac_init(EVP_MD_CTX *ctx, int cipher_nid)
22 {
23     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
24                 memset(c, 0, sizeof(OMAC_CTX));
25                 c->cipher_nid = cipher_nid;
26                 c->key_set    = 0;
27
28                 switch(cipher_nid) {
29                         case NID_magma_cbc:
30                                 c->dgst_size = 4;
31                         break;
32
33                         case NID_grasshopper_cbc:
34                                 c->dgst_size = 8;
35                         break;
36                 }
37
38     return 1;
39 }
40
41 static int magma_imit_init(EVP_MD_CTX *ctx)
42 {
43     return omac_init(ctx, NID_magma_cbc);
44 }
45
46 static int grasshopper_imit_init(EVP_MD_CTX *ctx)
47 {
48     return omac_init(ctx, NID_grasshopper_cbc);
49 }
50
51 static int omac_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
52 {
53     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
54                 if (!c->key_set)
55                 {
56         GOSTerr(GOST_F_OMAC_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
57         return 0;
58                 }
59
60                 return CMAC_Update(c->cmac_ctx, data, count);
61 }
62
63 int omac_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
64 {
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);
68
69     if (!c->key_set) {
70         GOSTerr(GOST_F_OMAC_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
71         return 0;
72     }
73
74                 CMAC_Final(c->cmac_ctx, mac, &mac_size);
75
76     int md_size = EVP_MD_meth_get_result_size(EVP_MD_CTX_md(ctx));
77     memcpy(md, mac, min(md_size, c->dgst_size));
78     return 1;
79 }
80
81 int omac_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
82 {
83     OMAC_CTX *c_to = EVP_MD_CTX_md_data(to);
84                 const OMAC_CTX *c_from = EVP_MD_CTX_md_data(from);
85
86     if (c_from && c_to) {
87                         c_to->dgst_size  = c_from->dgst_size;
88                         c_to->cipher_nid = c_from->cipher_nid;
89                         c_to->key_set    = c_from->key_set;
90     }
91                 else
92                 {
93                         return 0;
94                 }
95                 if (c_to->cmac_ctx == c_from->cmac_ctx)
96                 {
97                     c_to->cmac_ctx = CMAC_CTX_new();
98                 }
99                 return CMAC_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
100 }
101
102 /* Clean up imit ctx */
103 int omac_imit_cleanup(EVP_MD_CTX *ctx)
104 {
105     OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
106
107                 if (c)
108                 {
109                         CMAC_CTX_free(c->cmac_ctx);
110             memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_CTX));
111                 }
112     return 1;
113 }
114
115 static int omac_key(OMAC_CTX *c, const EVP_CIPHER *cipher, const unsigned char *key, size_t key_size)
116 {
117         int ret = 0;
118
119         c->cmac_ctx = CMAC_CTX_new();
120         if (c->cmac_ctx == NULL)
121         {
122                 GOSTerr(GOST_F_OMAC_KEY, ERR_R_MALLOC_FAILURE);
123                 return 0;
124         }
125
126         ret = CMAC_Init(c->cmac_ctx, key, key_size, cipher, NULL);
127         if (ret > 0)
128         {
129                 c->key_set = 1;
130         }
131         return 1;
132 }                                               
133
134 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
135 {
136     switch (type) {
137     case EVP_MD_CTRL_KEY_LEN:
138         *((unsigned int *)(ptr)) = 32;
139         return 1;
140     case EVP_MD_CTRL_SET_KEY:
141                                 {
142                                         OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
143                                         const EVP_MD *md = EVP_MD_CTX_md(ctx);
144                                         const EVP_CIPHER *cipher = NULL;
145
146                                         if (c->cipher_nid == NID_undef)
147                                         {
148                                                 switch (EVP_MD_nid(md))
149                                                 {
150                                                         case NID_magma_mac:
151                                                                 c->cipher_nid = NID_magma_cbc;
152                                                                 break;
153
154                                                         case NID_grasshopper_mac:
155                                                                 c->cipher_nid = NID_grasshopper_cbc;
156                                                                 break;
157                                                 }
158                                         }
159                                         cipher = EVP_get_cipherbynid(c->cipher_nid);
160
161                                         if (cipher == NULL)
162                                         {
163                                                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
164                                         }
165
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);
168                                                 return 0;
169                                         }
170                                         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
171
172                                         if (c->key_set)
173                                         {
174                                                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
175                                                 return 0;
176                                         }
177
178                                         if (arg == 0) {
179                                                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
180                                                 return omac_key(c, cipher, key->key, 32);
181
182                                         } else if (arg == 32) {
183                                                 return omac_key(c, cipher, ptr, 32);
184                                         }
185                                         GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
186                                         return 0;
187                                 }
188     case EVP_MD_CTRL_MAC_LEN:
189         {
190                                         OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
191                                         switch (c->cipher_nid)
192                                         {
193                                                 case NID_magma_cbc:
194             if (arg < 1 || arg > 8) {
195                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
196                 return 0;
197             }
198             c->dgst_size = arg;
199                                                 break;
200                                                 case NID_grasshopper_cbc:
201             if (arg < 1 || arg > 16) {
202                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
203                 return 0;
204             }
205             c->dgst_size = arg;
206                                                 break;
207                                                 default:
208                                                 return 0;
209                                         }
210           return 1;
211         }
212
213     default:
214         return 0;
215     }
216 }
217
218 static EVP_MD *_hidden_magma_mac_md = NULL;
219
220 EVP_MD *magma_omac(void)
221 {
222     if (_hidden_magma_mac_md == NULL) {
223         EVP_MD *md;
224
225         if ((md = EVP_MD_meth_new(NID_magma_mac, NID_undef)) == NULL
226             || !EVP_MD_meth_set_result_size(md, 4)
227             || !EVP_MD_meth_set_input_blocksize(md, 8)
228             || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
229             || !EVP_MD_meth_set_flags(md, 0)
230             || !EVP_MD_meth_set_init(md, magma_imit_init)
231             || !EVP_MD_meth_set_update(md, omac_imit_update)
232             || !EVP_MD_meth_set_final(md, omac_imit_final)
233             || !EVP_MD_meth_set_copy(md, omac_imit_copy)
234             || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
235             || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
236             EVP_MD_meth_free(md);
237             md = NULL;
238         }
239        _hidden_magma_mac_md = md;
240     }
241     return _hidden_magma_mac_md;
242 }
243
244 void magma_omac_destroy(void)
245 {
246     EVP_MD_meth_free(_hidden_magma_mac_md);
247     _hidden_magma_mac_md = NULL;
248 }
249
250 static EVP_MD *_hidden_grasshopper_mac_md = NULL;
251
252 EVP_MD *grasshopper_omac(void)
253 {
254     if (_hidden_grasshopper_mac_md == NULL) {
255         EVP_MD *md;
256
257         if ((md = EVP_MD_meth_new(NID_grasshopper_mac, NID_undef)) == NULL
258             || !EVP_MD_meth_set_result_size(md, 8)
259             || !EVP_MD_meth_set_input_blocksize(md, 8)
260             || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_CTX))
261             || !EVP_MD_meth_set_flags(md, 0)
262             || !EVP_MD_meth_set_init(md, grasshopper_imit_init)
263             || !EVP_MD_meth_set_update(md, omac_imit_update)
264             || !EVP_MD_meth_set_final(md, omac_imit_final)
265             || !EVP_MD_meth_set_copy(md, omac_imit_copy)
266             || !EVP_MD_meth_set_cleanup(md, omac_imit_cleanup)
267             || !EVP_MD_meth_set_ctrl(md, omac_imit_ctrl)) {
268             EVP_MD_meth_free(md);
269             md = NULL;
270         }
271        _hidden_grasshopper_mac_md = md;
272     }
273     return _hidden_grasshopper_mac_md;
274 }
275
276 void grasshopper_omac_destroy(void)
277 {
278     EVP_MD_meth_free(_hidden_grasshopper_mac_md);
279     _hidden_grasshopper_mac_md = NULL;
280 }
281