1*f0865ec9SKyle Evans /* 2*f0865ec9SKyle Evans * Copyright (C) 2021 - This file is part of libecc project 3*f0865ec9SKyle Evans * 4*f0865ec9SKyle Evans * Authors: 5*f0865ec9SKyle Evans * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr> 6*f0865ec9SKyle Evans * Ryad BENADJILA <ryadbenadjila@gmail.com> 7*f0865ec9SKyle Evans * 8*f0865ec9SKyle Evans * This software is licensed under a dual BSD and GPL v2 license. 9*f0865ec9SKyle Evans * See LICENSE file at the root folder of the project. 10*f0865ec9SKyle Evans */ 11*f0865ec9SKyle Evans #include <libecc/lib_ecc_config.h> 12*f0865ec9SKyle Evans #ifdef WITH_HASH_RIPEMD160 13*f0865ec9SKyle Evans 14*f0865ec9SKyle Evans #ifndef __RIPEMD160_H__ 15*f0865ec9SKyle Evans #define __RIPEMD160_H__ 16*f0865ec9SKyle Evans 17*f0865ec9SKyle Evans #include <libecc/words/words.h> 18*f0865ec9SKyle Evans #include <libecc/utils/utils.h> 19*f0865ec9SKyle Evans 20*f0865ec9SKyle Evans #define RIPEMD160_STATE_SIZE 5 21*f0865ec9SKyle Evans #define RIPEMD160_BLOCK_SIZE 64 22*f0865ec9SKyle Evans #define RIPEMD160_DIGEST_SIZE 20 23*f0865ec9SKyle Evans #define RIPEMD160_DIGEST_SIZE_BITS 160 24*f0865ec9SKyle Evans 25*f0865ec9SKyle Evans /* Compute max hash digest and block sizes */ 26*f0865ec9SKyle Evans #ifndef MAX_DIGEST_SIZE 27*f0865ec9SKyle Evans #define MAX_DIGEST_SIZE 0 28*f0865ec9SKyle Evans #endif 29*f0865ec9SKyle Evans #if (MAX_DIGEST_SIZE < RIPEMD160_DIGEST_SIZE) 30*f0865ec9SKyle Evans #undef MAX_DIGEST_SIZE 31*f0865ec9SKyle Evans #define MAX_DIGEST_SIZE RIPEMD160_DIGEST_SIZE 32*f0865ec9SKyle Evans #endif 33*f0865ec9SKyle Evans 34*f0865ec9SKyle Evans #ifndef MAX_DIGEST_SIZE_BITS 35*f0865ec9SKyle Evans #define MAX_DIGEST_SIZE_BITS 0 36*f0865ec9SKyle Evans #endif 37*f0865ec9SKyle Evans #if (MAX_DIGEST_SIZE_BITS < RIPEMD160_DIGEST_SIZE_BITS) 38*f0865ec9SKyle Evans #undef MAX_DIGEST_SIZE_BITS 39*f0865ec9SKyle Evans #define MAX_DIGEST_SIZE_BITS RIPEMD160_DIGEST_SIZE_BITS 40*f0865ec9SKyle Evans #endif 41*f0865ec9SKyle Evans 42*f0865ec9SKyle Evans #ifndef MAX_BLOCK_SIZE 43*f0865ec9SKyle Evans #define MAX_BLOCK_SIZE 0 44*f0865ec9SKyle Evans #endif 45*f0865ec9SKyle Evans #if (MAX_BLOCK_SIZE < RIPEMD160_BLOCK_SIZE) 46*f0865ec9SKyle Evans #undef MAX_BLOCK_SIZE 47*f0865ec9SKyle Evans #define MAX_BLOCK_SIZE RIPEMD160_BLOCK_SIZE 48*f0865ec9SKyle Evans #endif 49*f0865ec9SKyle Evans 50*f0865ec9SKyle Evans #define RIPEMD160_HASH_MAGIC ((word_t)(0x7392018463926719ULL)) 51*f0865ec9SKyle Evans #define RIPEMD160_HASH_CHECK_INITIALIZED(A, ret, err) \ 52*f0865ec9SKyle Evans MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == RIPEMD160_HASH_MAGIC), ret, err) 53*f0865ec9SKyle Evans 54*f0865ec9SKyle Evans typedef struct { 55*f0865ec9SKyle Evans /* Number of bytes processed */ 56*f0865ec9SKyle Evans u64 ripemd160_total; 57*f0865ec9SKyle Evans /* Internal state */ 58*f0865ec9SKyle Evans u32 ripemd160_state[RIPEMD160_STATE_SIZE]; 59*f0865ec9SKyle Evans /* Internal buffer to handle updates in a block */ 60*f0865ec9SKyle Evans u8 ripemd160_buffer[RIPEMD160_BLOCK_SIZE]; 61*f0865ec9SKyle Evans /* Initialization magic value */ 62*f0865ec9SKyle Evans word_t magic; 63*f0865ec9SKyle Evans } ripemd160_context; 64*f0865ec9SKyle Evans 65*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int ripemd160_init(ripemd160_context *ctx); 66*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int ripemd160_update(ripemd160_context *ctx, const u8 *input, u32 ilen); 67*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int ripemd160_final(ripemd160_context *ctx, u8 output[RIPEMD160_DIGEST_SIZE]); 68*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int ripemd160_scattered(const u8 **inputs, const u32 *ilens, 69*f0865ec9SKyle Evans u8 output[RIPEMD160_DIGEST_SIZE]); 70*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int ripemd160(const u8 *input, u32 ilen, u8 output[RIPEMD160_DIGEST_SIZE]); 71*f0865ec9SKyle Evans 72*f0865ec9SKyle Evans #endif /* __RIPEMD160_H__ */ 73*f0865ec9SKyle Evans #endif /* WITH_HASH_RIPEMD160 */ 74