]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gosthash2012.c
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[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 #if defined(__x86_64__) || defined(__e2k__)
13 # ifdef _MSC_VER
14 #  include <intrin.h>
15 # else
16 #  include <x86intrin.h>
17 # endif
18 #endif
19
20 #if defined(_WIN32) || defined(_WINDOWS)
21 # define INLINE __inline
22 #else
23 # define INLINE inline
24 #endif
25
26 #define BSWAP64(x) \
27     (((x & 0xFF00000000000000ULL) >> 56) | \
28      ((x & 0x00FF000000000000ULL) >> 40) | \
29      ((x & 0x0000FF0000000000ULL) >> 24) | \
30      ((x & 0x000000FF00000000ULL) >>  8) | \
31      ((x & 0x00000000FF000000ULL) <<  8) | \
32      ((x & 0x0000000000FF0000ULL) << 24) | \
33      ((x & 0x000000000000FF00ULL) << 40) | \
34      ((x & 0x00000000000000FFULL) << 56))
35
36 /*
37  * Initialize gost2012 hash context structure
38  */
39 void init_gost2012_hash_ctx(gost2012_hash_ctx * CTX,
40                             const unsigned int digest_size)
41 {
42     memset(CTX, 0, sizeof(gost2012_hash_ctx));
43
44     CTX->digest_size = digest_size;
45     /*
46      * IV for 512-bit hash should be 0^512
47      * IV for 256-bit hash should be (00000001)^64
48      *
49      * It's already zeroed when CTX is cleared above, so we only
50      * need to set it to 0x01-s for 256-bit hash.
51      */
52     if (digest_size == 256)
53         memset(&CTX->h, 0x01, sizeof(uint512_u));
54 }
55
56 static INLINE void pad(gost2012_hash_ctx * CTX)
57 {
58     memset(&(CTX->buffer.B[CTX->bufsize]), 0, sizeof(CTX->buffer) - CTX->bufsize);
59     CTX->buffer.B[CTX->bufsize] = 1;
60
61 }
62
63 static INLINE void add512(union uint512_u * RESTRICT x,
64                           const union uint512_u * RESTRICT y)
65 {
66 #ifndef __GOST3411_BIG_ENDIAN__
67     unsigned int CF = 0;
68     unsigned int i;
69
70 # ifdef HAVE_ADDCARRY_U64
71     for (i = 0; i < 8; i++)
72         CF = _addcarry_u64(CF, x->QWORD[i] , y->QWORD[i], &(x->QWORD[i]));
73 # else
74     for (i = 0; i < 8; i++) {
75         const unsigned long long left = x->QWORD[i];
76         unsigned long long sum;
77
78         sum = left + y->QWORD[i] + CF;
79         /*
80          * (sum == left): is noop, because it's possible only
81          * when `left' is added with `0 + 0' or with `ULLONG_MAX + 1',
82          * in that case `CF' (carry) retain previous value, which is correct,
83          * because when `left + 0 + 0' there was no overflow (thus no carry),
84          * and when `left + ULLONG_MAX + 1' value is wrapped back to
85          * itself with overflow, thus creating carry.
86          *
87          * (sum != left):
88          * if `sum' is not wrapped (sum > left) there should not be carry,
89          * if `sum' is wrapped (sum < left) there should be carry.
90          */
91         if (sum != left)
92             CF = (sum < left);
93         x->QWORD[i] = sum;
94     }
95 # endif /* !__x86_64__ */
96 #else /* __GOST3411_BIG_ENDIAN__ */
97     const unsigned char *yp;
98     unsigned char *xp;
99     unsigned int i;
100     int buf;
101
102     xp = (unsigned char *)&x[0];
103     yp = (const unsigned char *)&y[0];
104
105     buf = 0;
106     for (i = 0; i < 64; i++) {
107         buf = xp[i] + yp[i] + (buf >> 8);
108         xp[i] = (unsigned char)buf & 0xFF;
109     }
110 #endif /* __GOST3411_BIG_ENDIAN__ */
111 }
112
113 static void g(union uint512_u *h, const union uint512_u * RESTRICT N,
114               const union uint512_u * RESTRICT m)
115 {
116 #ifdef __GOST3411_HAS_SSE2__
117     __m128i xmm0, xmm2, xmm4, xmm6; /* XMMR0-quadruple */
118     __m128i xmm1, xmm3, xmm5, xmm7; /* XMMR1-quadruple */
119     unsigned int i;
120
121     LOAD(N, xmm0, xmm2, xmm4, xmm6);
122     XLPS128M(h, xmm0, xmm2, xmm4, xmm6);
123
124     ULOAD(m, xmm1, xmm3, xmm5, xmm7);
125     XLPS128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
126
127     for (i = 0; i < 11; i++)
128         ROUND128(i, xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
129
130     XLPS128M((&C[11]), xmm0, xmm2, xmm4, xmm6);
131     X128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
132
133     X128M(h, xmm0, xmm2, xmm4, xmm6);
134     ULOAD(m, xmm1, xmm3, xmm5, xmm7);
135     X128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
136
137     STORE(h, xmm0, xmm2, xmm4, xmm6);
138 # ifndef __i386__
139     /* Restore the Floating-point status on the CPU */
140     /* This is only required on MMX, but EXTRACT32 is using MMX */
141     _mm_empty();
142 # endif
143 #else
144     union uint512_u Ki, data;
145     unsigned int i;
146
147     XLPS(h, N, (&data));
148
149     /* Starting E() */
150     Ki = data;
151     XLPS((&Ki), ((const union uint512_u *)&m[0]), (&data));
152
153     for (i = 0; i < 11; i++)
154         ROUND(i, (&Ki), (&data));
155
156     XLPS((&Ki), (&C[11]), (&Ki));
157     X((&Ki), (&data), (&data));
158     /* E() done */
159
160     X((&data), h, (&data));
161     X((&data), m, h);
162 #endif
163 }
164
165 static INLINE void stage2(gost2012_hash_ctx * CTX, const union uint512_u *data)
166 {
167     g(&(CTX->h), &(CTX->N), data);
168
169     add512(&(CTX->N), &buffer512);
170     add512(&(CTX->Sigma), data);
171 }
172
173 static INLINE void stage3(gost2012_hash_ctx * CTX)
174 {
175     pad(CTX);
176     g(&(CTX->h), &(CTX->N), &(CTX->buffer));
177     add512(&(CTX->Sigma), &CTX->buffer);
178
179     memset(&(CTX->buffer.B[0]), 0, sizeof(uint512_u));
180 #ifndef __GOST3411_BIG_ENDIAN__
181     CTX->buffer.QWORD[0] = CTX->bufsize << 3;
182 #else
183     CTX->buffer.QWORD[0] = BSWAP64(CTX->bufsize << 3);
184 #endif
185     add512(&(CTX->N), &(CTX->buffer));
186
187     g(&(CTX->h), &buffer0, &(CTX->N));
188     g(&(CTX->h), &buffer0, &(CTX->Sigma));
189 }
190
191 /*
192  * Hash block of arbitrary length
193  *
194  */
195 void gost2012_hash_block(gost2012_hash_ctx * CTX,
196                          const unsigned char *data, size_t len)
197 {
198     register size_t bufsize = CTX->bufsize;
199
200     if (bufsize == 0) {
201         while (len >= 64) {
202             memcpy(&CTX->buffer.B[0], data, 64);
203             stage2(CTX, &(CTX->buffer));
204             data += 64;
205             len -= 64;
206         }
207     }
208
209     while (len) {
210         register size_t chunksize = 64 - bufsize;
211         if (chunksize > len)
212             chunksize = len;
213
214         memcpy(&CTX->buffer.B[bufsize], data, chunksize);
215
216         bufsize += chunksize;
217         len -= chunksize;
218         data += chunksize;
219
220         if (bufsize == 64) {
221             stage2(CTX, &(CTX->buffer) );
222             bufsize = 0;
223         }
224     }
225     CTX->bufsize = bufsize;
226 }
227
228 /*
229  * Compute hash value from current state of ctx
230  * state of hash ctx becomes invalid and cannot be used for further
231  * hashing.
232  */
233 void gost2012_finish_hash(gost2012_hash_ctx * CTX, unsigned char *digest)
234 {
235     stage3(CTX);
236
237     CTX->bufsize = 0;
238
239     if (CTX->digest_size == 256)
240         memcpy(digest, &(CTX->h.QWORD[4]), 32);
241     else
242         memcpy(digest, &(CTX->h.QWORD[0]), 64);
243 }