xref: /freebsd/crypto/libecc/src/hash/sha512_core.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans  *  Copyright (C) 2017 - 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  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8*f0865ec9SKyle Evans  *
9*f0865ec9SKyle Evans  *  Contributors:
10*f0865ec9SKyle Evans  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11*f0865ec9SKyle Evans  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12*f0865ec9SKyle Evans  *
13*f0865ec9SKyle Evans  *  This software is licensed under a dual BSD and GPL v2 license.
14*f0865ec9SKyle Evans  *  See LICENSE file at the root folder of the project.
15*f0865ec9SKyle Evans  */
16*f0865ec9SKyle Evans #include <libecc/lib_ecc_config.h>
17*f0865ec9SKyle Evans #if defined(WITH_HASH_SHA512) || defined(WITH_HASH_SHA512_224) || defined(WITH_HASH_SHA512_256)
18*f0865ec9SKyle Evans #include <libecc/hash/sha512_core.h>
19*f0865ec9SKyle Evans 
20*f0865ec9SKyle Evans /* SHA-2 core processing. Returns 0 on success, -1 on error. */
sha512_core_process(sha512_core_context * ctx,const u8 data[SHA512_CORE_BLOCK_SIZE])21*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static int sha512_core_process(sha512_core_context *ctx,
22*f0865ec9SKyle Evans 			   const u8 data[SHA512_CORE_BLOCK_SIZE])
23*f0865ec9SKyle Evans {
24*f0865ec9SKyle Evans 	u64 a, b, c, d, e, f, g, h;
25*f0865ec9SKyle Evans 	u64 W[80];
26*f0865ec9SKyle Evans 	unsigned int i;
27*f0865ec9SKyle Evans 	int ret;
28*f0865ec9SKyle Evans 
29*f0865ec9SKyle Evans 	MUST_HAVE(((ctx != NULL) && (data != NULL)), ret, err);
30*f0865ec9SKyle Evans 
31*f0865ec9SKyle Evans 	/* Init our inner variables */
32*f0865ec9SKyle Evans 	a = ctx->sha512_state[0];
33*f0865ec9SKyle Evans 	b = ctx->sha512_state[1];
34*f0865ec9SKyle Evans 	c = ctx->sha512_state[2];
35*f0865ec9SKyle Evans 	d = ctx->sha512_state[3];
36*f0865ec9SKyle Evans 	e = ctx->sha512_state[4];
37*f0865ec9SKyle Evans 	f = ctx->sha512_state[5];
38*f0865ec9SKyle Evans 	g = ctx->sha512_state[6];
39*f0865ec9SKyle Evans 	h = ctx->sha512_state[7];
40*f0865ec9SKyle Evans 
41*f0865ec9SKyle Evans 	for (i = 0; i < 16; i++) {
42*f0865ec9SKyle Evans 		GET_UINT64_BE(W[i], data, 8 * i);
43*f0865ec9SKyle Evans 		SHA2CORE_SHA512(a, b, c, d, e, f, g, h, W[i], K_SHA512[i]);
44*f0865ec9SKyle Evans 	}
45*f0865ec9SKyle Evans 
46*f0865ec9SKyle Evans 	for (i = 16; i < 80; i++) {
47*f0865ec9SKyle Evans 		SHA2CORE_SHA512(a, b, c, d, e, f, g, h, UPDATEW_SHA512(W, i),
48*f0865ec9SKyle Evans 				K_SHA512[i]);
49*f0865ec9SKyle Evans 	}
50*f0865ec9SKyle Evans 
51*f0865ec9SKyle Evans 	/* Update state */
52*f0865ec9SKyle Evans 	ctx->sha512_state[0] += a;
53*f0865ec9SKyle Evans 	ctx->sha512_state[1] += b;
54*f0865ec9SKyle Evans 	ctx->sha512_state[2] += c;
55*f0865ec9SKyle Evans 	ctx->sha512_state[3] += d;
56*f0865ec9SKyle Evans 	ctx->sha512_state[4] += e;
57*f0865ec9SKyle Evans 	ctx->sha512_state[5] += f;
58*f0865ec9SKyle Evans 	ctx->sha512_state[6] += g;
59*f0865ec9SKyle Evans 	ctx->sha512_state[7] += h;
60*f0865ec9SKyle Evans 
61*f0865ec9SKyle Evans 	ret = 0;
62*f0865ec9SKyle Evans 
63*f0865ec9SKyle Evans err:
64*f0865ec9SKyle Evans 	return ret;
65*f0865ec9SKyle Evans }
66*f0865ec9SKyle Evans 
67*f0865ec9SKyle Evans /* Core update hash function. Returns 0 on success, -1 on error. */
sha512_core_update(sha512_core_context * ctx,const u8 * input,u32 ilen)68*f0865ec9SKyle Evans int sha512_core_update(sha512_core_context *ctx, const u8 *input, u32 ilen)
69*f0865ec9SKyle Evans {
70*f0865ec9SKyle Evans 	const u8 *data_ptr = input;
71*f0865ec9SKyle Evans 	u32 remain_ilen = ilen;
72*f0865ec9SKyle Evans 	u16 fill;
73*f0865ec9SKyle Evans 	u8 left;
74*f0865ec9SKyle Evans 	int ret;
75*f0865ec9SKyle Evans 
76*f0865ec9SKyle Evans 	MUST_HAVE(((ctx != NULL) && ((input != NULL) || (ilen == 0))), ret, err);
77*f0865ec9SKyle Evans 
78*f0865ec9SKyle Evans 	/* Nothing to process, return */
79*f0865ec9SKyle Evans 	if (ilen == 0) {
80*f0865ec9SKyle Evans 		ret = 0;
81*f0865ec9SKyle Evans 		goto err;
82*f0865ec9SKyle Evans 	}
83*f0865ec9SKyle Evans 
84*f0865ec9SKyle Evans 	/* Get what's left in our local buffer */
85*f0865ec9SKyle Evans 	left = ctx->sha512_total[0] & 0x7F;
86*f0865ec9SKyle Evans 	fill = (u16)(SHA512_CORE_BLOCK_SIZE - left);
87*f0865ec9SKyle Evans 
88*f0865ec9SKyle Evans 	ADD_UINT128_UINT64(ctx->sha512_total[0], ctx->sha512_total[1], ilen);
89*f0865ec9SKyle Evans 
90*f0865ec9SKyle Evans 	if ((left > 0) && (remain_ilen >= fill)) {
91*f0865ec9SKyle Evans 		/* Copy data at the end of the buffer */
92*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->sha512_buffer + left, data_ptr, fill); EG(ret, err);
93*f0865ec9SKyle Evans 		ret = sha512_core_process(ctx, ctx->sha512_buffer); EG(ret, err);
94*f0865ec9SKyle Evans 		data_ptr += fill;
95*f0865ec9SKyle Evans 		remain_ilen -= fill;
96*f0865ec9SKyle Evans 		left = 0;
97*f0865ec9SKyle Evans 	}
98*f0865ec9SKyle Evans 
99*f0865ec9SKyle Evans 	while (remain_ilen >= SHA512_CORE_BLOCK_SIZE) {
100*f0865ec9SKyle Evans 		ret = sha512_core_process(ctx, data_ptr); EG(ret, err);
101*f0865ec9SKyle Evans 		data_ptr += SHA512_CORE_BLOCK_SIZE;
102*f0865ec9SKyle Evans 		remain_ilen -= SHA512_CORE_BLOCK_SIZE;
103*f0865ec9SKyle Evans 	}
104*f0865ec9SKyle Evans 
105*f0865ec9SKyle Evans 	if (remain_ilen > 0) {
106*f0865ec9SKyle Evans 		ret = local_memcpy(ctx->sha512_buffer + left, data_ptr, remain_ilen); EG(ret, err);
107*f0865ec9SKyle Evans 	}
108*f0865ec9SKyle Evans 
109*f0865ec9SKyle Evans 	ret = 0;
110*f0865ec9SKyle Evans 
111*f0865ec9SKyle Evans err:
112*f0865ec9SKyle Evans 	return ret;
113*f0865ec9SKyle Evans }
114*f0865ec9SKyle Evans 
115*f0865ec9SKyle Evans /* Core finalize. Returns 0 on success, -1 on error. */
sha512_core_final(sha512_core_context * ctx,u8 * output,u32 output_size)116*f0865ec9SKyle Evans int sha512_core_final(sha512_core_context *ctx, u8 *output, u32 output_size)
117*f0865ec9SKyle Evans {
118*f0865ec9SKyle Evans 	unsigned int block_present = 0;
119*f0865ec9SKyle Evans 	u8 last_padded_block[2 * SHA512_CORE_BLOCK_SIZE];
120*f0865ec9SKyle Evans 	int ret;
121*f0865ec9SKyle Evans 
122*f0865ec9SKyle Evans 	MUST_HAVE(((ctx != NULL) && (output != NULL)), ret, err);
123*f0865ec9SKyle Evans 
124*f0865ec9SKyle Evans 	/* Fill in our last block with zeroes */
125*f0865ec9SKyle Evans 	ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);
126*f0865ec9SKyle Evans 
127*f0865ec9SKyle Evans 	/* This is our final step, so we proceed with the padding */
128*f0865ec9SKyle Evans 	block_present = ctx->sha512_total[0] % SHA512_CORE_BLOCK_SIZE;
129*f0865ec9SKyle Evans 	if (block_present != 0) {
130*f0865ec9SKyle Evans 		/* Copy what's left in our temporary context buffer */
131*f0865ec9SKyle Evans 		ret = local_memcpy(last_padded_block, ctx->sha512_buffer,
132*f0865ec9SKyle Evans 			     block_present); EG(ret, err);
133*f0865ec9SKyle Evans 	}
134*f0865ec9SKyle Evans 
135*f0865ec9SKyle Evans 	/* Put the 0x80 byte, beginning of padding  */
136*f0865ec9SKyle Evans 	last_padded_block[block_present] = 0x80;
137*f0865ec9SKyle Evans 
138*f0865ec9SKyle Evans 	/* Handle possible additional block */
139*f0865ec9SKyle Evans 	if (block_present > (SHA512_CORE_BLOCK_SIZE - 1 - (2 * sizeof(u64)))) {
140*f0865ec9SKyle Evans 		/* We need an additional block */
141*f0865ec9SKyle Evans 		PUT_MUL8_UINT128_BE(ctx->sha512_total[0], ctx->sha512_total[1],
142*f0865ec9SKyle Evans 				    last_padded_block,
143*f0865ec9SKyle Evans 				    2 * (SHA512_CORE_BLOCK_SIZE - sizeof(u64)));
144*f0865ec9SKyle Evans 		ret = sha512_core_process(ctx, last_padded_block); EG(ret, err);
145*f0865ec9SKyle Evans 		ret = sha512_core_process(ctx, last_padded_block + SHA512_CORE_BLOCK_SIZE); EG(ret, err);
146*f0865ec9SKyle Evans 	} else {
147*f0865ec9SKyle Evans 		/* We do not need an additional block */
148*f0865ec9SKyle Evans 		PUT_MUL8_UINT128_BE(ctx->sha512_total[0], ctx->sha512_total[1],
149*f0865ec9SKyle Evans 				    last_padded_block,
150*f0865ec9SKyle Evans 				    SHA512_CORE_BLOCK_SIZE - (2 * sizeof(u64)));
151*f0865ec9SKyle Evans 		ret = sha512_core_process(ctx, last_padded_block); EG(ret, err);
152*f0865ec9SKyle Evans 	}
153*f0865ec9SKyle Evans 
154*f0865ec9SKyle Evans 	/* Output the hash result truncated to the output size */
155*f0865ec9SKyle Evans 	if(output_size >= SHA512_CORE_DIGEST_SIZE){
156*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[0], output, 0);
157*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[1], output, 8);
158*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[2], output, 16);
159*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[3], output, 24);
160*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[4], output, 32);
161*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[5], output, 40);
162*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[6], output, 48);
163*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[7], output, 56);
164*f0865ec9SKyle Evans 	} else {
165*f0865ec9SKyle Evans 		u8 tmp_output[SHA512_CORE_DIGEST_SIZE] = { 0 };
166*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[0], tmp_output, 0);
167*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[1], tmp_output, 8);
168*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[2], tmp_output, 16);
169*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[3], tmp_output, 24);
170*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[4], tmp_output, 32);
171*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[5], tmp_output, 40);
172*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[6], tmp_output, 48);
173*f0865ec9SKyle Evans 		PUT_UINT64_BE(ctx->sha512_state[7], tmp_output, 56);
174*f0865ec9SKyle Evans 		ret = local_memcpy(output, tmp_output, output_size); EG(ret, err);
175*f0865ec9SKyle Evans 	}
176*f0865ec9SKyle Evans 
177*f0865ec9SKyle Evans 	ret = 0;
178*f0865ec9SKyle Evans 
179*f0865ec9SKyle Evans err:
180*f0865ec9SKyle Evans 	return ret;
181*f0865ec9SKyle Evans }
182*f0865ec9SKyle Evans 
183*f0865ec9SKyle Evans #else /* defined(WITH_HASH_SHA512) || defined(WITH_HASH_SHA512_224) || defined(WITH_HASH_SHA512_256) */
184*f0865ec9SKyle Evans 
185*f0865ec9SKyle Evans /*
186*f0865ec9SKyle Evans  * Dummy definition to avoid the empty translation unit ISO C warning
187*f0865ec9SKyle Evans  */
188*f0865ec9SKyle Evans typedef int dummy;
189*f0865ec9SKyle Evans #endif /* WITH_HASH_SHA512 */
190