Path: blob/main/crypto/libecc/src/examples/hash/md5.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 "md5.h"1112/* All the inner MD-5 operations */13static const u32 K_MD5[64] = {140xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,150x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,160xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,170x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,180xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,190x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,200xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,210x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d39122};2324static const u8 R_MD5[64] = {257, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,265, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,274, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,286, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 2129};3031#define F_MD5(x, y, z) (((x) & (y)) | ((~(x)) & (z)))32#define G_MD5(x, y, z) (((x) & (z)) | ((y) & (~(z))))33#define H_MD5(x, y, z) ((x) ^ (y) ^ (z))34#define I_MD5(x, y, z) ((y) ^ ((x) | ((~z))))3536/* SHA-2 core processing. Returns 0 on success, -1 on error. */37ATTRIBUTE_WARN_UNUSED_RET static inline int md5_process(md5_context *ctx,38const u8 data[MD5_BLOCK_SIZE])39{40u32 A, B, C, D, tmp;41u32 W[16];42int ret;43unsigned int i;4445MUST_HAVE((data != NULL), ret, err);46MD5_HASH_CHECK_INITIALIZED(ctx, ret, err);4748/* Init our inner variables */49A = ctx->md5_state[0];50B = ctx->md5_state[1];51C = ctx->md5_state[2];52D = ctx->md5_state[3];5354/* Load data */55for (i = 0; i < 16; i++) {56GET_UINT32_LE(W[i], data, (4 * i));57}58for (i = 0; i < 64; i++) {59u32 f, g;60if(i <= 15){61f = F_MD5(B, C, D);62g = i;63}64else if((i >= 16) && (i <= 31)){65f = G_MD5(B, C, D);66g = (((5 * i) + 1) % 16);67}68else if((i >= 32) && (i <= 47)){69f = H_MD5(B, C, D);70g = (((3 * i) + 5) % 16);71}72else{73f = I_MD5(B, C, D);74g = ((7 * i) % 16);75}76tmp = D;77D = C;78C = B;79B += ROTL_MD5((A + f + K_MD5[i] + W[g]), R_MD5[i]);80A = tmp;81}8283/* Update state */84ctx->md5_state[0] += A;85ctx->md5_state[1] += B;86ctx->md5_state[2] += C;87ctx->md5_state[3] += D;8889ret = 0;9091err:92return ret;93}9495/* Init hash function. Returns 0 on success, -1 on error. */96ATTRIBUTE_WARN_UNUSED_RET int md5_init(md5_context *ctx)97{98int ret;99100MUST_HAVE((ctx != NULL), ret, err);101102/* Sanity check on size */103MUST_HAVE((MD5_DIGEST_SIZE <= MAX_DIGEST_SIZE), ret, err);104105ctx->md5_total = 0;106ctx->md5_state[0] = 0x67452301;107ctx->md5_state[1] = 0xEFCDAB89;108ctx->md5_state[2] = 0x98BADCFE;109ctx->md5_state[3] = 0x10325476;110111/* Tell that we are initialized */112ctx->magic = MD5_HASH_MAGIC;113114ret = 0;115116err:117return ret;118}119120ATTRIBUTE_WARN_UNUSED_RET int md5_update(md5_context *ctx, const u8 *input, u32 ilen)121{122const u8 *data_ptr = input;123u32 remain_ilen = ilen;124u16 fill;125u8 left;126int ret;127128MUST_HAVE((input != NULL) || (ilen == 0), ret, err);129MD5_HASH_CHECK_INITIALIZED(ctx, ret, err);130131/* Nothing to process, return */132if (ilen == 0) {133ret = 0;134goto err;135}136137/* Get what's left in our local buffer */138left = (ctx->md5_total & 0x3F);139fill = (u16)(MD5_BLOCK_SIZE - left);140141ctx->md5_total += ilen;142143if ((left > 0) && (remain_ilen >= fill)) {144/* Copy data at the end of the buffer */145ret = local_memcpy(ctx->md5_buffer + left, data_ptr, fill); EG(ret, err);146ret = md5_process(ctx, ctx->md5_buffer); EG(ret, err);147data_ptr += fill;148remain_ilen -= fill;149left = 0;150}151152while (remain_ilen >= MD5_BLOCK_SIZE) {153ret = md5_process(ctx, data_ptr); EG(ret, err);154data_ptr += MD5_BLOCK_SIZE;155remain_ilen -= MD5_BLOCK_SIZE;156}157158if (remain_ilen > 0) {159ret = local_memcpy(ctx->md5_buffer + left, data_ptr, remain_ilen); EG(ret, err);160}161162ret = 0;163164err:165return ret;166}167168/* Finalize. Returns 0 on success, -1 on error.*/169ATTRIBUTE_WARN_UNUSED_RET int md5_final(md5_context *ctx, u8 output[MD5_DIGEST_SIZE])170{171unsigned int block_present = 0;172u8 last_padded_block[2 * MD5_BLOCK_SIZE];173int ret;174175MUST_HAVE((output != NULL), ret, err);176MD5_HASH_CHECK_INITIALIZED(ctx, ret, err);177178/* Fill in our last block with zeroes */179ret = local_memset(last_padded_block, 0, sizeof(last_padded_block)); EG(ret, err);180181/* This is our final step, so we proceed with the padding */182block_present = ctx->md5_total % MD5_BLOCK_SIZE;183if (block_present != 0) {184/* Copy what's left in our temporary context buffer */185ret = local_memcpy(last_padded_block, ctx->md5_buffer,186block_present); EG(ret, err);187}188189/* Put the 0x80 byte, beginning of padding */190last_padded_block[block_present] = 0x80;191192/* Handle possible additional block */193if (block_present > (MD5_BLOCK_SIZE - 1 - sizeof(u64))) {194/* We need an additional block */195PUT_UINT64_LE(8 * ctx->md5_total, last_padded_block,196(2 * MD5_BLOCK_SIZE) - sizeof(u64));197ret = md5_process(ctx, last_padded_block); EG(ret, err);198ret = md5_process(ctx, last_padded_block + MD5_BLOCK_SIZE); EG(ret, err);199} else {200/* We do not need an additional block */201PUT_UINT64_LE(8 * ctx->md5_total, last_padded_block,202MD5_BLOCK_SIZE - sizeof(u64));203ret = md5_process(ctx, last_padded_block); EG(ret, err);204}205206/* Output the hash result */207PUT_UINT32_LE(ctx->md5_state[0], output, 0);208PUT_UINT32_LE(ctx->md5_state[1], output, 4);209PUT_UINT32_LE(ctx->md5_state[2], output, 8);210PUT_UINT32_LE(ctx->md5_state[3], output, 12);211212/* Tell that we are uninitialized */213ctx->magic = WORD(0);214215ret = 0;216217err:218return ret;219}220221222/*223* Scattered version performing init/update/finalize on a vector of buffers224* 'inputs' with the length of each buffer passed via 'ilens'. The function225* loops on pointers in 'inputs' until it finds a NULL pointer. The function226* returns 0 on success, -1 on error.227*/228ATTRIBUTE_WARN_UNUSED_RET int md5_scattered(const u8 **inputs, const u32 *ilens,229u8 output[MD5_DIGEST_SIZE])230{231md5_context ctx;232int ret, pos = 0;233234MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);235236ret = md5_init(&ctx); EG(ret, err);237238while (inputs[pos] != NULL) {239ret = md5_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);240pos += 1;241}242243ret = md5_final(&ctx, output);244245err:246return ret;247}248249/*250* Single call version performing init/update/final on given input.251* Returns 0 on success, -1 on error.252*/253ATTRIBUTE_WARN_UNUSED_RET int md5(const u8 *input, u32 ilen, u8 output[MD5_DIGEST_SIZE])254{255md5_context ctx;256int ret;257258ret = md5_init(&ctx); EG(ret, err);259ret = md5_update(&ctx, input, ilen); EG(ret, err);260ret = md5_final(&ctx, output);261262err:263return ret;264}265266267