]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_omac_acpkm.c
Merge pull request #170 from hackomatic/wip
[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         int block_size, key_len;
154
155         /* Initialize CTR for ACPKM-Master */
156         if (!EVP_CIPHER_CTX_cipher(ctx->actx))
157             return 0;
158         /* block size of ACPKM cipher could be 1, but,
159          * cbc cipher is same with correct block_size */
160         block_size = EVP_CIPHER_CTX_block_size(ctx->cctx);
161         /* Wide IV = 1^{n/2} || 0,
162          * where a^r denotes the string that consists of r 'a' bits */
163         memset(acpkm_iv, 0xff, block_size / 2);
164         memset(acpkm_iv + block_size / 2, 0, block_size / 2);
165         if (!EVP_EncryptInit_ex(ctx->actx, NULL, NULL, key, acpkm_iv))
166             return 0;
167         /* EVP_CIPHER key_len may be different from EVP_CIPHER_CTX key_len */
168         key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
169
170         /* Generate first key material (K^1 || K^1_1) */
171         if (!EVP_Cipher(ctx->actx, ctx->km, zero_iv, key_len + block_size))
172             return 0;
173
174         /* Initialize cbc for CMAC */
175         if (!EVP_CIPHER_CTX_cipher(ctx->cctx) ||
176             !EVP_CIPHER_CTX_set_key_length(ctx->cctx, key_len))
177             return 0;
178         /* set CBC key to K^1 */
179         if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km, zero_iv))
180             return 0;
181         ctx->nlast_block = 0;
182     }
183     return 1;
184 }
185
186 /* Encrypt zeros with master key
187  * to generate T*-sized key material */
188 static int CMAC_ACPKM_Master(CMAC_ACPKM_CTX *ctx)
189 {
190     return EVP_Cipher(ctx->actx, ctx->km, zero_iv,
191         EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx)) +
192         EVP_CIPHER_CTX_block_size(ctx->cctx));
193 }
194
195 static int CMAC_ACPKM_Mesh(CMAC_ACPKM_CTX *ctx)
196 {
197     if (ctx->num < ctx->section_size)
198         return 1;
199     ctx->num = 0;
200     if (!CMAC_ACPKM_Master(ctx))
201         return 0;
202     /* Restart cbc with new key */
203     if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km,
204             EVP_CIPHER_CTX_iv(ctx->cctx)))
205         return 0;
206     return 1;
207 }
208
209 static int CMAC_ACPKM_Update(CMAC_ACPKM_CTX *ctx, const void *in, size_t dlen)
210 {
211     const unsigned char *data = in;
212     size_t bl;
213     if (ctx->nlast_block == -1)
214         return 0;
215     if (dlen == 0)
216         return 1;
217     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
218     /* Copy into partial block if we need to */
219     if (ctx->nlast_block > 0) {
220         size_t nleft;
221         nleft = bl - ctx->nlast_block;
222         if (dlen < nleft)
223             nleft = dlen;
224         memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
225         dlen -= nleft;
226         ctx->nlast_block += nleft;
227         /* If no more to process return */
228         if (dlen == 0)
229             return 1;
230         data += nleft;
231         /* Else not final block so encrypt it */
232         if (!CMAC_ACPKM_Mesh(ctx))
233             return 0;
234         if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
235             return 0;
236         ctx->num += bl;
237     }
238     /* Encrypt all but one of the complete blocks left */
239     while (dlen > bl) {
240         if (!CMAC_ACPKM_Mesh(ctx))
241             return 0;
242         if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
243             return 0;
244         dlen -= bl;
245         data += bl;
246         ctx->num += bl;
247     }
248     /* Copy any data left to last block buffer */
249     memcpy(ctx->last_block, data, dlen);
250     ctx->nlast_block = dlen;
251     return 1;
252
253 }
254
255 static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
256                             size_t *poutlen)
257 {
258     int i, bl, lb, key_len;
259     unsigned char *k1, k2[EVP_MAX_BLOCK_LENGTH];
260     if (ctx->nlast_block == -1)
261         return 0;
262     bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
263     *poutlen = (size_t) bl;
264     if (!out)
265         return 1;
266     lb = ctx->nlast_block;
267
268     if (!CMAC_ACPKM_Mesh(ctx))
269         return 0;
270     key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
271     /* Keys k1 and k2 */
272     k1 = ctx->km + key_len;
273     make_kn(k2, ctx->km + key_len, bl);
274
275     /* Is last block complete? */
276     if (lb == bl) {
277         for (i = 0; i < bl; i++)
278             out[i] = ctx->last_block[i] ^ k1[i];
279     } else {
280         ctx->last_block[lb] = 0x80;
281         if (bl - lb > 1)
282             memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
283         for (i = 0; i < bl; i++)
284             out[i] = ctx->last_block[i] ^ k2[i];
285     }
286     OPENSSL_cleanse(k1, bl);
287     OPENSSL_cleanse(k2, bl);
288     OPENSSL_cleanse(ctx->km, ACPKM_T_MAX);
289     if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
290         OPENSSL_cleanse(out, bl);
291         return 0;
292     }
293     return 1;
294 }
295
296 /*
297  * End of CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
298  */
299
300 typedef struct omac_acpkm_ctx {
301     CMAC_ACPKM_CTX *cmac_ctx;
302     size_t dgst_size;
303     int cipher_nid;
304     int key_set;
305 } OMAC_ACPKM_CTX;
306
307 #define MAX_GOST_OMAC_ACPKM_SIZE 16
308
309 static int omac_acpkm_init(EVP_MD_CTX *ctx, int cipher_nid)
310 {
311     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
312     memset(c, 0, sizeof(OMAC_ACPKM_CTX));
313     c->cipher_nid = cipher_nid;
314     c->key_set = 0;
315
316     switch (cipher_nid) {
317     case NID_grasshopper_cbc:
318         c->dgst_size = 16;
319         break;
320     }
321
322     return 1;
323 }
324
325 static int grasshopper_omac_acpkm_init(EVP_MD_CTX *ctx)
326 {
327     return omac_acpkm_init(ctx, NID_grasshopper_cbc);
328 }
329
330 static int omac_acpkm_imit_update(EVP_MD_CTX *ctx, const void *data,
331                                   size_t count)
332 {
333     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
334     if (!c->key_set) {
335         GOSTerr(GOST_F_OMAC_ACPKM_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
336         return 0;
337     }
338
339     return CMAC_ACPKM_Update(c->cmac_ctx, data, count);
340 }
341
342 int omac_acpkm_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
343 {
344     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
345     unsigned char mac[MAX_GOST_OMAC_ACPKM_SIZE];
346     size_t mac_size = sizeof(mac);
347
348     if (!c->key_set) {
349         GOSTerr(GOST_F_OMAC_ACPKM_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
350         return 0;
351     }
352
353     CMAC_ACPKM_Final(c->cmac_ctx, mac, &mac_size);
354
355     memcpy(md, mac, c->dgst_size);
356     return 1;
357 }
358
359 int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
360 {
361     OMAC_ACPKM_CTX *c_to = EVP_MD_CTX_md_data(to);
362     const OMAC_ACPKM_CTX *c_from = EVP_MD_CTX_md_data(from);
363
364     if (c_from && c_to) {
365         c_to->dgst_size = c_from->dgst_size;
366         c_to->cipher_nid = c_from->cipher_nid;
367         c_to->key_set = c_from->key_set;
368     } else {
369         return 0;
370     }
371     if (!c_from->cmac_ctx) {
372         if (c_to->cmac_ctx) {
373             CMAC_ACPKM_CTX_free(c_to->cmac_ctx);
374             c_to->cmac_ctx = NULL;
375         }
376         return 1;
377     }
378     if (c_to->cmac_ctx == c_from->cmac_ctx) {
379         c_to->cmac_ctx = CMAC_ACPKM_CTX_new();
380     }
381     return CMAC_ACPKM_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
382 }
383
384 /* Clean up imit ctx */
385 int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
386 {
387     OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
388
389     if (c) {
390         CMAC_ACPKM_CTX_free(c->cmac_ctx);
391         memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_ACPKM_CTX));
392     }
393     return 1;
394 }
395
396 static int omac_acpkm_key(OMAC_ACPKM_CTX *c, const EVP_CIPHER *cipher,
397                           const unsigned char *key, size_t key_size)
398 {
399     int ret = 0;
400
401     c->cmac_ctx = CMAC_ACPKM_CTX_new();
402     if (c->cmac_ctx == NULL) {
403         GOSTerr(GOST_F_OMAC_ACPKM_KEY, ERR_R_MALLOC_FAILURE);
404         return 0;
405     }
406
407     ret = CMAC_ACPKM_Init(c->cmac_ctx, key, key_size, cipher, NULL);
408     if (ret > 0) {
409         c->key_set = 1;
410     }
411     return 1;
412 }
413
414 int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
415 {
416     switch (type) {
417     case EVP_MD_CTRL_KEY_LEN:
418         *((unsigned int *)(ptr)) = 32;
419         return 1;
420     case EVP_MD_CTRL_SET_KEY:
421         {
422             OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
423             const EVP_MD *md = EVP_MD_CTX_md(ctx);
424             const EVP_CIPHER *cipher = NULL;
425
426             if (c->cipher_nid == NID_undef) {
427                 switch (EVP_MD_nid(md)) {
428                 case NID_grasshopper_mac:
429                 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
430                     c->cipher_nid = NID_grasshopper_cbc;
431                     break;
432                 }
433             }
434             cipher = EVP_get_cipherbynid(c->cipher_nid);
435             if (cipher == NULL) {
436                 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
437             }
438             if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
439                 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
440                 return 0;
441             }
442             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
443             if (c->key_set) {
444                 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_BAD_ORDER);
445                 return 0;
446             }
447             if (arg == 0) {
448                 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
449                 return omac_acpkm_key(c, cipher, key->key, 32);
450             } else if (arg == 32) {
451                 return omac_acpkm_key(c, cipher, ptr, 32);
452             }
453             GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
454             return 0;
455         }
456     case EVP_CTRL_KEY_MESH:
457         {
458             OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
459             if (!arg || (arg % EVP_MD_block_size(EVP_MD_CTX_md(ctx))))
460                 return -1;
461             c->cmac_ctx->section_size = arg;
462             if (ptr && *(int *)ptr) {
463                 /* Set parameter T */
464                 if (!EVP_CIPHER_CTX_ctrl(c->cmac_ctx->actx, EVP_CTRL_KEY_MESH, *(int *)ptr, NULL))
465                     return 0;
466             }
467             return 1;
468         }
469     case EVP_MD_CTRL_XOF_LEN:   /* Supported in OpenSSL */
470         {
471             OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
472             switch (c->cipher_nid) {
473             case NID_grasshopper_cbc:
474                 if (arg < 1 || arg > 16) {
475                     GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
476                     return 0;
477                 }
478                 c->dgst_size = arg;
479                 break;
480             case NID_magma_cbc:
481                 if (arg < 1 || arg > 8) {
482                     GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
483                     return 0;
484                 }
485                 c->dgst_size = arg;
486                 break;
487             default:
488                 return 0;
489             }
490             return 1;
491         }
492
493     default:
494         return 0;
495     }
496 }
497
498 static EVP_MD *_hidden_grasshopper_omac_acpkm_md = NULL;
499
500 EVP_MD *grasshopper_omac_acpkm(void)
501 {
502     if (_hidden_grasshopper_omac_acpkm_md == NULL) {
503         EVP_MD *md;
504
505         if ((md =
506              EVP_MD_meth_new(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
507               NID_undef)) == NULL
508             || !EVP_MD_meth_set_result_size(md, MAX_GOST_OMAC_ACPKM_SIZE)
509             || !EVP_MD_meth_set_input_blocksize(md, GRASSHOPPER_BLOCK_SIZE)
510             || !EVP_MD_meth_set_app_datasize(md, sizeof(OMAC_ACPKM_CTX))
511             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_XOF)
512             || !EVP_MD_meth_set_init(md, grasshopper_omac_acpkm_init)
513             || !EVP_MD_meth_set_update(md, omac_acpkm_imit_update)
514             || !EVP_MD_meth_set_final(md, omac_acpkm_imit_final)
515             || !EVP_MD_meth_set_copy(md, omac_acpkm_imit_copy)
516             || !EVP_MD_meth_set_cleanup(md, omac_acpkm_imit_cleanup)
517             || !EVP_MD_meth_set_ctrl(md, omac_acpkm_imit_ctrl)) {
518             EVP_MD_meth_free(md);
519             md = NULL;
520         }
521         _hidden_grasshopper_omac_acpkm_md = md;
522     }
523     return _hidden_grasshopper_omac_acpkm_md;
524 }
525
526 void grasshopper_omac_acpkm_destroy(void)
527 {
528     EVP_MD_meth_free(_hidden_grasshopper_omac_acpkm_md);
529     _hidden_grasshopper_omac_acpkm_md = NULL;
530 }