Add heartbeat extension bounds check.
[openssl.git] / engines / ccgost / gosthash.c
index 1acd16317bf384966f6dcaaf9163587bbe77edd8..a2cba77123929388fd0756fe37c3cc36562c5cae 100644 (file)
@@ -42,8 +42,8 @@ static void circle_xor8 (const byte *w, byte *k)
        byte buf[8];
        int i;
        memcpy(buf,w,8);
-       memcpy(k,w+8,24);
-       for(i=0;i<8;i++) 
+       memmove(k,w+8,24);
+       for(i=0;i<8;i++)
                k[i+24]=buf[i]^k[i];
        }
 
@@ -86,7 +86,7 @@ static void xor_blocks (byte *result,const byte *a,const byte *b,size_t len)
  */
 static int hash_step(gost_ctx *c,byte *H,const byte *M) 
        {
-       static byte U[32],W[32],V[32],S[32],Key[32];
+       byte U[32],W[32],V[32],S[32],Key[32];
        int i;
        /* Compute first key */
        xor_blocks(W,H,M,32);
@@ -180,12 +180,10 @@ int start_hash(gost_hash_ctx *ctx)
  */
 int hash_block(gost_hash_ctx *ctx,const byte *block, size_t length)
        {
-       const byte *curptr=block;
-       const byte *barrier=block+(length-32);/* Last byte we can safely hash*/
        if (ctx->left)
                {
                /*There are some bytes from previous step*/
-               int add_bytes = 32-ctx->left;
+               unsigned int add_bytes = 32-ctx->left;
                if (add_bytes>length)
                        {
                        add_bytes = length;
@@ -196,24 +194,25 @@ int hash_block(gost_hash_ctx *ctx,const byte *block, size_t length)
                        {
                        return 1;
                        }       
-               curptr=block+add_bytes;
+               block+=add_bytes;
+               length-=add_bytes;
                hash_step(ctx->cipher_ctx,ctx->H,ctx->remainder);
                add_blocks(32,ctx->S,ctx->remainder);
                ctx->len+=32;
                ctx->left=0;
                }
-       while (curptr<=barrier)
+       while (length>=32)
                {       
-               hash_step(ctx->cipher_ctx,ctx->H,curptr);
+               hash_step(ctx->cipher_ctx,ctx->H,block);
                        
-               add_blocks(32,ctx->S,curptr);
+               add_blocks(32,ctx->S,block);
                ctx->len+=32;
-               curptr+=32;
+               block+=32;
+               length-=32;
                }       
-       if (curptr!=block+length)
+       if (length)
                {
-               ctx->left=block+length-curptr;
-               memcpy(ctx->remainder,curptr,ctx->left);
+               memcpy(ctx->remainder,block,ctx->left=length);
                }       
        return 1;       
        }
@@ -228,7 +227,7 @@ int finish_hash(gost_hash_ctx *ctx,byte *hashval)
        byte buf[32];
        byte H[32];
        byte S[32];
-       long long fin_len=ctx->len;
+       ghosthash_len fin_len=ctx->len;
        byte *bptr;
        memcpy(H,ctx->H,32);
        memcpy(S,ctx->S,32);
@@ -245,7 +244,7 @@ int finish_hash(gost_hash_ctx *ctx,byte *hashval)
        fin_len<<=3; /* Hash length in BITS!!*/
        while(fin_len>0)
                {
-               *(bptr++)=fin_len&0xFF;
+               *(bptr++)=(byte)(fin_len&0xFF);
                fin_len>>=8;
                };
        hash_step(ctx->cipher_ctx,H,buf);
@@ -253,4 +252,3 @@ int finish_hash(gost_hash_ctx *ctx,byte *hashval)
        memcpy(hashval,H,32);
        return 1;
        }
-