Path: blob/main/crypto/libecc/src/examples/hash/sha0.c
34889 views
/*1* Copyright (C) 2021 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6*7* This software is licensed under a dual BSD and GPL v2 license.8* See LICENSE file at the root folder of the project.9*/10#include "sha0.h"1112#define ROTL_SHA0(x, n) ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))1314/* All the inner SHA-0 operations */15#define K1_SHA0 0x5a82799916#define K2_SHA0 0x6ed9eba117#define K3_SHA0 0x8f1bbcdc18#define K4_SHA0 0xca62c1d61920#define F1_SHA0(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))21#define F2_SHA0(x, y, z) ((x) ^ (y) ^ (z))22#define F3_SHA0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))23#define F4_SHA0(x, y, z) ((x) ^ (y) ^ (z))2425#define SHA0_EXPAND(W, i) (W[i & 15] = (W[i & 15] ^ W[(i - 14) & 15] ^ W[(i - 8) & 15] ^ W[(i - 3) & 15]))2627#define SHA0_SUBROUND(a, b, c, d, e, F, K, data) do { \28u32 A_, B_, C_, D_, E_; \29A_ = (e + ROTL_SHA0(a, 5) + F(b, c, d) + K + data); \30B_ = a; \31C_ = ROTL_SHA0(b, 30); \32D_ = c; \33E_ = d; \34/**/ \35a = A_; b = B_; c = C_; d = D_; e = E_; \36} while(0)3738/* SHA-0 core processing. Returns 0 on success, -1 on error. */39ATTRIBUTE_WARN_UNUSED_RET static inline int sha0_process(sha0_context *ctx,40const u8 data[SHA0_BLOCK_SIZE])41{42u32 A, B, C, D, E;43u32 W[16];44int ret;45unsigned int i;4647MUST_HAVE((data != NULL), ret, err);48SHA0_HASH_CHECK_INITIALIZED(ctx, ret, err);4950/* Init our inner variables */51A = ctx->sha0_state[0];52B = ctx->sha0_state[1];53C = ctx->sha0_state[2];54D = ctx->sha0_state[3];55E = ctx->sha0_state[4];5657/* Load data */58for (i = 0; i < 16; i++) {59GET_UINT32_BE(W[i], data, (4 * i));60}61for (i = 0; i < 80; i++) {62if(i <= 15){63SHA0_SUBROUND(A, B, C, D, E, F1_SHA0, K1_SHA0, W[i]);64}65else if((i >= 16) && (i <= 19)){66SHA0_SUBROUND(A, B, C, D, E, F1_SHA0, K1_SHA0, SHA0_EXPAND(W, i));67}68else if((i >= 20) && (i <= 39)){69SHA0_SUBROUND(A, B, C, D, E, F2_SHA0, K2_SHA0, SHA0_EXPAND(W, i));70}71else if((i >= 40) && (i <= 59)){72SHA0_SUBROUND(A, B, C, D, E, F3_SHA0, K3_SHA0, SHA0_EXPAND(W, i));73}74else{75SHA0_SUBROUND(A, B, C, D, E, F4_SHA0, K4_SHA0, SHA0_EXPAND(W, i));76}77}7879/* Update state */80ctx->sha0_state[0] += A;81ctx->sha0_state[1] += B;82ctx->sha0_state[2] += C;83ctx->sha0_state[3] += D;84ctx->sha0_state[4] += E;8586ret = 0;8788err:89return ret;90}9192/* Init hash function. Returns 0 on success, -1 on error. */93ATTRIBUTE_WARN_UNUSED_RET int sha0_init(sha0_context *ctx)94{95int ret;9697MUST_HAVE((ctx != NULL), ret, err);9899/* Sanity check on size */100MUST_HAVE((SHA0_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);101102ctx->sha0_total = 0;103ctx->sha0_state[0] = 0x67452301;104ctx->sha0_state[1] = 0xefcdab89;105ctx->sha0_state[2] = 0x98badcfe;106ctx->sha0_state[3] = 0x10325476;107ctx->sha0_state[4] = 0xc3d2e1f0;108109/* Tell that we are initialized */110ctx->magic = SHA0_HASH_MAGIC;111112ret = 0;113114err:115return ret;116}117118ATTRIBUTE_WARN_UNUSED_RET int sha0_update(sha0_context *ctx, const u8 *input, u32 ilen)119{120const u8 *data_ptr = input;121u32 remain_ilen = ilen;122u16 fill;123u8 left;124int ret;125126MUST_HAVE((input != NULL) || (ilen == 0), ret, err);127SHA0_HASH_CHECK_INITIALIZED(ctx, ret, err);128129/* Nothing to process, return */130if (ilen == 0) {131ret = 0;132goto err;133}134135/* Get what's left in our local buffer */136left = (ctx->sha0_total & 0x3F);137fill = (u16)(SHA0_BLOCK_SIZE - left);138139ctx->sha0_total += ilen;140141if ((left > 0) && (remain_ilen >= fill)) {142/* Copy data at the end of the buffer */143ret = local_memcpy(ctx->sha0_buffer + left, data_ptr, fill); EG(ret, err);144ret = sha0_process(ctx, ctx->sha0_buffer); EG(ret, err);145data_ptr += fill;146remain_ilen -= fill;147left = 0;148}149150while (remain_ilen >= SHA0_BLOCK_SIZE) {151ret = sha0_process(ctx, data_ptr); EG(ret, err);152data_ptr += SHA0_BLOCK_SIZE;153remain_ilen -= SHA0_BLOCK_SIZE;154}155156if (remain_ilen > 0) {157ret = local_memcpy(ctx->sha0_buffer + left, data_ptr, remain_ilen); EG(ret, err);158}159160ret = 0;161162err:163return ret;164}165166/* Finalize. Returns 0 on success, -1 on error.*/167ATTRIBUTE_WARN_UNUSED_RET int sha0_final(sha0_context *ctx, u8 output[SHA0_DIGEST_SIZE])168{169unsigned int block_present = 0;170u8 last_padded_block[2 * SHA0_BLOCK_SIZE];171int ret;172173MUST_HAVE((output != NULL), ret, err);174SHA0_HASH_CHECK_INITIALIZED(ctx, ret, err);175176/* Fill in our last block with zeroes */177ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);178179/* This is our final step, so we proceed with the padding */180block_present = ctx->sha0_total % SHA0_BLOCK_SIZE;181if (block_present != 0) {182/* Copy what's left in our temporary context buffer */183ret = local_memcpy(last_padded_block, ctx->sha0_buffer,184block_present); EG(ret, err);185}186187/* Put the 0x80 byte, beginning of padding */188last_padded_block[block_present] = 0x80;189190/* Handle possible additional block */191if (block_present > (SHA0_BLOCK_SIZE - 1 - sizeof(u64))) {192/* We need an additional block */193PUT_UINT64_BE(8 * ctx->sha0_total, last_padded_block,194(2 * SHA0_BLOCK_SIZE) - sizeof(u64));195ret = sha0_process(ctx, last_padded_block); EG(ret, err);196ret = sha0_process(ctx, last_padded_block + SHA0_BLOCK_SIZE); EG(ret, err);197} else {198/* We do not need an additional block */199PUT_UINT64_BE(8 * ctx->sha0_total, last_padded_block,200SHA0_BLOCK_SIZE - sizeof(u64));201ret = sha0_process(ctx, last_padded_block); EG(ret, err);202}203204/* Output the hash result */205PUT_UINT32_BE(ctx->sha0_state[0], output, 0);206PUT_UINT32_BE(ctx->sha0_state[1], output, 4);207PUT_UINT32_BE(ctx->sha0_state[2], output, 8);208PUT_UINT32_BE(ctx->sha0_state[3], output, 12);209PUT_UINT32_BE(ctx->sha0_state[4], output, 16);210211/* Tell that we are uninitialized */212ctx->magic = WORD(0);213214ret = 0;215216err:217return ret;218}219220221/*222* Scattered version performing init/update/finalize on a vector of buffers223* 'inputs' with the length of each buffer passed via 'ilens'. The function224* loops on pointers in 'inputs' until it finds a NULL pointer. The function225* returns 0 on success, -1 on error.226*/227ATTRIBUTE_WARN_UNUSED_RET int sha0_scattered(const u8 **inputs, const u32 *ilens,228u8 output[SHA0_DIGEST_SIZE])229{230sha0_context ctx;231int ret, pos = 0;232233MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);234235ret = sha0_init(&ctx); EG(ret, err);236237while (inputs[pos] != NULL) {238ret = sha0_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);239pos += 1;240}241242ret = sha0_final(&ctx, output);243244err:245return ret;246}247248/*249* Single call version performing init/update/final on given input.250* Returns 0 on success, -1 on error.251*/252ATTRIBUTE_WARN_UNUSED_RET int sha0(const u8 *input, u32 ilen, u8 output[SHA0_DIGEST_SIZE])253{254sha0_context ctx;255int ret;256257ret = sha0_init(&ctx); EG(ret, err);258ret = sha0_update(&ctx, input, ilen); EG(ret, err);259ret = sha0_final(&ctx, output);260261err:262return ret;263}264265266