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 = ctx->cipher_data;
203 if (ctx->app_data == NULL) {
204 if (!gost_cipher_set_param(c, paramNID))
206 ctx->app_data = ctx->cipher_data;
209 gost_key(&(c->cctx), key);
211 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
212 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
216 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
217 const unsigned char *iv, int enc)
219 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
220 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
224 gost_key(&(c->cctx), key);
226 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
227 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
231 /* Initializes EVP_CIPHER_CTX with default values */
232 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
233 const unsigned char *iv, int enc)
235 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
240 * Wrapper around gostcrypt function from gost89.c which perform key meshing
243 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
245 struct ossl_gost_cipher_ctx *c = ctx;
246 assert(c->count % 8 == 0 && c->count <= 1024);
247 if (c->key_meshing && c->count == 1024) {
248 cryptopro_key_meshing(&(c->cctx), iv);
250 gostcrypt(&(c->cctx), iv, buf);
251 c->count = c->count % 1024 + 8;
254 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
256 struct ossl_gost_cipher_ctx *c = ctx;
258 unsigned char buf1[8];
259 assert(c->count % 8 == 0 && c->count <= 1024);
260 if (c->key_meshing && c->count == 1024) {
261 cryptopro_key_meshing(&(c->cctx), iv);
264 gostcrypt(&(c->cctx), iv, buf1);
268 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
270 buf1[0] = (unsigned char)(g & 0xff);
271 buf1[1] = (unsigned char)((g >> 8) & 0xff);
272 buf1[2] = (unsigned char)((g >> 16) & 0xff);
273 buf1[3] = (unsigned char)((g >> 24) & 0xff);
274 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
277 if (go > g) /* overflow */
279 buf1[4] = (unsigned char)(g & 0xff);
280 buf1[5] = (unsigned char)((g >> 8) & 0xff);
281 buf1[6] = (unsigned char)((g >> 16) & 0xff);
282 buf1[7] = (unsigned char)((g >> 24) & 0xff);
284 gostcrypt(&(c->cctx), buf1, buf);
285 c->count = c->count % 1024 + 8;
288 /* GOST encryption in CFB mode */
289 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
290 const unsigned char *in, size_t inl)
292 const unsigned char *in_ptr = in;
293 unsigned char *out_ptr = out;
296 /* process partial block if any */
298 for (j = ctx->num, i = 0; j < 8 && i < inl;
299 j++, i++, in_ptr++, out_ptr++) {
301 ctx->buf[j + 8] = *in_ptr;
302 *out_ptr = ctx->buf[j] ^ (*in_ptr);
304 ctx->buf[j + 8] = *out_ptr;
307 memcpy(ctx->iv, ctx->buf + 8, 8);
315 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
317 * block cipher current iv
319 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
321 * xor next block of input text with it and output it
327 memcpy(ctx->iv, in_ptr, 8);
328 for (j = 0; j < 8; j++) {
329 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
332 /* Next iv is next block of cipher text */
334 memcpy(ctx->iv, out_ptr, 8);
336 /* Process rest of buffer */
338 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
340 memcpy(ctx->buf + 8, in_ptr, inl - i);
341 for (j = 0; i < inl; j++, i++) {
342 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
346 memcpy(ctx->buf + 8, out_ptr, j);
353 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
354 const unsigned char *in, size_t inl)
356 const unsigned char *in_ptr = in;
357 unsigned char *out_ptr = out;
360 /* process partial block if any */
362 for (j = ctx->num, i = 0; j < 8 && i < inl;
363 j++, i++, in_ptr++, out_ptr++) {
364 *out_ptr = ctx->buf[j] ^ (*in_ptr);
374 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
376 * block cipher current iv
379 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
381 * xor next block of input text with it and output it
386 for (j = 0; j < 8; j++) {
387 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
390 /* Process rest of buffer */
392 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
393 for (j = 0; i < inl; j++, i++) {
394 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
403 /* Cleaning up of EVP_CIPHER_CTX */
404 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
406 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
407 ctx->app_data = NULL;
411 /* Control function for gost cipher */
412 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
415 case EVP_CTRL_RAND_KEY:
417 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
418 GOSTerr(GOST_F_GOST_CIPHER_CTL,
419 GOST_R_RANDOM_GENERATOR_ERROR);
424 case EVP_CTRL_PBE_PRF_NID:
426 *((int *)ptr) = NID_id_HMACGostR3411_94;
433 GOSTerr(GOST_F_GOST_CIPHER_CTL,
434 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
440 /* Set cipher parameters from ASN1 structure */
441 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
444 unsigned char *buf = NULL;
445 unsigned char *p = NULL;
446 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
447 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
448 ASN1_OCTET_STRING *os = NULL;
450 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
453 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
454 GOST_CIPHER_PARAMS_free(gcp);
455 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
458 ASN1_OBJECT_free(gcp->enc_param_set);
459 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
461 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
462 p = buf = OPENSSL_malloc(len);
464 GOST_CIPHER_PARAMS_free(gcp);
465 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
468 i2d_GOST_CIPHER_PARAMS(gcp, &p);
469 GOST_CIPHER_PARAMS_free(gcp);
471 os = ASN1_OCTET_STRING_new();
473 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
475 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
480 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
484 /* Store parameters into ASN1 structure */
485 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
489 GOST_CIPHER_PARAMS *gcp = NULL;
491 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
492 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
496 p = params->value.sequence->data;
498 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
499 params->value.sequence->length);
501 len = gcp->iv->length;
502 if (len != ctx->cipher->iv_len) {
503 GOST_CIPHER_PARAMS_free(gcp);
504 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
507 if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
508 GOST_CIPHER_PARAMS_free(gcp);
511 memcpy(ctx->oiv, gcp->iv->data, len);
513 GOST_CIPHER_PARAMS_free(gcp);
518 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
520 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
521 memset(c->buffer, 0, sizeof(c->buffer));
522 memset(c->partial_block, 0, sizeof(c->partial_block));
526 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
530 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
531 const unsigned char *data)
533 unsigned char buffer[8];
535 * We are using local buffer for iv because CryptoPro doesn't interpret
536 * internal state of MAC algorithm as iv during keymeshing (but does
537 * initialize internal state from iv in key transport
539 assert(c->count % 8 == 0 && c->count <= 1024);
540 if (c->key_meshing && c->count == 1024) {
541 cryptopro_key_meshing(&(c->cctx), buffer);
543 mac_block(&(c->cctx), c->buffer, data);
544 c->count = c->count % 1024 + 8;
547 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
549 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
550 const unsigned char *p = data;
551 size_t bytes = count, i;
553 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
557 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
558 c->partial_block[i] = *p;
561 mac_block_mesh(c, c->partial_block);
568 mac_block_mesh(c, p);
573 memcpy(c->partial_block, p, bytes);
575 c->bytes_left = bytes;
579 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
581 struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx);
583 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
586 if (c->count == 0 && c->bytes_left) {
587 unsigned char buffer[8];
588 memset(buffer, 0, 8);
589 gost_imit_update(ctx, buffer, 8);
593 for (i = c->bytes_left; i < 8; i++) {
594 c->partial_block[i] = 0;
596 mac_block_mesh(c, c->partial_block);
598 get_mac(c->buffer, 32, md);
602 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
605 case EVP_MD_CTRL_KEY_LEN:
606 *((unsigned int *)(ptr)) = 32;
608 case EVP_MD_CTRL_SET_KEY:
611 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
615 gost_key(&(((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->cctx),
617 ((struct ossl_gost_imit_ctx *)(EVP_MD_CTX_md_data(ctx)))->key_set = 1;
626 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
628 memcpy(EVP_MD_CTX_md_data(to), EVP_MD_CTX_md_data(from),
629 sizeof(struct ossl_gost_imit_ctx));
633 /* Clean up imit ctx */
634 int gost_imit_cleanup(EVP_MD_CTX *ctx)
636 memset(EVP_MD_CTX_md_data(ctx), 0, sizeof(struct ossl_gost_imit_ctx));