xref: /freebsd/crypto/libecc/src/hash/belt-hash.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans  *  Copyright (C) 2022 - This file is part of libecc project
3*f0865ec9SKyle Evans  *
4*f0865ec9SKyle Evans  *  Authors:
5*f0865ec9SKyle Evans  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans  *
8*f0865ec9SKyle Evans  *  This software is licensed under a dual BSD and GPL v2 license.
9*f0865ec9SKyle Evans  *  See LICENSE file at the root folder of the project.
10*f0865ec9SKyle Evans  */
11*f0865ec9SKyle Evans #include <libecc/lib_ecc_config.h>
12*f0865ec9SKyle Evans #ifdef WITH_HASH_BELT_HASH
13*f0865ec9SKyle Evans 
14*f0865ec9SKyle Evans #include <libecc/hash/belt-hash.h>
15*f0865ec9SKyle Evans 
16*f0865ec9SKyle Evans /*
17*f0865ec9SKyle Evans  * This is an implementation of the BELT-HASH hash function as
18*f0865ec9SKyle Evans  * defined int STB 34.101.31.
19*f0865ec9SKyle Evans  */
20*f0865ec9SKyle Evans 
21*f0865ec9SKyle Evans 
22*f0865ec9SKyle Evans /*
23*f0865ec9SKyle Evans  * The BELT-HASH function uses an underlying BELT block cipher
24*f0865ec9SKyle Evans  * defined in STB 34.101.31. This is a simple and straitforward
25*f0865ec9SKyle Evans  * implementation.
26*f0865ec9SKyle Evans  */
27*f0865ec9SKyle Evans #define ROTL_BELT(x, n)      ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))
28*f0865ec9SKyle Evans 
29*f0865ec9SKyle Evans #define SWAP_BELT(x, y) do {		\
30*f0865ec9SKyle Evans 	u32 z;				\
31*f0865ec9SKyle Evans 	z = (x);			\
32*f0865ec9SKyle Evans 	(x) = (y);			\
33*f0865ec9SKyle Evans 	(y) = z;			\
34*f0865ec9SKyle Evans } while(0)
35*f0865ec9SKyle Evans 
36*f0865ec9SKyle Evans /* The S-Box */
37*f0865ec9SKyle Evans static u8 S[256] =
38*f0865ec9SKyle Evans {
39*f0865ec9SKyle Evans 	0xB1, 0x94, 0xBA, 0xC8, 0x0A, 0x08, 0xF5, 0x3B, 0x36, 0x6D, 0x00, 0x8E, 0x58, 0x4A, 0x5D, 0xE4,
40*f0865ec9SKyle Evans 	0x85, 0x04, 0xFA, 0x9D, 0x1B, 0xB6, 0xC7, 0xAC, 0x25, 0x2E, 0x72, 0xC2, 0x02, 0xFD, 0xCE, 0x0D,
41*f0865ec9SKyle Evans 	0x5B, 0xE3, 0xD6, 0x12, 0x17, 0xB9, 0x61, 0x81, 0xFE, 0x67, 0x86, 0xAD, 0x71, 0x6B, 0x89, 0x0B,
42*f0865ec9SKyle Evans 	0x5C, 0xB0, 0xC0, 0xFF, 0x33, 0xC3, 0x56, 0xB8, 0x35, 0xC4, 0x05, 0xAE, 0xD8, 0xE0, 0x7F, 0x99,
43*f0865ec9SKyle Evans 	0xE1, 0x2B, 0xDC, 0x1A, 0xE2, 0x82, 0x57, 0xEC, 0x70, 0x3F, 0xCC, 0xF0, 0x95, 0xEE, 0x8D, 0xF1,
44*f0865ec9SKyle Evans 	0xC1, 0xAB, 0x76, 0x38, 0x9F, 0xE6, 0x78, 0xCA, 0xF7, 0xC6, 0xF8, 0x60, 0xD5, 0xBB, 0x9C, 0x4F,
45*f0865ec9SKyle Evans 	0xF3, 0x3C, 0x65, 0x7B, 0x63, 0x7C, 0x30, 0x6A, 0xDD, 0x4E, 0xA7, 0x79, 0x9E, 0xB2, 0x3D, 0x31,
46*f0865ec9SKyle Evans 	0x3E, 0x98, 0xB5, 0x6E, 0x27, 0xD3, 0xBC, 0xCF, 0x59, 0x1E, 0x18, 0x1F, 0x4C, 0x5A, 0xB7, 0x93,
47*f0865ec9SKyle Evans 	0xE9, 0xDE, 0xE7, 0x2C, 0x8F, 0x0C, 0x0F, 0xA6, 0x2D, 0xDB, 0x49, 0xF4, 0x6F, 0x73, 0x96, 0x47,
48*f0865ec9SKyle Evans 	0x06, 0x07, 0x53, 0x16, 0xED, 0x24, 0x7A, 0x37, 0x39, 0xCB, 0xA3, 0x83, 0x03, 0xA9, 0x8B, 0xF6,
49*f0865ec9SKyle Evans 	0x92, 0xBD, 0x9B, 0x1C, 0xE5, 0xD1, 0x41, 0x01, 0x54, 0x45, 0xFB, 0xC9, 0x5E, 0x4D, 0x0E, 0xF2,
50*f0865ec9SKyle Evans 	0x68, 0x20, 0x80, 0xAA, 0x22, 0x7D, 0x64, 0x2F, 0x26, 0x87, 0xF9, 0x34, 0x90, 0x40, 0x55, 0x11,
51*f0865ec9SKyle Evans 	0xBE, 0x32, 0x97, 0x13, 0x43, 0xFC, 0x9A, 0x48, 0xA0, 0x2A, 0x88, 0x5F, 0x19, 0x4B, 0x09, 0xA1,
52*f0865ec9SKyle Evans 	0x7E, 0xCD, 0xA4, 0xD0, 0x15, 0x44, 0xAF, 0x8C, 0xA5, 0x84, 0x50, 0xBF, 0x66, 0xD2, 0xE8, 0x8A,
53*f0865ec9SKyle Evans 	0xA2, 0xD7, 0x46, 0x52, 0x42, 0xA8, 0xDF, 0xB3, 0x69, 0x74, 0xC5, 0x51, 0xEB, 0x23, 0x29, 0x21,
54*f0865ec9SKyle Evans 	0xD4, 0xEF, 0xD9, 0xB4, 0x3A, 0x62, 0x28, 0x75, 0x91, 0x14, 0x10, 0xEA, 0x77, 0x6C, 0xDA, 0x1D,
55*f0865ec9SKyle Evans };
56*f0865ec9SKyle Evans 
57*f0865ec9SKyle Evans /* */
58*f0865ec9SKyle Evans #define GET_BYTE(x, a) ( ((x) >> (a)) & 0xff )
59*f0865ec9SKyle Evans #define PUT_BYTE(x, a) ( (u32)(x) << (a) )
60*f0865ec9SKyle Evans #define SB(x, a) PUT_BYTE( S[GET_BYTE((x), (a))], (a) )
61*f0865ec9SKyle Evans 
62*f0865ec9SKyle Evans #define G(x, r) ROTL_BELT( SB((x), 24) | SB((x), 16) | SB((x), 8) | SB((x), 0), (r) )
63*f0865ec9SKyle Evans 
64*f0865ec9SKyle Evans static u32 KIdx[8][7] =
65*f0865ec9SKyle Evans {
66*f0865ec9SKyle Evans 	{ 0, 1, 2, 3, 4, 5, 6 },
67*f0865ec9SKyle Evans 	{ 7, 0, 1, 2, 3, 4, 5 },
68*f0865ec9SKyle Evans 	{ 6, 7, 0, 1, 2, 3, 4 },
69*f0865ec9SKyle Evans 	{ 5, 6, 7, 0, 1, 2, 3 },
70*f0865ec9SKyle Evans 	{ 4, 5, 6, 7, 0, 1, 2 },
71*f0865ec9SKyle Evans 	{ 3, 4, 5, 6, 7, 0, 1 },
72*f0865ec9SKyle Evans 	{ 2, 3, 4, 5, 6, 7, 0 },
73*f0865ec9SKyle Evans 	{ 1, 2, 3, 4, 5, 6, 7 },
74*f0865ec9SKyle Evans };
75*f0865ec9SKyle Evans 
belt_init(const u8 * k,u32 k_len,u8 ks[BELT_KEY_SCHED_LEN])76*f0865ec9SKyle Evans int belt_init(const u8 *k, u32 k_len, u8 ks[BELT_KEY_SCHED_LEN])
77*f0865ec9SKyle Evans {
78*f0865ec9SKyle Evans 	int ret = -1;
79*f0865ec9SKyle Evans 	unsigned int i;
80*f0865ec9SKyle Evans 
81*f0865ec9SKyle Evans 	switch(k_len){
82*f0865ec9SKyle Evans 		case 16:{
83*f0865ec9SKyle Evans 			for(i = 0; i < 16; i++){
84*f0865ec9SKyle Evans 				ks[i]      = k[i];
85*f0865ec9SKyle Evans 				ks[i + 16] = k[i];
86*f0865ec9SKyle Evans 			}
87*f0865ec9SKyle Evans 			break;
88*f0865ec9SKyle Evans 		}
89*f0865ec9SKyle Evans 		case 24:{
90*f0865ec9SKyle Evans 			for(i = 0; i < 24; i++){
91*f0865ec9SKyle Evans 				ks[i] = k[i];
92*f0865ec9SKyle Evans 			}
93*f0865ec9SKyle Evans 			for(i = 24; i < 32; i++){
94*f0865ec9SKyle Evans 				ks[i] = k[i - 24] ^ k[i - 20] ^ k[i - 16];
95*f0865ec9SKyle Evans 			}
96*f0865ec9SKyle Evans 			break;
97*f0865ec9SKyle Evans 		}
98*f0865ec9SKyle Evans 		case 32:{
99*f0865ec9SKyle Evans 			for(i = 0; i < 32; i++){
100*f0865ec9SKyle Evans 				ks[i] = k[i];
101*f0865ec9SKyle Evans 			}
102*f0865ec9SKyle Evans 			break;
103*f0865ec9SKyle Evans 		}
104*f0865ec9SKyle Evans 		default:{
105*f0865ec9SKyle Evans 			ret = -1;
106*f0865ec9SKyle Evans 			goto err;
107*f0865ec9SKyle Evans 		}
108*f0865ec9SKyle Evans 
109*f0865ec9SKyle Evans 
110*f0865ec9SKyle Evans 	}
111*f0865ec9SKyle Evans 
112*f0865ec9SKyle Evans 	ret = 0;
113*f0865ec9SKyle Evans err:
114*f0865ec9SKyle Evans 	return ret;
115*f0865ec9SKyle Evans }
116*f0865ec9SKyle Evans 
belt_encrypt(const u8 in[BELT_BLOCK_LEN],u8 out[BELT_BLOCK_LEN],const u8 ks[BELT_KEY_SCHED_LEN])117*f0865ec9SKyle Evans void belt_encrypt(const u8 in[BELT_BLOCK_LEN], u8 out[BELT_BLOCK_LEN], const u8 ks[BELT_KEY_SCHED_LEN])
118*f0865ec9SKyle Evans {
119*f0865ec9SKyle Evans 	u32 a, b, c, d, e;
120*f0865ec9SKyle Evans 	u32 i;
121*f0865ec9SKyle Evans 
122*f0865ec9SKyle Evans 	GET_UINT32_LE(a, in, 0);
123*f0865ec9SKyle Evans 	GET_UINT32_LE(b, in, 4);
124*f0865ec9SKyle Evans 	GET_UINT32_LE(c, in, 8);
125*f0865ec9SKyle Evans 	GET_UINT32_LE(d, in, 12);
126*f0865ec9SKyle Evans 
127*f0865ec9SKyle Evans 	for(i = 0; i < 8; i++){
128*f0865ec9SKyle Evans 		u32 key;
129*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][0]);
130*f0865ec9SKyle Evans 		b ^= G(a + key, 5);
131*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][1]);
132*f0865ec9SKyle Evans 		c ^= G(d + key, 21);
133*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][2]);
134*f0865ec9SKyle Evans 		a = (u32)(a - G(b + key, 13));
135*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][3]);
136*f0865ec9SKyle Evans 		e = G(b + c + key, 21) ^ (i + 1);
137*f0865ec9SKyle Evans 		b += e;
138*f0865ec9SKyle Evans 		c = (u32)(c - e);
139*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][4]);
140*f0865ec9SKyle Evans 		d += G(c + key, 13);
141*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][5]);
142*f0865ec9SKyle Evans 		b ^= G(a + key, 21);
143*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][6]);
144*f0865ec9SKyle Evans 		c ^= G(d + key, 5);
145*f0865ec9SKyle Evans 		SWAP_BELT(a, b);
146*f0865ec9SKyle Evans 		SWAP_BELT(c, d);
147*f0865ec9SKyle Evans 		SWAP_BELT(b, c);
148*f0865ec9SKyle Evans 	}
149*f0865ec9SKyle Evans 
150*f0865ec9SKyle Evans 	PUT_UINT32_LE(b, out, 0);
151*f0865ec9SKyle Evans 	PUT_UINT32_LE(d, out, 4);
152*f0865ec9SKyle Evans 	PUT_UINT32_LE(a, out, 8);
153*f0865ec9SKyle Evans 	PUT_UINT32_LE(c, out, 12);
154*f0865ec9SKyle Evans 
155*f0865ec9SKyle Evans 	return;
156*f0865ec9SKyle Evans }
157*f0865ec9SKyle Evans 
belt_decrypt(const u8 in[BELT_BLOCK_LEN],u8 out[BELT_BLOCK_LEN],const u8 ks[BELT_KEY_SCHED_LEN])158*f0865ec9SKyle Evans void belt_decrypt(const u8 in[BELT_BLOCK_LEN], u8 out[BELT_BLOCK_LEN], const u8 ks[BELT_KEY_SCHED_LEN])
159*f0865ec9SKyle Evans {
160*f0865ec9SKyle Evans 	u32 a, b, c, d, e;
161*f0865ec9SKyle Evans 	u32 i;
162*f0865ec9SKyle Evans 
163*f0865ec9SKyle Evans 	GET_UINT32_LE(a, in, 0);
164*f0865ec9SKyle Evans 	GET_UINT32_LE(b, in, 4);
165*f0865ec9SKyle Evans 	GET_UINT32_LE(c, in, 8);
166*f0865ec9SKyle Evans 	GET_UINT32_LE(d, in, 12);
167*f0865ec9SKyle Evans 
168*f0865ec9SKyle Evans 	for(i = 0; i < 8; i++){
169*f0865ec9SKyle Evans 		u32 key;
170*f0865ec9SKyle Evans 		u32 j = (7 - i);
171*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][6]);
172*f0865ec9SKyle Evans 		b ^= G(a + key, 5);
173*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][5]);
174*f0865ec9SKyle Evans 		c ^= G(d + key, 21);
175*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][4]);
176*f0865ec9SKyle Evans 		a = (u32)(a - G(b + key, 13));
177*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][3]);
178*f0865ec9SKyle Evans 		e = G(b + c + key, 21) ^ (j + 1);
179*f0865ec9SKyle Evans 		b += e;
180*f0865ec9SKyle Evans 		c = (u32)(c - e);
181*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][2]);
182*f0865ec9SKyle Evans 		d += G(c + key, 13);
183*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][1]);
184*f0865ec9SKyle Evans 		b ^= G(a + key, 21);
185*f0865ec9SKyle Evans 		GET_UINT32_LE(key, ks, 4*KIdx[i][0]);
186*f0865ec9SKyle Evans 		c ^= G(d + key, 5);
187*f0865ec9SKyle Evans 		SWAP_BELT(a, b);
188*f0865ec9SKyle Evans 		SWAP_BELT(c, d);
189*f0865ec9SKyle Evans 		SWAP_BELT(a, d);
190*f0865ec9SKyle Evans 	}
191*f0865ec9SKyle Evans 
192*f0865ec9SKyle Evans 	PUT_UINT32_LE(c, out, 0);
193*f0865ec9SKyle Evans 	PUT_UINT32_LE(a, out, 4);
194*f0865ec9SKyle Evans 	PUT_UINT32_LE(d, out, 8);
195*f0865ec9SKyle Evans 	PUT_UINT32_LE(b, out, 12);
196*f0865ec9SKyle Evans 
197*f0865ec9SKyle Evans 	return;
198*f0865ec9SKyle Evans }
199*f0865ec9SKyle Evans 
200*f0865ec9SKyle Evans /* BELT-HASH primitives */
sigma1_xor(const u8 x[2* BELT_BLOCK_LEN],const u8 h[2* BELT_BLOCK_LEN],u8 s[BELT_BLOCK_LEN],u8 use_xor)201*f0865ec9SKyle Evans static void sigma1_xor(const u8 x[2 * BELT_BLOCK_LEN], const u8 h[2 * BELT_BLOCK_LEN], u8 s[BELT_BLOCK_LEN], u8 use_xor){
202*f0865ec9SKyle Evans 	u8 tmp1[BELT_BLOCK_LEN];
203*f0865ec9SKyle Evans 	unsigned int i;
204*f0865ec9SKyle Evans 
205*f0865ec9SKyle Evans 	for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){
206*f0865ec9SKyle Evans 		tmp1[i] = (h[i] ^ h[i + BELT_BLOCK_LEN]);
207*f0865ec9SKyle Evans 		tmp1[i + (BELT_BLOCK_LEN / 2)] = (h[i + (BELT_BLOCK_LEN / 2)] ^ h[i + BELT_BLOCK_LEN + (BELT_BLOCK_LEN / 2)]);
208*f0865ec9SKyle Evans 	}
209*f0865ec9SKyle Evans 
210*f0865ec9SKyle Evans 	if(use_xor){
211*f0865ec9SKyle Evans 		u8 tmp2[BELT_BLOCK_LEN];
212*f0865ec9SKyle Evans 
213*f0865ec9SKyle Evans 		belt_encrypt(tmp1, tmp2, x);
214*f0865ec9SKyle Evans 
215*f0865ec9SKyle Evans 		for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){
216*f0865ec9SKyle Evans 			s[i] ^= (tmp1[i] ^ tmp2[i]);
217*f0865ec9SKyle Evans 			s[i + (BELT_BLOCK_LEN / 2)] ^= (tmp1[i + (BELT_BLOCK_LEN / 2)] ^ tmp2[i + (BELT_BLOCK_LEN / 2)]);
218*f0865ec9SKyle Evans 		}
219*f0865ec9SKyle Evans 	}
220*f0865ec9SKyle Evans 	else{
221*f0865ec9SKyle Evans 		belt_encrypt(tmp1, s, x);
222*f0865ec9SKyle Evans 		for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){
223*f0865ec9SKyle Evans 			s[i] ^= tmp1[i];
224*f0865ec9SKyle Evans 			s[i + (BELT_BLOCK_LEN / 2)] ^= tmp1[i + (BELT_BLOCK_LEN / 2)];
225*f0865ec9SKyle Evans 		}
226*f0865ec9SKyle Evans 	}
227*f0865ec9SKyle Evans 
228*f0865ec9SKyle Evans 	return;
229*f0865ec9SKyle Evans }
230*f0865ec9SKyle Evans 
sigma2(const u8 x[2* BELT_BLOCK_LEN],u8 const h[2* BELT_BLOCK_LEN],u8 result[2* BELT_BLOCK_LEN])231*f0865ec9SKyle Evans static void sigma2(const u8 x[2 * BELT_BLOCK_LEN], u8 const h[2 * BELT_BLOCK_LEN], u8 result[2 * BELT_BLOCK_LEN])
232*f0865ec9SKyle Evans {
233*f0865ec9SKyle Evans 	u8 teta[BELT_KEY_SCHED_LEN];
234*f0865ec9SKyle Evans 	u8 tmp[BELT_BLOCK_LEN];
235*f0865ec9SKyle Evans 	unsigned int i;
236*f0865ec9SKyle Evans 
237*f0865ec9SKyle Evans 	/* Copy the beginning of h for later in case it is lost */
238*f0865ec9SKyle Evans 	IGNORE_RET_VAL(local_memcpy(&tmp[0], &h[0], BELT_BLOCK_LEN));
239*f0865ec9SKyle Evans 
240*f0865ec9SKyle Evans 	sigma1_xor(x, h, teta, 0);
241*f0865ec9SKyle Evans 	IGNORE_RET_VAL(local_memcpy(&teta[BELT_BLOCK_LEN], &h[BELT_BLOCK_LEN], BELT_BLOCK_LEN));
242*f0865ec9SKyle Evans 
243*f0865ec9SKyle Evans 	belt_encrypt(x, result, teta);
244*f0865ec9SKyle Evans 	for(i = 0; i < BELT_BLOCK_LEN; i++){
245*f0865ec9SKyle Evans 		result[i]  ^= x[i];
246*f0865ec9SKyle Evans 		teta[i]    ^= 0xff;
247*f0865ec9SKyle Evans 		teta[i + BELT_BLOCK_LEN] = tmp[i];
248*f0865ec9SKyle Evans 	}
249*f0865ec9SKyle Evans 
250*f0865ec9SKyle Evans 	belt_encrypt(&x[BELT_BLOCK_LEN], &result[BELT_BLOCK_LEN], teta);
251*f0865ec9SKyle Evans 
252*f0865ec9SKyle Evans 	for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){
253*f0865ec9SKyle Evans 		result[i + BELT_BLOCK_LEN] ^= x[i + BELT_BLOCK_LEN];
254*f0865ec9SKyle Evans 		result[i + BELT_BLOCK_LEN + (BELT_BLOCK_LEN / 2)] ^= x[i + BELT_BLOCK_LEN + (BELT_BLOCK_LEN / 2)];
255*f0865ec9SKyle Evans 	}
256*f0865ec9SKyle Evans 
257*f0865ec9SKyle Evans 	return;
258*f0865ec9SKyle Evans }
259*f0865ec9SKyle Evans 
_belt_hash_process(const u8 x[2* BELT_BLOCK_LEN],u8 h[2* BELT_BLOCK_LEN],u8 s[BELT_BLOCK_LEN])260*f0865ec9SKyle Evans static void _belt_hash_process(const u8 x[2 * BELT_BLOCK_LEN], u8 h[2 * BELT_BLOCK_LEN], u8 s[BELT_BLOCK_LEN])
261*f0865ec9SKyle Evans {
262*f0865ec9SKyle Evans 	sigma1_xor(x, h, s, 1);
263*f0865ec9SKyle Evans 
264*f0865ec9SKyle Evans 	sigma2(x, h, h);
265*f0865ec9SKyle Evans 
266*f0865ec9SKyle Evans 	return;
267*f0865ec9SKyle Evans }
268*f0865ec9SKyle Evans 
belt_hash_process(belt_hash_context * ctx,const u8 data[BELT_HASH_BLOCK_SIZE])269*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static int belt_hash_process(belt_hash_context *ctx, const u8 data[BELT_HASH_BLOCK_SIZE])
270*f0865ec9SKyle Evans {
271*f0865ec9SKyle Evans 	_belt_hash_process(data, ctx->belt_hash_h, &(ctx->belt_hash_state[BELT_BLOCK_LEN]));
272*f0865ec9SKyle Evans 
273*f0865ec9SKyle Evans 	return 0;
274*f0865ec9SKyle Evans }
275*f0865ec9SKyle Evans 
belt_hash_finalize(const u8 s[2* BELT_BLOCK_LEN],const u8 h[2* BELT_BLOCK_LEN],u8 res[2* BELT_BLOCK_LEN])276*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static int belt_hash_finalize(const u8 s[2 * BELT_BLOCK_LEN], const u8 h[2 * BELT_BLOCK_LEN], u8 res[2 * BELT_BLOCK_LEN])
277*f0865ec9SKyle Evans {
278*f0865ec9SKyle Evans 	sigma2(s, h, res);
279*f0865ec9SKyle Evans 
280*f0865ec9SKyle Evans 	return 0;
281*f0865ec9SKyle Evans }
282*f0865ec9SKyle Evans 
belt_update_ctr(belt_hash_context * ctx,u8 len_bytes)283*f0865ec9SKyle Evans static void belt_update_ctr(belt_hash_context *ctx, u8 len_bytes)
284*f0865ec9SKyle Evans {
285*f0865ec9SKyle Evans 	/* Perform a simple addition on 128 bits on the first part of the state */
286*f0865ec9SKyle Evans 	u64 a0, a1, b, c;
287*f0865ec9SKyle Evans 
288*f0865ec9SKyle Evans 	GET_UINT64_LE(a0, (const u8*)(ctx->belt_hash_state), 0);
289*f0865ec9SKyle Evans 	GET_UINT64_LE(a1, (const u8*)(ctx->belt_hash_state), 8);
290*f0865ec9SKyle Evans 
291*f0865ec9SKyle Evans 	b = (u64)(len_bytes << 3);
292*f0865ec9SKyle Evans 
293*f0865ec9SKyle Evans 	c = (a0 + b);
294*f0865ec9SKyle Evans 	if(c < b){
295*f0865ec9SKyle Evans 		/* Handle carry */
296*f0865ec9SKyle Evans 		a1 += 1;
297*f0865ec9SKyle Evans 	}
298*f0865ec9SKyle Evans 
299*f0865ec9SKyle Evans 	/* Store the result */
300*f0865ec9SKyle Evans 	PUT_UINT64_LE(c,  (u8*)(ctx->belt_hash_state), 0);
301*f0865ec9SKyle Evans 	PUT_UINT64_LE(a1, (u8*)(ctx->belt_hash_state), 8);
302*f0865ec9SKyle Evans 
303*f0865ec9SKyle Evans 	return;
304*f0865ec9SKyle Evans }
305*f0865ec9SKyle Evans 
306*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
belt_hash_init(belt_hash_context * ctx)307*f0865ec9SKyle Evans int belt_hash_init(belt_hash_context *ctx)
308*f0865ec9SKyle Evans {
309*f0865ec9SKyle Evans 	int ret;
310*f0865ec9SKyle Evans 
311*f0865ec9SKyle Evans 	MUST_HAVE((ctx != NULL), ret, err);
312*f0865ec9SKyle Evans 
313*f0865ec9SKyle Evans 	ctx->belt_hash_total = 0;
314*f0865ec9SKyle Evans 
315*f0865ec9SKyle Evans 	ret = local_memset(ctx->belt_hash_state, 0, sizeof(ctx->belt_hash_state)); EG(ret, err);
316*f0865ec9SKyle Evans 
317*f0865ec9SKyle Evans 	PUT_UINT64_LE(0x3bf5080ac8ba94b1ULL, ctx->belt_hash_h,  0);
318*f0865ec9SKyle Evans 	PUT_UINT64_LE(0xe45d4a588e006d36ULL, ctx->belt_hash_h,  8);
319*f0865ec9SKyle Evans 	PUT_UINT64_LE(0xacc7b61b9dfa0485ULL, ctx->belt_hash_h, 16);
320*f0865ec9SKyle Evans 	PUT_UINT64_LE(0x0dcefd02c2722e25ULL, ctx->belt_hash_h, 24);
321*f0865ec9SKyle Evans 
322*f0865ec9SKyle Evans 	/* Tell that we are initialized */
323*f0865ec9SKyle Evans 	ctx->magic = BELT_HASH_HASH_MAGIC;
324*f0865ec9SKyle Evans 
325*f0865ec9SKyle Evans 	ret = 0;
326*f0865ec9SKyle Evans 
327*f0865ec9SKyle Evans err:
328*f0865ec9SKyle Evans 	return ret;
329*f0865ec9SKyle Evans }
330*f0865ec9SKyle Evans 
331*f0865ec9SKyle Evans /* Update hash function. Returns 0 on success, -1 on error. */
belt_hash_update(belt_hash_context * ctx,const u8 * input,u32 ilen)332*f0865ec9SKyle Evans int belt_hash_update(belt_hash_context *ctx, const u8 *input, u32 ilen)
333*f0865ec9SKyle Evans {
334*f0865ec9SKyle Evans 	const u8 *data_ptr = input;
335*f0865ec9SKyle Evans 	u32 remain_ilen = ilen;
336*f0865ec9SKyle Evans 	u16 fill;
337*f0865ec9SKyle Evans 	u8 left;
338*f0865ec9SKyle Evans 	int ret;
339*f0865ec9SKyle Evans 
340*f0865ec9SKyle Evans 	MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
341*f0865ec9SKyle Evans 	BELT_HASH_HASH_CHECK_INITIALIZED(ctx, ret, err);
342*f0865ec9SKyle Evans 
343*f0865ec9SKyle Evans 	/* Nothing to process, return */
344*f0865ec9SKyle Evans 	if (ilen == 0) {
345*f0865ec9SKyle Evans 		ret = 0;
346*f0865ec9SKyle Evans 		goto err;
347*f0865ec9SKyle Evans 	}
348*f0865ec9SKyle Evans 
349*f0865ec9SKyle Evans 	/* Get what's left in our local buffer */
350*f0865ec9SKyle Evans 	left = (ctx->belt_hash_total & (BELT_HASH_BLOCK_SIZE - 1));
351*f0865ec9SKyle Evans 	fill = (u16)(BELT_HASH_BLOCK_SIZE - left);
352*f0865ec9SKyle Evans 
353*f0865ec9SKyle Evans 	ctx->belt_hash_total += ilen;
354*f0865ec9SKyle Evans 
355*f0865ec9SKyle Evans 	if ((left > 0) && (remain_ilen >= fill)) {
356*f0865ec9SKyle Evans 		/* Copy data at the end of the buffer */
357*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->belt_hash_buffer + left, data_ptr, fill); EG(ret, err);
358*f0865ec9SKyle Evans 		/* Update the counter with one full block */
359*f0865ec9SKyle Evans 		belt_update_ctr(ctx, BELT_HASH_BLOCK_SIZE);
360*f0865ec9SKyle Evans 		/* Process */
361*f0865ec9SKyle Evans 		ret = belt_hash_process(ctx, ctx->belt_hash_buffer); EG(ret, err);
362*f0865ec9SKyle Evans 		data_ptr += fill;
363*f0865ec9SKyle Evans 		remain_ilen -= fill;
364*f0865ec9SKyle Evans 		left = 0;
365*f0865ec9SKyle Evans 	}
366*f0865ec9SKyle Evans 
367*f0865ec9SKyle Evans 	while (remain_ilen >= BELT_HASH_BLOCK_SIZE) {
368*f0865ec9SKyle Evans 		/* Update the counter with one full block */
369*f0865ec9SKyle Evans 		belt_update_ctr(ctx, BELT_HASH_BLOCK_SIZE);
370*f0865ec9SKyle Evans 		/* Process */
371*f0865ec9SKyle Evans 		ret = belt_hash_process(ctx, data_ptr); EG(ret, err);
372*f0865ec9SKyle Evans 		data_ptr += BELT_HASH_BLOCK_SIZE;
373*f0865ec9SKyle Evans 		remain_ilen -= BELT_HASH_BLOCK_SIZE;
374*f0865ec9SKyle Evans 	}
375*f0865ec9SKyle Evans 
376*f0865ec9SKyle Evans 	if (remain_ilen > 0) {
377*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->belt_hash_buffer + left, data_ptr, remain_ilen); EG(ret, err);
378*f0865ec9SKyle Evans 	}
379*f0865ec9SKyle Evans 
380*f0865ec9SKyle Evans 	ret = 0;
381*f0865ec9SKyle Evans 
382*f0865ec9SKyle Evans err:
383*f0865ec9SKyle Evans 	return ret;
384*f0865ec9SKyle Evans }
385*f0865ec9SKyle Evans 
386*f0865ec9SKyle Evans /* Finalize. Returns 0 on success, -1 on error.*/
belt_hash_final(belt_hash_context * ctx,u8 output[BELT_HASH_DIGEST_SIZE])387*f0865ec9SKyle Evans int belt_hash_final(belt_hash_context *ctx, u8 output[BELT_HASH_DIGEST_SIZE])
388*f0865ec9SKyle Evans {
389*f0865ec9SKyle Evans 	int ret;
390*f0865ec9SKyle Evans 	unsigned int i;
391*f0865ec9SKyle Evans 
392*f0865ec9SKyle Evans 	MUST_HAVE((output != NULL), ret, err);
393*f0865ec9SKyle Evans 	BELT_HASH_HASH_CHECK_INITIALIZED(ctx, ret, err);
394*f0865ec9SKyle Evans 
395*f0865ec9SKyle Evans 	if((ctx->belt_hash_total % BELT_HASH_BLOCK_SIZE) != 0){
396*f0865ec9SKyle Evans 		/* Pad our last block with zeroes */
397*f0865ec9SKyle Evans 		for(i = (ctx->belt_hash_total % BELT_HASH_BLOCK_SIZE); i < BELT_HASH_BLOCK_SIZE; i++){
398*f0865ec9SKyle Evans 			ctx->belt_hash_buffer[i] = 0;
399*f0865ec9SKyle Evans 		}
400*f0865ec9SKyle Evans 
401*f0865ec9SKyle Evans 		/* Update the counter with the remaining data */
402*f0865ec9SKyle Evans 		belt_update_ctr(ctx, (u8)(ctx->belt_hash_total % BELT_HASH_BLOCK_SIZE));
403*f0865ec9SKyle Evans 
404*f0865ec9SKyle Evans 		/* Process the last block */
405*f0865ec9SKyle Evans 		ret = belt_hash_process(ctx, ctx->belt_hash_buffer); EG(ret, err);
406*f0865ec9SKyle Evans 	}
407*f0865ec9SKyle Evans 
408*f0865ec9SKyle Evans 	/* Finalize and output the result */
409*f0865ec9SKyle Evans 	ret = belt_hash_finalize(ctx->belt_hash_state, ctx->belt_hash_h, output); EG(ret, err);
410*f0865ec9SKyle Evans 
411*f0865ec9SKyle Evans 	/* Tell that we are uninitialized */
412*f0865ec9SKyle Evans 	ctx->magic = WORD(0);
413*f0865ec9SKyle Evans 
414*f0865ec9SKyle Evans 	ret = 0;
415*f0865ec9SKyle Evans 
416*f0865ec9SKyle Evans err:
417*f0865ec9SKyle Evans 	return ret;
418*f0865ec9SKyle Evans }
419*f0865ec9SKyle Evans 
420*f0865ec9SKyle Evans /*
421*f0865ec9SKyle Evans  * Scattered version performing init/update/finalize on a vector of buffers
422*f0865ec9SKyle Evans  * 'inputs' with the length of each buffer passed via 'ilens'. The function
423*f0865ec9SKyle Evans  * loops on pointers in 'inputs' until it finds a NULL pointer. The function
424*f0865ec9SKyle Evans  * returns 0 on success, -1 on error.
425*f0865ec9SKyle Evans  */
belt_hash_scattered(const u8 ** inputs,const u32 * ilens,u8 output[BELT_HASH_DIGEST_SIZE])426*f0865ec9SKyle Evans int belt_hash_scattered(const u8 **inputs, const u32 *ilens,
427*f0865ec9SKyle Evans 		      u8 output[BELT_HASH_DIGEST_SIZE])
428*f0865ec9SKyle Evans {
429*f0865ec9SKyle Evans 	belt_hash_context ctx;
430*f0865ec9SKyle Evans 	int ret, pos = 0;
431*f0865ec9SKyle Evans 
432*f0865ec9SKyle Evans 	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
433*f0865ec9SKyle Evans 
434*f0865ec9SKyle Evans 	ret = belt_hash_init(&ctx); EG(ret, err);
435*f0865ec9SKyle Evans 
436*f0865ec9SKyle Evans 	while (inputs[pos] != NULL) {
437*f0865ec9SKyle Evans 		ret = belt_hash_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
438*f0865ec9SKyle Evans 		pos += 1;
439*f0865ec9SKyle Evans 	}
440*f0865ec9SKyle Evans 
441*f0865ec9SKyle Evans 	ret = belt_hash_final(&ctx, output);
442*f0865ec9SKyle Evans 
443*f0865ec9SKyle Evans err:
444*f0865ec9SKyle Evans 	return ret;
445*f0865ec9SKyle Evans }
446*f0865ec9SKyle Evans 
447*f0865ec9SKyle Evans /*
448*f0865ec9SKyle Evans  * Single call version performing init/update/final on given input.
449*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
450*f0865ec9SKyle Evans  */
belt_hash(const u8 * input,u32 ilen,u8 output[BELT_HASH_DIGEST_SIZE])451*f0865ec9SKyle Evans int belt_hash(const u8 *input, u32 ilen, u8 output[BELT_HASH_DIGEST_SIZE])
452*f0865ec9SKyle Evans {
453*f0865ec9SKyle Evans 	belt_hash_context ctx;
454*f0865ec9SKyle Evans 	int ret;
455*f0865ec9SKyle Evans 
456*f0865ec9SKyle Evans 	ret = belt_hash_init(&ctx); EG(ret, err);
457*f0865ec9SKyle Evans 	ret = belt_hash_update(&ctx, input, ilen); EG(ret, err);
458*f0865ec9SKyle Evans 	ret = belt_hash_final(&ctx, output);
459*f0865ec9SKyle Evans 
460*f0865ec9SKyle Evans err:
461*f0865ec9SKyle Evans 	return ret;
462*f0865ec9SKyle Evans }
463*f0865ec9SKyle Evans 
464*f0865ec9SKyle Evans #else /* WITH_HASH_BELT_HASH */
465*f0865ec9SKyle Evans 
466*f0865ec9SKyle Evans /*
467*f0865ec9SKyle Evans  * Dummy definition to avoid the empty translation unit ISO C warning
468*f0865ec9SKyle Evans  */
469*f0865ec9SKyle Evans typedef int dummy;
470*f0865ec9SKyle Evans 
471*f0865ec9SKyle Evans #endif /* WITH_HASH_BELT_HASH */
472