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