2 * Copyright (C) 2018,2020 Vitaly Chikunov <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_zalloc(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) {
71 EVP_CIPHER_CTX_free(ctx->cctx);
75 ctx->nlast_block = -1;
77 ctx->section_size = 4096; /* recommended value for Kuznyechik */
81 static void CMAC_ACPKM_CTX_cleanup(CMAC_ACPKM_CTX *ctx)
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;
91 static void CMAC_ACPKM_CTX_free(CMAC_ACPKM_CTX *ctx)
95 CMAC_ACPKM_CTX_cleanup(ctx);
96 EVP_CIPHER_CTX_free(ctx->cctx);
97 EVP_CIPHER_CTX_free(ctx->actx);
101 int CMAC_ACPKM_CTX_copy(CMAC_ACPKM_CTX *out, const CMAC_ACPKM_CTX *in)
104 if (in->nlast_block == -1)
106 if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
108 if (!EVP_CIPHER_CTX_copy(out->actx, in->actx))
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;
120 static int CMAC_ACPKM_Init(CMAC_ACPKM_CTX *ctx, const void *key, size_t keylen,
121 const EVP_CIPHER *cipher, ENGINE *impl)
123 /* All zeros means restart */
124 if (!key && !cipher && !impl && keylen == 0) {
125 /* Not initialised */
126 if (ctx->nlast_block == -1)
128 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
130 memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
131 ctx->nlast_block = 0;
132 /* No restart for ACPKM */
135 /* Initialise context */
137 const EVP_CIPHER *acpkm;
139 if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
141 if (!EVP_CIPHER_is_a(cipher, SN_grasshopper_cbc))
143 acpkm = cipher_gost_grasshopper_ctracpkm();
144 if (!EVP_EncryptInit_ex(ctx->actx, acpkm, impl, NULL, NULL))
147 /* Non-NULL key means initialisation is complete */
149 unsigned char acpkm_iv[EVP_MAX_BLOCK_LENGTH];
150 int block_size, key_len;
152 /* Initialize CTR for ACPKM-Master */
153 if (!EVP_CIPHER_CTX_cipher(ctx->actx))
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))
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));
167 /* Generate first key material (K^1 || K^1_1) */
168 if (!EVP_Cipher(ctx->actx, ctx->km, zero_iv, key_len + block_size))
171 /* Initialize cbc for CMAC */
172 if (!EVP_CIPHER_CTX_cipher(ctx->cctx) ||
173 !EVP_CIPHER_CTX_set_key_length(ctx->cctx, key_len))
175 /* set CBC key to K^1 */
176 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km, zero_iv))
178 ctx->nlast_block = 0;
183 /* Encrypt zeros with master key
184 * to generate T*-sized key material */
185 static int CMAC_ACPKM_Master(CMAC_ACPKM_CTX *ctx)
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));
192 static int CMAC_ACPKM_Mesh(CMAC_ACPKM_CTX *ctx)
194 if (ctx->num < ctx->section_size)
197 if (!CMAC_ACPKM_Master(ctx))
199 /* Restart cbc with new key */
200 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km,
201 EVP_CIPHER_CTX_iv(ctx->cctx)))
206 static int CMAC_ACPKM_Update(CMAC_ACPKM_CTX *ctx, const void *in, size_t dlen)
208 const unsigned char *data = in;
210 if (ctx->nlast_block == -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) {
218 nleft = bl - ctx->nlast_block;
221 memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
223 ctx->nlast_block += nleft;
224 /* If no more to process return */
228 /* Else not final block so encrypt it */
229 if (!CMAC_ACPKM_Mesh(ctx))
231 if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
235 /* Encrypt all but one of the complete blocks left */
237 if (!CMAC_ACPKM_Mesh(ctx))
239 if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
245 /* Copy any data left to last block buffer */
246 memcpy(ctx->last_block, data, dlen);
247 ctx->nlast_block = dlen;
252 static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
255 int i, bl, lb, key_len;
256 unsigned char *k1, k2[EVP_MAX_BLOCK_LENGTH];
257 if (ctx->nlast_block == -1)
259 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
260 *poutlen = (size_t) bl;
263 lb = ctx->nlast_block;
265 if (!CMAC_ACPKM_Mesh(ctx))
267 key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
269 k1 = ctx->km + key_len;
270 make_kn(k2, ctx->km + key_len, bl);
272 /* Is last block complete? */
274 for (i = 0; i < bl; i++)
275 out[i] = ctx->last_block[i] ^ k1[i];
277 ctx->last_block[lb] = 0x80;
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];
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);
294 * End of CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
297 typedef struct omac_acpkm_ctx {
298 CMAC_ACPKM_CTX *cmac_ctx;
300 const char *cipher_name;
304 #define MAX_GOST_OMAC_ACPKM_SIZE 16
306 static int omac_acpkm_init(EVP_MD_CTX *ctx, const char *cipher_name)
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;
313 switch (OBJ_txt2nid(cipher_name)) {
314 case NID_grasshopper_cbc:
322 static int grasshopper_omac_acpkm_init(EVP_MD_CTX *ctx)
324 return omac_acpkm_init(ctx, SN_grasshopper_cbc);
327 static int omac_acpkm_imit_update(EVP_MD_CTX *ctx, const void *data,
330 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
332 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
336 return CMAC_ACPKM_Update(c->cmac_ctx, data, count);
339 int omac_acpkm_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
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);
346 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
350 CMAC_ACPKM_Final(c->cmac_ctx, mac, &mac_size);
352 memcpy(md, mac, c->dgst_size);
356 static int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
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);
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;
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;
375 if (c_to->cmac_ctx == c_from->cmac_ctx) {
376 c_to->cmac_ctx = CMAC_ACPKM_CTX_new();
378 return CMAC_ACPKM_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
381 /* Clean up imit ctx */
382 static int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
384 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
387 CMAC_ACPKM_CTX_free(c->cmac_ctx);
388 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_ACPKM_CTX));
393 static int omac_acpkm_key(OMAC_ACPKM_CTX *c, const EVP_CIPHER *cipher,
394 const unsigned char *key, size_t key_size)
398 c->cmac_ctx = CMAC_ACPKM_CTX_new();
399 if (c->cmac_ctx == NULL) {
400 GOSTerr(GOST_F_OMAC_ACPKM_KEY, ERR_R_MALLOC_FAILURE);
404 ret = CMAC_ACPKM_Init(c->cmac_ctx, key, key_size, cipher, NULL);
411 int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
414 case EVP_MD_CTRL_KEY_LEN:
415 *((unsigned int *)(ptr)) = 32;
417 case EVP_MD_CTRL_SET_KEY:
419 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
420 const EVP_MD *md = EVP_MD_CTX_md(ctx);
421 EVP_CIPHER *cipher = NULL;
424 if (c->cipher_name == NULL) {
425 if (EVP_MD_is_a(md, SN_grasshopper_mac)
426 || EVP_MD_is_a(md, SN_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac))
427 c->cipher_name = SN_grasshopper_cbc;
430 (EVP_CIPHER *)EVP_get_cipherbyname(c->cipher_name)) == NULL
432 EVP_CIPHER_fetch(NULL, c->cipher_name, NULL)) == NULL) {
433 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
435 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
436 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
439 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
441 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_BAD_ORDER);
445 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
446 ret = omac_acpkm_key(c, cipher, key->key, 32);
448 } else if (arg == 32) {
449 ret = omac_acpkm_key(c, cipher, ptr, 32);
452 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
454 EVP_CIPHER_free(cipher);
457 case EVP_CTRL_KEY_MESH:
459 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
460 if (!arg || (arg % EVP_MD_block_size(EVP_MD_CTX_md(ctx))))
462 c->cmac_ctx->section_size = arg;
463 if (ptr && *(int *)ptr) {
464 /* Set parameter T */
465 if (EVP_CIPHER_get0_provider(EVP_CIPHER_CTX_cipher(c->cmac_ctx->actx))
467 if (!EVP_CIPHER_CTX_ctrl(c->cmac_ctx->actx, EVP_CTRL_KEY_MESH,
471 size_t cipher_key_mesh = (size_t)*(int *)ptr;
472 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
473 params[0] = OSSL_PARAM_construct_size_t("key-mesh",
475 if (!EVP_CIPHER_CTX_set_params(c->cmac_ctx->actx, params))
481 case EVP_MD_CTRL_XOF_LEN: /* Supported in OpenSSL */
483 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
484 switch (OBJ_txt2nid(c->cipher_name)) {
485 case NID_grasshopper_cbc:
486 if (arg < 1 || arg > 16) {
487 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
493 if (arg < 1 || arg > 8) {
494 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
510 GOST_digest kuznyechik_ctracpkm_omac_digest = {
511 .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
512 .result_size = MAX_GOST_OMAC_ACPKM_SIZE,
513 .input_blocksize = GRASSHOPPER_BLOCK_SIZE,
514 .app_datasize = sizeof(OMAC_ACPKM_CTX),
515 .flags = EVP_MD_FLAG_XOF,
516 .init = grasshopper_omac_acpkm_init,
517 .update = omac_acpkm_imit_update,
518 .final = omac_acpkm_imit_final,
519 .copy = omac_acpkm_imit_copy,
520 .cleanup = omac_acpkm_imit_cleanup,
521 .ctrl = omac_acpkm_imit_ctrl,