/src/libgcrypt/cipher/gostr3411-94.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* gostr3411-94.c - GOST R 34.11-94 hash function |
2 | | * Copyright (C) 2012 Free Software Foundation, Inc. |
3 | | * |
4 | | * This file is part of Libgcrypt. |
5 | | * |
6 | | * Libgcrypt is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as |
8 | | * published by the Free Software Foundation; either version 2.1 of |
9 | | * the License, or (at your option) any later version. |
10 | | * |
11 | | * Libgcrypt is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | |
21 | | #include <config.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "g10lib.h" |
27 | | #include "bithelp.h" |
28 | | #include "bufhelp.h" |
29 | | #include "cipher.h" |
30 | | #include "hash-common.h" |
31 | | |
32 | | #include "gost.h" |
33 | | |
34 | 0 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
35 | | |
36 | | typedef struct { |
37 | | gcry_md_block_ctx_t bctx; |
38 | | union { |
39 | | u32 h[8]; |
40 | | byte result[32]; |
41 | | }; |
42 | | u32 sigma[8]; |
43 | | u32 len; |
44 | | int cryptopro; |
45 | | } GOSTR3411_CONTEXT; |
46 | | |
47 | | static unsigned int |
48 | | transform (void *c, const unsigned char *data, size_t nblks); |
49 | | |
50 | | static void |
51 | | gost3411_init (void *context, unsigned int flags) |
52 | 0 | { |
53 | 0 | GOSTR3411_CONTEXT *hd = context; |
54 | |
|
55 | 0 | (void)flags; |
56 | |
|
57 | 0 | memset (hd->h, 0, 32); |
58 | 0 | memset (hd->sigma, 0, 32); |
59 | |
|
60 | 0 | hd->bctx.nblocks = 0; |
61 | 0 | hd->bctx.count = 0; |
62 | 0 | hd->bctx.blocksize_shift = _gcry_ctz(32); |
63 | 0 | hd->bctx.bwrite = transform; |
64 | 0 | hd->cryptopro = 0; |
65 | 0 | } |
66 | | |
67 | | static void |
68 | | gost3411_cp_init (void *context, unsigned int flags) |
69 | 0 | { |
70 | 0 | GOSTR3411_CONTEXT *hd = context; |
71 | 0 | gost3411_init (context, flags); |
72 | 0 | hd->cryptopro = 1; |
73 | 0 | } |
74 | | |
75 | | static void |
76 | | do_p (u32 *p, u32 *u, u32 *v) |
77 | 0 | { |
78 | 0 | int k; |
79 | 0 | u32 t[8]; |
80 | |
|
81 | 0 | for (k = 0; k < 8; k++) |
82 | 0 | t[k] = u[k] ^ v[k]; |
83 | |
|
84 | 0 | k = 0; |
85 | 0 | p[k+0] = ((t[0] >> (8*k)) & 0xff) << 0 | |
86 | 0 | ((t[2] >> (8*k)) & 0xff) << 8 | |
87 | 0 | ((t[4] >> (8*k)) & 0xff) << 16 | |
88 | 0 | ((t[6] >> (8*k)) & 0xff) << 24; |
89 | 0 | p[k+4] = ((t[1] >> (8*k)) & 0xff) << 0 | |
90 | 0 | ((t[3] >> (8*k)) & 0xff) << 8 | |
91 | 0 | ((t[5] >> (8*k)) & 0xff) << 16 | |
92 | 0 | ((t[7] >> (8*k)) & 0xff) << 24; |
93 | |
|
94 | 0 | k = 1; |
95 | 0 | p[k+0] = ((t[0] >> (8*k)) & 0xff) << 0 | |
96 | 0 | ((t[2] >> (8*k)) & 0xff) << 8 | |
97 | 0 | ((t[4] >> (8*k)) & 0xff) << 16 | |
98 | 0 | ((t[6] >> (8*k)) & 0xff) << 24; |
99 | 0 | p[k+4] = ((t[1] >> (8*k)) & 0xff) << 0 | |
100 | 0 | ((t[3] >> (8*k)) & 0xff) << 8 | |
101 | 0 | ((t[5] >> (8*k)) & 0xff) << 16 | |
102 | 0 | ((t[7] >> (8*k)) & 0xff) << 24; |
103 | |
|
104 | 0 | k = 2; |
105 | 0 | p[k+0] = ((t[0] >> (8*k)) & 0xff) << 0 | |
106 | 0 | ((t[2] >> (8*k)) & 0xff) << 8 | |
107 | 0 | ((t[4] >> (8*k)) & 0xff) << 16 | |
108 | 0 | ((t[6] >> (8*k)) & 0xff) << 24; |
109 | 0 | p[k+4] = ((t[1] >> (8*k)) & 0xff) << 0 | |
110 | 0 | ((t[3] >> (8*k)) & 0xff) << 8 | |
111 | 0 | ((t[5] >> (8*k)) & 0xff) << 16 | |
112 | 0 | ((t[7] >> (8*k)) & 0xff) << 24; |
113 | |
|
114 | 0 | k = 3; |
115 | 0 | p[k+0] = ((t[0] >> (8*k)) & 0xff) << 0 | |
116 | 0 | ((t[2] >> (8*k)) & 0xff) << 8 | |
117 | 0 | ((t[4] >> (8*k)) & 0xff) << 16 | |
118 | 0 | ((t[6] >> (8*k)) & 0xff) << 24; |
119 | 0 | p[k+4] = ((t[1] >> (8*k)) & 0xff) << 0 | |
120 | 0 | ((t[3] >> (8*k)) & 0xff) << 8 | |
121 | 0 | ((t[5] >> (8*k)) & 0xff) << 16 | |
122 | 0 | ((t[7] >> (8*k)) & 0xff) << 24; |
123 | 0 | } |
124 | | |
125 | | static void |
126 | | do_a (u32 *u) |
127 | 0 | { |
128 | 0 | u32 t[2]; |
129 | 0 | int i; |
130 | 0 | memcpy(t, u, 2*4); |
131 | 0 | for (i = 0; i < 6; i++) |
132 | 0 | u[i] = u[i+2]; |
133 | 0 | u[6] = u[0] ^ t[0]; |
134 | 0 | u[7] = u[1] ^ t[1]; |
135 | 0 | } |
136 | | /* apply do_a twice: 1 2 3 4 -> 3 4 1^2 2^3 */ |
137 | | static void |
138 | | do_a2 (u32 *u) |
139 | 0 | { |
140 | 0 | u32 t[4]; |
141 | 0 | int i; |
142 | 0 | memcpy (t, u, 16); |
143 | 0 | memcpy (u, u + 4, 16); |
144 | 0 | for (i = 0; i < 2; i++) |
145 | 0 | { |
146 | 0 | u[4+i] = t[i] ^ t[i + 2]; |
147 | 0 | u[6+i] = u[i] ^ t[i + 2]; |
148 | 0 | } |
149 | 0 | } |
150 | | |
151 | | static void |
152 | | do_apply_c2 (u32 *u) |
153 | 0 | { |
154 | 0 | u[ 0] ^= 0xff00ff00; |
155 | 0 | u[ 1] ^= 0xff00ff00; |
156 | 0 | u[ 2] ^= 0x00ff00ff; |
157 | 0 | u[ 3] ^= 0x00ff00ff; |
158 | 0 | u[ 4] ^= 0x00ffff00; |
159 | 0 | u[ 5] ^= 0xff0000ff; |
160 | 0 | u[ 6] ^= 0x000000ff; |
161 | 0 | u[ 7] ^= 0xff00ffff; |
162 | 0 | } |
163 | | |
164 | | #define do_chi_step12(e) \ |
165 | 0 | e[6] ^= ((e[6] >> 16) ^ e[7] ^ (e[7] >> 16) ^ e[4] ^ (e[5] >>16)) & 0xffff; |
166 | | |
167 | | #define do_chi_step13(e) \ |
168 | 0 | e[6] ^= ((e[7] ^ (e[7] >> 16) ^ e[0] ^ (e[4] >> 16) ^ e[6]) & 0xffff) << 16; |
169 | | |
170 | | #define do_chi_doublestep(e, i) \ |
171 | 0 | e[i] ^= (e[i] >> 16) ^ (e[(i+1)%8] << 16) ^ e[(i+1)%8] ^ (e[(i+1)%8] >> 16) ^ (e[(i+2)%8] << 16) ^ e[(i+6)%8] ^ (e[(i+7)%8] >> 16); \ |
172 | 0 | e[i] ^= (e[i] << 16); |
173 | | |
174 | | static void |
175 | | do_chi_submix12 (u32 *e, u32 *x) |
176 | 0 | { |
177 | 0 | e[6] ^= x[0]; |
178 | 0 | e[7] ^= x[1]; |
179 | 0 | e[0] ^= x[2]; |
180 | 0 | e[1] ^= x[3]; |
181 | 0 | e[2] ^= x[4]; |
182 | 0 | e[3] ^= x[5]; |
183 | 0 | e[4] ^= x[6]; |
184 | 0 | e[5] ^= x[7]; |
185 | 0 | } |
186 | | |
187 | | static void |
188 | | do_chi_submix13 (u32 *e, u32 *x) |
189 | 0 | { |
190 | 0 | e[6] ^= (x[0] << 16) | (x[7] >> 16); |
191 | 0 | e[7] ^= (x[1] << 16) | (x[0] >> 16); |
192 | 0 | e[0] ^= (x[2] << 16) | (x[1] >> 16); |
193 | 0 | e[1] ^= (x[3] << 16) | (x[2] >> 16); |
194 | 0 | e[2] ^= (x[4] << 16) | (x[3] >> 16); |
195 | 0 | e[3] ^= (x[5] << 16) | (x[4] >> 16); |
196 | 0 | e[4] ^= (x[6] << 16) | (x[5] >> 16); |
197 | 0 | e[5] ^= (x[7] << 16) | (x[6] >> 16); |
198 | 0 | } |
199 | | |
200 | | static void |
201 | | do_add (u32 *s, u32 *a) |
202 | 0 | { |
203 | 0 | u32 carry = 0; |
204 | 0 | int i; |
205 | |
|
206 | 0 | for (i = 0; i < 8; i++) |
207 | 0 | { |
208 | 0 | u32 op = carry + a[i]; |
209 | 0 | s[i] += op; |
210 | 0 | carry = (a[i] > op) || (op > s[i]); |
211 | 0 | } |
212 | 0 | } |
213 | | |
214 | | static unsigned int |
215 | | do_hash_step (GOSTR3411_CONTEXT *hd, u32 *h, u32 *m) |
216 | 0 | { |
217 | 0 | u32 u[8], v[8]; |
218 | 0 | u32 s[8]; |
219 | 0 | u32 k[8]; |
220 | 0 | unsigned int burn; |
221 | 0 | int i; |
222 | |
|
223 | 0 | memcpy (u, h, 32); |
224 | 0 | memcpy (v, m, 32); |
225 | |
|
226 | 0 | for (i = 0; i < 4; i++) { |
227 | 0 | do_p (k, u, v); |
228 | |
|
229 | 0 | burn = _gcry_gost_enc_data (k, &s[2*i], &s[2*i+1], h[2*i], h[2*i+1], hd->cryptopro); |
230 | |
|
231 | 0 | do_a (u); |
232 | 0 | if (i == 1) |
233 | 0 | do_apply_c2 (u); |
234 | 0 | do_a2 (v); |
235 | 0 | } |
236 | |
|
237 | 0 | for (i = 0; i < 5; i++) |
238 | 0 | { |
239 | 0 | do_chi_doublestep (s, 0); |
240 | 0 | do_chi_doublestep (s, 1); |
241 | 0 | do_chi_doublestep (s, 2); |
242 | 0 | do_chi_doublestep (s, 3); |
243 | 0 | do_chi_doublestep (s, 4); |
244 | | /* That is in total 12 + 1 + 61 = 74 = 16 * 4 + 10 rounds */ |
245 | 0 | if (i == 4) |
246 | 0 | break; |
247 | 0 | do_chi_doublestep (s, 5); |
248 | 0 | if (i == 0) |
249 | 0 | do_chi_submix12(s, m); |
250 | 0 | do_chi_step12 (s); |
251 | 0 | if (i == 0) |
252 | 0 | do_chi_submix13(s, h); |
253 | 0 | do_chi_step13 (s); |
254 | 0 | do_chi_doublestep (s, 7); |
255 | 0 | } |
256 | |
|
257 | 0 | memcpy (h, s+5, 12); |
258 | 0 | memcpy (h+3, s, 20); |
259 | |
|
260 | 0 | return /* burn_stack */ 4 * sizeof(void*) /* func call (ret addr + args) */ + |
261 | 0 | 4 * 32 + 2 * sizeof(int) /* stack */ + |
262 | 0 | max(burn /* _gcry_gost_enc_one */, |
263 | 0 | sizeof(void*) * 2 /* do_a2 call */ + |
264 | 0 | 16 + sizeof(int) /* do_a2 stack */ ); |
265 | 0 | } |
266 | | |
267 | | static unsigned int |
268 | | transform_blk (void *ctx, const unsigned char *data) |
269 | 0 | { |
270 | 0 | GOSTR3411_CONTEXT *hd = ctx; |
271 | 0 | u32 m[8]; |
272 | 0 | unsigned int burn; |
273 | 0 | int i; |
274 | |
|
275 | 0 | for (i = 0; i < 8; i++) |
276 | 0 | m[i] = buf_get_le32(data + i*4); |
277 | 0 | burn = do_hash_step (hd, hd->h, m); |
278 | 0 | do_add (hd->sigma, m); |
279 | |
|
280 | 0 | return /* burn_stack */ burn + 3 * sizeof(void*) + 32 + 2 * sizeof(void*); |
281 | 0 | } |
282 | | |
283 | | |
284 | | static unsigned int |
285 | | transform ( void *c, const unsigned char *data, size_t nblks ) |
286 | 0 | { |
287 | 0 | unsigned int burn; |
288 | |
|
289 | 0 | do |
290 | 0 | { |
291 | 0 | burn = transform_blk (c, data); |
292 | 0 | data += 32; |
293 | 0 | } |
294 | 0 | while (--nblks); |
295 | |
|
296 | 0 | return burn; |
297 | 0 | } |
298 | | |
299 | | |
300 | | /* |
301 | | The routine finally terminates the computation and returns the |
302 | | digest. The handle is prepared for a new cycle, but adding bytes |
303 | | to the handle will the destroy the returned buffer. Returns: 32 |
304 | | bytes with the message the digest. */ |
305 | | static void |
306 | | gost3411_final (void *context) |
307 | 0 | { |
308 | 0 | GOSTR3411_CONTEXT *hd = context; |
309 | 0 | size_t padlen = 0; |
310 | 0 | u32 l[8]; |
311 | 0 | int i; |
312 | 0 | MD_NBLOCKS_TYPE nblocks; |
313 | |
|
314 | 0 | if (hd->bctx.count > 0) |
315 | 0 | { |
316 | 0 | padlen = 32 - hd->bctx.count; |
317 | 0 | memset (hd->bctx.buf + hd->bctx.count, 0, padlen); |
318 | 0 | hd->bctx.count += padlen; |
319 | 0 | _gcry_md_block_write (hd, NULL, 0); /* flush */; |
320 | 0 | } |
321 | |
|
322 | 0 | if (hd->bctx.count != 0) |
323 | 0 | return; /* Something went wrong */ |
324 | | |
325 | 0 | memset (l, 0, 32); |
326 | |
|
327 | 0 | nblocks = hd->bctx.nblocks; |
328 | 0 | if (padlen) |
329 | 0 | { |
330 | 0 | nblocks --; |
331 | 0 | l[0] = 256 - padlen * 8; |
332 | 0 | } |
333 | 0 | l[0] |= nblocks << 8; |
334 | 0 | nblocks >>= 24; |
335 | |
|
336 | 0 | for (i = 1; i < 8 && nblocks != 0; i++) |
337 | 0 | { |
338 | 0 | l[i] = nblocks; |
339 | 0 | nblocks >>= 24; |
340 | 0 | } |
341 | |
|
342 | 0 | do_hash_step (hd, hd->h, l); |
343 | 0 | do_hash_step (hd, hd->h, hd->sigma); |
344 | 0 | for (i = 0; i < 8; i++) |
345 | 0 | hd->h[i] = le_bswap32(hd->h[i]); |
346 | 0 | } |
347 | | |
348 | | static byte * |
349 | | gost3411_read (void *context) |
350 | 0 | { |
351 | 0 | GOSTR3411_CONTEXT *hd = context; |
352 | |
|
353 | 0 | return hd->result; |
354 | 0 | } |
355 | | |
356 | | static const unsigned char asn[6] = /* Object ID is 1.2.643.2.2.3 */ |
357 | | { 0x2a, 0x85, 0x03, 0x02, 0x02, 0x03 }; |
358 | | |
359 | | static const gcry_md_oid_spec_t oid_spec_gostr3411[] = |
360 | | { |
361 | | /* iso.member-body.ru.rans.cryptopro.3 (gostR3411-94-with-gostR3410-2001) */ |
362 | | { "1.2.643.2.2.3" }, |
363 | | /* iso.member-body.ru.rans.cryptopro.9 (gostR3411-94) */ |
364 | | { "1.2.643.2.2.9" }, |
365 | | {NULL}, |
366 | | }; |
367 | | |
368 | | const gcry_md_spec_t _gcry_digest_spec_gost3411_94 = |
369 | | { |
370 | | GCRY_MD_GOSTR3411_94, {0, 0}, |
371 | | "GOSTR3411_94", NULL, 0, NULL, 32, |
372 | | gost3411_init, _gcry_md_block_write, gost3411_final, gost3411_read, NULL, |
373 | | NULL, |
374 | | sizeof (GOSTR3411_CONTEXT) |
375 | | }; |
376 | | const gcry_md_spec_t _gcry_digest_spec_gost3411_cp = |
377 | | { |
378 | | GCRY_MD_GOSTR3411_CP, {0, 0}, |
379 | | "GOSTR3411_CP", asn, DIM (asn), oid_spec_gostr3411, 32, |
380 | | gost3411_cp_init, _gcry_md_block_write, gost3411_final, gost3411_read, NULL, |
381 | | NULL, |
382 | | sizeof (GOSTR3411_CONTEXT) |
383 | | }; |