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 EVP_MD imit_gost_cpa = {
89 NID_id_Gost28147_89_MAC,
102 sizeof(struct ossl_gost_imit_ctx),
107 * Correspondence between gost parameter OIDs and substitution blocks
108 * NID field is filed by register_gost_NID function in engine.c
109 * upon engine initialization
112 struct gost_cipher_info gost_cipher_list[] = {
119 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
121 {NID_id_Gost28147_89_cc, &GostR3411_94_CryptoProParamSet, 0},
122 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
124 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
126 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
128 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
130 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
135 * get encryption parameters from crypto network settings FIXME For now we
136 * use environment var CRYPT_PARAMS as place to store these settings.
137 * Actually, it is better to use engine control command, read from
138 * configuration file to set them
140 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
143 struct gost_cipher_info *param;
145 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
146 if (!params || !strlen(params))
147 return &gost_cipher_list[1];
149 nid = OBJ_txt2nid(params);
150 if (nid == NID_undef) {
151 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
152 GOST_R_INVALID_CIPHER_PARAM_OID);
156 nid = OBJ_obj2nid(obj);
158 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
160 if (!param->sblock) {
161 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
167 /* Sets cipher param from paramset NID. */
168 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
170 const struct gost_cipher_info *param;
172 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
176 c->paramNID = param->nid;
177 c->key_meshing = param->key_meshing;
179 gost_init(&(c->cctx), param->sblock);
183 /* Initializes EVP_CIPHER_CTX by paramset NID */
184 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
185 const unsigned char *key,
186 const unsigned char *iv, int enc,
187 int paramNID, int mode)
189 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
190 if (ctx->app_data == NULL) {
191 if (!gost_cipher_set_param(c, paramNID))
193 ctx->app_data = ctx->cipher_data;
196 gost_key(&(c->cctx), key);
198 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
199 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
203 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
204 const unsigned char *iv, int enc)
206 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
207 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
211 gost_key(&(c->cctx), key);
213 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
214 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
218 /* Initializes EVP_CIPHER_CTX with default values */
219 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
220 const unsigned char *iv, int enc)
222 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
227 * Wrapper around gostcrypt function from gost89.c which perform key meshing
230 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
232 struct ossl_gost_cipher_ctx *c = ctx;
233 assert(c->count % 8 == 0 && c->count <= 1024);
234 if (c->key_meshing && c->count == 1024) {
235 cryptopro_key_meshing(&(c->cctx), iv);
237 gostcrypt(&(c->cctx), iv, buf);
238 c->count = c->count % 1024 + 8;
241 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
243 struct ossl_gost_cipher_ctx *c = ctx;
245 unsigned char buf1[8];
246 assert(c->count % 8 == 0 && c->count <= 1024);
247 if (c->key_meshing && c->count == 1024) {
248 cryptopro_key_meshing(&(c->cctx), iv);
251 gostcrypt(&(c->cctx), iv, buf1);
255 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
257 buf1[0] = (unsigned char)(g & 0xff);
258 buf1[1] = (unsigned char)((g >> 8) & 0xff);
259 buf1[2] = (unsigned char)((g >> 16) & 0xff);
260 buf1[3] = (unsigned char)((g >> 24) & 0xff);
261 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
264 if (go > g) /* overflow */
266 buf1[4] = (unsigned char)(g & 0xff);
267 buf1[5] = (unsigned char)((g >> 8) & 0xff);
268 buf1[6] = (unsigned char)((g >> 16) & 0xff);
269 buf1[7] = (unsigned char)((g >> 24) & 0xff);
271 gostcrypt(&(c->cctx), buf1, buf);
272 c->count = c->count % 1024 + 8;
275 /* GOST encryption in CFB mode */
276 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
277 const unsigned char *in, size_t inl)
279 const unsigned char *in_ptr = in;
280 unsigned char *out_ptr = out;
283 /* process partial block if any */
285 for (j = ctx->num, i = 0; j < 8 && i < inl;
286 j++, i++, in_ptr++, out_ptr++) {
288 ctx->buf[j + 8] = *in_ptr;
289 *out_ptr = ctx->buf[j] ^ (*in_ptr);
291 ctx->buf[j + 8] = *out_ptr;
294 memcpy(ctx->iv, ctx->buf + 8, 8);
302 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
304 * block cipher current iv
306 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
308 * xor next block of input text with it and output it
314 memcpy(ctx->iv, in_ptr, 8);
315 for (j = 0; j < 8; j++) {
316 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
319 /* Next iv is next block of cipher text */
321 memcpy(ctx->iv, out_ptr, 8);
323 /* Process rest of buffer */
325 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
327 memcpy(ctx->buf + 8, in_ptr, inl - i);
328 for (j = 0; i < inl; j++, i++) {
329 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
333 memcpy(ctx->buf + 8, out_ptr, j);
340 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
341 const unsigned char *in, size_t inl)
343 const unsigned char *in_ptr = in;
344 unsigned char *out_ptr = out;
347 /* process partial block if any */
349 for (j = ctx->num, i = 0; j < 8 && i < inl;
350 j++, i++, in_ptr++, out_ptr++) {
351 *out_ptr = ctx->buf[j] ^ (*in_ptr);
361 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
363 * block cipher current iv
366 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
368 * xor next block of input text with it and output it
373 for (j = 0; j < 8; j++) {
374 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
377 /* Process rest of buffer */
379 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
380 for (j = 0; i < inl; j++, i++) {
381 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
390 /* Cleaning up of EVP_CIPHER_CTX */
391 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
393 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
394 ctx->app_data = NULL;
398 /* Control function for gost cipher */
399 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
402 case EVP_CTRL_RAND_KEY:
404 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
405 GOSTerr(GOST_F_GOST_CIPHER_CTL,
406 GOST_R_RANDOM_GENERATOR_ERROR);
411 case EVP_CTRL_PBE_PRF_NID:
413 *((int *)ptr) = NID_id_HMACGostR3411_94;
420 GOSTerr(GOST_F_GOST_CIPHER_CTL,
421 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
427 /* Set cipher parameters from ASN1 structure */
428 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
431 unsigned char *buf = NULL;
432 unsigned char *p = NULL;
433 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
434 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
435 ASN1_OCTET_STRING *os = NULL;
437 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
440 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
441 GOST_CIPHER_PARAMS_free(gcp);
442 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
445 ASN1_OBJECT_free(gcp->enc_param_set);
446 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
448 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
449 p = buf = OPENSSL_malloc(len);
451 GOST_CIPHER_PARAMS_free(gcp);
452 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
455 i2d_GOST_CIPHER_PARAMS(gcp, &p);
456 GOST_CIPHER_PARAMS_free(gcp);
458 os = ASN1_OCTET_STRING_new();
460 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
462 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
467 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
471 /* Store parameters into ASN1 structure */
472 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
476 GOST_CIPHER_PARAMS *gcp = NULL;
478 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
479 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
483 p = params->value.sequence->data;
485 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
486 params->value.sequence->length);
488 len = gcp->iv->length;
489 if (len != ctx->cipher->iv_len) {
490 GOST_CIPHER_PARAMS_free(gcp);
491 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
494 if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
495 GOST_CIPHER_PARAMS_free(gcp);
498 memcpy(ctx->oiv, gcp->iv->data, len);
500 GOST_CIPHER_PARAMS_free(gcp);
505 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
507 struct ossl_gost_imit_ctx *c = ctx->md_data;
508 memset(c->buffer, 0, sizeof(c->buffer));
509 memset(c->partial_block, 0, sizeof(c->partial_block));
513 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
517 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
518 const unsigned char *data)
520 unsigned char buffer[8];
522 * We are using local buffer for iv because CryptoPro doesn't interpret
523 * internal state of MAC algorithm as iv during keymeshing (but does
524 * initialize internal state from iv in key transport
526 assert(c->count % 8 == 0 && c->count <= 1024);
527 if (c->key_meshing && c->count == 1024) {
528 cryptopro_key_meshing(&(c->cctx), buffer);
530 mac_block(&(c->cctx), c->buffer, data);
531 c->count = c->count % 1024 + 8;
534 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
536 struct ossl_gost_imit_ctx *c = ctx->md_data;
537 const unsigned char *p = data;
538 size_t bytes = count, i;
540 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
544 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
545 c->partial_block[i] = *p;
548 mac_block_mesh(c, c->partial_block);
555 mac_block_mesh(c, p);
560 memcpy(c->partial_block, p, bytes);
562 c->bytes_left = bytes;
566 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
568 struct ossl_gost_imit_ctx *c = ctx->md_data;
570 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
573 if (c->count == 0 && c->bytes_left) {
574 unsigned char buffer[8];
575 memset(buffer, 0, 8);
576 gost_imit_update(ctx, buffer, 8);
580 for (i = c->bytes_left; i < 8; i++) {
581 c->partial_block[i] = 0;
583 mac_block_mesh(c, c->partial_block);
585 get_mac(c->buffer, 32, md);
589 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
592 case EVP_MD_CTRL_KEY_LEN:
593 *((unsigned int *)(ptr)) = 32;
595 case EVP_MD_CTRL_SET_KEY:
598 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
602 gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
604 ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
613 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
615 memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
619 /* Clean up imit ctx */
620 int gost_imit_cleanup(EVP_MD_CTX *ctx)
622 memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));