Path: blob/main/crypto/libecc/src/examples/hash/gostr34_11_94.h
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#ifndef __GOSTR34_11_94_H__11#define __GOSTR34_11_94_H__1213/* Include libec for useful types and macros */14#include <libecc/libec.h>1516/****************************************************/17/*18* 32-bit integer manipulation macros19*/20#ifndef GET_UINT32_BE21#define GET_UINT32_BE(n, b, i) \22do { \23(n) = ( ((u32) (b)[(i) ]) << 24 ) \24| ( ((u32) (b)[(i) + 1]) << 16 ) \25| ( ((u32) (b)[(i) + 2]) << 8 ) \26| ( ((u32) (b)[(i) + 3]) ); \27} while( 0 )28#endif29#ifndef GET_UINT32_LE30#define GET_UINT32_LE(n, b, i) \31do { \32(n) = ( ((u32) (b)[(i) + 3]) << 24 ) \33| ( ((u32) (b)[(i) + 2]) << 16 ) \34| ( ((u32) (b)[(i) + 1]) << 8 ) \35| ( ((u32) (b)[(i) ]) ); \36} while( 0 )37#endif383940#ifndef PUT_UINT32_BE41#define PUT_UINT32_BE(n, b, i) \42do { \43(b)[(i) ] = (u8) ( (n) >> 24 ); \44(b)[(i) + 1] = (u8) ( (n) >> 16 ); \45(b)[(i) + 2] = (u8) ( (n) >> 8 ); \46(b)[(i) + 3] = (u8) ( (n) ); \47} while( 0 )48#endif4950#ifndef PUT_UINT32_LE51#define PUT_UINT32_LE(n, b, i) \52do { \53(b)[(i) + 3] = (u8) ( (n) >> 24 ); \54(b)[(i) + 2] = (u8) ( (n) >> 16 ); \55(b)[(i) + 1] = (u8) ( (n) >> 8 ); \56(b)[(i) ] = (u8) ( (n) ); \57} while( 0 )58#endif5960/*61* 64-bit integer manipulation macros62*/63#ifndef GET_UINT64_BE64#define GET_UINT64_BE(n,b,i) \65do { \66(n) = ( ((u64) (b)[(i) ]) << 56 ) \67| ( ((u64) (b)[(i) + 1]) << 48 ) \68| ( ((u64) (b)[(i) + 2]) << 40 ) \69| ( ((u64) (b)[(i) + 3]) << 32 ) \70| ( ((u64) (b)[(i) + 4]) << 24 ) \71| ( ((u64) (b)[(i) + 5]) << 16 ) \72| ( ((u64) (b)[(i) + 6]) << 8 ) \73| ( ((u64) (b)[(i) + 7]) ); \74} while( 0 )75#endif /* GET_UINT64_BE */7677#ifndef GET_UINT64_LE78#define GET_UINT64_LE(n,b,i) \79do { \80(n) = ( ((u64) (b)[(i) + 7]) << 56 ) \81| ( ((u64) (b)[(i) + 6]) << 48 ) \82| ( ((u64) (b)[(i) + 5]) << 40 ) \83| ( ((u64) (b)[(i) + 4]) << 32 ) \84| ( ((u64) (b)[(i) + 3]) << 24 ) \85| ( ((u64) (b)[(i) + 2]) << 16 ) \86| ( ((u64) (b)[(i) + 1]) << 8 ) \87| ( ((u64) (b)[(i) ]) ); \88} while( 0 )89#endif /* GET_UINT64_LE */9091#ifndef PUT_UINT64_BE92#define PUT_UINT64_BE(n,b,i) \93do { \94(b)[(i) ] = (u8) ( (n) >> 56 ); \95(b)[(i) + 1] = (u8) ( (n) >> 48 ); \96(b)[(i) + 2] = (u8) ( (n) >> 40 ); \97(b)[(i) + 3] = (u8) ( (n) >> 32 ); \98(b)[(i) + 4] = (u8) ( (n) >> 24 ); \99(b)[(i) + 5] = (u8) ( (n) >> 16 ); \100(b)[(i) + 6] = (u8) ( (n) >> 8 ); \101(b)[(i) + 7] = (u8) ( (n) ); \102} while( 0 )103#endif /* PUT_UINT64_BE */104105#ifndef PUT_UINT64_LE106#define PUT_UINT64_LE(n,b,i) \107do { \108(b)[(i) + 7] = (u8) ( (n) >> 56 ); \109(b)[(i) + 6] = (u8) ( (n) >> 48 ); \110(b)[(i) + 5] = (u8) ( (n) >> 40 ); \111(b)[(i) + 4] = (u8) ( (n) >> 32 ); \112(b)[(i) + 3] = (u8) ( (n) >> 24 ); \113(b)[(i) + 2] = (u8) ( (n) >> 16 ); \114(b)[(i) + 1] = (u8) ( (n) >> 8 ); \115(b)[(i) ] = (u8) ( (n) ); \116} while( 0 )117#endif /* PUT_UINT64_LE */118119#define GOSTR34_11_94_STATE_SIZE 4120#define GOSTR34_11_94_BLOCK_SIZE 32121#define GOSTR34_11_94_DIGEST_SIZE 32122#define GOSTR34_11_94_DIGEST_SIZE_BITS 256123124#define GOSTR34_11_94_HASH_MAGIC ((word_t)(0x1262734139734143ULL))125#define GOSTR34_11_94_HASH_CHECK_INITIALIZED(A, ret, err) \126MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == GOSTR34_11_94_HASH_MAGIC), ret, err)127128#define ROTL_GOSTR34_11_94(x, n) ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))129130/* All the inner operations */131132typedef enum {133GOST34_11_94_NORM = 0,134GOST34_11_94_RFC4357 = 1,135} gostr34_11_94_type;136137typedef struct {138/* "Type" of GOST, changing the SBOX to use */139gostr34_11_94_type gostr34_11_94_t;140/* Number of bytes processed */141u64 gostr34_11_94_total;142/* Internal state: 4 64-bit values */143u64 gostr34_11_94_state[GOSTR34_11_94_STATE_SIZE];144/* Internal buffer to handle updates in a block */145u8 gostr34_11_94_buffer[GOSTR34_11_94_BLOCK_SIZE];146/* The sum */147u64 gostr34_11_94_sum[GOSTR34_11_94_STATE_SIZE];148/* Initialization magic value */149word_t magic;150} gostr34_11_94_context;151152153/* Init hash function. Returns 0 on success, -1 on error. */154ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_init(gostr34_11_94_context *ctx);155156/* Function to modify the initial IV as it is not imposed by the RFCs */157ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_set_iv(gostr34_11_94_context *ctx, const u64 iv[GOSTR34_11_94_STATE_SIZE]);158159/* Function to modify the GOST type (that will dictate the underlying SBOX to use for block encryption) */160ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_set_type(gostr34_11_94_context *ctx, gostr34_11_94_type type);161162ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_update(gostr34_11_94_context *ctx, const u8 *input, u32 ilen);163164/* Finalize. Returns 0 on success, -1 on error.*/165ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_final(gostr34_11_94_context *ctx, u8 output[GOSTR34_11_94_DIGEST_SIZE]);166167/*168* Scattered version performing init/update/finalize on a vector of buffers169* 'inputs' with the length of each buffer passed via 'ilens'. The function170* loops on pointers in 'inputs' until it finds a NULL pointer. The function171* returns 0 on success, -1 on error.172*/173ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_scattered(const u8 **inputs, const u32 *ilens,174u8 output[GOSTR34_11_94_DIGEST_SIZE], gostr34_11_94_type type);175176ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_scattered_norm(const u8 **inputs, const u32 *ilens,177u8 output[GOSTR34_11_94_DIGEST_SIZE]);178179ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_scattered_rfc4357(const u8 **inputs, const u32 *ilens,180u8 output[GOSTR34_11_94_DIGEST_SIZE]);181182/*183* Single call version performing init/update/final on given input.184* Returns 0 on success, -1 on error.185*/186ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94(const u8 *input, u32 ilen, u8 output[GOSTR34_11_94_DIGEST_SIZE], gostr34_11_94_type type);187188ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_norm(const u8 *input, u32 ilen, u8 output[GOSTR34_11_94_DIGEST_SIZE]);189190ATTRIBUTE_WARN_UNUSED_RET int gostr34_11_94_rfc4357(const u8 *input, u32 ilen, u8 output[GOSTR34_11_94_DIGEST_SIZE]);191192#endif /* __GOSTR34_11_94_H__ */193194195