]> www.wagner.pp.ru Git - openssl-gost/engine.git/blobdiff - gosthash2012.c
gosthash2012: Change some byte (pointers) to union uint512_u
[openssl-gost/engine.git] / gosthash2012.c
index 3dacc3531955e6f12c43b59ea7277cb27518ea1b..0868d6c1c2113d90ac920cf8e76a24adce8edb2d 100644 (file)
@@ -48,13 +48,13 @@ void init_gost2012_hash_ctx(gost2012_hash_ctx * CTX,
 
 static INLINE void pad(gost2012_hash_ctx * CTX)
 {
-    memset(&(CTX->buffer[CTX->bufsize]), 0, sizeof(CTX->buffer) - CTX->bufsize);
-    CTX->buffer[CTX->bufsize] = 1;
+    memset(&(CTX->buffer.B[CTX->bufsize]), 0, sizeof(CTX->buffer) - CTX->bufsize);
+    CTX->buffer.B[CTX->bufsize] = 1;
 
 }
 
-static INLINE void add512(const union uint512_u *x,
-                          const union uint512_u *y, union uint512_u *r)
+static INLINE void add512(union uint512_u * RESTRICT x,
+                          const union uint512_u * RESTRICT y)
 {
 #ifndef __GOST3411_BIG_ENDIAN__
     unsigned int CF;
@@ -80,28 +80,27 @@ static INLINE void add512(const union uint512_u *x,
         */
        if (sum != left)
            CF = (sum < left);
-       r->QWORD[i] = sum;
+       x->QWORD[i] = sum;
     }
 #else
-    const unsigned char *xp, *yp;
-    unsigned char *rp;
+    const unsigned char *yp;
+    unsigned char *xp;
     unsigned int i;
     int buf;
 
-    xp = (const unsigned char *)&x[0];
+    xp = (unsigned char *)&x[0];
     yp = (const unsigned char *)&y[0];
-    rp = (unsigned char *)&r[0];
 
     buf = 0;
     for (i = 0; i < 64; i++) {
         buf = xp[i] + yp[i] + (buf >> 8);
-        rp[i] = (unsigned char)buf & 0xFF;
+        xp[i] = (unsigned char)buf & 0xFF;
     }
 #endif
 }
 
-static void g(union uint512_u *h, const union uint512_u *N,
-              const unsigned char *m)
+static void g(union uint512_u *h, const union uint512_u * RESTRICT N,
+              const union uint512_u * RESTRICT m)
 {
 #ifdef __GOST3411_HAS_SSE2__
     __m128i xmm0, xmm2, xmm4, xmm6; /* XMMR0-quadruple */
@@ -145,19 +144,16 @@ static void g(union uint512_u *h, const union uint512_u *N,
     /* E() done */
 
     X((&data), h, (&data));
-    X((&data), ((const union uint512_u *)&m[0]), h);
+    X((&data), m, h);
 #endif
 }
 
-static INLINE void stage2(gost2012_hash_ctx * CTX, const unsigned char *data)
+static INLINE void stage2(gost2012_hash_ctx * CTX, const union uint512_u *data)
 {
-    union uint512_u m;
+    g(&(CTX->h), &(CTX->N), data);
 
-    memcpy(&m, data, sizeof(m));
-    g(&(CTX->h), &(CTX->N), (const unsigned char *)&m);
-
-    add512(&(CTX->N), &buffer512, &(CTX->N));
-    add512(&(CTX->Sigma), &m, &(CTX->Sigma));
+    add512(&(CTX->N), &buffer512);
+    add512(&(CTX->Sigma), data);
 }
 
 static INLINE void stage3(gost2012_hash_ctx * CTX)
@@ -177,15 +173,14 @@ static INLINE void stage3(gost2012_hash_ctx * CTX)
 
     pad(CTX);
 
-    g(&(CTX->h), &(CTX->N), (const unsigned char *)&(CTX->buffer));
+    g(&(CTX->h), &(CTX->N), &(CTX->buffer));
 
-    add512(&(CTX->N), &buf, &(CTX->N));
-    add512(&(CTX->Sigma), (const union uint512_u *)&CTX->buffer[0],
-           &(CTX->Sigma));
+    add512(&(CTX->N), &buf);
+    add512(&(CTX->Sigma), &CTX->buffer);
 
-    g(&(CTX->h), &buffer0, (const unsigned char *)&(CTX->N));
+    g(&(CTX->h), &buffer0, &(CTX->N));
 
-    g(&(CTX->h), &buffer0, (const unsigned char *)&(CTX->Sigma));
+    g(&(CTX->h), &buffer0, &(CTX->Sigma));
 }
 
 /*
@@ -198,7 +193,8 @@ void gost2012_hash_block(gost2012_hash_ctx * CTX,
     size_t chunksize;
 
     while (len > 63 && CTX->bufsize == 0) {
-        stage2(CTX, data);
+        memcpy(CTX->buffer.B, data, 64);
+        stage2(CTX, &(CTX->buffer));
 
         data += 64;
         len -= 64;
@@ -209,14 +205,14 @@ void gost2012_hash_block(gost2012_hash_ctx * CTX,
         if (chunksize > len)
             chunksize = len;
 
-        memcpy(&CTX->buffer[CTX->bufsize], data, chunksize);
+        memcpy(&CTX->buffer.B[CTX->bufsize], data, chunksize);
 
         CTX->bufsize += chunksize;
         len -= chunksize;
         data += chunksize;
 
         if (CTX->bufsize == 64) {
-            stage2(CTX, CTX->buffer);
+            stage2(CTX, &(CTX->buffer));
 
             CTX->bufsize = 0;
         }