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,
99 sizeof(struct ossl_gost_imit_ctx),
104 * Correspondence between gost parameter OIDs and substitution blocks
105 * NID field is filed by register_gost_NID function in engine.c
106 * upon engine initialization
109 struct gost_cipher_info gost_cipher_list[] = {
116 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
118 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
120 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
122 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
124 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
126 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
131 * get encryption parameters from crypto network settings FIXME For now we
132 * use environment var CRYPT_PARAMS as place to store these settings.
133 * Actually, it is better to use engine control command, read from
134 * configuration file to set them
136 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
139 struct gost_cipher_info *param;
141 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
142 if (!params || !strlen(params))
143 return &gost_cipher_list[1];
145 nid = OBJ_txt2nid(params);
146 if (nid == NID_undef) {
147 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
148 GOST_R_INVALID_CIPHER_PARAM_OID);
152 nid = OBJ_obj2nid(obj);
154 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
156 if (!param->sblock) {
157 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
163 /* Sets cipher param from paramset NID. */
164 static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
166 const struct gost_cipher_info *param;
168 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
172 c->paramNID = param->nid;
173 c->key_meshing = param->key_meshing;
175 gost_init(&(c->cctx), param->sblock);
179 /* Initializes EVP_CIPHER_CTX by paramset NID */
180 static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
181 const unsigned char *key,
182 const unsigned char *iv, int enc,
183 int paramNID, int mode)
185 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
186 if (ctx->app_data == NULL) {
187 if (!gost_cipher_set_param(c, paramNID))
189 ctx->app_data = ctx->cipher_data;
192 gost_key(&(c->cctx), key);
194 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
195 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
199 static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
200 const unsigned char *iv, int enc)
202 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
203 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
207 gost_key(&(c->cctx), key);
209 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
210 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
214 /* Initializes EVP_CIPHER_CTX with default values */
215 int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
216 const unsigned char *iv, int enc)
218 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
223 * Wrapper around gostcrypt function from gost89.c which perform key meshing
226 static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
228 struct ossl_gost_cipher_ctx *c = ctx;
229 assert(c->count % 8 == 0 && c->count <= 1024);
230 if (c->key_meshing && c->count == 1024) {
231 cryptopro_key_meshing(&(c->cctx), iv);
233 gostcrypt(&(c->cctx), iv, buf);
234 c->count = c->count % 1024 + 8;
237 static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
239 struct ossl_gost_cipher_ctx *c = ctx;
241 unsigned char buf1[8];
242 assert(c->count % 8 == 0 && c->count <= 1024);
243 if (c->key_meshing && c->count == 1024) {
244 cryptopro_key_meshing(&(c->cctx), iv);
247 gostcrypt(&(c->cctx), iv, buf1);
251 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | ((word32) buf1[3] << 24);
253 buf1[0] = (unsigned char)(g & 0xff);
254 buf1[1] = (unsigned char)((g >> 8) & 0xff);
255 buf1[2] = (unsigned char)((g >> 16) & 0xff);
256 buf1[3] = (unsigned char)((g >> 24) & 0xff);
257 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | ((word32) buf1[7] << 24);
260 if (go > g) /* overflow */
262 buf1[4] = (unsigned char)(g & 0xff);
263 buf1[5] = (unsigned char)((g >> 8) & 0xff);
264 buf1[6] = (unsigned char)((g >> 16) & 0xff);
265 buf1[7] = (unsigned char)((g >> 24) & 0xff);
267 gostcrypt(&(c->cctx), buf1, buf);
268 c->count = c->count % 1024 + 8;
271 /* GOST encryption in CFB mode */
272 int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
273 const unsigned char *in, size_t inl)
275 const unsigned char *in_ptr = in;
276 unsigned char *out_ptr = out;
279 /* process partial block if any */
281 for (j = ctx->num, i = 0; j < 8 && i < inl;
282 j++, i++, in_ptr++, out_ptr++) {
284 ctx->buf[j + 8] = *in_ptr;
285 *out_ptr = ctx->buf[j] ^ (*in_ptr);
287 ctx->buf[j + 8] = *out_ptr;
290 memcpy(ctx->iv, ctx->buf + 8, 8);
298 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
300 * block cipher current iv
302 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
304 * xor next block of input text with it and output it
310 memcpy(ctx->iv, in_ptr, 8);
311 for (j = 0; j < 8; j++) {
312 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
315 /* Next iv is next block of cipher text */
317 memcpy(ctx->iv, out_ptr, 8);
319 /* Process rest of buffer */
321 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
323 memcpy(ctx->buf + 8, in_ptr, inl - i);
324 for (j = 0; i < inl; j++, i++) {
325 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
329 memcpy(ctx->buf + 8, out_ptr, j);
336 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
337 const unsigned char *in, size_t inl)
339 const unsigned char *in_ptr = in;
340 unsigned char *out_ptr = out;
343 /* process partial block if any */
345 for (j = ctx->num, i = 0; j < 8 && i < inl;
346 j++, i++, in_ptr++, out_ptr++) {
347 *out_ptr = ctx->buf[j] ^ (*in_ptr);
357 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
359 * block cipher current iv
362 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
364 * xor next block of input text with it and output it
369 for (j = 0; j < 8; j++) {
370 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
373 /* Process rest of buffer */
375 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
376 for (j = 0; i < inl; j++, i++) {
377 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
386 /* Cleaning up of EVP_CIPHER_CTX */
387 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
389 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
390 ctx->app_data = NULL;
394 /* Control function for gost cipher */
395 int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
398 case EVP_CTRL_RAND_KEY:
400 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
401 GOSTerr(GOST_F_GOST_CIPHER_CTL,
402 GOST_R_RANDOM_GENERATOR_ERROR);
407 case EVP_CTRL_PBE_PRF_NID:
409 *((int *)ptr) = NID_id_HMACGostR3411_94;
416 GOSTerr(GOST_F_GOST_CIPHER_CTL,
417 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
423 /* Set cipher parameters from ASN1 structure */
424 int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
427 unsigned char *buf = NULL;
428 unsigned char *p = NULL;
429 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
430 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
431 ASN1_OCTET_STRING *os = NULL;
433 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
436 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
437 GOST_CIPHER_PARAMS_free(gcp);
438 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
441 ASN1_OBJECT_free(gcp->enc_param_set);
442 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
444 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
445 p = buf = OPENSSL_malloc(len);
447 GOST_CIPHER_PARAMS_free(gcp);
448 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
451 i2d_GOST_CIPHER_PARAMS(gcp, &p);
452 GOST_CIPHER_PARAMS_free(gcp);
454 os = ASN1_OCTET_STRING_new();
456 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
458 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
463 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
467 /* Store parameters into ASN1 structure */
468 int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
472 GOST_CIPHER_PARAMS *gcp = NULL;
474 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
475 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
479 p = params->value.sequence->data;
481 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
482 params->value.sequence->length);
484 len = gcp->iv->length;
485 if (len != ctx->cipher->iv_len) {
486 GOST_CIPHER_PARAMS_free(gcp);
487 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
490 if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
491 GOST_CIPHER_PARAMS_free(gcp);
494 memcpy(ctx->oiv, gcp->iv->data, len);
496 GOST_CIPHER_PARAMS_free(gcp);
501 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
503 struct ossl_gost_imit_ctx *c = ctx->md_data;
504 memset(c->buffer, 0, sizeof(c->buffer));
505 memset(c->partial_block, 0, sizeof(c->partial_block));
509 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
513 static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
514 const unsigned char *data)
516 unsigned char buffer[8];
518 * We are using local buffer for iv because CryptoPro doesn't interpret
519 * internal state of MAC algorithm as iv during keymeshing (but does
520 * initialize internal state from iv in key transport
522 assert(c->count % 8 == 0 && c->count <= 1024);
523 if (c->key_meshing && c->count == 1024) {
524 cryptopro_key_meshing(&(c->cctx), buffer);
526 mac_block(&(c->cctx), c->buffer, data);
527 c->count = c->count % 1024 + 8;
530 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
532 struct ossl_gost_imit_ctx *c = ctx->md_data;
533 const unsigned char *p = data;
534 size_t bytes = count, i;
536 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
540 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
541 c->partial_block[i] = *p;
544 mac_block_mesh(c, c->partial_block);
551 mac_block_mesh(c, p);
556 memcpy(c->partial_block, p, bytes);
558 c->bytes_left = bytes;
562 int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
564 struct ossl_gost_imit_ctx *c = ctx->md_data;
566 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
569 if (c->count == 0 && c->bytes_left) {
570 unsigned char buffer[8];
571 memset(buffer, 0, 8);
572 gost_imit_update(ctx, buffer, 8);
576 for (i = c->bytes_left; i < 8; i++) {
577 c->partial_block[i] = 0;
579 mac_block_mesh(c, c->partial_block);
581 get_mac(c->buffer, 32, md);
585 int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
588 case EVP_MD_CTRL_KEY_LEN:
589 *((unsigned int *)(ptr)) = 32;
591 case EVP_MD_CTRL_SET_KEY:
594 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
598 gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
600 ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
609 int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
611 memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
615 /* Clean up imit ctx */
616 int gost_imit_cleanup(EVP_MD_CTX *ctx)
618 memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));