]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gosthash2012.c
Initial commit providing GOST 2012 algorithms.
[openssl-gost/engine.git] / gosthash2012.c
1 /*
2  * GOST R 34.11-2012 core functions.
3  *
4  * Copyright (c) 2013 Cryptocom LTD.
5  * This file is distributed under the same license as OpenSSL.
6  *
7  * Author: Alexey Degtyarev <alexey@renatasystems.org>
8  *
9  */
10
11 #include "gosthash2012.h"
12
13 #if defined(_WIN32) || defined(_WINDOWS)
14 # define INLINE __inline
15 #else
16 # define INLINE inline
17 #endif
18
19 #define BSWAP64(x) \
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))
28
29 /*
30  * Initialize gost2012 hash context structure
31  */
32 void init_gost2012_hash_ctx(gost2012_hash_ctx * CTX,
33                             const unsigned int digest_size)
34 {
35     unsigned int i;
36
37     memset(CTX, 0, sizeof(gost2012_hash_ctx));
38
39     CTX->digest_size = digest_size;
40     if (digest_size == 256)
41         memset(&CTX->h, 0x01, sizeof(uint512_u));
42     else
43         memset(&CTX->h, 0x00, sizeof(uint512_u));
44 }
45
46 static INLINE void pad(gost2012_hash_ctx * CTX)
47 {
48     unsigned char buf[64];
49
50     if (CTX->bufsize > 63)
51         return;
52
53     memset(&buf, 0x00, sizeof buf);
54     memcpy(&buf, CTX->buffer, CTX->bufsize);
55
56     buf[CTX->bufsize] = 0x01;
57     memcpy(CTX->buffer, &buf, sizeof buf);
58 }
59
60 static INLINE void add512(const union uint512_u *x,
61                           const union uint512_u *y, union uint512_u *r)
62 {
63 #ifndef __GOST3411_BIG_ENDIAN__
64     unsigned int CF, OF;
65     unsigned int i;
66
67     CF = 0;
68     for (i = 0; i < 8; i++) {
69         r->QWORD[i] = x->QWORD[i] + y->QWORD[i];
70         if (r->QWORD[i] < y->QWORD[i] || r->QWORD[i] < x->QWORD[i])
71             OF = 1;
72         else
73             OF = 0;
74
75         r->QWORD[i] += CF;
76         CF = OF;
77     }
78 #else
79     const unsigned char *xp, *yp;
80     unsigned char *rp;
81     unsigned int i;
82     int buf;
83
84     xp = (const unsigned char *)&x[0];
85     yp = (const unsigned char *)&y[0];
86     rp = (unsigned char *)&r[0];
87
88     buf = 0;
89     for (i = 0; i < 64; i++) {
90         buf = xp[i] + yp[i] + (buf >> 8);
91         rp[i] = (unsigned char)buf & 0xFF;
92     }
93 #endif
94 }
95
96 static void g(union uint512_u *h, const union uint512_u *N,
97               const unsigned char *m)
98 {
99 #ifdef __GOST3411_HAS_SSE2__
100     __m128i xmm0, xmm2, xmm4, xmm6; /* XMMR0-quadruple */
101     __m128i xmm1, xmm3, xmm5, xmm7; /* XMMR1-quadruple */
102     unsigned int i;
103
104     LOAD(N, xmm0, xmm2, xmm4, xmm6);
105     XLPS128M(h, xmm0, xmm2, xmm4, xmm6);
106
107     LOAD(m, xmm1, xmm3, xmm5, xmm7);
108     XLPS128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
109
110     for (i = 0; i < 11; i++)
111         ROUND128(i, xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
112
113     XLPS128M((&C[11]), xmm0, xmm2, xmm4, xmm6);
114     X128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
115
116     X128M(h, xmm0, xmm2, xmm4, xmm6);
117     X128M(m, xmm0, xmm2, xmm4, xmm6);
118
119     UNLOAD(h, xmm0, xmm2, xmm4, xmm6);
120
121     /* Restore the Floating-point status on the CPU */
122     _mm_empty();
123 #else
124     union uint512_u Ki, data;
125     unsigned int i;
126
127     XLPS(h, N, (&data));
128
129     /* Starting E() */
130     Ki = data;
131     XLPS((&Ki), ((const union uint512_u *)&m[0]), (&data));
132
133     for (i = 0; i < 11; i++)
134         ROUND(i, (&Ki), (&data));
135
136     XLPS((&Ki), (&C[11]), (&Ki));
137     X((&Ki), (&data), (&data));
138     /* E() done */
139
140     X((&data), h, (&data));
141     X((&data), ((const union uint512_u *)&m[0]), h);
142 #endif
143 }
144
145 static INLINE void stage2(gost2012_hash_ctx * CTX, const unsigned char *data)
146 {
147     g(&(CTX->h), &(CTX->N), data);
148
149     add512(&(CTX->N), &buffer512, &(CTX->N));
150     add512(&(CTX->Sigma), (const union uint512_u *)data, &(CTX->Sigma));
151 }
152
153 static INLINE void stage3(gost2012_hash_ctx * CTX)
154 {
155     ALIGN(16) union uint512_u buf;
156
157     memset(&buf, 0x00, sizeof buf);
158     memcpy(&buf, &(CTX->buffer), CTX->bufsize);
159     memcpy(&(CTX->buffer), &buf, sizeof(uint512_u));
160
161     memset(&buf, 0x00, sizeof buf);
162 #ifndef __GOST3411_BIG_ENDIAN__
163     buf.QWORD[0] = CTX->bufsize << 3;
164 #else
165     buf.QWORD[0] = BSWAP64(CTX->bufsize << 3);
166 #endif
167
168     pad(CTX);
169
170     g(&(CTX->h), &(CTX->N), (const unsigned char *)&(CTX->buffer));
171
172     add512(&(CTX->N), &buf, &(CTX->N));
173     add512(&(CTX->Sigma), (const union uint512_u *)&CTX->buffer[0],
174            &(CTX->Sigma));
175
176     g(&(CTX->h), &buffer0, (const unsigned char *)&(CTX->N));
177
178     g(&(CTX->h), &buffer0, (const unsigned char *)&(CTX->Sigma));
179     memcpy(&(CTX->hash), &(CTX->h), sizeof(uint512_u));
180 }
181
182 /*
183  * Hash block of arbitrary length
184  *
185  */
186 void gost2012_hash_block(gost2012_hash_ctx * CTX,
187                          const unsigned char *data, size_t len)
188 {
189     size_t chunksize;
190
191     while (len > 63 && CTX->bufsize == 0) {
192         stage2(CTX, data);
193
194         data += 64;
195         len -= 64;
196     }
197
198     while (len) {
199         chunksize = 64 - CTX->bufsize;
200         if (chunksize > len)
201             chunksize = len;
202
203         memcpy(&CTX->buffer[CTX->bufsize], data, chunksize);
204
205         CTX->bufsize += chunksize;
206         len -= chunksize;
207         data += chunksize;
208
209         if (CTX->bufsize == 64) {
210             stage2(CTX, CTX->buffer);
211
212             CTX->bufsize = 0;
213         }
214     }
215 }
216
217 /*
218  * Compute hash value from current state of ctx
219  * state of hash ctx becomes invalid and cannot be used for further
220  * hashing.
221  */
222 void gost2012_finish_hash(gost2012_hash_ctx * CTX, unsigned char *digest)
223 {
224     stage3(CTX);
225
226     CTX->bufsize = 0;
227
228     if (CTX->digest_size == 256)
229         memcpy(digest, &(CTX->hash.QWORD[4]), 32);
230     else
231         memcpy(digest, &(CTX->hash.QWORD[0]), 64);
232 }