GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/gost/gostr341194.c Lines: 99 108 91.7 %
Date: 2017-11-13 Branches: 18 22 81.8 %

Line Branch Exec Source
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
{
68
	int i, j;
69
70
847044
	for (i = 0; i < 4; i++)
71
5544288
		for (j = 0; j < 8; j++)
72
2464128
			k[i + 4 * j] = w[8 * i + j];
73
77004
}
74
75
/* was A_A */
76
static void
77
circle_xor8(const unsigned char *w, unsigned char *k)
78
{
79
346518
	unsigned char buf[8];
80
	int i;
81
82
173259
	memcpy(buf, w, 8);
83
173259
	memmove(k, w + 8, 24);
84
3118662
	for (i = 0; i < 8; i++)
85
1386072
		k[i + 24] = buf[i] ^ k[i];
86
173259
}
87
88
/* was R_R */
89
static void
90
transform_3(unsigned char *data)
91
{
92
	unsigned short int acc;
93
94
4273722
	acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) |
95
1424574
	     ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8);
96
1424574
	memmove(data, data + 2, 30);
97
1424574
	data[30] = acc & 0xff;
98
1424574
	data[31] = acc >> 8;
99
1424574
}
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
{
105
	int i;
106
	int carry = 0;
107
	int sum;
108
109
764805
	for (i = 0; i < n; i++) {
110
365280
		sum = (int)left[i] + (int)right[i] + carry;
111
365280
		left[i] = sum & 0xff;
112
365280
		carry = sum >> 8;
113
	}
114
11415
	return carry;
115
}
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
{
122
	size_t i;
123
124
7738902
	for (i = 0; i < len; i++)
125
3696192
		result[i] = a[i] ^ b[i];
126
115506
}
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
{
135
38502
	unsigned char U[32], W[32], V[32], S[32], Key[32];
136
	int i;
137
138
	/* Compute first key */
139
19251
	xor_blocks(W, H, M, 32);
140
19251
	swap_bytes(W, Key);
141
	/* Encrypt first 8 bytes of H with first key */
142
19251
	Gost2814789_set_key(&c->cipher, Key, 256);
143
19251
	Gost2814789_encrypt(H, S, &c->cipher);
144
145
	/* Compute second key */
146
19251
	circle_xor8(H, U);
147
19251
	circle_xor8(M, V);
148
19251
	circle_xor8(V, V);
149
19251
	xor_blocks(W, U, V, 32);
150
19251
	swap_bytes(W, Key);
151
	/* encrypt second 8 bytes of H with second key */
152
19251
	Gost2814789_set_key(&c->cipher, Key, 256);
153
19251
	Gost2814789_encrypt(H+8, S+8, &c->cipher);
154
155
	/* compute third key */
156
19251
	circle_xor8(U, U);
157
19251
	U[31] = ~U[31];
158
19251
	U[29] = ~U[29];
159
19251
	U[28] = ~U[28];
160
19251
	U[24] = ~U[24];
161
19251
	U[23] = ~U[23];
162
19251
	U[20] = ~U[20];
163
19251
	U[18] = ~U[18];
164
19251
	U[17] = ~U[17];
165
19251
	U[14] = ~U[14];
166
19251
	U[12] = ~U[12];
167
19251
	U[10] = ~U[10];
168
19251
	U[8] = ~U[8];
169
19251
	U[7] = ~U[7];
170
19251
	U[5] = ~U[5];
171
19251
	U[3] = ~U[3];
172
19251
	U[1] = ~U[1];
173
19251
	circle_xor8(V, V);
174
19251
	circle_xor8(V, V);
175
19251
	xor_blocks(W, U, V, 32);
176
19251
	swap_bytes(W, Key);
177
	/* encrypt third 8 bytes of H with third key */
178
19251
	Gost2814789_set_key(&c->cipher, Key, 256);
179
19251
	Gost2814789_encrypt(H+16, S+16, &c->cipher);
180
181
	/* Compute fourth key */
182
19251
	circle_xor8(U, U);
183
19251
	circle_xor8(V, V);
184
19251
	circle_xor8(V, V);
185
19251
	xor_blocks(W, U, V, 32);
186
19251
	swap_bytes(W, Key);
187
	/* Encrypt last 8 bytes with fourth key */
188
19251
	Gost2814789_set_key(&c->cipher, Key, 256);
189
19251
	Gost2814789_encrypt(H+24, S+24, &c->cipher);
190
191
500526
	for (i = 0; i < 12; i++)
192
231012
		transform_3(S);
193
19251
	xor_blocks(S, S, M, 32);
194
19251
	transform_3(S);
195
19251
	xor_blocks(S, S, H, 32);
196
2387124
	for (i = 0; i < 61; i++)
197
1174311
		transform_3(S);
198
19251
	memcpy(H, S, 32);
199
19251
	return 1;
200
19251
}
201
202
int
203
GOSTR341194_Init(GOSTR341194_CTX *c, int nid)
204
{
205
7848
	memset(c, 0, sizeof(*c));
206
3924
	return Gost2814789_set_sbox(&c->cipher, nid);
207
}
208
209
static void
210
GOSTR341194_block_data_order(GOSTR341194_CTX *ctx, const unsigned char *p,
211
    size_t num)
212
{
213
	int i;
214
215
49990
	for (i = 0; i < num; i++) {
216
9998
		hash_step(ctx, ctx->H, p);
217
9998
		add_blocks(32, ctx->S, p);
218
9998
		p += 32;
219
	}
220
9998
}
221
222
#define DATA_ORDER_IS_LITTLE_ENDIAN
223
224
#define HASH_CBLOCK	GOSTR341194_CBLOCK
225
#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
#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
{
237
7836
	unsigned char *p = (unsigned char *)c->data;
238
3918
	unsigned char T[32];
239
240
3918
	if (c->num > 0) {
241
1417
		memset(p + c->num, 0, 32 - c->num);
242
1417
		hash_step(c, c->H, p);
243
1417
		add_blocks(32, c->S, p);
244
1417
	}
245
246
3918
	p = T;
247
3918
	HOST_l2c(c->Nl, p);
248
3918
	HOST_l2c(c->Nh, p);
249
3918
	memset(p, 0, 32 - 8);
250
3918
	hash_step(c, c->H, T);
251
3918
	hash_step(c, c->H, c->S);
252
253
3918
	memcpy(md, c->H, 32);
254
255
3918
	return 1;
256
3918
}
257
258
unsigned char *
259
GOSTR341194(const unsigned char *d, size_t n, unsigned char *md, int nid)
260
{
261
	GOSTR341194_CTX c;
262
	static unsigned char m[GOSTR341194_LENGTH];
263
264
	if (md == NULL)
265
		md = m;
266
	if (!GOSTR341194_Init(&c, nid))
267
		return 0;
268
	GOSTR341194_Update(&c, d, n);
269
	GOSTR341194_Final(md, &c);
270
	explicit_bzero(&c, sizeof(c));
271
	return (md);
272
}
273
#endif