diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-23 22:36:41 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-23 22:40:29 +0200 |
commit | 2fd94aae29d53d9859bfc3f53777970def38e253 (patch) | |
tree | e45573486f44271d75ee22284953e5dcde303ca5 /libhashsum | |
parent | First commit (diff) | |
download | libhashsum-2fd94aae29d53d9859bfc3f53777970def38e253.tar.gz libhashsum-2fd94aae29d53d9859bfc3f53777970def38e253.tar.bz2 libhashsum-2fd94aae29d53d9859bfc3f53777970def38e253.tar.xz |
Fixes + add SHA0 and SHA1 using libsha1
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libhashsum.h | 72 | ||||
-rw-r--r-- | libhashsum_init_hasher.c | 4 | ||||
-rw-r--r-- | libhashsum_init_md2_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_md4_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_md5_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_ripemd_128_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_ripemd_160_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_ripemd_256_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_ripemd_320_hasher.c | 2 | ||||
-rw-r--r-- | libhashsum_init_sha0_hasher.c | 69 | ||||
-rw-r--r-- | libhashsum_init_sha1_hasher.c | 69 |
11 files changed, 208 insertions, 20 deletions
diff --git a/libhashsum.h b/libhashsum.h index 460b86b..600a2c0 100644 --- a/libhashsum.h +++ b/libhashsum.h @@ -5,6 +5,8 @@ #include <stddef.h> #include <stdint.h> +#include <libsha1.h> + #if defined(__GNUC__) # define LIBHASHSUM_USERET_ __attribute__((__warn_unused_result__)) @@ -25,7 +27,9 @@ enum libhashsum_algorithm { LIBHASHSUM_RIPEMD_128, /**< RIPEMD-128 */ LIBHASHSUM_RIPEMD_160, /**< RIPEMD-160 */ LIBHASHSUM_RIPEMD_256, /**< RIPEMD-256 */ - LIBHASHSUM_RIPEMD_320 /**< RIPEMD-320 */ + LIBHASHSUM_RIPEMD_320, /**< RIPEMD-320 */ + LIBHASHSUM_SHA0, /**< SHA0 */ + LIBHASHSUM_SHA1 /**< SHA1 */ }; @@ -64,6 +68,16 @@ enum libhashsum_algorithm { */ #define LIBHASHSUM_RIPEMD_320_HASH_SIZE 40 +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA0` + */ +#define LIBHASHSUM_SHA0_HASH_SIZE 20 + +/** + * The value of `struct libhashsum_hasher.hash_size` for `LIBHASHSUM_SHA1` + */ +#define LIBHASHSUM_SHA1_HASH_SIZE 20 + /** * Hash state @@ -152,6 +166,11 @@ union libhashsum_state { uint32_t w2[5]; uint64_t count; } ripemd_320; + + struct { + struct libsha1_state s; + uint8_t sum[20]; + } sha0, sha1; }; @@ -181,7 +200,8 @@ struct libhashsum_hasher { * This will be set to `NULL` when the structure * is initialised, but will be set to a pointer * to a buffer inside `.state` once `.finalise_const` - * or `.finalise` has been called + * or `.finalise` has been called with successful + * completion */ unsigned char *hash_output; @@ -206,27 +226,29 @@ struct libhashsum_hasher { * Update the hash state given it's final * input data * - * @param this The object containing this function pointer - * @param data The new input data - * @param bytes The number of bytes available in `data` - * @return 0 on success, -1 on failure + * @param this The object containing this function pointer + * @param data The new input data, the function may rewrite it's content + * @param bytes The number of bytes available in `data` for reading + * @param extra_bits Additional bits in `data` not covered by `bytes` + * @return 0 on success, -1 on failure * * @throws EINVAL `extra_bits` is greater than 7 * @throws EINVAL `extra_bits` is non-zero but `.supports_non_whole_bytes` is 0 */ LIBHASHSUM_1_NONNULL_ - int (*finalise_const)(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes); + int (*finalise_const)(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits); /** * Update the hash state given it's final * input data * - * @param this The object containing this function pointer - * @param data The new input data, the function may rewrite it's content - * @param bytes The number of bytes available in `data` for reading - * @param size `bytes` plus any number of additional bytes available - * for the function to write additional data block padding - * @return 0 on success, -1 on failure + * @param this The object containing this function pointer + * @param data The new input data, the function may rewrite it's content + * @param bytes The number of bytes available in `data` for reading + * @param extra_bits Additional bits in `data` not covered by `bytes` + * @param size `bytes` plus any number of additional bytes available + * for the function to write additional data block padding + * @return 0 on success, -1 on failure * * @throws EINVAL `extra_bits` is greater than 7 * @throws EINVAL `extra_bits` is non-zero but `.supports_non_whole_bytes` is 0 @@ -340,5 +362,29 @@ int libhashsum_init_ripemd_256_hasher(struct libhashsum_hasher *this); 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 + * + * @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_sha0_hasher(struct libhashsum_hasher *this); + +/** + * Create an initialised state for a SHA1 + * 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_sha1_hasher(struct libhashsum_hasher *this); + #endif diff --git a/libhashsum_init_hasher.c b/libhashsum_init_hasher.c index fc39758..91689ca 100644 --- a/libhashsum_init_hasher.c +++ b/libhashsum_init_hasher.c @@ -20,6 +20,10 @@ libhashsum_init_hasher(struct libhashsum_hasher *hasher, enum libhashsum_algorit return libhashsum_init_ripemd_256_hasher(hasher); case LIBHASHSUM_RIPEMD_320: return libhashsum_init_ripemd_320_hasher(hasher); + case LIBHASHSUM_SHA0: + return libhashsum_init_sha0_hasher(hasher); + case LIBHASHSUM_SHA1: + return libhashsum_init_sha1_hasher(hasher); default: errno = EINVAL; return -1; diff --git a/libhashsum_init_md2_hasher.c b/libhashsum_init_md2_hasher.c index 9c7148a..e3e929e 100644 --- a/libhashsum_init_md2_hasher.c +++ b/libhashsum_init_md2_hasher.c @@ -95,7 +95,7 @@ finalise_common(struct libhashsum_hasher *this, unsigned char *m, size_t bytes, LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const unsigned char *m = data; size_t r; diff --git a/libhashsum_init_md4_hasher.c b/libhashsum_init_md4_hasher.c index 9bba540..c51ebf8 100644 --- a/libhashsum_init_md4_hasher.c +++ b/libhashsum_init_md4_hasher.c @@ -134,7 +134,7 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const uint8_t *m = data; size_t r; diff --git a/libhashsum_init_md5_hasher.c b/libhashsum_init_md5_hasher.c index 4dcd363..292bd1e 100644 --- a/libhashsum_init_md5_hasher.c +++ b/libhashsum_init_md5_hasher.c @@ -150,7 +150,7 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const uint8_t *m = data; size_t r; diff --git a/libhashsum_init_ripemd_128_hasher.c b/libhashsum_init_ripemd_128_hasher.c index 71f0f29..d4e818f 100644 --- a/libhashsum_init_ripemd_128_hasher.c +++ b/libhashsum_init_ripemd_128_hasher.c @@ -170,7 +170,7 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const uint8_t *m = data; size_t r; diff --git a/libhashsum_init_ripemd_160_hasher.c b/libhashsum_init_ripemd_160_hasher.c index 8235844..476fa97 100644 --- a/libhashsum_init_ripemd_160_hasher.c +++ b/libhashsum_init_ripemd_160_hasher.c @@ -173,7 +173,7 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const uint8_t *m = data; size_t r; diff --git a/libhashsum_init_ripemd_256_hasher.c b/libhashsum_init_ripemd_256_hasher.c index 6491192..b1e9241 100644 --- a/libhashsum_init_ripemd_256_hasher.c +++ b/libhashsum_init_ripemd_256_hasher.c @@ -181,7 +181,7 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const uint8_t *m = data; size_t r; diff --git a/libhashsum_init_ripemd_320_hasher.c b/libhashsum_init_ripemd_320_hasher.c index 4690be8..d9ee4aa 100644 --- a/libhashsum_init_ripemd_320_hasher.c +++ b/libhashsum_init_ripemd_320_hasher.c @@ -187,7 +187,7 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign LIBHASHSUM_1_NONNULL_ static int -finalise_const(struct libhashsum_hasher *this, const void *data, unsigned extra_bits, size_t bytes) +finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits) { const uint8_t *m = data; size_t r; diff --git a/libhashsum_init_sha0_hasher.c b/libhashsum_init_sha0_hasher.c new file mode 100644 index 0000000..3ed5744 --- /dev/null +++ b/libhashsum_init_sha0_hasher.c @@ -0,0 +1,69 @@ +/* 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 & 63U; + if (bytes > SIZE_MAX >> 3) { + for (i = 0; i < 8; i++) { + libsha1_update(&this->state.sha0.s, m, bytes); + m = &m[bytes >> 3]; + } + } else if (bytes) { + libsha1_update(&this->state.sha0.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; + + libsha1_digest(&this->state.sha0.s, data, (bytes << 3) | extra_bits, this->state.sha0.sum); + memset(&this->state.sha0.s, 0, sizeof(this->state.sha0.s)); + this->hash_output = this->state.sha0.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_sha0_hasher(struct libhashsum_hasher *this) +{ + this->algorithm = LIBHASHSUM_SHA0; + this->input_block_size = 64U; + this->hash_size = sizeof(this->state.sha0.sum); + this->hash_output = NULL; + this->supports_non_whole_bytes = 0; + this->process = &process; + this->finalise_const = &finalise_const; + this->finalise = &finalise; + libsha1_init(&this->state.sha0.s, LIBSHA1_0); + return 0; +} diff --git a/libhashsum_init_sha1_hasher.c b/libhashsum_init_sha1_hasher.c new file mode 100644 index 0000000..6058b73 --- /dev/null +++ b/libhashsum_init_sha1_hasher.c @@ -0,0 +1,69 @@ +/* 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 & 63U; + if (bytes > SIZE_MAX >> 3) { + for (i = 0; i < 8; i++) { + libsha1_update(&this->state.sha1.s, m, bytes); + m = &m[bytes >> 3]; + } + } else if (bytes) { + libsha1_update(&this->state.sha1.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; + + libsha1_digest(&this->state.sha1.s, data, (bytes << 3) | extra_bits, this->state.sha1.sum); + memset(&this->state.sha1.s, 0, sizeof(this->state.sha1.s)); + this->hash_output = this->state.sha1.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_sha1_hasher(struct libhashsum_hasher *this) +{ + this->algorithm = LIBHASHSUM_SHA1; + this->input_block_size = 64U; + this->hash_size = sizeof(this->state.sha1.sum); + this->hash_output = NULL; + this->supports_non_whole_bytes = 0; + this->process = &process; + this->finalise_const = &finalise_const; + this->finalise = &finalise; + libsha1_init(&this->state.sha1.s, LIBSHA1_1); + return 0; +} |