From 4dc7dcef3136b49ff6cb34f28508965433863be8 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 23 Aug 2024 23:40:36 +0200 Subject: Fixes + add SHA2 support via libsha2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libhashsum.h | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 158 insertions(+), 27 deletions(-) (limited to 'libhashsum.h') diff --git a/libhashsum.h b/libhashsum.h index 600a2c0..8bf38c7 100644 --- a/libhashsum.h +++ b/libhashsum.h @@ -6,6 +6,7 @@ #include #include +#include #if defined(__GNUC__) @@ -21,15 +22,21 @@ * Hashing algorithm */ enum libhashsum_algorithm { - LIBHASHSUM_MD2, /**< MD2 */ - LIBHASHSUM_MD4, /**< MD4 */ - LIBHASHSUM_MD5, /**< MD5 */ - LIBHASHSUM_RIPEMD_128, /**< RIPEMD-128 */ - LIBHASHSUM_RIPEMD_160, /**< RIPEMD-160 */ - LIBHASHSUM_RIPEMD_256, /**< RIPEMD-256 */ - LIBHASHSUM_RIPEMD_320, /**< RIPEMD-320 */ - LIBHASHSUM_SHA0, /**< SHA0 */ - LIBHASHSUM_SHA1 /**< SHA1 */ + LIBHASHSUM_MD2, /**< MD2 */ + LIBHASHSUM_MD4, /**< MD4 */ + LIBHASHSUM_MD5, /**< MD5 */ + LIBHASHSUM_RIPEMD_128, /**< RIPEMD-128 */ + LIBHASHSUM_RIPEMD_160, /**< RIPEMD-160 */ + LIBHASHSUM_RIPEMD_256, /**< RIPEMD-256 */ + LIBHASHSUM_RIPEMD_320, /**< RIPEMD-320 */ + LIBHASHSUM_SHA0, /**< SHA0 */ + LIBHASHSUM_SHA1, /**< SHA1 */ + LIBHASHSUM_SHA_224, /**< SHA-224 (SHA2) */ + LIBHASHSUM_SHA_256, /**< SHA-256 (SHA2) */ + LIBHASHSUM_SHA_384, /**< SHA-384 (SHA2) */ + LIBHASHSUM_SHA_512, /**< SHA-512 (SHA2) */ + LIBHASHSUM_SHA_512_224, /**< SHA-512/224 (SHA2) */ + LIBHASHSUM_SHA_512_256 /**< SHA-512/256 (SHA2) */ }; @@ -78,6 +85,36 @@ enum libhashsum_algorithm { */ #define LIBHASHSUM_SHA1_HASH_SIZE 20 +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA_224` + */ +#define LIBHASHSUM_SHA_224_HASH_SIZE 28 + +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA_256` + */ +#define LIBHASHSUM_SHA_256_HASH_SIZE 32 + +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA_384` + */ +#define LIBHASHSUM_SHA_384_HASH_SIZE 48 + +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA_512` + */ +#define LIBHASHSUM_SHA_512_HASH_SIZE 64 + +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA_512_224` + */ +#define LIBHASHSUM_SHA_512_224_HASH_SIZE 28 + +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA_512_256` + */ +#define LIBHASHSUM_SHA_512_256_HASH_SIZE 32 + /** * Hash state @@ -171,6 +208,11 @@ union libhashsum_state { struct libsha1_state s; uint8_t sum[20]; } sha0, sha1; + + struct { + struct libsha2_state s; + uint8_t sum[64]; + } sha2; }; @@ -279,8 +321,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algorithm algorithm); /** - * Create an initialised state for a MD2 - * and return hash functions and details + * Create an initialised state for MD2 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -291,8 +333,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_md2_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a MD4 - * and return hash functions and details + * Create an initialised state for MD4 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -303,8 +345,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_md4_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a MD5 - * and return hash functions and details + * Create an initialised state for MD5 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -315,8 +357,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_md5_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a RIPEMD-128 - * and return hash functions and details + * Create an initialised state for RIPEMD-128 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -327,8 +369,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_ripemd_128_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a RIPEMD-160 - * and return hash functions and details + * Create an initialised state for RIPEMD-160 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -339,8 +381,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_ripemd_160_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a RIPEMD-256 - * and return hash functions and details + * Create an initialised state for RIPEMD-256 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -351,8 +393,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_ripemd_256_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a RIPEMD-320 - * and return hash functions and details + * Create an initialised state for RIPEMD-320 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -363,8 +405,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_ripemd_320_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a SHA0 - * and return hash functions and details + * Create an initialised state for SHA0 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -375,8 +417,8 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_sha0_hasher(struct libhashsum_hasher *this); /** - * Create an initialised state for a SHA1 - * and return hash functions and details + * Create an initialised state for SHA1 + * hashing and return hash functions and details * * @param this The output parameter for the functions, details, and state * @return 0 on success, -1 on failure @@ -386,5 +428,94 @@ int libhashsum_init_sha0_hasher(struct libhashsum_hasher *this); LIBHASHSUM_1_NONNULL_ int libhashsum_init_sha1_hasher(struct libhashsum_hasher *this); +/** + * Create an initialised state for SHA-224 (SHA2) + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @return 0 on success, -1 on failure + * + * Failure isn't actually possible, so this function always return 0 + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha_224_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for SHA-256 (SHA2) + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @return 0 on success, -1 on failure + * + * Failure isn't actually possible, so this function always return 0 + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha_256_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for SHA-384 (SHA2) + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @return 0 on success, -1 on failure + * + * Failure isn't actually possible, so this function always return 0 + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha_384_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for SHA-512 (SHA2) + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @return 0 on success, -1 on failure + * + * Failure isn't actually possible, so this function always return 0 + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha_512_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for SHA-512/224 (SHA2) + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @return 0 on success, -1 on failure + * + * Failure isn't actually possible, so this function always return 0 + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha_512_224_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for SHA-512/256 (SHA2) + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @return 0 on success, -1 on failure + * + * Failure isn't actually possible, so this function always return 0 + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha_512_256_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for SHA2 + * hashing and return hash functions and details + * + * @param this The output parameter for the functions, details, and state + * @param algobits 32 for a 32-bit algorithm, 64 for a 64-bit algorithm + * @param hashbits Hash output size in bits + * @return 0 on success, -1 on failure + * + * @throws EINVAL `algobits` is invalid (neither 32 nor 64) + * @throws EINVAL `hashbits` is invalid (neither 224, 256, 384, nor 512) + * @throws EINVAL The combination of `algobits` and `hashbits` is invalid + * (`hashbits` is 384 or 512 but `algobits` is 32) + */ +LIBHASHSUM_1_NONNULL_ +int libhashsum_init_sha2_hasher(struct libhashsum_hasher *this, unsigned algobits, size_t hashbits); + #endif -- cgit v1.2.3-70-g09d2