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 #ifdef WITH_HASH_SHA224
18*f0865ec9SKyle Evans
19*f0865ec9SKyle Evans #include <libecc/hash/sha224.h>
20*f0865ec9SKyle Evans
21*f0865ec9SKyle Evans /* SHA-2 core processing. Returns 0 on success, -1 on error. */
sha224_process(sha224_context * ctx,const u8 data[SHA224_BLOCK_SIZE])22*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET static int sha224_process(sha224_context *ctx,
23*f0865ec9SKyle Evans const u8 data[SHA224_BLOCK_SIZE])
24*f0865ec9SKyle Evans {
25*f0865ec9SKyle Evans u32 a, b, c, d, e, f, g, h;
26*f0865ec9SKyle Evans u32 W[64];
27*f0865ec9SKyle Evans unsigned int i;
28*f0865ec9SKyle Evans int ret;
29*f0865ec9SKyle Evans
30*f0865ec9SKyle Evans MUST_HAVE((data != NULL), ret, err);
31*f0865ec9SKyle Evans SHA224_HASH_CHECK_INITIALIZED(ctx, ret, err);
32*f0865ec9SKyle Evans
33*f0865ec9SKyle Evans /* Init our inner variables */
34*f0865ec9SKyle Evans a = ctx->sha224_state[0];
35*f0865ec9SKyle Evans b = ctx->sha224_state[1];
36*f0865ec9SKyle Evans c = ctx->sha224_state[2];
37*f0865ec9SKyle Evans d = ctx->sha224_state[3];
38*f0865ec9SKyle Evans e = ctx->sha224_state[4];
39*f0865ec9SKyle Evans f = ctx->sha224_state[5];
40*f0865ec9SKyle Evans g = ctx->sha224_state[6];
41*f0865ec9SKyle Evans h = ctx->sha224_state[7];
42*f0865ec9SKyle Evans
43*f0865ec9SKyle Evans for (i = 0; i < 16; i++) {
44*f0865ec9SKyle Evans GET_UINT32_BE(W[i], data, 4 * i);
45*f0865ec9SKyle Evans SHA2CORE_SHA256(a, b, c, d, e, f, g, h, W[i], K_SHA256[i]);
46*f0865ec9SKyle Evans }
47*f0865ec9SKyle Evans
48*f0865ec9SKyle Evans for (i = 16; i < 64; i++) {
49*f0865ec9SKyle Evans SHA2CORE_SHA256(a, b, c, d, e, f, g, h, UPDATEW_SHA256(W, i),
50*f0865ec9SKyle Evans K_SHA256[i]);
51*f0865ec9SKyle Evans }
52*f0865ec9SKyle Evans
53*f0865ec9SKyle Evans /* Update state */
54*f0865ec9SKyle Evans ctx->sha224_state[0] += a;
55*f0865ec9SKyle Evans ctx->sha224_state[1] += b;
56*f0865ec9SKyle Evans ctx->sha224_state[2] += c;
57*f0865ec9SKyle Evans ctx->sha224_state[3] += d;
58*f0865ec9SKyle Evans ctx->sha224_state[4] += e;
59*f0865ec9SKyle Evans ctx->sha224_state[5] += f;
60*f0865ec9SKyle Evans ctx->sha224_state[6] += g;
61*f0865ec9SKyle Evans ctx->sha224_state[7] += h;
62*f0865ec9SKyle Evans
63*f0865ec9SKyle Evans ret = 0;
64*f0865ec9SKyle Evans
65*f0865ec9SKyle Evans err:
66*f0865ec9SKyle Evans return ret;
67*f0865ec9SKyle Evans }
68*f0865ec9SKyle Evans
69*f0865ec9SKyle Evans /* Init hash function. Returns 0 on success, -1 on error. */
sha224_init(sha224_context * ctx)70*f0865ec9SKyle Evans int sha224_init(sha224_context *ctx)
71*f0865ec9SKyle Evans {
72*f0865ec9SKyle Evans int ret;
73*f0865ec9SKyle Evans
74*f0865ec9SKyle Evans MUST_HAVE((ctx != NULL), ret, err);
75*f0865ec9SKyle Evans
76*f0865ec9SKyle Evans ctx->sha224_total = 0;
77*f0865ec9SKyle Evans ctx->sha224_state[0] = 0xC1059ED8;
78*f0865ec9SKyle Evans ctx->sha224_state[1] = 0x367CD507;
79*f0865ec9SKyle Evans ctx->sha224_state[2] = 0x3070DD17;
80*f0865ec9SKyle Evans ctx->sha224_state[3] = 0xF70E5939;
81*f0865ec9SKyle Evans ctx->sha224_state[4] = 0xFFC00B31;
82*f0865ec9SKyle Evans ctx->sha224_state[5] = 0x68581511;
83*f0865ec9SKyle Evans ctx->sha224_state[6] = 0x64F98FA7;
84*f0865ec9SKyle Evans ctx->sha224_state[7] = 0xBEFA4FA4;
85*f0865ec9SKyle Evans
86*f0865ec9SKyle Evans /* Tell that we are initialized */
87*f0865ec9SKyle Evans ctx->magic = SHA224_HASH_MAGIC;
88*f0865ec9SKyle Evans
89*f0865ec9SKyle Evans ret = 0;
90*f0865ec9SKyle Evans
91*f0865ec9SKyle Evans err:
92*f0865ec9SKyle Evans return ret;
93*f0865ec9SKyle Evans }
94*f0865ec9SKyle Evans
95*f0865ec9SKyle Evans /* Update hash function. Returns 0 on success, -1 on error. */
sha224_update(sha224_context * ctx,const u8 * input,u32 ilen)96*f0865ec9SKyle Evans int sha224_update(sha224_context *ctx, const u8 *input, u32 ilen)
97*f0865ec9SKyle Evans {
98*f0865ec9SKyle Evans const u8 *data_ptr = input;
99*f0865ec9SKyle Evans u32 remain_ilen = ilen;
100*f0865ec9SKyle Evans u16 fill;
101*f0865ec9SKyle Evans u8 left;
102*f0865ec9SKyle Evans int ret;
103*f0865ec9SKyle Evans
104*f0865ec9SKyle Evans MUST_HAVE((input != NULL) || (ilen == 0), ret, err);
105*f0865ec9SKyle Evans SHA224_HASH_CHECK_INITIALIZED(ctx, ret, err);
106*f0865ec9SKyle Evans
107*f0865ec9SKyle Evans /* Nothing to process, return */
108*f0865ec9SKyle Evans if (ilen == 0) {
109*f0865ec9SKyle Evans ret = 0;
110*f0865ec9SKyle Evans goto err;
111*f0865ec9SKyle Evans }
112*f0865ec9SKyle Evans
113*f0865ec9SKyle Evans /* Get what's left in our local buffer */
114*f0865ec9SKyle Evans left = (ctx->sha224_total & 0x3F);
115*f0865ec9SKyle Evans fill = (u16)(SHA224_BLOCK_SIZE - left);
116*f0865ec9SKyle Evans
117*f0865ec9SKyle Evans ctx->sha224_total += ilen;
118*f0865ec9SKyle Evans
119*f0865ec9SKyle Evans if ((left > 0) && (remain_ilen >= fill)) {
120*f0865ec9SKyle Evans /* Copy data at the end of the buffer */
121*f0865ec9SKyle Evans ret = local_memcpy(ctx->sha224_buffer + left, data_ptr, fill); EG(ret, err);
122*f0865ec9SKyle Evans ret = sha224_process(ctx, ctx->sha224_buffer); EG(ret, err);
123*f0865ec9SKyle Evans data_ptr += fill;
124*f0865ec9SKyle Evans remain_ilen -= fill;
125*f0865ec9SKyle Evans left = 0;
126*f0865ec9SKyle Evans }
127*f0865ec9SKyle Evans
128*f0865ec9SKyle Evans while (remain_ilen >= SHA224_BLOCK_SIZE) {
129*f0865ec9SKyle Evans ret = sha224_process(ctx, data_ptr); EG(ret, err);
130*f0865ec9SKyle Evans data_ptr += SHA224_BLOCK_SIZE;
131*f0865ec9SKyle Evans remain_ilen -= SHA224_BLOCK_SIZE;
132*f0865ec9SKyle Evans }
133*f0865ec9SKyle Evans
134*f0865ec9SKyle Evans if (remain_ilen > 0) {
135*f0865ec9SKyle Evans ret = local_memcpy(ctx->sha224_buffer + left, data_ptr, remain_ilen); EG(ret, err);
136*f0865ec9SKyle Evans }
137*f0865ec9SKyle Evans
138*f0865ec9SKyle Evans ret = 0;
139*f0865ec9SKyle Evans
140*f0865ec9SKyle Evans err:
141*f0865ec9SKyle Evans return ret;
142*f0865ec9SKyle Evans }
143*f0865ec9SKyle Evans
144*f0865ec9SKyle Evans /* Finalize. Returns 0 on success, -1 on error.*/
sha224_final(sha224_context * ctx,u8 output[SHA224_DIGEST_SIZE])145*f0865ec9SKyle Evans int sha224_final(sha224_context *ctx, u8 output[SHA224_DIGEST_SIZE])
146*f0865ec9SKyle Evans {
147*f0865ec9SKyle Evans unsigned int block_present = 0;
148*f0865ec9SKyle Evans u8 last_padded_block[2 * SHA224_BLOCK_SIZE];
149*f0865ec9SKyle Evans int ret;
150*f0865ec9SKyle Evans
151*f0865ec9SKyle Evans MUST_HAVE((output != NULL), ret, err);
152*f0865ec9SKyle Evans SHA224_HASH_CHECK_INITIALIZED(ctx, ret, err);
153*f0865ec9SKyle Evans
154*f0865ec9SKyle Evans /* Fill in our last block with zeroes */
155*f0865ec9SKyle Evans ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);
156*f0865ec9SKyle Evans
157*f0865ec9SKyle Evans /* This is our final step, so we proceed with the padding */
158*f0865ec9SKyle Evans block_present = ctx->sha224_total % SHA224_BLOCK_SIZE;
159*f0865ec9SKyle Evans if (block_present != 0) {
160*f0865ec9SKyle Evans /* Copy what's left in our temporary context buffer */
161*f0865ec9SKyle Evans ret = local_memcpy(last_padded_block, ctx->sha224_buffer,
162*f0865ec9SKyle Evans block_present); EG(ret, err);
163*f0865ec9SKyle Evans }
164*f0865ec9SKyle Evans
165*f0865ec9SKyle Evans /* Put the 0x80 byte, beginning of padding */
166*f0865ec9SKyle Evans last_padded_block[block_present] = 0x80;
167*f0865ec9SKyle Evans
168*f0865ec9SKyle Evans /* Handle possible additional block */
169*f0865ec9SKyle Evans if (block_present > (SHA224_BLOCK_SIZE - 1 - sizeof(u64))) {
170*f0865ec9SKyle Evans /* We need an additional block */
171*f0865ec9SKyle Evans PUT_UINT64_BE(8 * ctx->sha224_total, last_padded_block,
172*f0865ec9SKyle Evans (2 * SHA224_BLOCK_SIZE) - sizeof(u64));
173*f0865ec9SKyle Evans ret = sha224_process(ctx, last_padded_block); EG(ret, err);
174*f0865ec9SKyle Evans ret = sha224_process(ctx, last_padded_block + SHA224_BLOCK_SIZE); EG(ret, err);
175*f0865ec9SKyle Evans } else {
176*f0865ec9SKyle Evans /* We do not need an additional block */
177*f0865ec9SKyle Evans PUT_UINT64_BE(8 * ctx->sha224_total, last_padded_block,
178*f0865ec9SKyle Evans SHA224_BLOCK_SIZE - sizeof(u64));
179*f0865ec9SKyle Evans ret = sha224_process(ctx, last_padded_block); EG(ret, err);
180*f0865ec9SKyle Evans }
181*f0865ec9SKyle Evans
182*f0865ec9SKyle Evans /* Output the hash result */
183*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[0], output, 0);
184*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[1], output, 4);
185*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[2], output, 8);
186*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[3], output, 12);
187*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[4], output, 16);
188*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[5], output, 20);
189*f0865ec9SKyle Evans PUT_UINT32_BE(ctx->sha224_state[6], output, 24);
190*f0865ec9SKyle Evans
191*f0865ec9SKyle Evans /* Tell that we are uninitialized */
192*f0865ec9SKyle Evans ctx->magic = WORD(0);
193*f0865ec9SKyle Evans
194*f0865ec9SKyle Evans ret = 0;
195*f0865ec9SKyle Evans
196*f0865ec9SKyle Evans err:
197*f0865ec9SKyle Evans return ret;
198*f0865ec9SKyle Evans }
199*f0865ec9SKyle Evans
200*f0865ec9SKyle Evans /*
201*f0865ec9SKyle Evans * Scattered version performing init/update/finalize on a vector of buffers
202*f0865ec9SKyle Evans * 'inputs' with the length of each buffer passed via 'ilens'. The function
203*f0865ec9SKyle Evans * loops on pointers in 'inputs' until it finds a NULL pointer. The function
204*f0865ec9SKyle Evans * returns 0 on success, -1 on error.
205*f0865ec9SKyle Evans */
sha224_scattered(const u8 ** inputs,const u32 * ilens,u8 output[SHA224_DIGEST_SIZE])206*f0865ec9SKyle Evans int sha224_scattered(const u8 **inputs, const u32 *ilens,
207*f0865ec9SKyle Evans u8 output[SHA224_DIGEST_SIZE])
208*f0865ec9SKyle Evans {
209*f0865ec9SKyle Evans sha224_context ctx;
210*f0865ec9SKyle Evans int ret, pos = 0;
211*f0865ec9SKyle Evans
212*f0865ec9SKyle Evans MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
213*f0865ec9SKyle Evans
214*f0865ec9SKyle Evans ret = sha224_init(&ctx); EG(ret, err);
215*f0865ec9SKyle Evans
216*f0865ec9SKyle Evans while (inputs[pos] != NULL) {
217*f0865ec9SKyle Evans ret = sha224_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
218*f0865ec9SKyle Evans pos += 1;
219*f0865ec9SKyle Evans }
220*f0865ec9SKyle Evans
221*f0865ec9SKyle Evans ret = sha224_final(&ctx, output);
222*f0865ec9SKyle Evans
223*f0865ec9SKyle Evans err:
224*f0865ec9SKyle Evans return ret;
225*f0865ec9SKyle Evans }
226*f0865ec9SKyle Evans
227*f0865ec9SKyle Evans /*
228*f0865ec9SKyle Evans * Single call version performing init/update/final on given input.
229*f0865ec9SKyle Evans * Returns 0 on success, -1 on error.
230*f0865ec9SKyle Evans */
sha224(const u8 * input,u32 ilen,u8 output[SHA224_DIGEST_SIZE])231*f0865ec9SKyle Evans int sha224(const u8 *input, u32 ilen, u8 output[SHA224_DIGEST_SIZE])
232*f0865ec9SKyle Evans {
233*f0865ec9SKyle Evans sha224_context ctx;
234*f0865ec9SKyle Evans int ret;
235*f0865ec9SKyle Evans
236*f0865ec9SKyle Evans ret = sha224_init(&ctx); EG(ret, err);
237*f0865ec9SKyle Evans ret = sha224_update(&ctx, input, ilen); EG(ret, err);
238*f0865ec9SKyle Evans ret = sha224_final(&ctx, output);
239*f0865ec9SKyle Evans
240*f0865ec9SKyle Evans err:
241*f0865ec9SKyle Evans return ret;
242*f0865ec9SKyle Evans }
243*f0865ec9SKyle Evans
244*f0865ec9SKyle Evans #else /* WITH_HASH_SHA224 */
245*f0865ec9SKyle Evans
246*f0865ec9SKyle Evans /*
247*f0865ec9SKyle Evans * Dummy definition to avoid the empty translation unit ISO C warning
248*f0865ec9SKyle Evans */
249*f0865ec9SKyle Evans typedef int dummy;
250*f0865ec9SKyle Evans #endif /* WITH_HASH_SHA224 */
251