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