Path: blob/main/crypto/libecc/src/examples/hash/md4.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 "md4.h"1112/* All the inner MD-4 operations */13static const u32 C1_MD4[13] = {140, 4, 8, 12, 0, 1, 2, 3, 3, 7, 11, 19, 015};16static const u32 C2_MD4[13] = {170, 1, 2, 3, 0, 4, 8, 12, 3, 5, 9, 13, 0x5a82799918};19static const u32 C3_MD4[13] = {200, 2, 1, 3, 0, 8, 4, 12, 3, 9, 11, 15, 0x6ed9eba121};2223#define F_MD4(x, y, z) (((x) & (y)) | ((~(x)) & (z)))24#define G_MD4(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))25#define H_MD4(x, y, z) ((x) ^ (y) ^ (z))2627/* SHA-2 core processing. Returns 0 on success, -1 on error. */28ATTRIBUTE_WARN_UNUSED_RET static inline int md4_process(md4_context *ctx,29const u8 data[MD4_BLOCK_SIZE])30{31u32 A, B, C, D;32u32 W[16];33u32 idx;34int ret;35unsigned int i;3637MUST_HAVE((data != NULL), ret, err);38MD4_HASH_CHECK_INITIALIZED(ctx, ret, err);3940/* Init our inner variables */41A = ctx->md4_state[0];42B = ctx->md4_state[1];43C = ctx->md4_state[2];44D = ctx->md4_state[3];4546/* Load data */47for (i = 0; i < 16; i++) {48GET_UINT32_LE(W[i], data, (4 * i));49}50/* Proceed with the compression */51for (i = 0; i < 4; i++) {52idx = (C1_MD4[i] + C1_MD4[4]);53A = ROTL_MD4((A + F_MD4(B, C, D) + W[idx] + C1_MD4[12]), C1_MD4[8]);54idx = (C1_MD4[i] + C1_MD4[5]);55D = ROTL_MD4((D + F_MD4(A, B, C) + W[idx] + C1_MD4[12]), C1_MD4[9]);56idx = (C1_MD4[i] + C1_MD4[6]);57C = ROTL_MD4((C + F_MD4(D, A, B) + W[idx] + C1_MD4[12]), C1_MD4[10]);58idx = (C1_MD4[i] + C1_MD4[7]);59B = ROTL_MD4((B + F_MD4(C, D, A) + W[idx] + C1_MD4[12]), C1_MD4[11]);60}61for (i = 0; i < 4; i++) {62idx = (C2_MD4[i] + C2_MD4[4]);63A = ROTL_MD4((A + G_MD4(B, C, D) + W[idx] + C2_MD4[12]), C2_MD4[8]);64idx = (C2_MD4[i] + C2_MD4[5]);65D = ROTL_MD4((D + G_MD4(A, B, C) + W[idx] + C2_MD4[12]), C2_MD4[9]);66idx = (C2_MD4[i] + C2_MD4[6]);67C = ROTL_MD4((C + G_MD4(D, A, B) + W[idx] + C2_MD4[12]), C2_MD4[10]);68idx = (C2_MD4[i] + C2_MD4[7]);69B = ROTL_MD4((B + G_MD4(C, D, A) + W[idx] + C2_MD4[12]), C2_MD4[11]);70}71for (i = 0; i < 4; i++) {72idx = (C3_MD4[i] + C3_MD4[4]);73A = ROTL_MD4((A + H_MD4(B, C, D) + W[idx] + C3_MD4[12]), C3_MD4[8]);74idx = (C3_MD4[i] + C3_MD4[5]);75D = ROTL_MD4((D + H_MD4(A, B, C) + W[idx] + C3_MD4[12]), C3_MD4[9]);76idx = (C3_MD4[i] + C3_MD4[6]);77C = ROTL_MD4((C + H_MD4(D, A, B) + W[idx] + C3_MD4[12]), C3_MD4[10]);78idx = (C3_MD4[i] + C3_MD4[7]);79B = ROTL_MD4((B + H_MD4(C, D, A) + W[idx] + C3_MD4[12]), C3_MD4[11]);80}8182/* Update state */83ctx->md4_state[0] += A;84ctx->md4_state[1] += B;85ctx->md4_state[2] += C;86ctx->md4_state[3] += D;8788ret = 0;8990err:91return ret;92}9394/* Init hash function. Returns 0 on success, -1 on error. */95ATTRIBUTE_WARN_UNUSED_RET int md4_init(md4_context *ctx)96{97int ret;9899MUST_HAVE((ctx != NULL), ret, err);100101/* Sanity check on size */102MUST_HAVE((MD4_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);103104ctx->md4_total = 0;105ctx->md4_state[0] = 0x67452301;106ctx->md4_state[1] = 0xEFCDAB89;107ctx->md4_state[2] = 0x98BADCFE;108ctx->md4_state[3] = 0x10325476;109110/* Tell that we are initialized */111ctx->magic = MD4_HASH_MAGIC;112113ret = 0;114115err:116return ret;117}118119ATTRIBUTE_WARN_UNUSED_RET int md4_update(md4_context *ctx, const u8 *input, u32 ilen)120{121const u8 *data_ptr = input;122u32 remain_ilen = ilen;123u16 fill;124u8 left;125int ret;126127MUST_HAVE((input != NULL) || (ilen == 0), ret, err);128MD4_HASH_CHECK_INITIALIZED(ctx, ret, err);129130/* Nothing to process, return */131if (ilen == 0) {132ret = 0;133goto err;134}135136/* Get what's left in our local buffer */137left = (ctx->md4_total & 0x3F);138fill = (u16)(MD4_BLOCK_SIZE - left);139140ctx->md4_total += ilen;141142if ((left > 0) && (remain_ilen >= fill)) {143/* Copy data at the end of the buffer */144ret = local_memcpy(ctx->md4_buffer + left, data_ptr, fill); EG(ret, err);145ret = md4_process(ctx, ctx->md4_buffer); EG(ret, err);146data_ptr += fill;147remain_ilen -= fill;148left = 0;149}150151while (remain_ilen >= MD4_BLOCK_SIZE) {152ret = md4_process(ctx, data_ptr); EG(ret, err);153data_ptr += MD4_BLOCK_SIZE;154remain_ilen -= MD4_BLOCK_SIZE;155}156157if (remain_ilen > 0) {158ret = local_memcpy(ctx->md4_buffer + left, data_ptr, remain_ilen); EG(ret, err);159}160161ret = 0;162163err:164return ret;165}166167/* Finalize. Returns 0 on success, -1 on error.*/168ATTRIBUTE_WARN_UNUSED_RET int md4_final(md4_context *ctx, u8 output[MD4_DIGEST_SIZE])169{170unsigned int block_present = 0;171u8 last_padded_block[2 * MD4_BLOCK_SIZE];172int ret;173174MUST_HAVE((output != NULL), ret, err);175MD4_HASH_CHECK_INITIALIZED(ctx, ret, err);176177/* Fill in our last block with zeroes */178ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);179180/* This is our final step, so we proceed with the padding */181block_present = ctx->md4_total % MD4_BLOCK_SIZE;182if (block_present != 0) {183/* Copy what's left in our temporary context buffer */184ret = local_memcpy(last_padded_block, ctx->md4_buffer,185block_present); EG(ret, err);186}187188/* Put the 0x80 byte, beginning of padding */189last_padded_block[block_present] = 0x80;190191/* Handle possible additional block */192if (block_present > (MD4_BLOCK_SIZE - 1 - sizeof(u64))) {193/* We need an additional block */194PUT_UINT64_LE(8 * ctx->md4_total, last_padded_block,195(2 * MD4_BLOCK_SIZE) - sizeof(u64));196ret = md4_process(ctx, last_padded_block); EG(ret, err);197ret = md4_process(ctx, last_padded_block + MD4_BLOCK_SIZE); EG(ret, err);198} else {199/* We do not need an additional block */200PUT_UINT64_LE(8 * ctx->md4_total, last_padded_block,201MD4_BLOCK_SIZE - sizeof(u64));202ret = md4_process(ctx, last_padded_block); EG(ret, err);203}204205/* Output the hash result */206PUT_UINT32_LE(ctx->md4_state[0], output, 0);207PUT_UINT32_LE(ctx->md4_state[1], output, 4);208PUT_UINT32_LE(ctx->md4_state[2], output, 8);209PUT_UINT32_LE(ctx->md4_state[3], output, 12);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 md4_scattered(const u8 **inputs, const u32 *ilens,228u8 output[MD4_DIGEST_SIZE])229{230md4_context ctx;231int ret, pos = 0;232233MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);234235ret = md4_init(&ctx); EG(ret, err);236237while (inputs[pos] != NULL) {238ret = md4_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);239pos += 1;240}241242ret = md4_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 md4(const u8 *input, u32 ilen, u8 output[MD4_DIGEST_SIZE])253{254md4_context ctx;255int ret;256257ret = md4_init(&ctx); EG(ret, err);258ret = md4_update(&ctx, input, ilen); EG(ret, err);259ret = md4_final(&ctx, output);260261err:262return ret;263}264265266