2 * GOST R 34.11-2012 core functions.
4 * Copyright (c) 2013 Cryptocom LTD.
5 * This file is distributed under the same license as OpenSSL.
7 * Author: Alexey Degtyarev <alexey@renatasystems.org>
11 #include "gosthash2012.h"
13 #if defined(_WIN32) || defined(_WINDOWS)
14 # define INLINE __inline
16 # define INLINE inline
20 (((x & 0xFF00000000000000ULL) >> 56) | \
21 ((x & 0x00FF000000000000ULL) >> 40) | \
22 ((x & 0x0000FF0000000000ULL) >> 24) | \
23 ((x & 0x000000FF00000000ULL) >> 8) | \
24 ((x & 0x00000000FF000000ULL) << 8) | \
25 ((x & 0x0000000000FF0000ULL) << 24) | \
26 ((x & 0x000000000000FF00ULL) << 40) | \
27 ((x & 0x00000000000000FFULL) << 56))
30 * Initialize gost2012 hash context structure
32 void init_gost2012_hash_ctx(gost2012_hash_ctx * CTX,
33 const unsigned int digest_size)
35 memset(CTX, 0, sizeof(gost2012_hash_ctx));
37 CTX->digest_size = digest_size;
39 * IV for 512-bit hash should be 0^512
40 * IV for 256-bit hash should be (00000001)^64
42 * It's already zeroed when CTX is cleared above, so we only
43 * need to set it to 0x01-s for 256-bit hash.
45 if (digest_size == 256)
46 memset(&CTX->h, 0x01, sizeof(uint512_u));
49 static INLINE void pad(gost2012_hash_ctx * CTX)
51 unsigned char buf[64];
53 memset(&buf, 0x00, sizeof buf);
54 memcpy(&buf, CTX->buffer, CTX->bufsize);
56 buf[CTX->bufsize] = 0x01;
57 memcpy(CTX->buffer, &buf, sizeof buf);
60 static INLINE void add512(const union uint512_u *x,
61 const union uint512_u *y, union uint512_u *r)
63 #ifndef __GOST3411_BIG_ENDIAN__
65 unsigned long long tmp;
69 for (i = 0; i < 8; i++)
71 /* Detecting integer overflow condition for three numbers
72 * in a portable way is tricky a little. */
74 /* Step 1: numbers cause overflow */
75 tmp = x->QWORD[i] + y->QWORD[i];
77 /* Compare with any of two summands, no need to check both */
78 if (tmp < x->QWORD[i])
83 /* Step 2: carry bit causes overflow */
86 if (CF > 0 && tmp == 0)
94 const unsigned char *xp, *yp;
99 xp = (const unsigned char *)&x[0];
100 yp = (const unsigned char *)&y[0];
101 rp = (unsigned char *)&r[0];
104 for (i = 0; i < 64; i++) {
105 buf = xp[i] + yp[i] + (buf >> 8);
106 rp[i] = (unsigned char)buf & 0xFF;
111 static void g(union uint512_u *h, const union uint512_u *N,
112 const unsigned char *m)
114 #ifdef __GOST3411_HAS_SSE2__
115 __m128i xmm0, xmm2, xmm4, xmm6; /* XMMR0-quadruple */
116 __m128i xmm1, xmm3, xmm5, xmm7; /* XMMR1-quadruple */
119 LOAD(N, xmm0, xmm2, xmm4, xmm6);
120 XLPS128M(h, xmm0, xmm2, xmm4, xmm6);
122 LOAD(m, xmm1, xmm3, xmm5, xmm7);
123 XLPS128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
125 for (i = 0; i < 11; i++)
126 ROUND128(i, xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
128 XLPS128M((&C[11]), xmm0, xmm2, xmm4, xmm6);
129 X128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
131 X128M(h, xmm0, xmm2, xmm4, xmm6);
132 X128M(m, xmm0, xmm2, xmm4, xmm6);
134 UNLOAD(h, xmm0, xmm2, xmm4, xmm6);
136 /* Restore the Floating-point status on the CPU */
139 union uint512_u Ki, data;
146 XLPS((&Ki), ((const union uint512_u *)&m[0]), (&data));
148 for (i = 0; i < 11; i++)
149 ROUND(i, (&Ki), (&data));
151 XLPS((&Ki), (&C[11]), (&Ki));
152 X((&Ki), (&data), (&data));
155 X((&data), h, (&data));
156 X((&data), ((const union uint512_u *)&m[0]), h);
160 static INLINE void stage2(gost2012_hash_ctx * CTX, const unsigned char *data)
164 memcpy(&m, data, sizeof(m));
165 g(&(CTX->h), &(CTX->N), (const unsigned char *)&m);
167 add512(&(CTX->N), &buffer512, &(CTX->N));
168 add512(&(CTX->Sigma), &m, &(CTX->Sigma));
171 static INLINE void stage3(gost2012_hash_ctx * CTX)
173 ALIGN(16) union uint512_u buf;
175 memset(&buf, 0x00, sizeof buf);
176 memcpy(&buf, &(CTX->buffer), CTX->bufsize);
177 memcpy(&(CTX->buffer), &buf, sizeof(uint512_u));
179 memset(&buf, 0x00, sizeof buf);
180 #ifndef __GOST3411_BIG_ENDIAN__
181 buf.QWORD[0] = CTX->bufsize << 3;
183 buf.QWORD[0] = BSWAP64(CTX->bufsize << 3);
188 g(&(CTX->h), &(CTX->N), (const unsigned char *)&(CTX->buffer));
190 add512(&(CTX->N), &buf, &(CTX->N));
191 add512(&(CTX->Sigma), (const union uint512_u *)&CTX->buffer[0],
194 g(&(CTX->h), &buffer0, (const unsigned char *)&(CTX->N));
196 g(&(CTX->h), &buffer0, (const unsigned char *)&(CTX->Sigma));
200 * Hash block of arbitrary length
203 void gost2012_hash_block(gost2012_hash_ctx * CTX,
204 const unsigned char *data, size_t len)
208 while (len > 63 && CTX->bufsize == 0) {
216 chunksize = 64 - CTX->bufsize;
220 memcpy(&CTX->buffer[CTX->bufsize], data, chunksize);
222 CTX->bufsize += chunksize;
226 if (CTX->bufsize == 64) {
227 stage2(CTX, CTX->buffer);
235 * Compute hash value from current state of ctx
236 * state of hash ctx becomes invalid and cannot be used for further
239 void gost2012_finish_hash(gost2012_hash_ctx * CTX, unsigned char *digest)
245 if (CTX->digest_size == 256)
246 memcpy(digest, &(CTX->h.QWORD[4]), 32);
248 memcpy(digest, &(CTX->h.QWORD[0]), 64);