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) {
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 switch (EVP_CIPHER_nid(cipher)) {
142 case NID_grasshopper_cbc:
143 acpkm = cipher_gost_grasshopper_ctracpkm();
148 if (!EVP_EncryptInit_ex(ctx->actx, acpkm, impl, NULL, NULL))
151 /* Non-NULL key means initialisation is complete */
153 unsigned char acpkm_iv[EVP_MAX_BLOCK_LENGTH];
154 int block_size, key_len;
156 /* Initialize CTR for ACPKM-Master */
157 if (!EVP_CIPHER_CTX_cipher(ctx->actx))
159 /* block size of ACPKM cipher could be 1, but,
160 * cbc cipher is same with correct block_size */
161 block_size = EVP_CIPHER_CTX_block_size(ctx->cctx);
162 /* Wide IV = 1^{n/2} || 0,
163 * where a^r denotes the string that consists of r 'a' bits */
164 memset(acpkm_iv, 0xff, block_size / 2);
165 memset(acpkm_iv + block_size / 2, 0, block_size / 2);
166 if (!EVP_EncryptInit_ex(ctx->actx, NULL, NULL, key, acpkm_iv))
168 /* EVP_CIPHER key_len may be different from EVP_CIPHER_CTX key_len */
169 key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
171 /* Generate first key material (K^1 || K^1_1) */
172 if (!EVP_Cipher(ctx->actx, ctx->km, zero_iv, key_len + block_size))
175 /* Initialize cbc for CMAC */
176 if (!EVP_CIPHER_CTX_cipher(ctx->cctx) ||
177 !EVP_CIPHER_CTX_set_key_length(ctx->cctx, key_len))
179 /* set CBC key to K^1 */
180 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km, zero_iv))
182 ctx->nlast_block = 0;
187 /* Encrypt zeros with master key
188 * to generate T*-sized key material */
189 static int CMAC_ACPKM_Master(CMAC_ACPKM_CTX *ctx)
191 return EVP_Cipher(ctx->actx, ctx->km, zero_iv,
192 EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx)) +
193 EVP_CIPHER_CTX_block_size(ctx->cctx));
196 static int CMAC_ACPKM_Mesh(CMAC_ACPKM_CTX *ctx)
198 if (ctx->num < ctx->section_size)
201 if (!CMAC_ACPKM_Master(ctx))
203 /* Restart cbc with new key */
204 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, ctx->km,
205 EVP_CIPHER_CTX_iv(ctx->cctx)))
210 static int CMAC_ACPKM_Update(CMAC_ACPKM_CTX *ctx, const void *in, size_t dlen)
212 const unsigned char *data = in;
214 if (ctx->nlast_block == -1)
218 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
219 /* Copy into partial block if we need to */
220 if (ctx->nlast_block > 0) {
222 nleft = bl - ctx->nlast_block;
225 memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
227 ctx->nlast_block += nleft;
228 /* If no more to process return */
232 /* Else not final block so encrypt it */
233 if (!CMAC_ACPKM_Mesh(ctx))
235 if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
239 /* Encrypt all but one of the complete blocks left */
241 if (!CMAC_ACPKM_Mesh(ctx))
243 if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
249 /* Copy any data left to last block buffer */
250 memcpy(ctx->last_block, data, dlen);
251 ctx->nlast_block = dlen;
256 static int CMAC_ACPKM_Final(CMAC_ACPKM_CTX *ctx, unsigned char *out,
259 int i, bl, lb, key_len;
260 unsigned char *k1, k2[EVP_MAX_BLOCK_LENGTH];
261 if (ctx->nlast_block == -1)
263 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
264 *poutlen = (size_t) bl;
267 lb = ctx->nlast_block;
269 if (!CMAC_ACPKM_Mesh(ctx))
271 key_len = EVP_CIPHER_key_length(EVP_CIPHER_CTX_cipher(ctx->actx));
273 k1 = ctx->km + key_len;
274 make_kn(k2, ctx->km + key_len, bl);
276 /* Is last block complete? */
278 for (i = 0; i < bl; i++)
279 out[i] = ctx->last_block[i] ^ k1[i];
281 ctx->last_block[lb] = 0x80;
283 memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
284 for (i = 0; i < bl; i++)
285 out[i] = ctx->last_block[i] ^ k2[i];
287 OPENSSL_cleanse(k1, bl);
288 OPENSSL_cleanse(k2, bl);
289 OPENSSL_cleanse(ctx->km, ACPKM_T_MAX);
290 if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
291 OPENSSL_cleanse(out, bl);
298 * End of CMAC code from crypto/cmac/cmac.c with ACPKM tweaks
301 typedef struct omac_acpkm_ctx {
302 CMAC_ACPKM_CTX *cmac_ctx;
308 #define MAX_GOST_OMAC_ACPKM_SIZE 16
310 static int omac_acpkm_init(EVP_MD_CTX *ctx, int cipher_nid)
312 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
313 memset(c, 0, sizeof(OMAC_ACPKM_CTX));
314 c->cipher_nid = cipher_nid;
317 switch (cipher_nid) {
318 case NID_grasshopper_cbc:
326 static int grasshopper_omac_acpkm_init(EVP_MD_CTX *ctx)
328 return omac_acpkm_init(ctx, NID_grasshopper_cbc);
331 static int omac_acpkm_imit_update(EVP_MD_CTX *ctx, const void *data,
334 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
336 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
340 return CMAC_ACPKM_Update(c->cmac_ctx, data, count);
343 int omac_acpkm_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
345 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
346 unsigned char mac[MAX_GOST_OMAC_ACPKM_SIZE];
347 size_t mac_size = sizeof(mac);
350 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
354 CMAC_ACPKM_Final(c->cmac_ctx, mac, &mac_size);
356 memcpy(md, mac, c->dgst_size);
360 int omac_acpkm_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
362 OMAC_ACPKM_CTX *c_to = EVP_MD_CTX_md_data(to);
363 const OMAC_ACPKM_CTX *c_from = EVP_MD_CTX_md_data(from);
365 if (c_from && c_to) {
366 c_to->dgst_size = c_from->dgst_size;
367 c_to->cipher_nid = c_from->cipher_nid;
368 c_to->key_set = c_from->key_set;
372 if (!c_from->cmac_ctx) {
373 if (c_to->cmac_ctx) {
374 CMAC_ACPKM_CTX_free(c_to->cmac_ctx);
375 c_to->cmac_ctx = NULL;
379 if (c_to->cmac_ctx == c_from->cmac_ctx) {
380 c_to->cmac_ctx = CMAC_ACPKM_CTX_new();
382 return CMAC_ACPKM_CTX_copy(c_to->cmac_ctx, c_from->cmac_ctx);
385 /* Clean up imit ctx */
386 int omac_acpkm_imit_cleanup(EVP_MD_CTX *ctx)
388 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
391 CMAC_ACPKM_CTX_free(c->cmac_ctx);
392 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(OMAC_ACPKM_CTX));
397 static int omac_acpkm_key(OMAC_ACPKM_CTX *c, const EVP_CIPHER *cipher,
398 const unsigned char *key, size_t key_size)
402 c->cmac_ctx = CMAC_ACPKM_CTX_new();
403 if (c->cmac_ctx == NULL) {
404 GOSTerr(GOST_F_OMAC_ACPKM_KEY, ERR_R_MALLOC_FAILURE);
408 ret = CMAC_ACPKM_Init(c->cmac_ctx, key, key_size, cipher, NULL);
415 int omac_acpkm_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
418 case EVP_MD_CTRL_KEY_LEN:
419 *((unsigned int *)(ptr)) = 32;
421 case EVP_MD_CTRL_SET_KEY:
423 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
424 const EVP_MD *md = EVP_MD_CTX_md(ctx);
425 const EVP_CIPHER *cipher = NULL;
427 if (c->cipher_nid == NID_undef) {
428 switch (EVP_MD_nid(md)) {
429 case NID_grasshopper_mac:
430 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac:
431 c->cipher_nid = NID_grasshopper_cbc;
435 cipher = EVP_get_cipherbynid(c->cipher_nid);
436 if (cipher == NULL) {
437 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_CIPHER_NOT_FOUND);
439 if (EVP_MD_meth_get_init(EVP_MD_CTX_md(ctx)) (ctx) <= 0) {
440 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_MAC_KEY_NOT_SET);
443 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NO_INIT);
445 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_BAD_ORDER);
449 struct gost_mac_key *key = (struct gost_mac_key *)ptr;
450 return omac_acpkm_key(c, cipher, key->key, 32);
451 } else if (arg == 32) {
452 return omac_acpkm_key(c, cipher, ptr, 32);
454 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_SIZE);
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_CTX_ctrl(c->cmac_ctx->actx, EVP_CTRL_KEY_MESH, *(int *)ptr, NULL))
470 case EVP_MD_CTRL_XOF_LEN: /* Supported in OpenSSL */
472 OMAC_ACPKM_CTX *c = EVP_MD_CTX_md_data(ctx);
473 switch (c->cipher_nid) {
474 case NID_grasshopper_cbc:
475 if (arg < 1 || arg > 16) {
476 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
482 if (arg < 1 || arg > 8) {
483 GOSTerr(GOST_F_OMAC_ACPKM_IMIT_CTRL, GOST_R_INVALID_MAC_SIZE);
499 GOST_digest kuznyechik_ctracpkm_omac_digest = {
500 .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac,
501 .result_size = MAX_GOST_OMAC_ACPKM_SIZE,
502 .input_blocksize = GRASSHOPPER_BLOCK_SIZE,
503 .app_datasize = sizeof(OMAC_ACPKM_CTX),
504 .flags = EVP_MD_FLAG_XOF,
505 .init = grasshopper_omac_acpkm_init,
506 .update = omac_acpkm_imit_update,
507 .final = omac_acpkm_imit_final,
508 .copy = omac_acpkm_imit_copy,
509 .cleanup = omac_acpkm_imit_cleanup,
510 .ctrl = omac_acpkm_imit_ctrl,
513 EVP_MD *grasshopper_omac_acpkm(void)
515 return GOST_init_digest(&kuznyechik_ctracpkm_omac_digest);
518 void grasshopper_omac_acpkm_destroy(void)
520 GOST_deinit_digest(&kuznyechik_ctracpkm_omac_digest);