]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_omac.c
Add static to functions that not need to be exported
[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 static 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 static 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 static 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 /* Called directly by gost_kexp15() */
157 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
158 {
159     switch (type) {
160     case EVP_MD_CTRL_KEY_LEN:
161         *((unsigned int *)(ptr)) = 32;
162         return 1;
163     case EVP_MD_CTRL_SET_KEY:
164         {
165             OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
166             const EVP_MD *md = EVP_MD_CTX_md(ctx);
167             const EVP_CIPHER *cipher = NULL;
168             int ret = 0;
169
170             if (c->cipher_nid == NID_undef) {
171                 switch (EVP_MD_nid(md)) {
172                 case NID_magma_mac:
173                     c->cipher_nid = NID_magma_cbc;
174                     break;
175
176                 case NID_grasshopper_mac:
177                     c->cipher_nid = NID_grasshopper_cbc;
178                     break;
179                 }
180             }
181             cipher = EVP_get_cipherbynid(c->cipher_nid);
182
183             if (cipher == NULL) {
184                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
185             }
186
187             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
188                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
189                 return 0;
190             }
191             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
192
193             if (c->key_set) {
194                 GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
195                 return 0;
196             }
197
198             if (arg == 0) {
199                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
200                 ret = omac_key(c, cipher, key->key, 32);
201                 if (ret > 0)
202                     memcpy(c->key, key->key, 32);
203                 return ret;
204             } else if (arg == 32) {
205                 ret = omac_key(c, cipher, ptr, 32);
206                 if (ret > 0)
207                     memcpy(c->key, ptr, 32);
208                 return ret;
209             }
210             GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
211             return 0;
212         }
213     case EVP_MD_CTRL_XOF_LEN:   /* Supported in OpenSSL */
214         {
215             OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
216             switch (c->cipher_nid) {
217             case NID_magma_cbc:
218                 if (arg < 1 || arg > 8) {
219                     GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
220                     return 0;
221                 }
222                 c->dgst_size = arg;
223                 break;
224             case NID_grasshopper_cbc:
225                 if (arg < 1 || arg > 16) {
226                     GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
227                     return 0;
228                 }
229                 c->dgst_size = arg;
230                 break;
231             default:
232                 return 0;
233             }
234             return 1;
235         }
236 #ifdef EVP_MD_CTRL_TLSTREE
237     case EVP_MD_CTRL_TLSTREE:
238         {
239             OMAC_CTX *c = EVP_MD_CTX_md_data(ctx);
240             if (c->key_set) {
241                 unsigned char diversed_key[32];
242                 return gost_tlstree(c->cipher_nid, c->key, diversed_key,
243                                     (const unsigned char *)ptr) ?
244                     omac_key(c, EVP_get_cipherbynid(c->cipher_nid),
245                              diversed_key, 32) : 0;
246             }
247             GOSTerr(GOST_F_OMAC_IMIT_CTRL, GOST_R_BAD_ORDER);
248             return 0;
249         }
250         return 0;
251 #endif
252     default:
253         return 0;
254     }
255 }
256
257 static GOST_digest omac_template_digest = {
258     .input_blocksize = 8,
259     .app_datasize = sizeof(OMAC_CTX),
260     .flags = EVP_MD_FLAG_XOF,
261     .update = omac_imit_update,
262     .final = omac_imit_final,
263     .copy = omac_imit_copy,
264     .cleanup = omac_imit_cleanup,
265     .ctrl = omac_imit_ctrl,
266 };
267
268 GOST_digest magma_mac_digest = {
269     .nid = NID_magma_mac,
270     .template = &omac_template_digest,
271     .result_size = 8,
272     .init = magma_imit_init,
273 };
274
275 GOST_digest grasshopper_mac_digest = {
276     .nid = NID_grasshopper_mac,
277     .template = &omac_template_digest,
278     .result_size = 16,
279     .init = grasshopper_imit_init,
280 };