/src/libressl/crypto/gost/gostr341194.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: gostr341194.c,v 1.5 2015/09/10 15:56:25 jsing Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
4 | | * Copyright (c) 2005-2006 Cryptocom LTD |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * |
13 | | * 2. Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in |
15 | | * the documentation and/or other materials provided with the |
16 | | * distribution. |
17 | | * |
18 | | * 3. All advertising materials mentioning features or use of this |
19 | | * software must display the following acknowledgment: |
20 | | * "This product includes software developed by the OpenSSL Project |
21 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
22 | | * |
23 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
24 | | * endorse or promote products derived from this software without |
25 | | * prior written permission. For written permission, please contact |
26 | | * openssl-core@openssl.org. |
27 | | * |
28 | | * 5. Products derived from this software may not be called "OpenSSL" |
29 | | * nor may "OpenSSL" appear in their names without prior written |
30 | | * permission of the OpenSSL Project. |
31 | | * |
32 | | * 6. Redistributions of any form whatsoever must retain the following |
33 | | * acknowledgment: |
34 | | * "This product includes software developed by the OpenSSL Project |
35 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
36 | | * |
37 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
38 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
39 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
40 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
41 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
42 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
43 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
44 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
45 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
46 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
47 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
48 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
49 | | * ==================================================================== |
50 | | */ |
51 | | |
52 | | #include <string.h> |
53 | | |
54 | | #include <openssl/opensslconf.h> |
55 | | |
56 | | #ifndef OPENSSL_NO_GOST |
57 | | #include <openssl/crypto.h> |
58 | | #include <openssl/objects.h> |
59 | | #include <openssl/gost.h> |
60 | | |
61 | | #include "gost_locl.h" |
62 | | |
63 | | /* Following functions are various bit meshing routines used in |
64 | | * GOST R 34.11-94 algorithms */ |
65 | | static void |
66 | | swap_bytes(unsigned char *w, unsigned char *k) |
67 | 0 | { |
68 | 0 | int i, j; |
69 | |
|
70 | 0 | for (i = 0; i < 4; i++) |
71 | 0 | for (j = 0; j < 8; j++) |
72 | 0 | k[i + 4 * j] = w[8 * i + j]; |
73 | 0 | } |
74 | | |
75 | | /* was A_A */ |
76 | | static void |
77 | | circle_xor8(const unsigned char *w, unsigned char *k) |
78 | 0 | { |
79 | 0 | unsigned char buf[8]; |
80 | 0 | int i; |
81 | |
|
82 | 0 | memcpy(buf, w, 8); |
83 | 0 | memmove(k, w + 8, 24); |
84 | 0 | for (i = 0; i < 8; i++) |
85 | 0 | k[i + 24] = buf[i] ^ k[i]; |
86 | 0 | } |
87 | | |
88 | | /* was R_R */ |
89 | | static void |
90 | | transform_3(unsigned char *data) |
91 | 0 | { |
92 | 0 | unsigned short int acc; |
93 | |
|
94 | 0 | acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) | |
95 | 0 | ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8); |
96 | 0 | memmove(data, data + 2, 30); |
97 | 0 | data[30] = acc & 0xff; |
98 | 0 | data[31] = acc >> 8; |
99 | 0 | } |
100 | | |
101 | | /* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/ |
102 | | static int |
103 | | add_blocks(int n, unsigned char *left, const unsigned char *right) |
104 | 0 | { |
105 | 0 | int i; |
106 | 0 | int carry = 0; |
107 | 0 | int sum; |
108 | |
|
109 | 0 | for (i = 0; i < n; i++) { |
110 | 0 | sum = (int)left[i] + (int)right[i] + carry; |
111 | 0 | left[i] = sum & 0xff; |
112 | 0 | carry = sum >> 8; |
113 | 0 | } |
114 | 0 | return carry; |
115 | 0 | } |
116 | | |
117 | | /* Xor two sequences of bytes */ |
118 | | static void |
119 | | xor_blocks(unsigned char *result, const unsigned char *a, |
120 | | const unsigned char *b, size_t len) |
121 | 0 | { |
122 | 0 | size_t i; |
123 | |
|
124 | 0 | for (i = 0; i < len; i++) |
125 | 0 | result[i] = a[i] ^ b[i]; |
126 | 0 | } |
127 | | |
128 | | /* |
129 | | * Calculate H(i+1) = Hash(Hi,Mi) |
130 | | * Where H and M are 32 bytes long |
131 | | */ |
132 | | static int |
133 | | hash_step(GOSTR341194_CTX *c, unsigned char *H, const unsigned char *M) |
134 | 0 | { |
135 | 0 | unsigned char U[32], W[32], V[32], S[32], Key[32]; |
136 | 0 | int i; |
137 | | |
138 | | /* Compute first key */ |
139 | 0 | xor_blocks(W, H, M, 32); |
140 | 0 | swap_bytes(W, Key); |
141 | | /* Encrypt first 8 bytes of H with first key */ |
142 | 0 | Gost2814789_set_key(&c->cipher, Key, 256); |
143 | 0 | Gost2814789_encrypt(H, S, &c->cipher); |
144 | | |
145 | | /* Compute second key */ |
146 | 0 | circle_xor8(H, U); |
147 | 0 | circle_xor8(M, V); |
148 | 0 | circle_xor8(V, V); |
149 | 0 | xor_blocks(W, U, V, 32); |
150 | 0 | swap_bytes(W, Key); |
151 | | /* encrypt second 8 bytes of H with second key */ |
152 | 0 | Gost2814789_set_key(&c->cipher, Key, 256); |
153 | 0 | Gost2814789_encrypt(H+8, S+8, &c->cipher); |
154 | | |
155 | | /* compute third key */ |
156 | 0 | circle_xor8(U, U); |
157 | 0 | U[31] = ~U[31]; |
158 | 0 | U[29] = ~U[29]; |
159 | 0 | U[28] = ~U[28]; |
160 | 0 | U[24] = ~U[24]; |
161 | 0 | U[23] = ~U[23]; |
162 | 0 | U[20] = ~U[20]; |
163 | 0 | U[18] = ~U[18]; |
164 | 0 | U[17] = ~U[17]; |
165 | 0 | U[14] = ~U[14]; |
166 | 0 | U[12] = ~U[12]; |
167 | 0 | U[10] = ~U[10]; |
168 | 0 | U[8] = ~U[8]; |
169 | 0 | U[7] = ~U[7]; |
170 | 0 | U[5] = ~U[5]; |
171 | 0 | U[3] = ~U[3]; |
172 | 0 | U[1] = ~U[1]; |
173 | 0 | circle_xor8(V, V); |
174 | 0 | circle_xor8(V, V); |
175 | 0 | xor_blocks(W, U, V, 32); |
176 | 0 | swap_bytes(W, Key); |
177 | | /* encrypt third 8 bytes of H with third key */ |
178 | 0 | Gost2814789_set_key(&c->cipher, Key, 256); |
179 | 0 | Gost2814789_encrypt(H+16, S+16, &c->cipher); |
180 | | |
181 | | /* Compute fourth key */ |
182 | 0 | circle_xor8(U, U); |
183 | 0 | circle_xor8(V, V); |
184 | 0 | circle_xor8(V, V); |
185 | 0 | xor_blocks(W, U, V, 32); |
186 | 0 | swap_bytes(W, Key); |
187 | | /* Encrypt last 8 bytes with fourth key */ |
188 | 0 | Gost2814789_set_key(&c->cipher, Key, 256); |
189 | 0 | Gost2814789_encrypt(H+24, S+24, &c->cipher); |
190 | |
|
191 | 0 | for (i = 0; i < 12; i++) |
192 | 0 | transform_3(S); |
193 | 0 | xor_blocks(S, S, M, 32); |
194 | 0 | transform_3(S); |
195 | 0 | xor_blocks(S, S, H, 32); |
196 | 0 | for (i = 0; i < 61; i++) |
197 | 0 | transform_3(S); |
198 | 0 | memcpy(H, S, 32); |
199 | 0 | return 1; |
200 | 0 | } |
201 | | |
202 | | int |
203 | | GOSTR341194_Init(GOSTR341194_CTX *c, int nid) |
204 | 0 | { |
205 | 0 | memset(c, 0, sizeof(*c)); |
206 | 0 | return Gost2814789_set_sbox(&c->cipher, nid); |
207 | 0 | } |
208 | | |
209 | | static void |
210 | | GOSTR341194_block_data_order(GOSTR341194_CTX *ctx, const unsigned char *p, |
211 | | size_t num) |
212 | 0 | { |
213 | 0 | int i; |
214 | |
|
215 | 0 | for (i = 0; i < num; i++) { |
216 | 0 | hash_step(ctx, ctx->H, p); |
217 | 0 | add_blocks(32, ctx->S, p); |
218 | 0 | p += 32; |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | #define DATA_ORDER_IS_LITTLE_ENDIAN |
223 | | |
224 | 0 | #define HASH_CBLOCK GOSTR341194_CBLOCK |
225 | 0 | #define HASH_LONG GOSTR341194_LONG |
226 | | #define HASH_CTX GOSTR341194_CTX |
227 | | #define HASH_UPDATE GOSTR341194_Update |
228 | | #define HASH_TRANSFORM GOSTR341194_Transform |
229 | | #define HASH_NO_FINAL 1 |
230 | 0 | #define HASH_BLOCK_DATA_ORDER GOSTR341194_block_data_order |
231 | | |
232 | | #include "md32_common.h" |
233 | | |
234 | | int |
235 | | GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX * c) |
236 | 0 | { |
237 | 0 | unsigned char *p = (unsigned char *)c->data; |
238 | 0 | unsigned char T[32]; |
239 | |
|
240 | 0 | if (c->num > 0) { |
241 | 0 | memset(p + c->num, 0, 32 - c->num); |
242 | 0 | hash_step(c, c->H, p); |
243 | 0 | add_blocks(32, c->S, p); |
244 | 0 | } |
245 | |
|
246 | 0 | p = T; |
247 | 0 | HOST_l2c(c->Nl, p); |
248 | 0 | HOST_l2c(c->Nh, p); |
249 | 0 | memset(p, 0, 32 - 8); |
250 | 0 | hash_step(c, c->H, T); |
251 | 0 | hash_step(c, c->H, c->S); |
252 | |
|
253 | 0 | memcpy(md, c->H, 32); |
254 | |
|
255 | 0 | return 1; |
256 | 0 | } |
257 | | |
258 | | unsigned char * |
259 | | GOSTR341194(const unsigned char *d, size_t n, unsigned char *md, int nid) |
260 | 0 | { |
261 | 0 | GOSTR341194_CTX c; |
262 | 0 | static unsigned char m[GOSTR341194_LENGTH]; |
263 | |
|
264 | 0 | if (md == NULL) |
265 | 0 | md = m; |
266 | 0 | if (!GOSTR341194_Init(&c, nid)) |
267 | 0 | return 0; |
268 | 0 | GOSTR341194_Update(&c, d, n); |
269 | 0 | GOSTR341194_Final(md, &c); |
270 | 0 | explicit_bzero(&c, sizeof(c)); |
271 | 0 | return (md); |
272 | 0 | } |
273 | | #endif |