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