engine/cchost: fix bugs.
[openssl.git] / engines / ccgost / gost_crypt.c
index 9b9f9d8eeb1ed6410bd838550a7c02aca2c2638f..05429908f42a7d83b53ee57c68a9ec4bf3ea5552 100644 (file)
@@ -7,7 +7,6 @@
  *          Requires OpenSSL 0.9.9 for compilation                    *
  **********************************************************************/
 #include <string.h>
-#include <openssl/crypto.h>
 #include "gost89.h"
 #include <openssl/rand.h>
 #include "e_gost_err.h"
@@ -207,12 +206,12 @@ int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
 static void gost_crypt_mesh (void *ctx,unsigned char *iv,unsigned char *buf)
        {
        struct ossl_gost_cipher_ctx *c = ctx;
-       if (c->count&&c->key_meshing && c->count%1024==0)
+       if (c->key_meshing && c->count==1024)
                {
                cryptopro_key_meshing(&(c->cctx),iv);
                }       
        gostcrypt(&(c->cctx),iv,buf);
-       c->count+=8;
+       c->count = (c->count+8)%1024 + 1024;
        }
 
 static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
@@ -220,7 +219,7 @@ static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
        struct ossl_gost_cipher_ctx *c = ctx;
        word32 g,go;
        unsigned char buf1[8];
-       if (c->count && c->key_meshing && c->count %1024 ==0)
+       if (c->key_meshing && c->count==1024)
                {
                cryptopro_key_meshing(&(c->cctx),iv);
                }
@@ -249,7 +248,7 @@ static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
        buf1[7]=(unsigned char)((g>>24)&0xff);
        memcpy(iv,buf1,8);
        gostcrypt(&(c->cctx),buf1,buf);
-       c->count +=8;
+       c->count = (c->count+8)%1024 + 1024;
        }
 
 /* GOST encryption in CFB mode */
@@ -496,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;
@@ -511,12 +511,12 @@ static void mac_block_mesh(struct ossl_gost_imit_ctx *c,const unsigned char *dat
         * interpret internal state of MAC algorithm as iv during keymeshing
         * (but does initialize internal state from iv in key transport
         */
-       if (c->key_meshing&& c->count && c->count %1024 ==0)
+       if (c->key_meshing && c->count==1024)
                {
                cryptopro_key_meshing(&(c->cctx),buffer);
                }
        mac_block(&(c->cctx),c->buffer,data);
-       c->count +=8;
+       c->count = (c->count+1)%1024 + 1024;
        }
 
 int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
@@ -565,6 +565,12 @@ int gost_imit_final(EVP_MD_CTX *ctx,unsigned char *md)
                GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
                return 0;
        }
+       if (c->count==0 && c->bytes_left)
+               {
+               unsigned char buffer[8];
+               memset(buffer, 0, 8);
+               gost_imit_update(ctx, buffer, 8);
+               }
        if (c->bytes_left)
                {
                int i;