1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of GOST R 34.11-94 hash function *
7 * uses on gost89.c and gost89.h Doesn't need OpenSSL *
8 **********************************************************************/
15 * Use OPENSSL_malloc for memory allocation if compiled with
16 * -DOPENSSL_BUILD, and libc malloc otherwise
20 # include <openssl/crypto.h>
21 # define MYALLOC(size) OPENSSL_malloc(size)
22 # define MYFREE(ptr) OPENSSL_free(ptr)
24 # define MYALLOC(size) malloc(size)
25 # define MYFREE(ptr) free(ptr)
29 * Following functions are various bit meshing routines used in GOST R
32 static void swap_bytes(byte * w, byte * k)
35 for (i = 0; i < 4; i++)
36 for (j = 0; j < 8; j++)
37 k[i + 4 * j] = w[8 * i + j];
42 static void circle_xor8(const byte * w, byte * k)
47 memmove(k, w + 8, 24);
48 for (i = 0; i < 8; i++)
49 k[i + 24] = buf[i] ^ k[i];
53 static void transform_3(byte * data)
55 unsigned short int acc;
56 acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) |
57 ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8);
58 memmove(data, data + 2, 30);
59 data[30] = acc & 0xff;
63 /* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/
64 static int add_blocks(int n, byte * left, const byte * right)
69 for (i = 0; i < n; i++) {
70 sum = (int)left[i] + (int)right[i] + carry;
77 /* Xor two sequences of bytes */
78 static void xor_blocks(byte * result, const byte * a, const byte * b,
82 for (i = 0; i < len; i++)
83 result[i] = a[i] ^ b[i];
87 * Calculate H(i+1) = Hash(Hi,Mi)
88 * Where H and M are 32 bytes long
90 static int hash_step(gost_ctx * c, byte * H, const byte * M)
92 byte U[32], W[32], V[32], S[32], Key[32];
94 /* Compute first key */
95 xor_blocks(W, H, M, 32);
97 /* Encrypt first 8 bytes of H with first key */
98 gost_enc_with_key(c, Key, H, S);
99 /* Compute second key */
103 xor_blocks(W, U, V, 32);
105 /* encrypt second 8 bytes of H with second key */
106 gost_enc_with_key(c, Key, H + 8, S + 8);
107 /* compute third key */
127 xor_blocks(W, U, V, 32);
129 /* encrypt third 8 bytes of H with third key */
130 gost_enc_with_key(c, Key, H + 16, S + 16);
131 /* Compute fourth key */
135 xor_blocks(W, U, V, 32);
137 /* Encrypt last 8 bytes with fourth key */
138 gost_enc_with_key(c, Key, H + 24, S + 24);
139 for (i = 0; i < 12; i++)
141 xor_blocks(S, S, M, 32);
143 xor_blocks(S, S, H, 32);
144 for (i = 0; i < 61; i++)
151 * Initialize gost_hash ctx - cleans up temporary structures and set up
152 * substitution blocks
154 int init_gost_hash_ctx(gost_hash_ctx * ctx,
155 const gost_subst_block * subst_block)
157 memset(ctx, 0, sizeof(*ctx));
158 ctx->cipher_ctx = (gost_ctx *) MYALLOC(sizeof(gost_ctx));
159 if (!ctx->cipher_ctx) {
162 gost_init(ctx->cipher_ctx, subst_block);
167 * Free cipher CTX if it is dynamically allocated. Do not use
168 * if cipher ctx is statically allocated as in OpenSSL implementation of
169 * GOST hash algroritm
172 void done_gost_hash_ctx(gost_hash_ctx * ctx)
175 * No need to use gost_destroy, because cipher keys are not really secret
178 MYFREE(ctx->cipher_ctx);
182 * reset state of hash context to begin hashing new message
184 int start_hash(gost_hash_ctx * ctx)
186 if (!ctx->cipher_ctx)
188 memset(&(ctx->H), 0, 32);
189 memset(&(ctx->S), 0, 32);
196 * Hash block of arbitrary length
200 int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
204 * There are some bytes from previous step
206 unsigned int add_bytes = 32 - ctx->left;
207 if (add_bytes > length) {
210 memcpy(&(ctx->remainder[ctx->left]), block, add_bytes);
211 ctx->left += add_bytes;
212 if (ctx->left < 32) {
217 hash_step(ctx->cipher_ctx, ctx->H, ctx->remainder);
218 add_blocks(32, ctx->S, ctx->remainder);
222 while (length >= 32) {
223 hash_step(ctx->cipher_ctx, ctx->H, block);
225 add_blocks(32, ctx->S, block);
231 memcpy(ctx->remainder, block, ctx->left = length);
237 * Compute hash value from current state of ctx
238 * state of hash ctx becomes invalid and cannot be used for further
241 int finish_hash(gost_hash_ctx * ctx, byte * hashval)
246 ghosthash_len fin_len = ctx->len;
248 memcpy(H, ctx->H, 32);
249 memcpy(S, ctx->S, 32);
252 memcpy(buf, ctx->remainder, ctx->left);
253 hash_step(ctx->cipher_ctx, H, buf);
254 add_blocks(32, S, buf);
255 fin_len += ctx->left;
259 hash_step(ctx->cipher_ctx, H, buf);
261 fin_len <<= 3; /* Hash length in BITS!! */
262 while (fin_len > 0) {
263 *(bptr++) = (byte) (fin_len & 0xFF);
266 hash_step(ctx->cipher_ctx, H, buf);
267 hash_step(ctx->cipher_ctx, H, S);
268 memcpy(hashval, H, 32);