2 * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
5 * Contents licensed under the terms of the OpenSSL license
6 * See https://www.openssl.org/source/license.html for details
9 #include <openssl/cmac.h>
10 #include <openssl/conf.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
14 #include "e_gost_err.h"
16 #include "gost_grasshopper_defines.h"
17 #include "gost_grasshopper_cipher.h"
19 #define ACPKM_T_MAX (GRASSHOPPER_KEY_SIZE + GRASSHOPPER_BLOCK_SIZE)
21 * CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
23 struct CMAC_ACPKM_CTX_st {
24 /* Cipher context to use */
26 /* CTR-ACPKM cipher */
28 unsigned char km[ACPKM_T_MAX]; /* Key material */
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 */
35 unsigned int section_size; /* N */
36 unsigned int num; /* processed bytes until section_size */
38 typedef struct CMAC_ACPKM_CTX_st CMAC_ACPKM_CTX;
40 static unsigned char zero_iv[ACPKM_T_MAX];
42 /* Make temporary keys K1 and K2 */
44 static void make_kn(unsigned char *k1, unsigned char *l, int bl)
47 /* Shift block to left, including carry */
48 for (i = 0; i < bl; i++) {
50 if (i < bl - 1 && l[i + 1] & 0x80)
53 /* If MSB set fixup with R */
55 k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b;
58 static CMAC_ACPKM_CTX *CMAC_ACPKM_CTX_new(void)
61 ctx = OPENSSL_malloc(sizeof(CMAC_ACPKM_CTX));
64 ctx->cctx = EVP_CIPHER_CTX_new();
65 if (ctx->cctx == NULL) {
69 ctx->actx = EVP_CIPHER_CTX_new();
70 if (ctx->actx == NULL) {
74 ctx->nlast_block = -1;
76 ctx->section_size = 4096; /* recommended value for Kuznyechik */
80 static void CMAC_ACPKM_CTX_cleanup(CMAC_ACPKM_CTX *ctx)
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;
90 static void CMAC_ACPKM_CTX_free(CMAC_ACPKM_CTX *ctx)
94 CMAC_ACPKM_CTX_cleanup(ctx);
95 EVP_CIPHER_CTX_free(ctx->cctx);
96 EVP_CIPHER_CTX_free(ctx->actx);
100 int CMAC_ACPKM_CTX_copy(CMAC_ACPKM_CTX *out, const CMAC_ACPKM_CTX *in)
103 if (in->nlast_block == -1)
105 if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
107 if (!EVP_CIPHER_CTX_copy(out->actx, in->actx))
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;
119 static int CMAC_ACPKM_Init(CMAC_ACPKM_CTX *ctx, const void *key, size_t keylen,
120 const EVP_CIPHER *cipher, ENGINE *impl)
122 /* All zeros means restart */
123 if (!key && !cipher && !impl && keylen == 0) {
124 /* Not initialised */
125 if (ctx->nlast_block == -1)
127 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
129 memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
130 ctx->nlast_block = 0;
131 /* No restart for ACPKM */
134 /* Initialise context */
136 const EVP_CIPHER *acpkm;
138 if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
140 switch (EVP_CIPHER_nid(cipher)) {
141 case NID_grasshopper_cbc:
142 acpkm = cipher_gost_grasshopper_ctracpkm();
147 if (!EVP_EncryptInit_ex(ctx->actx, acpkm, impl, NULL, NULL))
150 /* Non-NULL key means initialisation is complete */
152 unsigned char acpkm_iv[EVP_MAX_BLOCK_LENGTH];
154 /* Initialize CTR for ACPKM-Master */
155 if (!EVP_CIPHER_CTX_cipher(ctx->actx))
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))
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));
169 /* Generate first key material (K^1 || K^1_1) */
170 if (!EVP_Cipher(ctx->actx, ctx->km, zero_iv, key_len + block_size))
173 /* Initialize cbc for CMAC */
174 if (!EVP_CIPHER_CTX_cipher(ctx->cctx) ||
175 !EVP_CIPHER_CTX_set_key_length(ctx->cctx, key_len))
177 /* set CBC key to K^1 */
178 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km, zero_iv))
180 ctx->nlast_block = 0;
185 /* Encrypt zeros with master key
186 * to generate T*-sized key material */
187 static int CMAC_ACPKM_Master(CMAC_ACPKM_CTX *ctx)
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));
194 static int CMAC_ACPKM_Mesh(CMAC_ACPKM_CTX *ctx)
196 if (ctx->num < ctx->section_size)
199 if (!CMAC_ACPKM_Master(ctx))
201 /* Restart cbc with new key */
202 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km,
203 EVP_CIPHER_CTX_iv(ctx->cctx)))
208 static int CMAC_ACPKM_Update(CMAC_ACPKM_CTX *ctx, const void *in, size_t dlen)
210 const unsigned char *data = in;
212 if (ctx->nlast_block == -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) {
220 nleft = bl - ctx->nlast_block;
223 memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
225 ctx->nlast_block += nleft;
226 /* If no more to process return */
230 /* Else not final block so encrypt it */
231 if (!CMAC_ACPKM_Mesh(ctx))
233 if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
237 /* Encrypt all but one of the complete blocks left */
239 if (!CMAC_ACPKM_Mesh(ctx))
241 if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
247 /* Copy any data left to last block buffer */
248 memcpy(ctx->last_block, data, dlen);
249 ctx->nlast_block = dlen;
254 static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
258 if (ctx->nlast_block == -1)
260 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
261 *poutlen = (size_t) bl;
264 lb = ctx->nlast_block;
266 if (!CMAC_ACPKM_Mesh(ctx))
268 int key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
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);
274 /* Is last block complete? */
276 for (i = 0; i < bl; i++)
277 out[i] = ctx->last_block[i] ^ k1[i];
279 ctx->last_block[lb] = 0x80;
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];
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);
296 * End of CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
299 typedef struct omac_acpkm_ctx {
300 CMAC_ACPKM_CTX *cmac_ctx;
306 #define MAX_GOST_OMAC_ACPKM_SIZE 16
308 static int omac_acpkm_init(EVP_MD_CTX *ctx, int cipher_nid)
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;
315 switch (cipher_nid) {
316 case NID_grasshopper_cbc:
324 static int grasshopper_omac_acpkm_init(EVP_MD_CTX *ctx)
326 return omac_acpkm_init(ctx, NID_grasshopper_cbc);
329 static int omac_acpkm_imit_update(EVP_MD_CTX *ctx, const void *data,
332 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
334 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
338 return CMAC_ACPKM_Update(c->cmac_ctx, data, count);
341 int omac_acpkm_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
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);
348 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
352 CMAC_ACPKM_Final(c->cmac_ctx, mac, &mac_size);
354 memcpy(md, mac, c->dgst_size);
358 int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
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);
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;
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;
377 if (c_to->cmac_ctx == c_from->cmac_ctx) {
378 c_to->cmac_ctx = CMAC_ACPKM_CTX_new();
380 return CMAC_ACPKM_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
383 /* Clean up imit ctx */
384 int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
386 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
389 CMAC_ACPKM_CTX_free(c->cmac_ctx);
390 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_ACPKM_CTX));
395 static int omac_acpkm_key(OMAC_ACPKM_CTX *c, const EVP_CIPHER *cipher,
396 const unsigned char *key, size_t key_size)
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);
406 ret = CMAC_ACPKM_Init(c->cmac_ctx, key, key_size, cipher, NULL);
413 int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
416 case EVP_MD_CTRL_KEY_LEN:
417 *((unsigned int *)(ptr)) = 32;
419 case EVP_MD_CTRL_SET_KEY:
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;
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;
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);
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);
441 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
443 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_BAD_ORDER);
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);
452 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
455 case EVP_CTRL_KEY_MESH:
457 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
458 if (!arg || (arg % EVP_MD_block_size(EVP_MD_CTX_md(ctx))))
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))
468 case EVP_MD_CTRL_MAC_LEN:
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);
480 if (arg < 1 || arg > 8) {
481 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
497 static EVP_MD *_hidden_grasshopper_omac_acpkm_md = NULL;
499 EVP_MD *grasshopper_omac_acpkm(void)
501 if (_hidden_grasshopper_omac_acpkm_md == NULL) {
505 EVP_MD_meth_new(NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
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, 0)
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);
520 _hidden_grasshopper_omac_acpkm_md = md;
522 return _hidden_grasshopper_omac_acpkm_md;
525 void grasshopper_omac_acpkm_destroy(void)
527 EVP_MD_meth_free(_hidden_grasshopper_omac_acpkm_md);
528 _hidden_grasshopper_omac_acpkm_md = NULL;