Path: blob/main/crypto/libecc/src/hash/belt-hash.c
34879 views
/*1* Copyright (C) 2022 - 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 <libecc/lib_ecc_config.h>11#ifdef WITH_HASH_BELT_HASH1213#include <libecc/hash/belt-hash.h>1415/*16* This is an implementation of the BELT-HASH hash function as17* defined int STB 34.101.31.18*/192021/*22* The BELT-HASH function uses an underlying BELT block cipher23* defined in STB 34.101.31. This is a simple and straitforward24* implementation.25*/26#define ROTL_BELT(x, n) ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))2728#define SWAP_BELT(x, y) do { \29u32 z; \30z = (x); \31(x) = (y); \32(y) = z; \33} while(0)3435/* The S-Box */36static u8 S[256] =37{380xB1, 0x94, 0xBA, 0xC8, 0x0A, 0x08, 0xF5, 0x3B, 0x36, 0x6D, 0x00, 0x8E, 0x58, 0x4A, 0x5D, 0xE4,390x85, 0x04, 0xFA, 0x9D, 0x1B, 0xB6, 0xC7, 0xAC, 0x25, 0x2E, 0x72, 0xC2, 0x02, 0xFD, 0xCE, 0x0D,400x5B, 0xE3, 0xD6, 0x12, 0x17, 0xB9, 0x61, 0x81, 0xFE, 0x67, 0x86, 0xAD, 0x71, 0x6B, 0x89, 0x0B,410x5C, 0xB0, 0xC0, 0xFF, 0x33, 0xC3, 0x56, 0xB8, 0x35, 0xC4, 0x05, 0xAE, 0xD8, 0xE0, 0x7F, 0x99,420xE1, 0x2B, 0xDC, 0x1A, 0xE2, 0x82, 0x57, 0xEC, 0x70, 0x3F, 0xCC, 0xF0, 0x95, 0xEE, 0x8D, 0xF1,430xC1, 0xAB, 0x76, 0x38, 0x9F, 0xE6, 0x78, 0xCA, 0xF7, 0xC6, 0xF8, 0x60, 0xD5, 0xBB, 0x9C, 0x4F,440xF3, 0x3C, 0x65, 0x7B, 0x63, 0x7C, 0x30, 0x6A, 0xDD, 0x4E, 0xA7, 0x79, 0x9E, 0xB2, 0x3D, 0x31,450x3E, 0x98, 0xB5, 0x6E, 0x27, 0xD3, 0xBC, 0xCF, 0x59, 0x1E, 0x18, 0x1F, 0x4C, 0x5A, 0xB7, 0x93,460xE9, 0xDE, 0xE7, 0x2C, 0x8F, 0x0C, 0x0F, 0xA6, 0x2D, 0xDB, 0x49, 0xF4, 0x6F, 0x73, 0x96, 0x47,470x06, 0x07, 0x53, 0x16, 0xED, 0x24, 0x7A, 0x37, 0x39, 0xCB, 0xA3, 0x83, 0x03, 0xA9, 0x8B, 0xF6,480x92, 0xBD, 0x9B, 0x1C, 0xE5, 0xD1, 0x41, 0x01, 0x54, 0x45, 0xFB, 0xC9, 0x5E, 0x4D, 0x0E, 0xF2,490x68, 0x20, 0x80, 0xAA, 0x22, 0x7D, 0x64, 0x2F, 0x26, 0x87, 0xF9, 0x34, 0x90, 0x40, 0x55, 0x11,500xBE, 0x32, 0x97, 0x13, 0x43, 0xFC, 0x9A, 0x48, 0xA0, 0x2A, 0x88, 0x5F, 0x19, 0x4B, 0x09, 0xA1,510x7E, 0xCD, 0xA4, 0xD0, 0x15, 0x44, 0xAF, 0x8C, 0xA5, 0x84, 0x50, 0xBF, 0x66, 0xD2, 0xE8, 0x8A,520xA2, 0xD7, 0x46, 0x52, 0x42, 0xA8, 0xDF, 0xB3, 0x69, 0x74, 0xC5, 0x51, 0xEB, 0x23, 0x29, 0x21,530xD4, 0xEF, 0xD9, 0xB4, 0x3A, 0x62, 0x28, 0x75, 0x91, 0x14, 0x10, 0xEA, 0x77, 0x6C, 0xDA, 0x1D,54};5556/* */57#define GET_BYTE(x, a) ( ((x) >> (a)) & 0xff )58#define PUT_BYTE(x, a) ( (u32)(x) << (a) )59#define SB(x, a) PUT_BYTE( S[GET_BYTE((x), (a))], (a) )6061#define G(x, r) ROTL_BELT( SB((x), 24) | SB((x), 16) | SB((x), 8) | SB((x), 0), (r) )6263static u32 KIdx[8][7] =64{65{ 0, 1, 2, 3, 4, 5, 6 },66{ 7, 0, 1, 2, 3, 4, 5 },67{ 6, 7, 0, 1, 2, 3, 4 },68{ 5, 6, 7, 0, 1, 2, 3 },69{ 4, 5, 6, 7, 0, 1, 2 },70{ 3, 4, 5, 6, 7, 0, 1 },71{ 2, 3, 4, 5, 6, 7, 0 },72{ 1, 2, 3, 4, 5, 6, 7 },73};7475int belt_init(const u8 *k, u32 k_len, u8 ks[BELT_KEY_SCHED_LEN])76{77int ret = -1;78unsigned int i;7980switch(k_len){81case 16:{82for(i = 0; i < 16; i++){83ks[i] = k[i];84ks[i + 16] = k[i];85}86break;87}88case 24:{89for(i = 0; i < 24; i++){90ks[i] = k[i];91}92for(i = 24; i < 32; i++){93ks[i] = k[i - 24] ^ k[i - 20] ^ k[i - 16];94}95break;96}97case 32:{98for(i = 0; i < 32; i++){99ks[i] = k[i];100}101break;102}103default:{104ret = -1;105goto err;106}107108109}110111ret = 0;112err:113return ret;114}115116void belt_encrypt(const u8 in[BELT_BLOCK_LEN], u8 out[BELT_BLOCK_LEN], const u8 ks[BELT_KEY_SCHED_LEN])117{118u32 a, b, c, d, e;119u32 i;120121GET_UINT32_LE(a, in, 0);122GET_UINT32_LE(b, in, 4);123GET_UINT32_LE(c, in, 8);124GET_UINT32_LE(d, in, 12);125126for(i = 0; i < 8; i++){127u32 key;128GET_UINT32_LE(key, ks, 4*KIdx[i][0]);129b ^= G(a + key, 5);130GET_UINT32_LE(key, ks, 4*KIdx[i][1]);131c ^= G(d + key, 21);132GET_UINT32_LE(key, ks, 4*KIdx[i][2]);133a = (u32)(a - G(b + key, 13));134GET_UINT32_LE(key, ks, 4*KIdx[i][3]);135e = G(b + c + key, 21) ^ (i + 1);136b += e;137c = (u32)(c - e);138GET_UINT32_LE(key, ks, 4*KIdx[i][4]);139d += G(c + key, 13);140GET_UINT32_LE(key, ks, 4*KIdx[i][5]);141b ^= G(a + key, 21);142GET_UINT32_LE(key, ks, 4*KIdx[i][6]);143c ^= G(d + key, 5);144SWAP_BELT(a, b);145SWAP_BELT(c, d);146SWAP_BELT(b, c);147}148149PUT_UINT32_LE(b, out, 0);150PUT_UINT32_LE(d, out, 4);151PUT_UINT32_LE(a, out, 8);152PUT_UINT32_LE(c, out, 12);153154return;155}156157void belt_decrypt(const u8 in[BELT_BLOCK_LEN], u8 out[BELT_BLOCK_LEN], const u8 ks[BELT_KEY_SCHED_LEN])158{159u32 a, b, c, d, e;160u32 i;161162GET_UINT32_LE(a, in, 0);163GET_UINT32_LE(b, in, 4);164GET_UINT32_LE(c, in, 8);165GET_UINT32_LE(d, in, 12);166167for(i = 0; i < 8; i++){168u32 key;169u32 j = (7 - i);170GET_UINT32_LE(key, ks, 4*KIdx[i][6]);171b ^= G(a + key, 5);172GET_UINT32_LE(key, ks, 4*KIdx[i][5]);173c ^= G(d + key, 21);174GET_UINT32_LE(key, ks, 4*KIdx[i][4]);175a = (u32)(a - G(b + key, 13));176GET_UINT32_LE(key, ks, 4*KIdx[i][3]);177e = G(b + c + key, 21) ^ (j + 1);178b += e;179c = (u32)(c - e);180GET_UINT32_LE(key, ks, 4*KIdx[i][2]);181d += G(c + key, 13);182GET_UINT32_LE(key, ks, 4*KIdx[i][1]);183b ^= G(a + key, 21);184GET_UINT32_LE(key, ks, 4*KIdx[i][0]);185c ^= G(d + key, 5);186SWAP_BELT(a, b);187SWAP_BELT(c, d);188SWAP_BELT(a, d);189}190191PUT_UINT32_LE(c, out, 0);192PUT_UINT32_LE(a, out, 4);193PUT_UINT32_LE(d, out, 8);194PUT_UINT32_LE(b, out, 12);195196return;197}198199/* BELT-HASH primitives */200static void sigma1_xor(const u8 x[2 * BELT_BLOCK_LEN], const u8 h[2 * BELT_BLOCK_LEN], u8 s[BELT_BLOCK_LEN], u8 use_xor){201u8 tmp1[BELT_BLOCK_LEN];202unsigned int i;203204for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){205tmp1[i] = (h[i] ^ h[i + BELT_BLOCK_LEN]);206tmp1[i + (BELT_BLOCK_LEN / 2)] = (h[i + (BELT_BLOCK_LEN / 2)] ^ h[i + BELT_BLOCK_LEN + (BELT_BLOCK_LEN / 2)]);207}208209if(use_xor){210u8 tmp2[BELT_BLOCK_LEN];211212belt_encrypt(tmp1, tmp2, x);213214for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){215s[i] ^= (tmp1[i] ^ tmp2[i]);216s[i + (BELT_BLOCK_LEN / 2)] ^= (tmp1[i + (BELT_BLOCK_LEN / 2)] ^ tmp2[i + (BELT_BLOCK_LEN / 2)]);217}218}219else{220belt_encrypt(tmp1, s, x);221for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){222s[i] ^= tmp1[i];223s[i + (BELT_BLOCK_LEN / 2)] ^= tmp1[i + (BELT_BLOCK_LEN / 2)];224}225}226227return;228}229230static void sigma2(const u8 x[2 * BELT_BLOCK_LEN], u8 const h[2 * BELT_BLOCK_LEN], u8 result[2 * BELT_BLOCK_LEN])231{232u8 teta[BELT_KEY_SCHED_LEN];233u8 tmp[BELT_BLOCK_LEN];234unsigned int i;235236/* Copy the beginning of h for later in case it is lost */237IGNORE_RET_VAL(local_memcpy(&tmp[0], &h[0], BELT_BLOCK_LEN));238239sigma1_xor(x, h, teta, 0);240IGNORE_RET_VAL(local_memcpy(&teta[BELT_BLOCK_LEN], &h[BELT_BLOCK_LEN], BELT_BLOCK_LEN));241242belt_encrypt(x, result, teta);243for(i = 0; i < BELT_BLOCK_LEN; i++){244result[i] ^= x[i];245teta[i] ^= 0xff;246teta[i + BELT_BLOCK_LEN] = tmp[i];247}248249belt_encrypt(&x[BELT_BLOCK_LEN], &result[BELT_BLOCK_LEN], teta);250251for(i = 0; i < (BELT_BLOCK_LEN / 2); i++){252result[i + BELT_BLOCK_LEN] ^= x[i + BELT_BLOCK_LEN];253result[i + BELT_BLOCK_LEN + (BELT_BLOCK_LEN / 2)] ^= x[i + BELT_BLOCK_LEN + (BELT_BLOCK_LEN / 2)];254}255256return;257}258259static void _belt_hash_process(const u8 x[2 * BELT_BLOCK_LEN], u8 h[2 * BELT_BLOCK_LEN], u8 s[BELT_BLOCK_LEN])260{261sigma1_xor(x, h, s, 1);262263sigma2(x, h, h);264265return;266}267268ATTRIBUTE_WARN_UNUSED_RET static int belt_hash_process(belt_hash_context *ctx, const u8 data[BELT_HASH_BLOCK_SIZE])269{270_belt_hash_process(data, ctx->belt_hash_h, &(ctx->belt_hash_state[BELT_BLOCK_LEN]));271272return 0;273}274275ATTRIBUTE_WARN_UNUSED_RET static int belt_hash_finalize(const u8 s[2 * BELT_BLOCK_LEN], const u8 h[2 * BELT_BLOCK_LEN], u8 res[2 * BELT_BLOCK_LEN])276{277sigma2(s, h, res);278279return 0;280}281282static void belt_update_ctr(belt_hash_context *ctx, u8 len_bytes)283{284/* Perform a simple addition on 128 bits on the first part of the state */285u64 a0, a1, b, c;286287GET_UINT64_LE(a0, (const u8*)(ctx->belt_hash_state), 0);288GET_UINT64_LE(a1, (const u8*)(ctx->belt_hash_state), 8);289290b = (u64)(len_bytes << 3);291292c = (a0 + b);293if(c < b){294/* Handle carry */295a1 += 1;296}297298/* Store the result */299PUT_UINT64_LE(c, (u8*)(ctx->belt_hash_state), 0);300PUT_UINT64_LE(a1, (u8*)(ctx->belt_hash_state), 8);301302return;303}304305/* Init hash function. Returns 0 on success, -1 on error. */306int belt_hash_init(belt_hash_context *ctx)307{308int ret;309310MUST_HAVE((ctx != NULL), ret, err);311312ctx->belt_hash_total = 0;313314ret = local_memset(ctx->belt_hash_state, 0, sizeof(ctx->belt_hash_state)); EG(ret, err);315316PUT_UINT64_LE(0x3bf5080ac8ba94b1ULL, ctx->belt_hash_h, 0);317PUT_UINT64_LE(0xe45d4a588e006d36ULL, ctx->belt_hash_h, 8);318PUT_UINT64_LE(0xacc7b61b9dfa0485ULL, ctx->belt_hash_h, 16);319PUT_UINT64_LE(0x0dcefd02c2722e25ULL, ctx->belt_hash_h, 24);320321/* Tell that we are initialized */322ctx->magic = BELT_HASH_HASH_MAGIC;323324ret = 0;325326err:327return ret;328}329330/* Update hash function. Returns 0 on success, -1 on error. */331int belt_hash_update(belt_hash_context *ctx, const u8 *input, u32 ilen)332{333const u8 *data_ptr = input;334u32 remain_ilen = ilen;335u16 fill;336u8 left;337int ret;338339MUST_HAVE((input != NULL) || (ilen == 0), ret, err);340BELT_HASH_HASH_CHECK_INITIALIZED(ctx, ret, err);341342/* Nothing to process, return */343if (ilen == 0) {344ret = 0;345goto err;346}347348/* Get what's left in our local buffer */349left = (ctx->belt_hash_total & (BELT_HASH_BLOCK_SIZE - 1));350fill = (u16)(BELT_HASH_BLOCK_SIZE - left);351352ctx->belt_hash_total += ilen;353354if ((left > 0) && (remain_ilen >= fill)) {355/* Copy data at the end of the buffer */356ret = local_memcpy(ctx->belt_hash_buffer + left, data_ptr, fill); EG(ret, err);357/* Update the counter with one full block */358belt_update_ctr(ctx, BELT_HASH_BLOCK_SIZE);359/* Process */360ret = belt_hash_process(ctx, ctx->belt_hash_buffer); EG(ret, err);361data_ptr += fill;362remain_ilen -= fill;363left = 0;364}365366while (remain_ilen >= BELT_HASH_BLOCK_SIZE) {367/* Update the counter with one full block */368belt_update_ctr(ctx, BELT_HASH_BLOCK_SIZE);369/* Process */370ret = belt_hash_process(ctx, data_ptr); EG(ret, err);371data_ptr += BELT_HASH_BLOCK_SIZE;372remain_ilen -= BELT_HASH_BLOCK_SIZE;373}374375if (remain_ilen > 0) {376ret = local_memcpy(ctx->belt_hash_buffer + left, data_ptr, remain_ilen); EG(ret, err);377}378379ret = 0;380381err:382return ret;383}384385/* Finalize. Returns 0 on success, -1 on error.*/386int belt_hash_final(belt_hash_context *ctx, u8 output[BELT_HASH_DIGEST_SIZE])387{388int ret;389unsigned int i;390391MUST_HAVE((output != NULL), ret, err);392BELT_HASH_HASH_CHECK_INITIALIZED(ctx, ret, err);393394if((ctx->belt_hash_total % BELT_HASH_BLOCK_SIZE) != 0){395/* Pad our last block with zeroes */396for(i = (ctx->belt_hash_total % BELT_HASH_BLOCK_SIZE); i < BELT_HASH_BLOCK_SIZE; i++){397ctx->belt_hash_buffer[i] = 0;398}399400/* Update the counter with the remaining data */401belt_update_ctr(ctx, (u8)(ctx->belt_hash_total % BELT_HASH_BLOCK_SIZE));402403/* Process the last block */404ret = belt_hash_process(ctx, ctx->belt_hash_buffer); EG(ret, err);405}406407/* Finalize and output the result */408ret = belt_hash_finalize(ctx->belt_hash_state, ctx->belt_hash_h, output); EG(ret, err);409410/* Tell that we are uninitialized */411ctx->magic = WORD(0);412413ret = 0;414415err:416return ret;417}418419/*420* Scattered version performing init/update/finalize on a vector of buffers421* 'inputs' with the length of each buffer passed via 'ilens'. The function422* loops on pointers in 'inputs' until it finds a NULL pointer. The function423* returns 0 on success, -1 on error.424*/425int belt_hash_scattered(const u8 **inputs, const u32 *ilens,426u8 output[BELT_HASH_DIGEST_SIZE])427{428belt_hash_context ctx;429int ret, pos = 0;430431MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);432433ret = belt_hash_init(&ctx); EG(ret, err);434435while (inputs[pos] != NULL) {436ret = belt_hash_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);437pos += 1;438}439440ret = belt_hash_final(&ctx, output);441442err:443return ret;444}445446/*447* Single call version performing init/update/final on given input.448* Returns 0 on success, -1 on error.449*/450int belt_hash(const u8 *input, u32 ilen, u8 output[BELT_HASH_DIGEST_SIZE])451{452belt_hash_context ctx;453int ret;454455ret = belt_hash_init(&ctx); EG(ret, err);456ret = belt_hash_update(&ctx, input, ilen); EG(ret, err);457ret = belt_hash_final(&ctx, output);458459err:460return ret;461}462463#else /* WITH_HASH_BELT_HASH */464465/*466* Dummy definition to avoid the empty translation unit ISO C warning467*/468typedef int dummy;469470#endif /* WITH_HASH_BELT_HASH */471472473