Deleted Added
full compact
gost_crypt.c (256281) gost_crypt.c (280304)
1/**********************************************************************
2 * gost_crypt.c *
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
5 * *
6 * OpenSSL interface to GOST 28147-89 cipher functions *
7 * Requires OpenSSL 0.9.9 for compilation *
8 **********************************************************************/

--- 5 unchanged lines hidden (view full) ---

14
15#if !defined(CCGOST_DEBUG) && !defined(DEBUG)
16# ifndef NDEBUG
17# define NDEBUG
18# endif
19#endif
20#include <assert.h>
21
1/**********************************************************************
2 * gost_crypt.c *
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
5 * *
6 * OpenSSL interface to GOST 28147-89 cipher functions *
7 * Requires OpenSSL 0.9.9 for compilation *
8 **********************************************************************/

--- 5 unchanged lines hidden (view full) ---

14
15#if !defined(CCGOST_DEBUG) && !defined(DEBUG)
16# ifndef NDEBUG
17# define NDEBUG
18# endif
19#endif
20#include <assert.h>
21
22static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
23 const unsigned char *iv, int enc);
24static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
25 const unsigned char *iv, int enc);
26/* Handles block of data in CFB mode */
27static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
28 const unsigned char *in, size_t inl);
29/* Handles block of data in CNT mode */
30static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
31 const unsigned char *in, size_t inl);
32/* Cleanup function */
22static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
23 const unsigned char *iv, int enc);
24static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
25 const unsigned char *iv, int enc);
26/* Handles block of data in CFB mode */
27static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
28 const unsigned char *in, size_t inl);
29/* Handles block of data in CNT mode */
30static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
31 const unsigned char *in, size_t inl);
32/* Cleanup function */
33static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
34/* set/get cipher parameters */
33static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
34/* set/get cipher parameters */
35static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params);
36static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params);
35static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
36static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
37/* Control function */
37/* Control function */
38static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr);
38static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
39
39
40EVP_CIPHER cipher_gost =
41 {
42 NID_id_Gost28147_89,
43 1,/*block_size*/
44 32,/*key_size*/
45 8,/*iv_len */
46 EVP_CIPH_CFB_MODE| EVP_CIPH_NO_PADDING |
47 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
48 gost_cipher_init,
49 gost_cipher_do_cfb,
50 gost_cipher_cleanup,
51 sizeof(struct ossl_gost_cipher_ctx),/* ctx_size */
52 gost89_set_asn1_parameters,
53 gost89_get_asn1_parameters,
54 gost_cipher_ctl,
55 NULL,
56 };
40EVP_CIPHER cipher_gost = {
41 NID_id_Gost28147_89,
42 1, /* block_size */
43 32, /* key_size */
44 8, /* iv_len */
45 EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
46 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
47 gost_cipher_init,
48 gost_cipher_do_cfb,
49 gost_cipher_cleanup,
50 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
51 gost89_set_asn1_parameters,
52 gost89_get_asn1_parameters,
53 gost_cipher_ctl,
54 NULL,
55};
57
56
58EVP_CIPHER cipher_gost_cpacnt =
59 {
60 NID_gost89_cnt,
61 1,/*block_size*/
62 32,/*key_size*/
63 8,/*iv_len */
64 EVP_CIPH_OFB_MODE| EVP_CIPH_NO_PADDING |
65 EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
66 gost_cipher_init_cpa,
67 gost_cipher_do_cnt,
68 gost_cipher_cleanup,
69 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
70 gost89_set_asn1_parameters,
71 gost89_get_asn1_parameters,
72 gost_cipher_ctl,
73 NULL,
74 };
57EVP_CIPHER cipher_gost_cpacnt = {
58 NID_gost89_cnt,
59 1, /* block_size */
60 32, /* key_size */
61 8, /* iv_len */
62 EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
63 EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
64 gost_cipher_init_cpa,
65 gost_cipher_do_cnt,
66 gost_cipher_cleanup,
67 sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
68 gost89_set_asn1_parameters,
69 gost89_get_asn1_parameters,
70 gost_cipher_ctl,
71 NULL,
72};
75
76/* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
77/* Init functions which set specific parameters */
78static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
79/* process block of data */
80static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
81/* Return computed value */
73
74/* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
75/* Init functions which set specific parameters */
76static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
77/* process block of data */
78static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
79/* Return computed value */
82static int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md);
80static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
83/* Copies context */
81/* Copies context */
84static int gost_imit_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from);
82static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
85static int gost_imit_cleanup(EVP_MD_CTX *ctx);
86/* Control function, knows how to set MAC key.*/
83static int gost_imit_cleanup(EVP_MD_CTX *ctx);
84/* Control function, knows how to set MAC key.*/
87static int gost_imit_ctrl(EVP_MD_CTX *ctx,int type, int arg, void *ptr);
85static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
88
86
89EVP_MD imit_gost_cpa =
90 {
91 NID_id_Gost28147_89_MAC,
92 NID_undef,
93 4,
94 0,
95 gost_imit_init_cpa,
96 gost_imit_update,
97 gost_imit_final,
98 gost_imit_copy,
99 gost_imit_cleanup,
100 NULL,
101 NULL,
102 {0,0,0,0,0},
103 8,
104 sizeof(struct ossl_gost_imit_ctx),
105 gost_imit_ctrl
106 };
87EVP_MD imit_gost_cpa = {
88 NID_id_Gost28147_89_MAC,
89 NID_undef,
90 4,
91 0,
92 gost_imit_init_cpa,
93 gost_imit_update,
94 gost_imit_final,
95 gost_imit_copy,
96 gost_imit_cleanup,
97 NULL,
98 NULL,
99 {0, 0, 0, 0, 0},
100 8,
101 sizeof(struct ossl_gost_imit_ctx),
102 gost_imit_ctrl
103};
107
104
108/*
105/*
109 * Correspondence between gost parameter OIDs and substitution blocks
110 * NID field is filed by register_gost_NID function in engine.c
111 * upon engine initialization
112 */
113
106 * Correspondence between gost parameter OIDs and substitution blocks
107 * NID field is filed by register_gost_NID function in engine.c
108 * upon engine initialization
109 */
110
114struct gost_cipher_info gost_cipher_list[]=
115 {
116/* NID */ /* Subst block */ /* Key meshing*/
117/*{NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},*/
118 {NID_id_Gost28147_89_cc,&GostR3411_94_CryptoProParamSet,0},
119 {NID_id_Gost28147_89_CryptoPro_A_ParamSet,&Gost28147_CryptoProParamSetA,1},
120 {NID_id_Gost28147_89_CryptoPro_B_ParamSet,&Gost28147_CryptoProParamSetB,1},
121 {NID_id_Gost28147_89_CryptoPro_C_ParamSet,&Gost28147_CryptoProParamSetC,1},
122 {NID_id_Gost28147_89_CryptoPro_D_ParamSet,&Gost28147_CryptoProParamSetD,1},
123 {NID_id_Gost28147_89_TestParamSet,&Gost28147_TestParamSet,1},
124 {NID_undef,NULL,0}
125 };
111struct gost_cipher_info gost_cipher_list[] = {
112 /*- NID *//*
113 * Subst block
114 *//*
115 * Key meshing
116 */
117 /*
118 * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
119 */
120 {NID_id_Gost28147_89_cc, &GostR3411_94_CryptoProParamSet, 0},
121 {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
122 1},
123 {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
124 1},
125 {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
126 1},
127 {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
128 1},
129 {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
130 {NID_undef, NULL, 0}
131};
126
132
127/* get encryption parameters from crypto network settings
128 FIXME For now we use environment var CRYPT_PARAMS as place to
129 store these settings. Actually, it is better to use engine control command, read from configuration file to set them */
133/*
134 * get encryption parameters from crypto network settings FIXME For now we
135 * use environment var CRYPT_PARAMS as place to store these settings.
136 * Actually, it is better to use engine control command, read from
137 * configuration file to set them
138 */
130const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
139const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
131 {
132 int nid;
133 struct gost_cipher_info *param;
134 if (!obj)
135 {
136 const char * params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
137 if (!params || !strlen(params))
138 return &gost_cipher_list[1];
140{
141 int nid;
142 struct gost_cipher_info *param;
143 if (!obj) {
144 const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
145 if (!params || !strlen(params))
146 return &gost_cipher_list[1];
139
147
140 nid = OBJ_txt2nid(params);
141 if (nid == NID_undef)
142 {
143 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
144 GOST_R_INVALID_CIPHER_PARAM_OID);
145 return NULL;
146 }
147 }
148 else
149 {
150 nid= OBJ_obj2nid(obj);
151 }
152 for (param=gost_cipher_list;param->sblock!=NULL && param->nid!=nid;
153 param++);
154 if (!param->sblock)
155 {
156 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,GOST_R_INVALID_CIPHER_PARAMS);
157 return NULL;
158 }
159 return param;
160 }
148 nid = OBJ_txt2nid(params);
149 if (nid == NID_undef) {
150 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
151 GOST_R_INVALID_CIPHER_PARAM_OID);
152 return NULL;
153 }
154 } else {
155 nid = OBJ_obj2nid(obj);
156 }
157 for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
158 param++) ;
159 if (!param->sblock) {
160 GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
161 return NULL;
162 }
163 return param;
164}
161
162/* Sets cipher param from paramset NID. */
165
166/* Sets cipher param from paramset NID. */
163static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c,int nid)
164 {
165 const struct gost_cipher_info *param;
166 param=get_encryption_params((nid==NID_undef?NULL:OBJ_nid2obj(nid)));
167 if (!param) return 0;
168
169 c->paramNID = param->nid;
170 c->key_meshing=param->key_meshing;
171 c->count=0;
172 gost_init(&(c->cctx), param->sblock);
173 return 1;
174 }
167static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
168{
169 const struct gost_cipher_info *param;
170 param =
171 get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
172 if (!param)
173 return 0;
175
174
175 c->paramNID = param->nid;
176 c->key_meshing = param->key_meshing;
177 c->count = 0;
178 gost_init(&(c->cctx), param->sblock);
179 return 1;
180}
181
176/* Initializes EVP_CIPHER_CTX by paramset NID */
182/* Initializes EVP_CIPHER_CTX by paramset NID */
177static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx, const unsigned char *key,
178 const unsigned char *iv, int enc, int paramNID,int mode)
179 {
180 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
181 if (ctx->app_data == NULL)
182 {
183 if (!gost_cipher_set_param(c,paramNID)) return 0;
184 ctx->app_data = ctx->cipher_data;
185 }
186 if (key) gost_key(&(c->cctx),key);
187 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
188 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
189 return 1;
190 }
183static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
184 const unsigned char *key,
185 const unsigned char *iv, int enc,
186 int paramNID, int mode)
187{
188 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
189 if (ctx->app_data == NULL) {
190 if (!gost_cipher_set_param(c, paramNID))
191 return 0;
192 ctx->app_data = ctx->cipher_data;
193 }
194 if (key)
195 gost_key(&(c->cctx), key);
196 if (iv)
197 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
198 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
199 return 1;
200}
191
192static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
201
202static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
193 const unsigned char *iv, int enc)
194 {
195 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
196 gost_init(&(c->cctx),&Gost28147_CryptoProParamSetA);
197 c->key_meshing=1;
198 c->count=0;
199 if(key) gost_key(&(c->cctx),key);
200 if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
201 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
202 return 1;
203 }
203 const unsigned char *iv, int enc)
204{
205 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
206 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
207 c->key_meshing = 1;
208 c->count = 0;
209 if (key)
210 gost_key(&(c->cctx), key);
211 if (iv)
212 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
213 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
214 return 1;
215}
204
205/* Initializes EVP_CIPHER_CTX with default values */
206int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
216
217/* Initializes EVP_CIPHER_CTX with default values */
218int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
207 const unsigned char *iv, int enc)
208 {
209 return gost_cipher_init_param(ctx,key,iv,enc,NID_undef,EVP_CIPH_CFB_MODE);
210 }
211/* Wrapper around gostcrypt function from gost89.c which perform
212 * key meshing when nesseccary
219 const unsigned char *iv, int enc)
220{
221 return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
222 EVP_CIPH_CFB_MODE);
223}
224
225/*
226 * Wrapper around gostcrypt function from gost89.c which perform key meshing
227 * when nesseccary
213 */
228 */
214static void gost_crypt_mesh (void *ctx,unsigned char *iv,unsigned char *buf)
215 {
216 struct ossl_gost_cipher_ctx *c = ctx;
217 assert(c->count%8 == 0 && c->count <= 1024);
218 if (c->key_meshing && c->count==1024)
219 {
220 cryptopro_key_meshing(&(c->cctx),iv);
221 }
222 gostcrypt(&(c->cctx),iv,buf);
223 c->count = c->count%1024 + 8;
224 }
229static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
230{
231 struct ossl_gost_cipher_ctx *c = ctx;
232 assert(c->count % 8 == 0 && c->count <= 1024);
233 if (c->key_meshing && c->count == 1024) {
234 cryptopro_key_meshing(&(c->cctx), iv);
235 }
236 gostcrypt(&(c->cctx), iv, buf);
237 c->count = c->count % 1024 + 8;
238}
225
239
226static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
227 {
228 struct ossl_gost_cipher_ctx *c = ctx;
229 word32 g,go;
230 unsigned char buf1[8];
231 assert(c->count%8 == 0 && c->count <= 1024);
232 if (c->key_meshing && c->count==1024)
233 {
234 cryptopro_key_meshing(&(c->cctx),iv);
235 }
236 if (c->count==0)
237 {
238 gostcrypt(&(c->cctx),iv,buf1);
239 }
240 else
241 {
242 memcpy(buf1,iv,8);
243 }
244 g = buf1[0]|(buf1[1]<<8)|(buf1[2]<<16)|(buf1[3]<<24);
245 g += 0x01010101;
246 buf1[0]=(unsigned char)(g&0xff);
247 buf1[1]=(unsigned char)((g>>8)&0xff);
248 buf1[2]=(unsigned char)((g>>16)&0xff);
249 buf1[3]=(unsigned char)((g>>24)&0xff);
250 g = buf1[4]|(buf1[5]<<8)|(buf1[6]<<16)|(buf1[7]<<24);
251 go = g;
252 g += 0x01010104;
253 if (go > g) /* overflow*/
254 g++;
255 buf1[4]=(unsigned char)(g&0xff);
256 buf1[5]=(unsigned char)((g>>8)&0xff);
257 buf1[6]=(unsigned char)((g>>16)&0xff);
258 buf1[7]=(unsigned char)((g>>24)&0xff);
259 memcpy(iv,buf1,8);
260 gostcrypt(&(c->cctx),buf1,buf);
261 c->count = c->count%1024 + 8;
262 }
240static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
241{
242 struct ossl_gost_cipher_ctx *c = ctx;
243 word32 g, go;
244 unsigned char buf1[8];
245 assert(c->count % 8 == 0 && c->count <= 1024);
246 if (c->key_meshing && c->count == 1024) {
247 cryptopro_key_meshing(&(c->cctx), iv);
248 }
249 if (c->count == 0) {
250 gostcrypt(&(c->cctx), iv, buf1);
251 } else {
252 memcpy(buf1, iv, 8);
253 }
254 g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | (buf1[3] << 24);
255 g += 0x01010101;
256 buf1[0] = (unsigned char)(g & 0xff);
257 buf1[1] = (unsigned char)((g >> 8) & 0xff);
258 buf1[2] = (unsigned char)((g >> 16) & 0xff);
259 buf1[3] = (unsigned char)((g >> 24) & 0xff);
260 g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | (buf1[7] << 24);
261 go = g;
262 g += 0x01010104;
263 if (go > g) /* overflow */
264 g++;
265 buf1[4] = (unsigned char)(g & 0xff);
266 buf1[5] = (unsigned char)((g >> 8) & 0xff);
267 buf1[6] = (unsigned char)((g >> 16) & 0xff);
268 buf1[7] = (unsigned char)((g >> 24) & 0xff);
269 memcpy(iv, buf1, 8);
270 gostcrypt(&(c->cctx), buf1, buf);
271 c->count = c->count % 1024 + 8;
272}
263
264/* GOST encryption in CFB mode */
273
274/* GOST encryption in CFB mode */
265int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
266 const unsigned char *in, size_t inl)
267 {
268 const unsigned char *in_ptr=in;
269 unsigned char *out_ptr=out;
270 size_t i=0;
271 size_t j=0;
275int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
276 const unsigned char *in, size_t inl)
277{
278 const unsigned char *in_ptr = in;
279 unsigned char *out_ptr = out;
280 size_t i = 0;
281 size_t j = 0;
272/* process partial block if any */
282/* process partial block if any */
273 if (ctx->num)
274 {
275 for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++)
276 {
277 if (!ctx->encrypt) ctx->buf[j+8]=*in_ptr;
278 *out_ptr=ctx->buf[j]^(*in_ptr);
279 if (ctx->encrypt) ctx->buf[j+8]=*out_ptr;
280 }
281 if (j==8)
282 {
283 memcpy(ctx->iv,ctx->buf+8,8);
284 ctx->num=0;
285 }
286 else
287 {
288 ctx->num=j;
289 return 1;
290 }
291 }
283 if (ctx->num) {
284 for (j = ctx->num, i = 0; j < 8 && i < inl;
285 j++, i++, in_ptr++, out_ptr++) {
286 if (!ctx->encrypt)
287 ctx->buf[j + 8] = *in_ptr;
288 *out_ptr = ctx->buf[j] ^ (*in_ptr);
289 if (ctx->encrypt)
290 ctx->buf[j + 8] = *out_ptr;
291 }
292 if (j == 8) {
293 memcpy(ctx->iv, ctx->buf + 8, 8);
294 ctx->num = 0;
295 } else {
296 ctx->num = j;
297 return 1;
298 }
299 }
292
300
293 for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8)
294 {
295 /*block cipher current iv */
296 gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
297 /*xor next block of input text with it and output it*/
298 /*output this block */
299 if (!ctx->encrypt) memcpy(ctx->iv,in_ptr,8);
300 for (j=0;j<8;j++)
301 {
302 out_ptr[j]=ctx->buf[j]^in_ptr[j];
303 }
304 /* Encrypt */
305 /* Next iv is next block of cipher text*/
306 if (ctx->encrypt) memcpy(ctx->iv,out_ptr,8);
307 }
301 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
302 /*
303 * block cipher current iv
304 */
305 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
306 /*
307 * xor next block of input text with it and output it
308 */
309 /*
310 * output this block
311 */
312 if (!ctx->encrypt)
313 memcpy(ctx->iv, in_ptr, 8);
314 for (j = 0; j < 8; j++) {
315 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
316 }
317 /* Encrypt */
318 /* Next iv is next block of cipher text */
319 if (ctx->encrypt)
320 memcpy(ctx->iv, out_ptr, 8);
321 }
308/* Process rest of buffer */
322/* Process rest of buffer */
309 if (i<inl)
310 {
311 gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
312 if (!ctx->encrypt) memcpy(ctx->buf+8,in_ptr,inl-i);
313 for (j=0;i<inl;j++,i++)
314 {
315 out_ptr[j]=ctx->buf[j]^in_ptr[j];
316 }
317 ctx->num = j;
318 if (ctx->encrypt) memcpy(ctx->buf+8,out_ptr,j);
319 }
320 else
321 {
322 ctx->num = 0;
323 }
324 return 1;
325 }
323 if (i < inl) {
324 gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
325 if (!ctx->encrypt)
326 memcpy(ctx->buf + 8, in_ptr, inl - i);
327 for (j = 0; i < inl; j++, i++) {
328 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
329 }
330 ctx->num = j;
331 if (ctx->encrypt)
332 memcpy(ctx->buf + 8, out_ptr, j);
333 } else {
334 ctx->num = 0;
335 }
336 return 1;
337}
326
327static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
338
339static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
328 const unsigned char *in, size_t inl)
329 {
330 const unsigned char *in_ptr=in;
331 unsigned char *out_ptr=out;
332 size_t i=0;
333 size_t j;
340 const unsigned char *in, size_t inl)
341{
342 const unsigned char *in_ptr = in;
343 unsigned char *out_ptr = out;
344 size_t i = 0;
345 size_t j;
334/* process partial block if any */
346/* process partial block if any */
335 if (ctx->num)
336 {
337 for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++)
338 {
339 *out_ptr=ctx->buf[j]^(*in_ptr);
340 }
341 if (j==8)
342 {
343 ctx->num=0;
344 }
345 else
346 {
347 ctx->num=j;
348 return 1;
349 }
350 }
347 if (ctx->num) {
348 for (j = ctx->num, i = 0; j < 8 && i < inl;
349 j++, i++, in_ptr++, out_ptr++) {
350 *out_ptr = ctx->buf[j] ^ (*in_ptr);
351 }
352 if (j == 8) {
353 ctx->num = 0;
354 } else {
355 ctx->num = j;
356 return 1;
357 }
358 }
351
359
352 for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8)
353 {
354 /*block cipher current iv */
355 /* Encrypt */
356 gost_cnt_next(ctx->cipher_data,ctx->iv,ctx->buf);
357 /*xor next block of input text with it and output it*/
358 /*output this block */
359 for (j=0;j<8;j++)
360 {
361 out_ptr[j]=ctx->buf[j]^in_ptr[j];
362 }
363 }
360 for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
361 /*
362 * block cipher current iv
363 */
364 /* Encrypt */
365 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
366 /*
367 * xor next block of input text with it and output it
368 */
369 /*
370 * output this block
371 */
372 for (j = 0; j < 8; j++) {
373 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
374 }
375 }
364/* Process rest of buffer */
376/* Process rest of buffer */
365 if (i<inl)
366 {
367 gost_cnt_next(ctx->cipher_data,ctx->iv,ctx->buf);
368 for (j=0;i<inl;j++,i++)
369 {
370 out_ptr[j]=ctx->buf[j]^in_ptr[j];
371 }
372 ctx->num = j;
373 }
374 else
375 {
376 ctx->num = 0;
377 }
378 return 1;
379 }
377 if (i < inl) {
378 gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
379 for (j = 0; i < inl; j++, i++) {
380 out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
381 }
382 ctx->num = j;
383 } else {
384 ctx->num = 0;
385 }
386 return 1;
387}
380
381/* Cleaning up of EVP_CIPHER_CTX */
388
389/* Cleaning up of EVP_CIPHER_CTX */
382int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
383 {
384 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
385 ctx->app_data = NULL;
386 return 1;
387 }
390int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
391{
392 gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
393 ctx->app_data = NULL;
394 return 1;
395}
388
389/* Control function for gost cipher */
396
397/* Control function for gost cipher */
390int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr)
391 {
392 switch (type)
393 {
394 case EVP_CTRL_RAND_KEY:
395 {
396 if (RAND_bytes((unsigned char *)ptr,ctx->key_len)<=0)
397 {
398 GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_RANDOM_GENERATOR_ERROR);
399 return -1;
400 }
401 break;
402 }
403 case EVP_CTRL_PBE_PRF_NID:
404 if (ptr) {
405 *((int *)ptr)= NID_id_HMACGostR3411_94;
406 return 1;
407 } else {
408 return 0;
409 }
410
411 default:
412 GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
413 return -1;
414 }
415 return 1;
416 }
398int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
399{
400 switch (type) {
401 case EVP_CTRL_RAND_KEY:
402 {
403 if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
404 GOSTerr(GOST_F_GOST_CIPHER_CTL,
405 GOST_R_RANDOM_GENERATOR_ERROR);
406 return -1;
407 }
408 break;
409 }
410 case EVP_CTRL_PBE_PRF_NID:
411 if (ptr) {
412 *((int *)ptr) = NID_id_HMACGostR3411_94;
413 return 1;
414 } else {
415 return 0;
416 }
417
417
418 default:
419 GOSTerr(GOST_F_GOST_CIPHER_CTL,
420 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
421 return -1;
422 }
423 return 1;
424}
425
418/* Set cipher parameters from ASN1 structure */
426/* Set cipher parameters from ASN1 structure */
419int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
420 {
421 int len=0;
422 unsigned char *buf=NULL;
423 unsigned char *p=NULL;
424 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
425 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
426 ASN1_OCTET_STRING *os = NULL;
427 if (!gcp)
428 {
429 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
430 return 0;
431 }
432 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len))
433 {
434 GOST_CIPHER_PARAMS_free(gcp);
435 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
436 return 0;
437 }
438 ASN1_OBJECT_free(gcp->enc_param_set);
439 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
427int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
428{
429 int len = 0;
430 unsigned char *buf = NULL;
431 unsigned char *p = NULL;
432 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
433 GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
434 ASN1_OCTET_STRING *os = NULL;
435 if (!gcp) {
436 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
437 return 0;
438 }
439 if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
440 GOST_CIPHER_PARAMS_free(gcp);
441 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
442 return 0;
443 }
444 ASN1_OBJECT_free(gcp->enc_param_set);
445 gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
440
446
441 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
442 p = buf = (unsigned char*)OPENSSL_malloc(len);
443 if (!buf)
444 {
445 GOST_CIPHER_PARAMS_free(gcp);
446 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
447 return 0;
448 }
449 i2d_GOST_CIPHER_PARAMS(gcp, &p);
450 GOST_CIPHER_PARAMS_free(gcp);
447 len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
448 p = buf = (unsigned char *)OPENSSL_malloc(len);
449 if (!buf) {
450 GOST_CIPHER_PARAMS_free(gcp);
451 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
452 return 0;
453 }
454 i2d_GOST_CIPHER_PARAMS(gcp, &p);
455 GOST_CIPHER_PARAMS_free(gcp);
451
456
452 os = ASN1_OCTET_STRING_new();
457 os = ASN1_OCTET_STRING_new();
453
458
454 if(!os || !ASN1_OCTET_STRING_set(os, buf, len))
455 {
456 OPENSSL_free(buf);
457 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
458 return 0;
459 }
460 OPENSSL_free(buf);
459 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
460 OPENSSL_free(buf);
461 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
462 return 0;
463 }
464 OPENSSL_free(buf);
461
465
462 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
463 return 1;
464 }
466 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
467 return 1;
468}
465
466/* Store parameters into ASN1 structure */
469
470/* Store parameters into ASN1 structure */
467int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
468 {
469 int ret = -1;
470 int len;
471 GOST_CIPHER_PARAMS *gcp = NULL;
472 unsigned char *p;
473 struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
474 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE)
475 {
476 return ret;
477 }
471int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
472{
473 int ret = -1;
474 int len;
475 GOST_CIPHER_PARAMS *gcp = NULL;
476 unsigned char *p;
477 struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
478 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
479 return ret;
480 }
478
481
479 p = params->value.sequence->data;
482 p = params->value.sequence->data;
480
483
481 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
482 params->value.sequence->length);
484 gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
485 params->value.sequence->length);
483
486
484 len = gcp->iv->length;
485 if (len != ctx->cipher->iv_len)
486 {
487 GOST_CIPHER_PARAMS_free(gcp);
488 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS,
489 GOST_R_INVALID_IV_LENGTH);
490 return -1;
491 }
492 if (!gost_cipher_set_param(c,OBJ_obj2nid(gcp->enc_param_set)))
493 {
494 GOST_CIPHER_PARAMS_free(gcp);
495 return -1;
496 }
497 memcpy(ctx->oiv, gcp->iv->data, len);
487 len = gcp->iv->length;
488 if (len != ctx->cipher->iv_len) {
489 GOST_CIPHER_PARAMS_free(gcp);
490 GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
491 return -1;
492 }
493 if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
494 GOST_CIPHER_PARAMS_free(gcp);
495 return -1;
496 }
497 memcpy(ctx->oiv, gcp->iv->data, len);
498
498
499 GOST_CIPHER_PARAMS_free(gcp);
499 GOST_CIPHER_PARAMS_free(gcp);
500
500
501 return 1;
502 }
501 return 1;
502}
503
503
504
505int gost_imit_init_cpa(EVP_MD_CTX *ctx)
504int gost_imit_init_cpa(EVP_MD_CTX *ctx)
506 {
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));
510 c->count = 0;
511 c->bytes_left=0;
512 c->key_meshing=1;
513 gost_init(&(c->cctx),&Gost28147_CryptoProParamSetA);
514 return 1;
515 }
505{
506 struct ossl_gost_imit_ctx *c = ctx->md_data;
507 memset(c->buffer, 0, sizeof(c->buffer));
508 memset(c->partial_block, 0, sizeof(c->partial_block));
509 c->count = 0;
510 c->bytes_left = 0;
511 c->key_meshing = 1;
512 gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
513 return 1;
514}
516
515
517static void mac_block_mesh(struct ossl_gost_imit_ctx *c,const unsigned char *data)
518 {
519 unsigned char buffer[8];
520 /* We are using local buffer for iv because CryptoPro doesn't
521 * interpret internal state of MAC algorithm as iv during keymeshing
522 * (but does initialize internal state from iv in key transport
523 */
524 assert(c->count%8 == 0 && c->count <= 1024);
525 if (c->key_meshing && c->count==1024)
526 {
527 cryptopro_key_meshing(&(c->cctx),buffer);
528 }
529 mac_block(&(c->cctx),c->buffer,data);
530 c->count = c->count%1024 + 8;
531 }
516static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
517 const unsigned char *data)
518{
519 unsigned char buffer[8];
520 /*
521 * We are using local buffer for iv because CryptoPro doesn't interpret
522 * internal state of MAC algorithm as iv during keymeshing (but does
523 * initialize internal state from iv in key transport
524 */
525 assert(c->count % 8 == 0 && c->count <= 1024);
526 if (c->key_meshing && c->count == 1024) {
527 cryptopro_key_meshing(&(c->cctx), buffer);
528 }
529 mac_block(&(c->cctx), c->buffer, data);
530 c->count = c->count % 1024 + 8;
531}
532
533int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
532
533int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
534 {
535 struct ossl_gost_imit_ctx *c = ctx->md_data;
536 const unsigned char *p = data;
537 size_t bytes = count,i;
538 if (!(c->key_set)) {
539 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
540 return 0;
541 }
542 if (c->bytes_left)
543 {
544 for (i=c->bytes_left;i<8&&bytes>0;bytes--,i++,p++)
545 {
546 c->partial_block[i]=*p;
547 }
548 if (i==8)
549 {
550 mac_block_mesh(c,c->partial_block);
551 }
552 else
553 {
554 c->bytes_left = i;
555 return 1;
556 }
557 }
558 while (bytes>8)
559 {
560 mac_block_mesh(c,p);
561 p+=8;
562 bytes-=8;
563 }
564 if (bytes>0)
565 {
566 memcpy(c->partial_block,p,bytes);
567 }
568 c->bytes_left=bytes;
569 return 1;
570 }
534{
535 struct ossl_gost_imit_ctx *c = ctx->md_data;
536 const unsigned char *p = data;
537 size_t bytes = count, i;
538 if (!(c->key_set)) {
539 GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
540 return 0;
541 }
542 if (c->bytes_left) {
543 for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
544 c->partial_block[i] = *p;
545 }
546 if (i == 8) {
547 mac_block_mesh(c, c->partial_block);
548 } else {
549 c->bytes_left = i;
550 return 1;
551 }
552 }
553 while (bytes > 8) {
554 mac_block_mesh(c, p);
555 p += 8;
556 bytes -= 8;
557 }
558 if (bytes > 0) {
559 memcpy(c->partial_block, p, bytes);
560 }
561 c->bytes_left = bytes;
562 return 1;
563}
571
564
572int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md)
573 {
574 struct ossl_gost_imit_ctx *c = ctx->md_data;
575 if (!c->key_set) {
576 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
577 return 0;
578 }
579 if (c->count==0 && c->bytes_left)
580 {
581 unsigned char buffer[8];
582 memset(buffer, 0, 8);
583 gost_imit_update(ctx, buffer, 8);
584 }
585 if (c->bytes_left)
586 {
587 int i;
588 for (i=c->bytes_left;i<8;i++)
589 {
590 c->partial_block[i]=0;
591 }
592 mac_block_mesh(c,c->partial_block);
593 }
594 get_mac(c->buffer,32,md);
595 return 1;
596 }
565int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
566{
567 struct ossl_gost_imit_ctx *c = ctx->md_data;
568 if (!c->key_set) {
569 GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
570 return 0;
571 }
572 if (c->count == 0 && c->bytes_left) {
573 unsigned char buffer[8];
574 memset(buffer, 0, 8);
575 gost_imit_update(ctx, buffer, 8);
576 }
577 if (c->bytes_left) {
578 int i;
579 for (i = c->bytes_left; i < 8; i++) {
580 c->partial_block[i] = 0;
581 }
582 mac_block_mesh(c, c->partial_block);
583 }
584 get_mac(c->buffer, 32, md);
585 return 1;
586}
597
587
598int gost_imit_ctrl(EVP_MD_CTX *ctx,int type, int arg, void *ptr)
599 {
600 switch (type)
601 {
602 case EVP_MD_CTRL_KEY_LEN:
603 *((unsigned int*)(ptr)) = 32;
604 return 1;
605 case EVP_MD_CTRL_SET_KEY:
606 {
607 if (arg!=32) {
608 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
609 return 0;
610 }
588int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
589{
590 switch (type) {
591 case EVP_MD_CTRL_KEY_LEN:
592 *((unsigned int *)(ptr)) = 32;
593 return 1;
594 case EVP_MD_CTRL_SET_KEY:
595 {
596 if (arg != 32) {
597 GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
598 return 0;
599 }
611
600
612 gost_key(&(((struct ossl_gost_imit_ctx*)(ctx->md_data))->cctx),ptr) ;
613 ((struct ossl_gost_imit_ctx*)(ctx->md_data))->key_set = 1;
614 return 1;
601 gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
602 ptr);
603 ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
604 return 1;
615
605
616 }
617 default:
618 return 0;
619 }
620 }
606 }
607 default:
608 return 0;
609 }
610}
621
611
622int gost_imit_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
623 {
624 memcpy(to->md_data,from->md_data,sizeof(struct ossl_gost_imit_ctx));
625 return 1;
626 }
612int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
613{
614 memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
615 return 1;
616}
627
628/* Clean up imit ctx */
629int gost_imit_cleanup(EVP_MD_CTX *ctx)
617
618/* Clean up imit ctx */
619int gost_imit_cleanup(EVP_MD_CTX *ctx)
630 {
631 memset(ctx->md_data,0,sizeof(struct ossl_gost_imit_ctx));
632 return 1;
633 }
634
620{
621 memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));
622 return 1;
623}