]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_omac_acpkm.c
Get rid of EVP_MD_CTRL_MAC_LEN
[openssl-gost/engine.git] / gost_omac_acpkm.c
1 /*
2  * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
3  * Copyright (c) 2010 The OpenSSL Project.  All rights reserved.
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 #include "gost_grasshopper_defines.h"
17 #include "gost_grasshopper_cipher.h"
18
19 #define ACPKM_T_MAX (GRASSHOPPER_KEY_SIZE + GRASSHOPPER_BLOCK_SIZE)
20 /*
21  * CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
22  */
23 struct CMAC_ACPKM_CTX_st {
24     /* Cipher context to use */
25     EVP_CIPHER_CTX *cctx;
26     /* CTR-ACPKM cipher */
27     EVP_CIPHER_CTX *actx;
28     unsigned char km[ACPKM_T_MAX]; /* Key material */
29     /* Temporary block */
30     unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
31     /* Last (possibly partial) block */
32     unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
33     /* Number of bytes in last block: -1 means context not initialised */
34     int nlast_block;
35     unsigned int section_size; /* N */
36     unsigned int num; /* processed bytes until section_size */
37 };
38 typedef struct CMAC_ACPKM_CTX_st CMAC_ACPKM_CTX;
39
40 static unsigned char zero_iv[ACPKM_T_MAX];
41
42 /* Make temporary keys K1 and K2 */
43
44 static void make_kn(unsigned char *k1, unsigned char *l, int bl)
45 {
46     int i;
47     /* Shift block to left, including carry */
48     for (i = 0; i < bl; i++) {
49         k1[i] = l[i] << 1;
50         if (i < bl - 1 && l[i + 1] & 0x80)
51             k1[i] |= 1;
52     }
53     /* If MSB set fixup with R */
54     if (l[0] & 0x80)
55         k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b;
56 }
57
58 static CMAC_ACPKM_CTX *CMAC_ACPKM_CTX_new(void)
59 {
60     CMAC_ACPKM_CTX *ctx;
61     ctx = OPENSSL_malloc(sizeof(CMAC_ACPKM_CTX));
62     if (!ctx)
63         return NULL;
64     ctx->cctx = EVP_CIPHER_CTX_new();
65     if (ctx->cctx == NULL) {
66         OPENSSL_free(ctx);
67         return NULL;
68     }
69     ctx->actx = EVP_CIPHER_CTX_new();
70     if (ctx->actx == NULL) {
71         OPENSSL_free(ctx);
72         return NULL;
73     }
74     ctx->nlast_block = -1;
75     ctx->num = 0;
76     ctx->section_size = 4096; /* recommended value for Kuznyechik */
77     return ctx;
78 }
79
80 static void CMAC_ACPKM_CTX_cleanup(CMAC_ACPKM_CTX *ctx)
81 {
82     EVP_CIPHER_CTX_cleanup(ctx->cctx);
83     EVP_CIPHER_CTX_cleanup(ctx->actx);
84     OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
85     OPENSSL_cleanse(ctx->km, ACPKM_T_MAX);
86     OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
87     ctx->nlast_block = -1;
88 }
89
90 static void CMAC_ACPKM_CTX_free(CMAC_ACPKM_CTX *ctx)
91 {
92     if (!ctx)
93         return;
94     CMAC_ACPKM_CTX_cleanup(ctx);
95     EVP_CIPHER_CTX_free(ctx->cctx);
96     EVP_CIPHER_CTX_free(ctx->actx);
97     OPENSSL_free(ctx);
98 }
99
100 int CMAC_ACPKM_CTX_copy(CMAC_ACPKM_CTX *out, const CMAC_ACPKM_CTX *in)
101 {
102     int bl;
103     if (in->nlast_block == -1)
104         return 0;
105     if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
106         return 0;
107     if (!EVP_CIPHER_CTX_copy(out->actx, in->actx))
108         return 0;
109     bl = EVP_CIPHER_CTX_block_size(in->cctx);
110     memcpy(out->km, in->km, ACPKM_T_MAX);
111     memcpy(out->tbl, in->tbl, bl);
112     memcpy(out->last_block, in->last_block, bl);
113     out->nlast_block = in->nlast_block;
114     out->section_size = in->section_size;
115     out->num = in->num;
116     return 1;
117 }
118
119 static int CMAC_ACPKM_Init(CMAC_ACPKM_CTX *ctx, const void *key, size_t keylen,
120                            const EVP_CIPHER *cipher, ENGINE *impl)
121 {
122     /* All zeros means restart */
123     if (!key && !cipher && !impl && keylen == 0) {
124         /* Not initialised */
125         if (ctx->nlast_block == -1)
126             return 0;
127         if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
128             return 0;
129         memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
130         ctx->nlast_block = 0;
131         /* No restart for ACPKM */
132         return 1;
133     }
134     /* Initialise context */
135     if (cipher) {
136         const EVP_CIPHER *acpkm;
137
138         if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
139             return 0;
140         switch (EVP_CIPHER_nid(cipher)) {
141             case NID_grasshopper_cbc:
142                 acpkm = cipher_gost_grasshopper_ctracpkm();
143                 break;
144             default:
145                 return 0;
146         }
147         if (!EVP_EncryptInit_ex(ctx->actx, acpkm, impl, NULL, NULL))
148             return 0;
149     }
150     /* Non-NULL key means initialisation is complete */
151     if (key) {
152         unsigned char acpkm_iv[EVP_MAX_BLOCK_LENGTH];
153
154         /* Initialize CTR for ACPKM-Master */
155         if (!EVP_CIPHER_CTX_cipher(ctx->actx))
156             return 0;
157         /* block size of ACPKM cipher could be 1, but,
158          * cbc cipher is same with correct block_size */
159         const int block_size = EVP_CIPHER_CTX_block_size(ctx->cctx);
160         /* Wide IV = 1^{n/2} || 0,
161          * where a^r denotes the string that consists of r 'a' bits */
162         memset(acpkm_iv, 0xff, block_size / 2);
163         memset(acpkm_iv + block_size / 2, 0, block_size / 2);
164         if (!EVP_EncryptInit_ex(ctx->actx, NULL, NULL, key, acpkm_iv))
165             return 0;
166         /* EVP_CIPHER key_len may be different from EVP_CIPHER_CTX key_len */
167         int key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
168
169         /* Generate first key material (K^1 || K^1_1) */
170         if (!EVP_Cipher(ctx->actx, ctx->km, zero_iv, key_len + block_size))
171             return 0;
172
173         /* Initialize cbc for CMAC */
174         if (!EVP_CIPHER_CTX_cipher(ctx->cctx) ||
175             !EVP_CIPHER_CTX_set_key_length(ctx->cctx, key_len))
176             return 0;
177         /* set CBC key to K^1 */
178         if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km, zero_iv))
179             return 0;
180         ctx->nlast_block = 0;
181     }
182     return 1;
183 }
184
185 /* Encrypt zeros with master key
186  * to generate T*-sized key material */
187 static int CMAC_ACPKM_Master(CMAC_ACPKM_CTX *ctx)
188 {
189     return EVP_Cipher(ctx->actx, ctx->km, zero_iv,
190         EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx)) +
191         EVP_CIPHER_CTX_block_size(ctx->cctx));
192 }
193
194 static int CMAC_ACPKM_Mesh(CMAC_ACPKM_CTX *ctx)
195 {
196     if (ctx->num < ctx->section_size)
197         return 1;
198     ctx->num = 0;
199     if (!CMAC_ACPKM_Master(ctx))
200         return 0;
201     /* Restart cbc with new key */
202     if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km,
203             EVP_CIPHER_CTX_iv(ctx->cctx)))
204         return 0;
205     return 1;
206 }
207
208 static int CMAC_ACPKM_Update(CMAC_ACPKM_CTX *ctx, const void *in, size_t dlen)
209 {
210     const unsigned char *data = in;
211     size_t bl;
212     if (ctx->nlast_block == -1)
213         return 0;
214     if (dlen == 0)
215         return 1;
216     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
217     /* Copy into partial block if we need to */
218     if (ctx->nlast_block > 0) {
219         size_t nleft;
220         nleft = bl - ctx->nlast_block;
221         if (dlen < nleft)
222             nleft = dlen;
223         memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
224         dlen -= nleft;
225         ctx->nlast_block += nleft;
226         /* If no more to process return */
227         if (dlen == 0)
228             return 1;
229         data += nleft;
230         /* Else not final block so encrypt it */
231         if (!CMAC_ACPKM_Mesh(ctx))
232             return 0;
233         if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
234             return 0;
235         ctx->num += bl;
236     }
237     /* Encrypt all but one of the complete blocks left */
238     while (dlen > bl) {
239         if (!CMAC_ACPKM_Mesh(ctx))
240             return 0;
241         if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
242             return 0;
243         dlen -= bl;
244         data += bl;
245         ctx->num += bl;
246     }
247     /* Copy any data left to last block buffer */
248     memcpy(ctx->last_block, data, dlen);
249     ctx->nlast_block = dlen;
250     return 1;
251
252 }
253
254 static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
255                             size_t *poutlen)
256 {
257     int i, bl, lb;
258     if (ctx->nlast_block == -1)
259         return 0;
260     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
261     *poutlen = (size_t) bl;
262     if (!out)
263         return 1;
264     lb = ctx->nlast_block;
265
266     if (!CMAC_ACPKM_Mesh(ctx))
267         return 0;
268     int key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
269     /* Keys k1 and k2 */
270     unsigned char *k1 = ctx->km + key_len;
271     unsigned char k2[EVP_MAX_BLOCK_LENGTH];
272     make_kn(k2, ctx->km + key_len, bl);
273
274     /* Is last block complete? */
275     if (lb == bl) {
276         for (i = 0; i < bl; i++)
277             out[i] = ctx->last_block[i] ^ k1[i];
278     } else {
279         ctx->last_block[lb] = 0x80;
280         if (bl - lb > 1)
281             memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
282         for (i = 0; i < bl; i++)
283             out[i] = ctx->last_block[i] ^ k2[i];
284     }
285     OPENSSL_cleanse(k1, bl);
286     OPENSSL_cleanse(k2, bl);
287     OPENSSL_cleanse(ctx->km, ACPKM_T_MAX);
288     if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
289         OPENSSL_cleanse(out, bl);
290         return 0;
291     }
292     return 1;
293 }
294
295 /*
296  * End of CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
297  */
298
299 typedef struct omac_acpkm_ctx {
300     CMAC_ACPKM_CTX *cmac_ctx;
301     size_t dgst_size;
302     int cipher_nid;
303     int key_set;
304 } OMAC_ACPKM_CTX;
305
306 #define MAX_GOST_OMAC_ACPKM_SIZE 16
307
308 static int omac_acpkm_init(EVP_MD_CTX *ctx, int cipher_nid)
309 {
310     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
311     memset(c, 0, sizeof(OMAC_ACPKM_CTX));
312     c->cipher_nid = cipher_nid;
313     c->key_set = 0;
314
315     switch (cipher_nid) {
316     case NID_grasshopper_cbc:
317         c->dgst_size = 16;
318         break;
319     }
320
321     return 1;
322 }
323
324 static int grasshopper_omac_acpkm_init(EVP_MD_CTX *ctx)
325 {
326     return omac_acpkm_init(ctx, NID_grasshopper_cbc);
327 }
328
329 static int omac_acpkm_imit_update(EVP_MD_CTX *ctx, const void *data,
330                                   size_t count)
331 {
332     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
333     if (!c->key_set) {
334         GOSTerr(GOST_F_OMAC_ACPKM_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
335         return 0;
336     }
337
338     return CMAC_ACPKM_Update(c->cmac_ctx, data, count);
339 }
340
341 int omac_acpkm_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
342 {
343     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
344     unsigned char mac[MAX_GOST_OMAC_ACPKM_SIZE];
345     size_t mac_size = sizeof(mac);
346
347     if (!c->key_set) {
348         GOSTerr(GOST_F_OMAC_ACPKM_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
349         return 0;
350     }
351
352     CMAC_ACPKM_Final(c->cmac_ctx, mac, &mac_size);
353
354     memcpy(md, mac, c->dgst_size);
355     return 1;
356 }
357
358 int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
359 {
360     OMAC_ACPKM_CTX *c_to = EVP_MD_CTX_md_data(to);
361     const OMAC_ACPKM_CTX *c_from = EVP_MD_CTX_md_data(from);
362
363     if (c_from && c_to) {
364         c_to->dgst_size = c_from->dgst_size;
365         c_to->cipher_nid = c_from->cipher_nid;
366         c_to->key_set = c_from->key_set;
367     } else {
368         return 0;
369     }
370     if (!c_from->cmac_ctx) {
371         if (c_to->cmac_ctx) {
372             CMAC_ACPKM_CTX_free(c_to->cmac_ctx);
373             c_to->cmac_ctx = NULL;
374         }
375         return 1;
376     }
377     if (c_to->cmac_ctx == c_from->cmac_ctx) {
378         c_to->cmac_ctx = CMAC_ACPKM_CTX_new();
379     }
380     return CMAC_ACPKM_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
381 }
382
383 /* Clean up imit ctx */
384 int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
385 {
386     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
387
388     if (c) {
389         CMAC_ACPKM_CTX_free(c->cmac_ctx);
390         memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_ACPKM_CTX));
391     }
392     return 1;
393 }
394
395 static int omac_acpkm_key(OMAC_ACPKM_CTX *c, const EVP_CIPHER *cipher,
396                           const unsigned char *key, size_t key_size)
397 {
398     int ret = 0;
399
400     c->cmac_ctx = CMAC_ACPKM_CTX_new();
401     if (c->cmac_ctx == NULL) {
402         GOSTerr(GOST_F_OMAC_ACPKM_KEY, ERR_R_MALLOC_FAILURE);
403         return 0;
404     }
405
406     ret = CMAC_ACPKM_Init(c->cmac_ctx, key, key_size, cipher, NULL);
407     if (ret > 0) {
408         c->key_set = 1;
409     }
410     return 1;
411 }
412
413 int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
414 {
415     switch (type) {
416     case EVP_MD_CTRL_KEY_LEN:
417         *((unsigned int *)(ptr)) = 32;
418         return 1;
419     case EVP_MD_CTRL_SET_KEY:
420         {
421             OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
422             const EVP_MD *md = EVP_MD_CTX_md(ctx);
423             const EVP_CIPHER *cipher = NULL;
424
425             if (c->cipher_nid == NID_undef) {
426                 switch (EVP_MD_nid(md)) {
427                 case NID_grasshopper_mac:
428                 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
429                     c->cipher_nid = NID_grasshopper_cbc;
430                     break;
431                 }
432             }
433             cipher = EVP_get_cipherbynid(c->cipher_nid);
434             if (cipher == NULL) {
435                 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
436             }
437             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
438                 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
439                 return 0;
440             }
441             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
442             if (c->key_set) {
443                 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_BAD_ORDER);
444                 return 0;
445             }
446             if (arg == 0) {
447                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
448                 return omac_acpkm_key(c, cipher, key->key, 32);
449             } else if (arg == 32) {
450                 return omac_acpkm_key(c, cipher, ptr, 32);
451             }
452             GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
453             return 0;
454         }
455     case EVP_CTRL_KEY_MESH:
456         {
457             OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
458             if (!arg || (arg % EVP_MD_block_size(EVP_MD_CTX_md(ctx))))
459                 return -1;
460             c->cmac_ctx->section_size = arg;
461             if (ptr && *(int *)ptr) {
462                 /* Set parameter T */
463                 if (!EVP_CIPHER_CTX_ctrl(c->cmac_ctx->actx, EVP_CTRL_KEY_MESH, *(int *)ptr, NULL))
464                     return 0;
465             }
466             return 1;
467         }
468     case EVP_MD_CTRL_XOF_LEN:   /* Supported in OpenSSL */
469         {
470             OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
471             switch (c->cipher_nid) {
472             case NID_grasshopper_cbc:
473                 if (arg < 1 || arg > 16) {
474                     GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
475                     return 0;
476                 }
477                 c->dgst_size = arg;
478                 break;
479             case NID_magma_cbc:
480                 if (arg < 1 || arg > 8) {
481                     GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
482                     return 0;
483                 }
484                 c->dgst_size = arg;
485                 break;
486             default:
487                 return 0;
488             }
489             return 1;
490         }
491
492     default:
493         return 0;
494     }
495 }
496
497 static EVP_MD *_hidden_grasshopper_omac_acpkm_md = NULL;
498
499 EVP_MD *grasshopper_omac_acpkm(void)
500 {
501     if (_hidden_grasshopper_omac_acpkm_md == NULL) {
502         EVP_MD *md;
503
504         if ((md =
505              EVP_MD_meth_new(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
506               NID_undef)) == NULL
507             || !EVP_MD_meth_set_result_size(md, MAX_GOST_OMAC_ACPKM_SIZE)
508             || !EVP_MD_meth_set_input_blocksize(md, GRASSHOPPER_BLOCK_SIZE)
509             || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_ACPKM_CTX))
510             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
511             || !EVP_MD_meth_set_init(md, grasshopper_omac_acpkm_init)
512             || !EVP_MD_meth_set_update(md, omac_acpkm_imit_update)
513             || !EVP_MD_meth_set_final(md, omac_acpkm_imit_final)
514             || !EVP_MD_meth_set_copy(md, omac_acpkm_imit_copy)
515             || !EVP_MD_meth_set_cleanup(md, omac_acpkm_imit_cleanup)
516             || !EVP_MD_meth_set_ctrl(md, omac_acpkm_imit_ctrl)) {
517             EVP_MD_meth_free(md);
518             md = NULL;
519         }
520         _hidden_grasshopper_omac_acpkm_md = md;
521     }
522     return _hidden_grasshopper_omac_acpkm_md;
523 }
524
525 void grasshopper_omac_acpkm_destroy(void)
526 {
527     EVP_MD_meth_free(_hidden_grasshopper_omac_acpkm_md);
528     _hidden_grasshopper_omac_acpkm_md = NULL;
529 }