3 * This file is distributed under the same license as OpenSSL
6 #if defined(__cplusplus)
10 #include "gost_grasshopper_cipher.h"
11 #include "gost_grasshopper_defines.h"
12 #include "gost_grasshopper_math.h"
13 #include "gost_grasshopper_core.h"
15 #include <openssl/evp.h>
16 #include <openssl/rand.h>
17 #include <openssl/err.h>
21 #include "e_gost_err.h"
23 enum GRASSHOPPER_CIPHER_TYPE {
24 GRASSHOPPER_CIPHER_ECB = 0,
25 GRASSHOPPER_CIPHER_CBC,
26 GRASSHOPPER_CIPHER_OFB,
27 GRASSHOPPER_CIPHER_CFB,
28 GRASSHOPPER_CIPHER_CTR,
29 GRASSHOPPER_CIPHER_CTRACPKM,
32 static EVP_CIPHER* gost_grasshopper_ciphers[6] = {
33 [GRASSHOPPER_CIPHER_ECB] = NULL,
34 [GRASSHOPPER_CIPHER_CBC] = NULL,
35 [GRASSHOPPER_CIPHER_OFB] = NULL,
36 [GRASSHOPPER_CIPHER_CFB] = NULL,
37 [GRASSHOPPER_CIPHER_CTR] = NULL,
38 [GRASSHOPPER_CIPHER_CTRACPKM] = NULL,
41 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c);
42 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c);
44 struct GRASSHOPPER_CIPHER_PARAMS {
46 grasshopper_init_cipher_func init_cipher;
47 grasshopper_do_cipher_func do_cipher;
48 grasshopper_destroy_cipher_func destroy_cipher;
55 static struct GRASSHOPPER_CIPHER_PARAMS gost_cipher_params[6] = {
56 [GRASSHOPPER_CIPHER_ECB] = {
58 gost_grasshopper_cipher_init_ecb,
59 gost_grasshopper_cipher_do_ecb,
62 sizeof(gost_grasshopper_cipher_ctx),
66 [GRASSHOPPER_CIPHER_CBC] = {
68 gost_grasshopper_cipher_init_cbc,
69 gost_grasshopper_cipher_do_cbc,
72 sizeof(gost_grasshopper_cipher_ctx),
76 [GRASSHOPPER_CIPHER_OFB] = {
78 gost_grasshopper_cipher_init_ofb,
79 gost_grasshopper_cipher_do_ofb,
80 gost_grasshopper_cipher_destroy_ofb,
82 sizeof(gost_grasshopper_cipher_ctx_ofb),
86 [GRASSHOPPER_CIPHER_CFB] = {
88 gost_grasshopper_cipher_init_cfb,
89 gost_grasshopper_cipher_do_cfb,
92 sizeof(gost_grasshopper_cipher_ctx),
96 [GRASSHOPPER_CIPHER_CTR] = {
98 gost_grasshopper_cipher_init_ctr,
99 gost_grasshopper_cipher_do_ctr,
100 gost_grasshopper_cipher_destroy_ctr,
102 sizeof(gost_grasshopper_cipher_ctx_ctr),
103 /* IV size is set to match full block, to make it responsibility of
104 * user to assign correct values (IV || 0), and to make naive context
105 * copy possible (for software such as openssh) */
109 [GRASSHOPPER_CIPHER_CTRACPKM] = {
110 NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm,
111 gost_grasshopper_cipher_init_ctracpkm,
112 gost_grasshopper_cipher_do_ctracpkm,
113 gost_grasshopper_cipher_destroy_ctr,
115 sizeof(gost_grasshopper_cipher_ctx_ctr),
121 /* first 256 bit of D from draft-irtf-cfrg-re-keying-12 */
122 static const unsigned char ACPKM_D_2018[] = {
123 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 64 bit */
124 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 128 bit */
125 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
126 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 256 bit */
129 static void acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *ctx)
131 gost_grasshopper_cipher_ctx *c = &ctx->c;
132 unsigned char newkey[GRASSHOPPER_KEY_SIZE];
133 const int J = GRASSHOPPER_KEY_SIZE / GRASSHOPPER_BLOCK_SIZE;
136 for (n = 0; n < J; n++) {
137 const unsigned char *D_n = &ACPKM_D_2018[n * GRASSHOPPER_BLOCK_SIZE];
139 grasshopper_encrypt_block(&c->encrypt_round_keys,
140 (grasshopper_w128_t *)D_n,
141 (grasshopper_w128_t *)&newkey[n * GRASSHOPPER_BLOCK_SIZE],
144 gost_grasshopper_cipher_key(c, newkey);
147 /* Set 256 bit key into context */
148 GRASSHOPPER_INLINE void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k) {
150 for (i = 0; i < 2; i++) {
151 grasshopper_copy128(&c->key.k.k[i], (const grasshopper_w128_t*) (k + i * 16));
153 grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
154 grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
157 /* Cleans up key from context */
158 GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c) {
160 for (i = 0; i < 2; i++) {
161 grasshopper_zero128(&c->key.k.k[i]);
163 for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
164 grasshopper_zero128(&c->encrypt_round_keys.k[i]);
166 for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
167 grasshopper_zero128(&c->decrypt_round_keys.k[i]);
169 grasshopper_zero128(&c->buffer);
172 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c) {
173 gost_grasshopper_cipher_ctx_ofb* ctx = (gost_grasshopper_cipher_ctx_ofb*) c;
175 grasshopper_zero128(&ctx->buffer1);
178 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c) {
179 gost_grasshopper_cipher_ctx_ctr* ctx = (gost_grasshopper_cipher_ctx_ctr*) c;
181 grasshopper_zero128(&ctx->partial_buffer);
184 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
185 const unsigned char* iv, int enc) {
186 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
188 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
189 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
193 gost_grasshopper_cipher_key(c, key);
197 memcpy((unsigned char*) EVP_CIPHER_CTX_original_iv(ctx), iv,
198 EVP_CIPHER_CTX_iv_length(ctx));
201 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
202 EVP_CIPHER_CTX_original_iv(ctx),
203 EVP_CIPHER_CTX_iv_length(ctx));
205 grasshopper_zero128(&c->buffer);
210 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
211 const unsigned char* iv,
213 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
214 c->type = GRASSHOPPER_CIPHER_ECB;
215 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
218 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key,
219 const unsigned char* iv,
221 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
222 c->type = GRASSHOPPER_CIPHER_CBC;
223 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
226 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
227 const unsigned char* iv,
229 gost_grasshopper_cipher_ctx_ofb* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
231 c->c.type = GRASSHOPPER_CIPHER_OFB;
233 grasshopper_zero128(&c->buffer1);
235 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
238 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
239 const unsigned char* iv,
241 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
242 c->type = GRASSHOPPER_CIPHER_CFB;
243 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
246 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key,
247 const unsigned char* iv,
249 gost_grasshopper_cipher_ctx_ctr* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
251 c->c.type = GRASSHOPPER_CIPHER_CTR;
252 EVP_CIPHER_CTX_set_num(ctx, 0);
254 grasshopper_zero128(&c->partial_buffer);
256 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
259 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX *ctx, const unsigned char *key,
260 const unsigned char *iv,
262 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
264 /* NB: setting type makes EVP do_cipher callback useless */
265 c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
266 EVP_CIPHER_CTX_set_num(ctx, 0);
267 c->section_size = 4096;
268 c->skip_sections = 1;
270 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
273 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
274 const unsigned char* in, size_t inl) {
275 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
276 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
278 return params->do_cipher(ctx, out, in, inl);
281 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
282 const unsigned char* in, size_t inl) {
283 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
284 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
285 const unsigned char* current_in = in;
286 unsigned char* current_out = out;
287 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
290 for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
292 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) current_in,
293 (grasshopper_w128_t*) current_out,
296 grasshopper_decrypt_block(&c->decrypt_round_keys, (grasshopper_w128_t*) current_in,
297 (grasshopper_w128_t*) current_out,
305 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
306 const unsigned char* in, size_t inl) {
307 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
308 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
309 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
310 const unsigned char* current_in = in;
311 unsigned char* current_out = out;
312 grasshopper_w128_t* currentInputBlock;
313 grasshopper_w128_t* currentOutputBlock;
314 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
316 grasshopper_w128_t* currentBlock;
318 currentBlock = (grasshopper_w128_t*) iv;
320 for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
321 currentInputBlock = (grasshopper_w128_t*) current_in;
322 currentOutputBlock = (grasshopper_w128_t*) current_out;
324 grasshopper_append128(currentBlock, currentInputBlock);
325 grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock, currentOutputBlock, &c->buffer);
326 grasshopper_copy128(currentBlock, currentOutputBlock);
328 grasshopper_decrypt_block(&c->decrypt_round_keys, currentInputBlock, currentOutputBlock, &c->buffer);
329 grasshopper_append128(currentOutputBlock, currentBlock);
330 grasshopper_copy128(currentBlock, currentInputBlock);
337 void inc_counter(unsigned char* counter, size_t counter_bytes)
340 unsigned int n = counter_bytes;
351 /* increment counter (128-bit int) by 1 */
352 static void ctr128_inc(unsigned char *counter)
354 inc_counter(counter, 16);
357 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
358 const unsigned char* in, size_t inl) {
359 gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
360 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
361 const unsigned char* current_in = in;
362 unsigned char* current_out = out;
363 grasshopper_w128_t* currentInputBlock;
364 grasshopper_w128_t* currentOutputBlock;
365 unsigned int n = EVP_CIPHER_CTX_num(ctx);
370 *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
372 n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
374 EVP_CIPHER_CTX_set_num(ctx, n);
375 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
377 grasshopper_w128_t* iv_buffer = (grasshopper_w128_t*) iv;
380 for (i = 0; i < blocks; i++) {
381 currentInputBlock = (grasshopper_w128_t*) current_in;
382 currentOutputBlock = (grasshopper_w128_t*) current_out;
383 grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer, currentOutputBlock, &c->c.buffer);
384 grasshopper_append128(currentOutputBlock, currentInputBlock);
385 ctr128_inc(iv_buffer->b);
386 current_in += GRASSHOPPER_BLOCK_SIZE;
387 current_out += GRASSHOPPER_BLOCK_SIZE;
391 lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
393 currentInputBlock = (grasshopper_w128_t*) current_in;
394 currentOutputBlock = (grasshopper_w128_t*) current_out;
395 grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer, &c->partial_buffer, &c->c.buffer);
396 for (i = 0; i < lasted; i++) {
397 currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
399 EVP_CIPHER_CTX_set_num(ctx, i);
400 ctr128_inc(iv_buffer->b);
406 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *ctx, unsigned int num)
408 if (!ctx->section_size ||
409 (num & (ctx->section_size - 1)))
411 if (ctx->skip_sections) {
412 /* In no master key mode first section is using original key */
413 --ctx->skip_sections;
416 acpkm_grasshopper(ctx);
419 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
420 /* If meshing is not configured via ctrl (setting section_size)
421 * this function works exactly like plain ctr */
422 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx, unsigned char *out,
423 const unsigned char *in, size_t inl) {
424 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
425 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
426 unsigned int num = EVP_CIPHER_CTX_num(ctx);
428 while ((num & GRASSHOPPER_BLOCK_MASK) && inl) {
429 *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
433 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
437 for (i = 0; i < blocks; i++) {
438 apply_acpkm_grasshopper(c, num);
439 grasshopper_encrypt_block(&c->c.encrypt_round_keys,
440 (grasshopper_w128_t *)iv, (grasshopper_w128_t *)out, &c->c.buffer);
441 grasshopper_append128((grasshopper_w128_t *)out, (grasshopper_w128_t *)in);
443 in += GRASSHOPPER_BLOCK_SIZE;
444 out += GRASSHOPPER_BLOCK_SIZE;
445 num += GRASSHOPPER_BLOCK_SIZE;
449 size_t lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
451 apply_acpkm_grasshopper(c, num);
452 grasshopper_encrypt_block(&c->c.encrypt_round_keys,
453 (grasshopper_w128_t *)iv, &c->partial_buffer, &c->c.buffer);
454 for (i = 0; i < lasted; i++)
455 out[i] = c->partial_buffer.b[i] ^ in[i];
459 EVP_CIPHER_CTX_set_num(ctx, num);
465 * Fixed 128-bit IV implementation make shift regiser redundant.
467 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb* ctx, grasshopper_w128_t* iv,
468 grasshopper_w128_t* buf) {
469 memcpy(&ctx->buffer1, iv, 16);
470 grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
474 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
475 const unsigned char* in, size_t inl) {
476 gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_get_cipher_data(ctx);
477 const unsigned char* in_ptr = in;
478 unsigned char* out_ptr = out;
479 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
480 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
481 int num = EVP_CIPHER_CTX_num(ctx);
485 /* process partial block if any */
487 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
488 j++, i++, in_ptr++, out_ptr++) {
489 *out_ptr = buf[j] ^ (*in_ptr);
491 if (j == GRASSHOPPER_BLOCK_SIZE) {
492 EVP_CIPHER_CTX_set_num(ctx, 0);
494 EVP_CIPHER_CTX_set_num(ctx, (int) j);
499 for (; i + GRASSHOPPER_BLOCK_SIZE <
500 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
502 * block cipher current iv
505 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
508 * xor next block of input text with it and output it
513 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
514 out_ptr[j] = buf[j] ^ in_ptr[j];
518 /* Process rest of buffer */
520 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
521 for (j = 0; i < inl; j++, i++) {
522 out_ptr[j] = buf[j] ^ in_ptr[j];
524 EVP_CIPHER_CTX_set_num(ctx, (int) j);
526 EVP_CIPHER_CTX_set_num(ctx, 0);
532 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
533 const unsigned char* in, size_t inl) {
534 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
535 const unsigned char* in_ptr = in;
536 unsigned char* out_ptr = out;
537 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
538 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
539 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
540 int num = EVP_CIPHER_CTX_num(ctx);
544 /* process partial block if any */
546 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl; j++, i++, in_ptr++, out_ptr++) {
548 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
550 *out_ptr = buf[j] ^ (*in_ptr);
552 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
555 if (j == GRASSHOPPER_BLOCK_SIZE) {
556 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
557 EVP_CIPHER_CTX_set_num(ctx, 0);
559 EVP_CIPHER_CTX_set_num(ctx, (int) j);
564 for (; i + GRASSHOPPER_BLOCK_SIZE <
565 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
567 * block cipher current iv
569 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
572 * xor next block of input text with it and output it
578 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
580 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
581 out_ptr[j] = buf[j] ^ in_ptr[j];
584 /* Next iv is next block of cipher text */
586 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
590 /* Process rest of buffer */
592 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
595 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
597 for (j = 0; i < inl; j++, i++) {
598 out_ptr[j] = buf[j] ^ in_ptr[j];
600 EVP_CIPHER_CTX_set_num(ctx, (int) j);
602 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
605 EVP_CIPHER_CTX_set_num(ctx, 0);
611 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
612 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
613 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
615 gost_grasshopper_cipher_destroy(c);
616 if (params->destroy_cipher != NULL) {
617 params->destroy_cipher(c);
620 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
625 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
627 unsigned char* buf = NULL;
628 ASN1_OCTET_STRING* os = NULL;
630 os = ASN1_OCTET_STRING_new();
632 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
634 GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
639 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
643 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
646 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
653 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
655 case EVP_CTRL_RAND_KEY: {
656 if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
657 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
662 case EVP_CTRL_KEY_MESH:
663 if (arg <= 1 || ((arg - 1) & arg))
665 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
666 c->section_size = arg;
667 c->skip_sections = 1;
670 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
676 GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
677 return EVP_CIPHER_meth_new(cipher_type,
678 block_size /* block_size */,
679 GRASSHOPPER_KEY_SIZE /* key_size */);
682 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding) {
683 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size) &&
684 EVP_CIPHER_meth_set_flags(cipher, (unsigned long) (
686 ((!padding) ? EVP_CIPH_NO_PADDING : 0) |
687 ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0) |
689 EVP_CIPH_ALWAYS_CALL_INIT)
691 EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup) &&
692 EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_grasshopper_set_asn1_parameters) &&
693 EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_grasshopper_get_asn1_parameters) &&
694 EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl) &&
695 EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
698 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
700 struct GRASSHOPPER_CIPHER_PARAMS* params;
702 cipher = &gost_grasshopper_ciphers[num];
704 if (*cipher == NULL) {
705 params = &gost_cipher_params[num];
707 int nid = params->nid;
708 grasshopper_init_cipher_func init_cipher = params->init_cipher;
709 int block_size = params->block_size;
710 int ctx_size = params->ctx_size;
711 int iv_size = params->iv_size;
712 bool padding = params->padding;
714 *cipher = cipher_gost_grasshopper_create(nid, block_size);
715 if (*cipher == NULL) {
719 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding) ||
720 !EVP_CIPHER_meth_set_init(*cipher, init_cipher) ||
721 !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
722 EVP_CIPHER_meth_free(*cipher);
730 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ecb() {
731 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
734 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cbc() {
735 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
738 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ofb() {
739 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
742 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cfb() {
743 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
746 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctr() {
747 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
750 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctracpkm() {
751 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTRACPKM);
754 void cipher_gost_grasshopper_destroy(void)
756 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
757 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
758 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
759 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
760 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
761 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
762 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
763 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
764 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
765 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
766 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM]);
767 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
770 #if defined(__cplusplus)