1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * OpenSSL interface to GOST 28147-89 cipher functions *
7 * Requires OpenSSL 0.9.9 for compilation *
8 **********************************************************************/
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include "e_gost_err.h"
16 #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
23 static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
24 const unsigned char *iv, int enc);
25 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
26 const unsigned char *iv, int enc);
27 /* Handles block of data in CFB mode */
28 static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
29 const unsigned char *in, size_t inl);
30 /* Handles block of data in CNT mode */
31 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
32 const unsigned char *in, size_t inl);
33 /* Cleanup function */
34 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
35 /* set/get cipher parameters */
36 static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
37 static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
38 /* Control function */
39 static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
41 EVP_CIPHER cipher_gost = {
46 EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
47 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
51 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
52 gost89_set_asn1_parameters,
53 gost89_get_asn1_parameters,
58 EVP_CIPHER cipher_gost_cpacnt = {
63 EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
64 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
68 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
69 gost89_set_asn1_parameters,
70 gost89_get_asn1_parameters,
75 /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
76 /* Init functions which set specific parameters */
77 static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
78 /* process block of data */
79 static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
80 /* Return computed value */
81 static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
83 static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
84 static int gost_imit_cleanup(EVP_MD_CTX *ctx);
85 /* Control function, knows how to set MAC key.*/
86 static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
88 static EVP_MD *_hidden_Gost28147_89_MAC_md = NULL;
89 EVP_MD *imit_gost_cpa(void)
92 if (_hidden_Gost28147_89_MAC_md == NULL) {
95 if ((md = EVP_MD_meth_new(NID_id_Gost28147_89_MAC, NID_undef)) == NULL
96 || !EVP_MD_meth_set_result_size(md, 4)
97 || !EVP_MD_meth_set_input_blocksize(md, 8)
98 || !EVP_MD_meth_set_app_datasize(md,
99 sizeof(struct ossl_gost_imit_ctx))
100 || !EVP_MD_meth_set_flags(md, 0)
101 || !EVP_MD_meth_set_init(md, gost_imit_init_cpa)
102 || !EVP_MD_meth_set_update(md, gost_imit_update)
103 || !EVP_MD_meth_set_final(md, gost_imit_final)
104 || !EVP_MD_meth_set_copy(md, gost_imit_copy)
105 || !EVP_MD_meth_set_cleanup(md, gost_imit_cleanup)
106 || !EVP_MD_meth_set_ctrl(md, gost_imit_ctrl)) {
107 EVP_MD_meth_free(md);
110 _hidden_Gost28147_89_MAC_md = md;
112 return _hidden_Gost28147_89_MAC_md;
114 void imit_gost_cpa_destroy(void)
116 EVP_MD_meth_free(_hidden_Gost28147_89_MAC_md);
117 _hidden_Gost28147_89_MAC_md = NULL;
121 * Correspondence between gost parameter OIDs and substitution blocks
122 * NID field is filed by register_gost_NID function in engine.c
123 * upon engine initialization
126 struct gost_cipher_info gost_cipher_list[] = {
133 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
135 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
137 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
139 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
141 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
143 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
148 * get encryption parameters from crypto network settings FIXME For now we
149 * use environment var CRYPT_PARAMS as place to store these settings.
150 * Actually, it is better to use engine control command, read from
151 * configuration file to set them
153 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
156 struct gost_cipher_info *param;
158 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
159 if (!params || !strlen(params))
160 return &gost_cipher_list[1];
162 nid = OBJ_txt2nid(params);
163 if (nid == NID_undef) {
164 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
165 GOST_R_INVALID_CIPHER_PARAM_OID);
169 nid = OBJ_obj2nid(obj);
171 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
173 if (!param->sblock) {
174 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
180 /* Sets cipher param from paramset NID. */
181 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
183 const struct gost_cipher_info *param;
185 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
189 c->paramNID = param->nid;
190 c->key_meshing = param->key_meshing;
192 gost_init(&(c->cctx), param->sblock);
196 /* Initializes EVP_CIPHER_CTX by paramset NID */
197 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
198 const unsigned char *key,
199 const unsigned char *iv, int enc,
200 int paramNID, int mode)
202 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
203 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
204 if (!gost_cipher_set_param(c, paramNID))
206 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_cipher_data(ctx));
209 gost_key(&(c->cctx), key);
211 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
212 EVP_CIPHER_CTX_iv_length(ctx));
213 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
214 EVP_CIPHER_CTX_original_iv(ctx),
215 EVP_CIPHER_CTX_iv_length(ctx));
219 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
220 const unsigned char *iv, int enc)
222 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
223 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
227 gost_key(&(c->cctx), key);
229 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
230 EVP_CIPHER_CTX_iv_length(ctx));
231 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
232 EVP_CIPHER_CTX_original_iv(ctx),
233 EVP_CIPHER_CTX_iv_length(ctx));
237 /* Initializes EVP_CIPHER_CTX with default values */
238 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
239 const unsigned char *iv, int enc)
241 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
246 * Wrapper around gostcrypt function from gost89.c which perform key meshing
249 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
251 struct ossl_gost_cipher_ctx *c = ctx;
252 assert(c->count % 8 == 0 && c->count <= 1024);
253 if (c->key_meshing && c->count == 1024) {
254 cryptopro_key_meshing(&(c->cctx), iv);
256 gostcrypt(&(c->cctx), iv, buf);
257 c->count = c->count % 1024 + 8;
260 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
262 struct ossl_gost_cipher_ctx *c = ctx;
264 unsigned char buf1[8];
265 assert(c->count % 8 == 0 && c->count <= 1024);
266 if (c->key_meshing && c->count == 1024) {
267 cryptopro_key_meshing(&(c->cctx), iv);
270 gostcrypt(&(c->cctx), iv, buf1);
274 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
276 buf1[0] = (unsigned char)(g & 0xff);
277 buf1[1] = (unsigned char)((g >> 8) & 0xff);
278 buf1[2] = (unsigned char)((g >> 16) & 0xff);
279 buf1[3] = (unsigned char)((g >> 24) & 0xff);
280 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
283 if (go > g) /* overflow */
285 buf1[4] = (unsigned char)(g & 0xff);
286 buf1[5] = (unsigned char)((g >> 8) & 0xff);
287 buf1[6] = (unsigned char)((g >> 16) & 0xff);
288 buf1[7] = (unsigned char)((g >> 24) & 0xff);
290 gostcrypt(&(c->cctx), buf1, buf);
291 c->count = c->count % 1024 + 8;
294 /* GOST encryption in CFB mode */
295 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
296 const unsigned char *in, size_t inl)
298 const unsigned char *in_ptr = in;
299 unsigned char *out_ptr = out;
300 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
303 /* process partial block if any */
304 if (EVP_CIPHER_CTX_num(ctx)) {
305 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
306 j++, i++, in_ptr++, out_ptr++) {
307 if (!EVP_CIPHER_CTX_encrypting(ctx))
308 buf[j + 8] = *in_ptr;
309 *out_ptr = buf[j] ^ (*in_ptr);
310 if (EVP_CIPHER_CTX_encrypting(ctx))
311 buf[j + 8] = *out_ptr;
314 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), buf + 8, 8);
315 EVP_CIPHER_CTX_set_num(ctx, 0);
317 EVP_CIPHER_CTX_set_num(ctx, j);
322 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
324 * block cipher current iv
326 gost_crypt_mesh(EVP_CIPHER_CTX_cipher_data(ctx),
327 EVP_CIPHER_CTX_iv_noconst(ctx), buf);
329 * xor next block of input text with it and output it
334 if (!EVP_CIPHER_CTX_encrypting(ctx))
335 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), in_ptr, 8);
336 for (j = 0; j < 8; j++) {
337 out_ptr[j] = buf[j] ^ in_ptr[j];
340 /* Next iv is next block of cipher text */
341 if (EVP_CIPHER_CTX_encrypting(ctx))
342 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), out_ptr, 8);
344 /* Process rest of buffer */
346 gost_crypt_mesh(EVP_CIPHER_CTX_cipher_data(ctx),
347 EVP_CIPHER_CTX_iv_noconst(ctx), buf);
348 if (!EVP_CIPHER_CTX_encrypting(ctx))
349 memcpy(buf + 8, in_ptr, inl - i);
350 for (j = 0; i < inl; j++, i++) {
351 out_ptr[j] = buf[j] ^ in_ptr[j];
353 EVP_CIPHER_CTX_set_num(ctx, j);
354 if (EVP_CIPHER_CTX_encrypting(ctx))
355 memcpy(buf + 8, out_ptr, j);
357 EVP_CIPHER_CTX_set_num(ctx, 0);
362 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
363 const unsigned char *in, size_t inl)
365 const unsigned char *in_ptr = in;
366 unsigned char *out_ptr = out;
367 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
370 /* process partial block if any */
371 if (EVP_CIPHER_CTX_num(ctx)) {
372 for (j = EVP_CIPHER_CTX_num(ctx), i = 0; j < 8 && i < inl;
373 j++, i++, in_ptr++, out_ptr++) {
374 *out_ptr = buf[j] ^ (*in_ptr);
377 EVP_CIPHER_CTX_set_num(ctx, 0);
379 EVP_CIPHER_CTX_set_num(ctx, j);
384 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
386 * block cipher current iv
389 gost_cnt_next(EVP_CIPHER_CTX_cipher_data(ctx),
390 EVP_CIPHER_CTX_iv_noconst(ctx), buf);
392 * xor next block of input text with it and output it
397 for (j = 0; j < 8; j++) {
398 out_ptr[j] = buf[j] ^ in_ptr[j];
401 /* Process rest of buffer */
403 gost_cnt_next(EVP_CIPHER_CTX_cipher_data(ctx),
404 EVP_CIPHER_CTX_iv_noconst(ctx), buf);
405 for (j = 0; i < inl; j++, i++) {
406 out_ptr[j] = buf[j] ^ in_ptr[j];
408 EVP_CIPHER_CTX_set_num(ctx, j);
410 EVP_CIPHER_CTX_set_num(ctx, 0);
415 /* Cleaning up of EVP_CIPHER_CTX */
416 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
418 gost_destroy(&((struct ossl_gost_cipher_ctx *)
419 EVP_CIPHER_CTX_cipher_data(ctx))->cctx);
420 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
424 /* Control function for gost cipher */
425 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
428 case EVP_CTRL_RAND_KEY:
430 if (RAND_bytes((unsigned char *)ptr,
431 EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
432 GOSTerr(GOST_F_GOST_CIPHER_CTL,
433 GOST_R_RANDOM_GENERATOR_ERROR);
438 case EVP_CTRL_PBE_PRF_NID:
440 *((int *)ptr) = NID_id_HMACGostR3411_94;
447 GOSTerr(GOST_F_GOST_CIPHER_CTL,
448 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
454 /* Set cipher parameters from ASN1 structure */
455 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
458 unsigned char *buf = NULL;
459 unsigned char *p = NULL;
460 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
461 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
462 ASN1_OCTET_STRING *os = NULL;
464 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
467 if (!ASN1_OCTET_STRING_set(gcp->iv, EVP_CIPHER_CTX_iv(ctx),
468 EVP_CIPHER_CTX_iv_length(ctx))) {
469 GOST_CIPHER_PARAMS_free(gcp);
470 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
473 ASN1_OBJECT_free(gcp->enc_param_set);
474 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
476 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
477 p = buf = OPENSSL_malloc(len);
479 GOST_CIPHER_PARAMS_free(gcp);
480 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
483 i2d_GOST_CIPHER_PARAMS(gcp, &p);
484 GOST_CIPHER_PARAMS_free(gcp);
486 os = ASN1_OCTET_STRING_new();
488 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
490 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
495 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
499 /* Store parameters into ASN1 structure */
500 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
504 GOST_CIPHER_PARAMS *gcp = NULL;
506 struct ossl_gost_cipher_ctx *c = EVP_CIPHER_CTX_cipher_data(ctx);
507 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
511 p = params->value.sequence->data;
513 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
514 params->value.sequence->length);
516 len = gcp->iv->length;
517 if (len != EVP_CIPHER_CTX_iv_length(ctx)) {
518 GOST_CIPHER_PARAMS_free(gcp);
519 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
522 if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
523 GOST_CIPHER_PARAMS_free(gcp);
526 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), gcp->iv->data,
527 EVP_CIPHER_CTX_iv_length(ctx));
529 GOST_CIPHER_PARAMS_free(gcp);
534 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
536 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
537 memset(c->buffer, 0, sizeof(c->buffer));
538 memset(c->partial_block, 0, sizeof(c->partial_block));
542 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
546 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
547 const unsigned char *data)
549 unsigned char buffer[8];
551 * We are using local buffer for iv because CryptoPro doesn't interpret
552 * internal state of MAC algorithm as iv during keymeshing (but does
553 * initialize internal state from iv in key transport
555 assert(c->count % 8 == 0 && c->count <= 1024);
556 if (c->key_meshing && c->count == 1024) {
557 cryptopro_key_meshing(&(c->cctx), buffer);
559 mac_block(&(c->cctx), c->buffer, data);
560 c->count = c->count % 1024 + 8;
563 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
565 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
566 const unsigned char *p = data;
567 size_t bytes = count, i;
569 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
573 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
574 c->partial_block[i] = *p;
577 mac_block_mesh(c, c->partial_block);
584 mac_block_mesh(c, p);
589 memcpy(c->partial_block, p, bytes);
591 c->bytes_left = bytes;
595 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
597 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
599 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
602 if (c->count == 0 && c->bytes_left) {
603 unsigned char buffer[8];
604 memset(buffer, 0, 8);
605 gost_imit_update(ctx, buffer, 8);
609 for (i = c->bytes_left; i < 8; i++) {
610 c->partial_block[i] = 0;
612 mac_block_mesh(c, c->partial_block);
614 get_mac(c->buffer, 32, md);
618 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
621 case EVP_MD_CTRL_KEY_LEN:
622 *((unsigned int *)(ptr)) = 32;
624 case EVP_MD_CTRL_SET_KEY:
627 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
631 gost_key(&(((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->cctx),
633 ((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->key_set = 1;
642 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
644 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
645 sizeof(struct ossl_gost_imit_ctx));
649 /* Clean up imit ctx */
650 int gost_imit_cleanup(EVP_MD_CTX *ctx)
652 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));