gosthash94.c 14.4 KB
Newer Older
1
2
/* gosthash94.c - an implementation of GOST Hash Function
 *
3
 * based on the Russian Standard GOST R 34.11-94.
4
 * English description in RFC 5831.
5
6
7
 * See also RFC 4357.
 *
 * Copyright: 2009-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
8
 * Copyright: 2019 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Ported to nettle by Nikos Mavrogiannopoulos.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <string.h>

#include "macros.h"
#include "nettle-write.h"
#include "gosthash94.h"
44
#include "gost28147-internal.h"
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/**
 * Initialize algorithm context before calculating hash
 * with test parameters set.
 *
 * @param ctx context to initalize
 */
void
gosthash94_init (struct gosthash94_ctx *ctx)
{
    memset (ctx, 0, sizeof (struct gosthash94_ctx));
}

/**
 * The core transformation. Process a 512-bit block.
 *
 * @param hash intermediate message hash
 * @param block the message block to process
 */
static void
65
66
gost_block_compress (struct gosthash94_ctx *ctx, const uint32_t *block,
		     const uint32_t sbox[4][256])
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
    unsigned i;
    uint32_t key[8], u[8], v[8], w[8], s[8];

    /* u := hash, v := <256-bit message block> */
    memcpy (u, ctx->hash, sizeof (u));
    memcpy (v, block, sizeof (v));

    /* w := u xor v */
    w[0] = u[0] ^ v[0], w[1] = u[1] ^ v[1];
    w[2] = u[2] ^ v[2], w[3] = u[3] ^ v[3];
    w[4] = u[4] ^ v[4], w[5] = u[5] ^ v[5];
    w[6] = u[6] ^ v[6], w[7] = u[7] ^ v[7];

    /* calculate keys, encrypt hash and store result to the s[] array */
    for (i = 0;; i += 2)
      {
          /* key generation: key_i := P(w) */
          key[0] =
              (w[0] & 0x000000ff) | ((w[2] & 0x000000ff) << 8) |
              ((w[4] & 0x000000ff) << 16) | ((w[6] & 0x000000ff) << 24);
          key[1] =
              ((w[0] & 0x0000ff00) >> 8) | (w[2] & 0x0000ff00) |
              ((w[4] & 0x0000ff00) << 8) | ((w[6] & 0x0000ff00) << 16);
          key[2] =
              ((w[0] & 0x00ff0000) >> 16) | ((w[2] & 0x00ff0000) >> 8) |
              (w[4] & 0x00ff0000) | ((w[6] & 0x00ff0000) << 8);
          key[3] =
              ((w[0] & 0xff000000) >> 24) | ((w[2] & 0xff000000) >> 16) |
              ((w[4] & 0xff000000) >> 8) | (w[6] & 0xff000000);
          key[4] =
              (w[1] & 0x000000ff) | ((w[3] & 0x000000ff) << 8) |
              ((w[5] & 0x000000ff) << 16) | ((w[7] & 0x000000ff) << 24);
          key[5] =
              ((w[1] & 0x0000ff00) >> 8) | (w[3] & 0x0000ff00) |
              ((w[5] & 0x0000ff00) << 8) | ((w[7] & 0x0000ff00) << 16);
          key[6] =
              ((w[1] & 0x00ff0000) >> 16) | ((w[3] & 0x00ff0000) >> 8) |
              (w[5] & 0x00ff0000) | ((w[7] & 0x00ff0000) << 8);
          key[7] =
              ((w[1] & 0xff000000) >> 24) | ((w[3] & 0xff000000) >> 16) |
              ((w[5] & 0xff000000) >> 8) | (w[7] & 0xff000000);

          /* encryption: s_i := E_{key_i} (h_i) */
111
          _gost28147_encrypt_block (key, sbox, &ctx->hash[i], &s[i]);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

          if (i == 0)
            {
                /* w:= A(u) ^ A^2(v) */
                w[0] = u[2] ^ v[4], w[1] = u[3] ^ v[5];
                w[2] = u[4] ^ v[6], w[3] = u[5] ^ v[7];
                w[4] = u[6] ^ (v[0] ^= v[2]);
                w[5] = u[7] ^ (v[1] ^= v[3]);
                w[6] = (u[0] ^= u[2]) ^ (v[2] ^= v[4]);
                w[7] = (u[1] ^= u[3]) ^ (v[3] ^= v[5]);
            }
          else if ((i & 2) != 0)
            {
                if (i == 6)
                    break;

                /* w := A^2(u) xor A^4(v) xor C_3; u := A(u) xor C_3 */
                /* C_3=0xff00ffff000000ffff0000ff00ffff0000ff00ff00ff00ffff00ff00ff00ff00 */
                u[2] ^= u[4] ^ 0x000000ff;
                u[3] ^= u[5] ^ 0xff00ffff;
                u[4] ^= 0xff00ff00;
                u[5] ^= 0xff00ff00;
                u[6] ^= 0x00ff00ff;
                u[7] ^= 0x00ff00ff;
                u[0] ^= 0x00ffff00;
                u[1] ^= 0xff0000ff;

                w[0] = u[4] ^ v[0];
                w[2] = u[6] ^ v[2];
                w[4] = u[0] ^ (v[4] ^= v[6]);
                w[6] = u[2] ^ (v[6] ^= v[0]);
                w[1] = u[5] ^ v[1];
                w[3] = u[7] ^ v[3];
                w[5] = u[1] ^ (v[5] ^= v[7]);
                w[7] = u[3] ^ (v[7] ^= v[1]);
            }
          else
            {
                /* i==4 here */
                /* w:= A( A^2(u) xor C_3 ) xor A^6(v) */
                w[0] = u[6] ^ v[4], w[1] = u[7] ^ v[5];
                w[2] = u[0] ^ v[6], w[3] = u[1] ^ v[7];
                w[4] = u[2] ^ (v[0] ^= v[2]);
                w[5] = u[3] ^ (v[1] ^= v[3]);
                w[6] = (u[4] ^= u[6]) ^ (v[2] ^= v[4]);
                w[7] = (u[5] ^= u[7]) ^ (v[3] ^= v[5]);
            }
      }

    /* step hash function: x(block, hash) := psi^61(hash xor psi(block xor psi^12(S))) */

    /* 12 rounds of the LFSR and xor in <message block> */
    u[0] = block[0] ^ s[6];
    u[1] = block[1] ^ s[7];
    u[2] =
        block[2] ^ (s[0] << 16) ^ (s[0] >> 16) ^ (s[0] & 0xffff) ^ (s[1] &
                                                                    0xffff)
        ^ (s[1] >> 16) ^ (s[2] << 16) ^ s[6] ^ (s[6] << 16) ^ (s[7] &
                                                               0xffff0000)
        ^ (s[7] >> 16);
    u[3] =
        block[3] ^ (s[0] & 0xffff) ^ (s[0] << 16) ^ (s[1] & 0xffff) ^ (s[1]
                                                                       <<
                                                                       16)
        ^ (s[1] >> 16) ^ (s[2] << 16) ^ (s[2] >> 16) ^ (s[3] << 16) ^ s[6]
        ^ (s[6] << 16) ^ (s[6] >> 16) ^ (s[7] & 0xffff) ^ (s[7] << 16) ^
        (s[7] >> 16);
    u[4] =
        block[4] ^ (s[0] & 0xffff0000) ^ (s[0] << 16) ^ (s[0] >> 16) ^
        (s[1] & 0xffff0000) ^ (s[1] >> 16) ^ (s[2] << 16) ^ (s[2] >> 16) ^
        (s[3] << 16) ^ (s[3] >> 16) ^ (s[4] << 16) ^ (s[6] << 16) ^ (s[6]
                                                                     >> 16)
        ^ (s[7] & 0xffff) ^ (s[7] << 16) ^ (s[7] >> 16);
    u[5] =
        block[5] ^ (s[0] << 16) ^ (s[0] >> 16) ^ (s[0] & 0xffff0000) ^
        (s[1] & 0xffff) ^ s[2] ^ (s[2] >> 16) ^ (s[3] << 16) ^ (s[3] >> 16)
        ^ (s[4] << 16) ^ (s[4] >> 16) ^ (s[5] << 16) ^ (s[6] << 16) ^ (s[6]
                                                                       >>
                                                                       16)
        ^ (s[7] & 0xffff0000) ^ (s[7] << 16) ^ (s[7] >> 16);
    u[6] =
        block[6] ^ s[0] ^ (s[1] >> 16) ^ (s[2] << 16) ^ s[3] ^ (s[3] >> 16)
        ^ (s[4] << 16) ^ (s[4] >> 16) ^ (s[5] << 16) ^ (s[5] >> 16) ^ s[6]
        ^ (s[6] << 16) ^ (s[6] >> 16) ^ (s[7] << 16);
    u[7] =
        block[7] ^ (s[0] & 0xffff0000) ^ (s[0] << 16) ^ (s[1] & 0xffff) ^
        (s[1] << 16) ^ (s[2] >> 16) ^ (s[3] << 16) ^ s[4] ^ (s[4] >> 16) ^
        (s[5] << 16) ^ (s[5] >> 16) ^ (s[6] >> 16) ^ (s[7] & 0xffff) ^
        (s[7] << 16) ^ (s[7] >> 16);

    /* 1 round of the LFSR (a mixing transformation) and xor with <hash> */
    v[0] = ctx->hash[0] ^ (u[1] << 16) ^ (u[0] >> 16);
    v[1] = ctx->hash[1] ^ (u[2] << 16) ^ (u[1] >> 16);
    v[2] = ctx->hash[2] ^ (u[3] << 16) ^ (u[2] >> 16);
    v[3] = ctx->hash[3] ^ (u[4] << 16) ^ (u[3] >> 16);
    v[4] = ctx->hash[4] ^ (u[5] << 16) ^ (u[4] >> 16);
    v[5] = ctx->hash[5] ^ (u[6] << 16) ^ (u[5] >> 16);
    v[6] = ctx->hash[6] ^ (u[7] << 16) ^ (u[6] >> 16);
    v[7] =
        ctx->
        hash[7] ^ (u[0] & 0xffff0000) ^ (u[0] << 16) ^ (u[1] & 0xffff0000)
        ^ (u[1] << 16) ^ (u[6] << 16) ^ (u[7] & 0xffff0000) ^ (u[7] >> 16);

    /* 61 rounds of LFSR, mixing up hash */
    ctx->hash[0] = (v[0] & 0xffff0000) ^ (v[0] << 16) ^ (v[0] >> 16) ^
        (v[1] >> 16) ^ (v[1] & 0xffff0000) ^ (v[2] << 16) ^
        (v[3] >> 16) ^ (v[4] << 16) ^ (v[5] >> 16) ^ v[5] ^
        (v[6] >> 16) ^ (v[7] << 16) ^ (v[7] >> 16) ^ (v[7] & 0xffff);
    ctx->hash[1] = (v[0] << 16) ^ (v[0] >> 16) ^ (v[0] & 0xffff0000) ^
        (v[1] & 0xffff) ^ v[2] ^ (v[2] >> 16) ^ (v[3] << 16) ^
        (v[4] >> 16) ^ (v[5] << 16) ^ (v[6] << 16) ^ v[6] ^
        (v[7] & 0xffff0000) ^ (v[7] >> 16);
    ctx->hash[2] = (v[0] & 0xffff) ^ (v[0] << 16) ^ (v[1] << 16) ^
        (v[1] >> 16) ^ (v[1] & 0xffff0000) ^ (v[2] << 16) ^ (v[3] >> 16) ^
        v[3] ^ (v[4] << 16) ^ (v[5] >> 16) ^ v[6] ^ (v[6] >> 16) ^
        (v[7] & 0xffff) ^ (v[7] << 16) ^ (v[7] >> 16);
    ctx->hash[3] = (v[0] << 16) ^ (v[0] >> 16) ^ (v[0] & 0xffff0000) ^
        (v[1] & 0xffff0000) ^ (v[1] >> 16) ^ (v[2] << 16) ^
        (v[2] >> 16) ^ v[2] ^ (v[3] << 16) ^ (v[4] >> 16) ^ v[4] ^
        (v[5] << 16) ^ (v[6] << 16) ^ (v[7] & 0xffff) ^ (v[7] >> 16);
    ctx->hash[4] =
        (v[0] >> 16) ^ (v[1] << 16) ^ v[1] ^ (v[2] >> 16) ^ v[2] ^ (v[3] <<
                                                                    16) ^
        (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ (v[5] >> 16) ^ v[5] ^ (v[6] <<
                                                                    16) ^
        (v[6] >> 16) ^ (v[7] << 16);
    ctx->hash[5] =
        (v[0] << 16) ^ (v[0] & 0xffff0000) ^ (v[1] << 16) ^ (v[1] >> 16) ^
        (v[1] & 0xffff0000) ^ (v[2] << 16) ^ v[2] ^ (v[3] >> 16) ^ v[3] ^
        (v[4] << 16) ^ (v[4] >> 16) ^ v[4] ^ (v[5] << 16) ^ (v[6] << 16) ^
        (v[6] >> 16) ^ v[6] ^ (v[7] << 16) ^ (v[7] >> 16) ^ (v[7] &
                                                             0xffff0000);
    ctx->hash[6] =
        v[0] ^ v[2] ^ (v[2] >> 16) ^ v[3] ^ (v[3] << 16) ^ v[4] ^ (v[4] >>
                                                                   16) ^
        (v[5] << 16) ^ (v[5] >> 16) ^ v[5] ^ (v[6] << 16) ^ (v[6] >> 16) ^
        v[6] ^ (v[7] << 16) ^ v[7];
    ctx->hash[7] =
        v[0] ^ (v[0] >> 16) ^ (v[1] << 16) ^ (v[1] >> 16) ^ (v[2] << 16) ^
        (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ v[4] ^ (v[5] >> 16) ^ v[5] ^
        (v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16) ^ v[7];
}

/**
 * This function calculates hash value by 256-bit blocks.
 * It updates 256-bit check sum as follows:
 *    *(uint256_t)(ctx->sum) += *(uint256_t*)block;
 * and then updates intermediate hash value ctx->hash 
 * by calling gost_block_compress().
 *
 * @param ctx algorithm context
 * @param block the 256-bit message block to process
 */
static void
266
267
gost_compute_sum_and_hash (struct gosthash94_ctx *ctx, const uint8_t *block,
			   const uint32_t sbox[4][256])
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{
    uint32_t block_le[8];
    unsigned i, carry;

    /* compute the 256-bit sum */
    for (i = carry = 0; i < 8; i++, block += 4)
      {
	  block_le[i] = LE_READ_UINT32(block);
          ctx->sum[i] += carry;
	  carry = (ctx->sum[i] < carry);
          ctx->sum[i] += block_le[i];
          carry += (ctx->sum[i] < block_le[i]);
      }

    /* update message hash */
283
    gost_block_compress (ctx, block_le, sbox);
284
285
}

286
287
#define COMPRESS(ctx, block) gost_compute_sum_and_hash((ctx), (block), sbox);

288
289
290
291
292
293
294
295
/**
 * Calculate message hash.
 * Can be called repeatedly with chunks of the message to be hashed.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param msg message chunk
 * @param size length of the message chunk
 */
296
297
298
299
static void
gosthash94_update_int (struct gosthash94_ctx *ctx,
		       size_t length, const uint8_t *msg,
		       const uint32_t sbox[4][256])
300
{
301
    MD_UPDATE(ctx, length, msg, COMPRESS, ctx->count++);
302
303
}

304
305
306
307
308
309
310
311
312
313
314
315
316
/**
 * Calculate message hash.
 * Can be called repeatedly with chunks of the message to be hashed.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param msg message chunk
 * @param size length of the message chunk
 */
void
gosthash94_update (struct gosthash94_ctx *ctx,
		   size_t length, const uint8_t *msg)
{
  gosthash94_update_int (ctx, length, msg,
317
			 _gost28147_param_test_3411.sbox);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
}

/**
 * Calculate message hash.
 * Can be called repeatedly with chunks of the message to be hashed.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param msg message chunk
 * @param size length of the message chunk
 */
void
gosthash94cp_update (struct gosthash94_ctx *ctx,
		     size_t length, const uint8_t *msg)
{
  gosthash94_update_int (ctx, length, msg,
333
			 _gost28147_param_CryptoPro_3411.sbox);
334
335
}

336
337
338
339
340
341
/**
 * Finish hashing and store message digest into given array.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param result calculated hash in binary form
 */
342
343
344
345
static void
gosthash94_write_digest (struct gosthash94_ctx *ctx,
			 size_t length, uint8_t *result,
			 const uint32_t sbox[4][256])
346
{
347
    uint32_t msg32[GOSTHASH94_BLOCK_SIZE / 4];
348
349
350
351

    assert(length <= GOSTHASH94_DIGEST_SIZE);

    /* pad the last block with zeroes and hash it */
352
    if (ctx->index > 0)
353
      {
354
355
          memset (ctx->block + ctx->index, 0, GOSTHASH94_BLOCK_SIZE - ctx->index);
          gost_compute_sum_and_hash (ctx, ctx->block, sbox);
356
357
358
      }

    /* hash the message length and the sum */
359
360
    msg32[0] = (ctx->count << 8) | (ctx->index << 3);
    msg32[1] = ctx->count >> 24;
361
362
    memset (msg32 + 2, 0, sizeof (uint32_t) * 6);

363
364
    gost_block_compress (ctx, msg32, sbox);
    gost_block_compress (ctx, ctx->sum, sbox);
365
366
367
368
369

    /* convert hash state to result bytes */
    _nettle_write_le32(length, result, ctx->hash);
    gosthash94_init (ctx);
}
370
371
372
373
374
375

void
gosthash94_digest (struct gosthash94_ctx *ctx,
		   size_t length, uint8_t *result)
{
  gosthash94_write_digest (ctx, length, result,
376
			   _gost28147_param_test_3411.sbox);
377
378
379
380
381
382
383
}

void
gosthash94cp_digest (struct gosthash94_ctx *ctx,
		     size_t length, uint8_t *result)
{
  gosthash94_write_digest (ctx, length, result,
384
			   _gost28147_param_CryptoPro_3411.sbox);
385
}