diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-23 23:40:36 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-23 23:40:36 +0200 |
commit | 4dc7dcef3136b49ff6cb34f28508965433863be8 (patch) | |
tree | 2909c34a9351f3567bccec4741ecd53b98327d31 /libhashsum | |
parent | Fixes + add SHA0 and SHA1 using libsha1 (diff) | |
download | libhashsum-4dc7dcef3136b49ff6cb34f28508965433863be8.tar.gz libhashsum-4dc7dcef3136b49ff6cb34f28508965433863be8.tar.bz2 libhashsum-4dc7dcef3136b49ff6cb34f28508965433863be8.tar.xz |
Fixes + add SHA2 support via libsha2
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libhashsum.h | 185 | ||||
-rw-r--r-- | libhashsum_init_hasher.c | 12 | ||||
-rw-r--r-- | libhashsum_init_sha0_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_sha1_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_sha2_hasher.c | 93 | ||||
-rw-r--r-- | libhashsum_init_sha_224_hasher.c | 9 | ||||
-rw-r--r-- | libhashsum_init_sha_256_hasher.c | 9 | ||||
-rw-r--r-- | libhashsum_init_sha_384_hasher.c | 9 | ||||
-rw-r--r-- | libhashsum_init_sha_512_224_hasher.c | 9 | ||||
-rw-r--r-- | libhashsum_init_sha_512_256_hasher.c | 9 | ||||
-rw-r--r-- | libhashsum_init_sha_512_hasher.c | 9 |
11 files changed, 319 insertions, 29 deletions
diff --git a/libhashsum.h b/libhashsum.h index 600a2c0..8bf38c7 100644 --- a/libhashsum.h +++ b/libhashsum.h @@ -6,6 +6,7 @@ #include <stdint.h> #include <libsha1.h> +#include <libsha2.h> #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 diff --git a/libhashsum_init_hasher.c b/libhashsum_init_hasher.c index 91689ca..d747e52 100644 --- a/libhashsum_init_hasher.c +++ b/libhashsum_init_hasher.c @@ -24,6 +24,18 @@ libhashsum_init_hasher(struct libhashsum_hasher *hasher, enum libhashsum_algorit return libhashsum_init_sha0_hasher(hasher); case LIBHASHSUM_SHA1: return libhashsum_init_sha1_hasher(hasher); + case LIBHASHSUM_SHA_224: + return libhashsum_init_sha_224_hasher(hasher); + case LIBHASHSUM_SHA_256: + return libhashsum_init_sha_256_hasher(hasher); + case LIBHASHSUM_SHA_384: + return libhashsum_init_sha_384_hasher(hasher); + case LIBHASHSUM_SHA_512: + return libhashsum_init_sha_512_hasher(hasher); + case LIBHASHSUM_SHA_512_224: + return libhashsum_init_sha_512_224_hasher(hasher); + case LIBHASHSUM_SHA_512_256: + return libhashsum_init_sha_512_256_hasher(hasher); default: errno = EINVAL; return -1; diff --git a/libhashsum_init_sha0_hasher.c b/libhashsum_init_sha0_hasher.c index 3ed5744..e9c0ea4 100644 --- a/libhashsum_init_sha0_hasher.c +++ b/libhashsum_init_sha0_hasher.c @@ -60,7 +60,7 @@ libhashsum_init_sha0_hasher(struct libhashsum_hasher *this) this->input_block_size = 64U; this->hash_size = sizeof(this->state.sha0.sum); this->hash_output = NULL; - this->supports_non_whole_bytes = 0; + this->supports_non_whole_bytes = 1; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_sha1_hasher.c b/libhashsum_init_sha1_hasher.c index 6058b73..25c7496 100644 --- a/libhashsum_init_sha1_hasher.c +++ b/libhashsum_init_sha1_hasher.c @@ -60,7 +60,7 @@ libhashsum_init_sha1_hasher(struct libhashsum_hasher *this) this->input_block_size = 64U; this->hash_size = sizeof(this->state.sha1.sum); this->hash_output = NULL; - this->supports_non_whole_bytes = 0; + this->supports_non_whole_bytes = 1; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_sha2_hasher.c b/libhashsum_init_sha2_hasher.c new file mode 100644 index 0000000..ab64517 --- /dev/null +++ b/libhashsum_init_sha2_hasher.c @@ -0,0 +1,93 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +LIBHASHSUM_1_NONNULL_ +static size_t +process(struct libhashsum_hasher *this, const void *data, size_t bytes) +{ + const uint8_t *m = data; + int i; + bytes -= bytes & (this->input_block_size - 1U); + if (bytes > SIZE_MAX >> 3) { + for (i = 0; i < 8; i++) { + libsha2_update(&this->state.sha2.s, m, bytes); + m = &m[bytes >> 3]; + } + } else if (bytes) { + libsha2_update(&this->state.sha2.s, m, bytes << 3); + } + return bytes; +} + + +LIBHASHSUM_1_NONNULL_ +static int +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) +{ + const uint8_t *m = data; + size_t r; + + if (extra_bits > 7U) { + errno = EINVAL; + return -1; + } + + r = process(this, m, bytes); + m = &m[r]; + bytes -= r; + + libsha2_digest(&this->state.sha2.s, data, (bytes << 3) | extra_bits, this->state.sha2.sum); + memset(&this->state.sha2.s, 0, sizeof(this->state.sha2.s)); + this->hash_output = this->state.sha2.sum; + return 0; +} + + +LIBHASHSUM_1_NONNULL_ +static int +finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extra_bits, size_t size) +{ + (void) size; + return finalise_const(this, data, bytes, extra_bits); +} + + +int +libhashsum_init_sha2_hasher(struct libhashsum_hasher *this, unsigned algobits, size_t hashbits) +{ + enum libsha2_algorithm algo; + + if (algobits == 32U && hashbits == 224U) { + this->algorithm = LIBHASHSUM_SHA_224; + algo = LIBSHA2_224; + } else if (algobits == 32U && hashbits == 256U) { + this->algorithm = LIBHASHSUM_SHA_256; + algo = LIBSHA2_256; + } else if (algobits == 64U && hashbits == 384U) { + this->algorithm = LIBHASHSUM_SHA_384; + algo = LIBSHA2_384; + } else if (algobits == 64U && hashbits == 512U) { + this->algorithm = LIBHASHSUM_SHA_512; + algo = LIBSHA2_512; + } else if (algobits == 64U && hashbits == 224U) { + this->algorithm = LIBHASHSUM_SHA_512_224; + algo = LIBSHA2_512_224; + } else if (algobits == 64U && hashbits == 256U) { + this->algorithm = LIBHASHSUM_SHA_512_256; + algo = LIBSHA2_512_256; + } else { + errno = EINVAL; + return -1; + } + + this->input_block_size = (size_t)algobits << 1; + this->hash_size = hashbits >> 3; + this->hash_output = NULL; + this->supports_non_whole_bytes = 1; + this->process = &process; + this->finalise_const = &finalise_const; + this->finalise = &finalise; + libsha2_init(&this->state.sha2.s, algo); + return 0; +} diff --git a/libhashsum_init_sha_224_hasher.c b/libhashsum_init_sha_224_hasher.c new file mode 100644 index 0000000..cbeb5bb --- /dev/null +++ b/libhashsum_init_sha_224_hasher.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libhashsum_init_sha_224_hasher(struct libhashsum_hasher *this) +{ + return libhashsum_init_sha2_hasher(this, 32, 224); +} diff --git a/libhashsum_init_sha_256_hasher.c b/libhashsum_init_sha_256_hasher.c new file mode 100644 index 0000000..213cbd1 --- /dev/null +++ b/libhashsum_init_sha_256_hasher.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libhashsum_init_sha_256_hasher(struct libhashsum_hasher *this) +{ + return libhashsum_init_sha2_hasher(this, 32, 256); +} diff --git a/libhashsum_init_sha_384_hasher.c b/libhashsum_init_sha_384_hasher.c new file mode 100644 index 0000000..605f181 --- /dev/null +++ b/libhashsum_init_sha_384_hasher.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libhashsum_init_sha_384_hasher(struct libhashsum_hasher *this) +{ + return libhashsum_init_sha2_hasher(this, 64, 384); +} diff --git a/libhashsum_init_sha_512_224_hasher.c b/libhashsum_init_sha_512_224_hasher.c new file mode 100644 index 0000000..b74a6ab --- /dev/null +++ b/libhashsum_init_sha_512_224_hasher.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libhashsum_init_sha_512_224_hasher(struct libhashsum_hasher *this) +{ + return libhashsum_init_sha2_hasher(this, 64, 224); +} diff --git a/libhashsum_init_sha_512_256_hasher.c b/libhashsum_init_sha_512_256_hasher.c new file mode 100644 index 0000000..22da9a8 --- /dev/null +++ b/libhashsum_init_sha_512_256_hasher.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libhashsum_init_sha_512_256_hasher(struct libhashsum_hasher *this) +{ + return libhashsum_init_sha2_hasher(this, 64, 256); +} diff --git a/libhashsum_init_sha_512_hasher.c b/libhashsum_init_sha_512_hasher.c new file mode 100644 index 0000000..a626fe7 --- /dev/null +++ b/libhashsum_init_sha_512_hasher.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libhashsum_init_sha_512_hasher(struct libhashsum_hasher *this) +{ + return libhashsum_init_sha2_hasher(this, 64, 512); +} |