Version skew reduction: trivia (I hope).
[openssl.git] / engines / ccgost / gost_crypt.c
index d115d0bf3b8924a5b55e5923c8911bb7c0f401e8..cde58c0e9b7cc65cc04c426a8b8645d4a9d25ef6 100644 (file)
@@ -17,10 +17,10 @@ static int  gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
        const unsigned char *iv, int enc);
 /* Handles block of data in CFB mode */                        
 static int     gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
-       const unsigned char *in, unsigned int inl);
+       const unsigned char *in, size_t inl);
 /* Handles block of data in CNT mode */                        
 static int     gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
-       const unsigned char *in, unsigned int inl);
+       const unsigned char *in, size_t inl);
 /* Cleanup function */                 
 static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
 /* set/get cipher parameters */
@@ -34,7 +34,7 @@ EVP_CIPHER cipher_gost =
        NID_id_Gost28147_89,
        1,/*block_size*/
        32,/*key_size*/
-       8,/*iv_len - ñèíõðîïîñûëêà*/
+       8,/*iv_len */
        EVP_CIPH_CFB_MODE| EVP_CIPH_NO_PADDING |
        EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
        gost_cipher_init,
@@ -52,7 +52,7 @@ EVP_CIPHER cipher_gost_cpacnt =
        NID_gost89_cnt,
        1,/*block_size*/
        32,/*key_size*/
-       8,/*iv_len - ñèíõðîïîñûëêà*/
+       8,/*iv_len */
        EVP_CIPH_OFB_MODE| EVP_CIPH_NO_PADDING |
        EVP_CIPH_CUSTOM_IV| EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
        gost_cipher_init_cpa,
@@ -127,7 +127,7 @@ const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
                {
                const char * params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
                if (!params || !strlen(params)) 
-                       return &gost_cipher_list[0];
+                       return &gost_cipher_list[1];
 
                nid = OBJ_txt2nid(params);
                if (nid == NID_undef)
@@ -253,12 +253,12 @@ static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
 
 /* GOST encryption in CFB mode */
 int    gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
-       const unsigned char *in, unsigned int inl)
+       const unsigned char *in, size_t inl)
        {
        const unsigned char *in_ptr=in;
        unsigned char *out_ptr=out;
-       unsigned int i=0;
-       unsigned int j=0;
+       size_t i=0;
+       size_t j=0;
 /* process partial block if any */
        if (ctx->num) 
                {
@@ -299,7 +299,7 @@ int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
        if (i<inl)
                {
                gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf);
-               if (!ctx->encrypt) memcpy(ctx->buf+8,in_ptr,j);
+               if (!ctx->encrypt) memcpy(ctx->buf+8,in_ptr,inl-i);
                for (j=0;i<inl;j++,i++)
                        {
                        out_ptr[j]=ctx->buf[j]^in_ptr[j];
@@ -315,12 +315,12 @@ int       gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
        }
 
 static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
-       const unsigned char *in, unsigned int inl)
+       const unsigned char *in, size_t inl)
        {
        const unsigned char *in_ptr=in;
        unsigned char *out_ptr=out;
-       unsigned int i=0;
-       unsigned int j;
+       size_t i=0;
+       size_t j;
 /* process partial block if any */
        if (ctx->num) 
                {
@@ -371,7 +371,7 @@ static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
 /* Cleaning up of EVP_CIPHER_CTX */
 int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx) 
        {
-       gost_destroy((gost_ctx *)ctx->cipher_data);
+       gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
        ctx->app_data = NULL;
        return 1;
        }       
@@ -390,6 +390,14 @@ int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr)
                        }
                break;
                }
+               case EVP_CTRL_PBE_PRF_NID:
+                       if (ptr) {
+                               *((int *)ptr)=  NID_id_HMACGostR3411_94;
+                               return 1;
+                       } else {
+                               return 0;
+                       }       
+                               
                default:
                        GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
                        return -1;
@@ -451,13 +459,15 @@ int  gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
        int ret = -1;
        int len; 
        GOST_CIPHER_PARAMS *gcp = NULL;
-       unsigned char *p = params->value.sequence->data;
+       unsigned char *p;
        struct ossl_gost_cipher_ctx *c=ctx->cipher_data;
        if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE)
                {
                return ret;
                }
 
+       p = params->value.sequence->data;
+
        gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
                params->value.sequence->length);
 
@@ -485,7 +495,8 @@ int  gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params)
 int gost_imit_init_cpa(EVP_MD_CTX *ctx)
        {
        struct ossl_gost_imit_ctx *c = ctx->md_data;
-       memset(c->buffer,0,16);
+       memset(c->buffer,0,sizeof(c->buffer));
+       memset(c->partial_block,0,sizeof(c->partial_block));
        c->count = 0;
        c->bytes_left=0;
        c->key_meshing=1;
@@ -550,7 +561,10 @@ int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
 int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md)
        {
        struct ossl_gost_imit_ctx *c = ctx->md_data;
-       if (!c->key_set) return 0;
+       if (!c->key_set) {
+               GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
+               return 0;
+       }
        if (c->bytes_left)
                {
                int i;