]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gosthash.c
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / gosthash.c
1 /**********************************************************************
2  *                          gosthash.c                                *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *    Implementation of GOST R 34.11-94 hash function                 *
7  *       uses on gost89.c and gost89.h Doesn't need OpenSSL           *
8  **********************************************************************/
9 #include <string.h>
10
11 #include "gost89.h"
12 #include "gosthash.h"
13
14 /*
15  * Use OPENSSL_malloc for memory allocation if compiled with
16  * -DOPENSSL_BUILD, and libc malloc otherwise
17  */
18 #ifndef MYALLOC
19 # ifdef OPENSSL_BUILD
20 #  include <openssl/crypto.h>
21 #  define MYALLOC(size) OPENSSL_malloc(size)
22 #  define MYFREE(ptr) OPENSSL_free(ptr)
23 # else
24 #  define MYALLOC(size) malloc(size)
25 #  define MYFREE(ptr) free(ptr)
26 # endif
27 #endif
28 /*
29  * Following functions are various bit meshing routines used in GOST R
30  * 34.11-94 algorithms
31  */
32 static void swap_bytes(byte * w, byte * k)
33 {
34     int i, j;
35     for (i = 0; i < 4; i++)
36         for (j = 0; j < 8; j++)
37             k[i + 4 * j] = w[8 * i + j];
38
39 }
40
41 /* was A_A */
42 static void circle_xor8(const byte * w, byte * k)
43 {
44     byte buf[8];
45     int i;
46     memcpy(buf, w, 8);
47     memmove(k, w + 8, 24);
48     for (i = 0; i < 8; i++)
49         k[i + 24] = buf[i] ^ k[i];
50 }
51
52 /* was R_R */
53 static void transform_3(byte * data)
54 {
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;
60     data[31] = acc >> 8;
61 }
62
63 /* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/
64 static int add_blocks(int n, byte * left, const byte * right)
65 {
66     int i;
67     int carry = 0;
68     for (i = 0; i < n; i++) {
69         int sum = (int)left[i] + (int)right[i] + carry;
70         left[i] = sum & 0xff;
71         carry = sum >> 8;
72     }
73     return carry;
74 }
75
76 /* Xor two sequences of bytes */
77 static void xor_blocks(byte * result, const byte * a, const byte * b,
78                        size_t len)
79 {
80     size_t i;
81     for (i = 0; i < len; i++)
82         result[i] = a[i] ^ b[i];
83 }
84
85 /*
86  *      Calculate H(i+1) = Hash(Hi,Mi)
87  *      Where H and M are 32 bytes long
88  */
89 static int hash_step(gost_ctx * c, byte * H, const byte * M)
90 {
91     byte U[32], W[32], V[32], S[32], Key[32];
92     int i;
93     /* Compute first key */
94     xor_blocks(W, H, M, 32);
95     swap_bytes(W, Key);
96     /* Encrypt first 8 bytes of H with first key */
97     gost_enc_with_key(c, Key, H, S);
98     /* Compute second key */
99     circle_xor8(H, U);
100     circle_xor8(M, V);
101     circle_xor8(V, V);
102     xor_blocks(W, U, V, 32);
103     swap_bytes(W, Key);
104     /* encrypt second 8 bytes of H with second key */
105     gost_enc_with_key(c, Key, H + 8, S + 8);
106     /* compute third key */
107     circle_xor8(U, U);
108     U[31] = ~U[31];
109     U[29] = ~U[29];
110     U[28] = ~U[28];
111     U[24] = ~U[24];
112     U[23] = ~U[23];
113     U[20] = ~U[20];
114     U[18] = ~U[18];
115     U[17] = ~U[17];
116     U[14] = ~U[14];
117     U[12] = ~U[12];
118     U[10] = ~U[10];
119     U[8] = ~U[8];
120     U[7] = ~U[7];
121     U[5] = ~U[5];
122     U[3] = ~U[3];
123     U[1] = ~U[1];
124     circle_xor8(V, V);
125     circle_xor8(V, V);
126     xor_blocks(W, U, V, 32);
127     swap_bytes(W, Key);
128     /* encrypt third 8 bytes of H with third key */
129     gost_enc_with_key(c, Key, H + 16, S + 16);
130     /* Compute fourth key */
131     circle_xor8(U, U);
132     circle_xor8(V, V);
133     circle_xor8(V, V);
134     xor_blocks(W, U, V, 32);
135     swap_bytes(W, Key);
136     /* Encrypt last 8 bytes with fourth key */
137     gost_enc_with_key(c, Key, H + 24, S + 24);
138     for (i = 0; i < 12; i++)
139         transform_3(S);
140     xor_blocks(S, S, M, 32);
141     transform_3(S);
142     xor_blocks(S, S, H, 32);
143     for (i = 0; i < 61; i++)
144         transform_3(S);
145     memcpy(H, S, 32);
146     return 1;
147 }
148
149 /*
150  * Initialize gost_hash ctx - cleans up temporary structures and set up
151  * substitution blocks
152  */
153 int init_gost_hash_ctx(gost_hash_ctx * ctx,
154                        const gost_subst_block * subst_block)
155 {
156     memset(ctx, 0, sizeof(*ctx));
157     ctx->cipher_ctx = (gost_ctx *) MYALLOC(sizeof(gost_ctx));
158     if (!ctx->cipher_ctx) {
159         return 0;
160     }
161     gost_init(ctx->cipher_ctx, subst_block);
162     return 1;
163 }
164
165 /*
166  * Free cipher CTX if it is dynamically allocated. Do not use
167  * if cipher ctx is statically allocated as in OpenSSL implementation of
168  * GOST hash algroritm
169  *
170  */
171 void done_gost_hash_ctx(gost_hash_ctx * ctx)
172 {
173     /*
174      * No need to use gost_destroy, because cipher keys are not really secret
175      * when hashing
176      */
177     MYFREE(ctx->cipher_ctx);
178 }
179
180 /*
181  * reset state of hash context to begin hashing new message
182  */
183 int start_hash(gost_hash_ctx * ctx)
184 {
185     if (!ctx->cipher_ctx)
186         return 0;
187     memset(&(ctx->H), 0, 32);
188     memset(&(ctx->S), 0, 32);
189     ctx->len = 0L;
190     ctx->left = 0;
191     return 1;
192 }
193
194 /*
195  * Hash block of arbitrary length
196  *
197  *
198  */
199 int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
200 {
201     if (ctx->left) {
202         /*
203          * There are some bytes from previous step
204          */
205         unsigned int add_bytes = 32 - ctx->left;
206         if (add_bytes > length) {
207             add_bytes = length;
208         }
209         memcpy(&(ctx->remainder[ctx->left]), block, add_bytes);
210         ctx->left += add_bytes;
211         if (ctx->left < 32) {
212             return 1;
213         }
214         block += add_bytes;
215         length -= add_bytes;
216         hash_step(ctx->cipher_ctx, ctx->H, ctx->remainder);
217         add_blocks(32, ctx->S, ctx->remainder);
218         ctx->len += 32;
219         ctx->left = 0;
220     }
221     while (length >= 32) {
222         hash_step(ctx->cipher_ctx, ctx->H, block);
223
224         add_blocks(32, ctx->S, block);
225         ctx->len += 32;
226         block += 32;
227         length -= 32;
228     }
229     if (length) {
230         memcpy(ctx->remainder, block, ctx->left = length);
231     }
232     return 1;
233 }
234
235 /*
236  * Compute hash value from current state of ctx
237  * state of hash ctx becomes invalid and cannot be used for further
238  * hashing.
239  */
240 int finish_hash(gost_hash_ctx * ctx, byte * hashval)
241 {
242     byte buf[32];
243     byte H[32];
244     byte S[32];
245     ghosthash_len fin_len = ctx->len;
246     byte *bptr;
247     memcpy(H, ctx->H, 32);
248     memcpy(S, ctx->S, 32);
249     if (ctx->left) {
250         memset(buf, 0, 32);
251         memcpy(buf, ctx->remainder, ctx->left);
252         hash_step(ctx->cipher_ctx, H, buf);
253         add_blocks(32, S, buf);
254         fin_len += ctx->left;
255     }
256     memset(buf, 0, 32);
257     if (fin_len == 0)
258         hash_step(ctx->cipher_ctx, H, buf);
259     bptr = buf;
260     fin_len <<= 3;              /* Hash length in BITS!! */
261     while (fin_len > 0) {
262         *(bptr++) = (byte) (fin_len & 0xFF);
263         fin_len >>= 8;
264     };
265     hash_step(ctx->cipher_ctx, H, buf);
266     hash_step(ctx->cipher_ctx, H, S);
267     memcpy(hashval, H, 32);
268     return 1;
269 }