diff options
Diffstat (limited to '')
| -rw-r--r-- | Makefile | 12 | ||||
| -rw-r--r-- | common.h | 1 | ||||
| -rw-r--r-- | config.mk | 2 | ||||
| -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 | ||||
| -rw-r--r-- | sha0.c | 15 | ||||
| -rw-r--r-- | sha1.c | 27 | 
16 files changed, 260 insertions, 25 deletions
| @@ -24,7 +24,9 @@ OBJ =\  	libhashsum_init_ripemd_128_hasher.o\  	libhashsum_init_ripemd_160_hasher.o\  	libhashsum_init_ripemd_256_hasher.o\ -	libhashsum_init_ripemd_320_hasher.o +	libhashsum_init_ripemd_320_hasher.o\ +	libhashsum_init_sha0_hasher.o\ +	libhashsum_init_sha1_hasher.o  HDR =\  	libhashsum.h\ @@ -37,7 +39,9 @@ TEST =\  	ripemd-128.t\  	ripemd-160.t\  	ripemd-256.t\ -	ripemd-320.t +	ripemd-320.t\ +	sha0.t\ +	sha1.t  LOBJ = $(OBJ:.o=.lo)  TOBJ = $(TEST:.t=.o) @@ -56,10 +60,10 @@ $(TEST): libhashsum.a  	$(CC) -fPIC -c -o $@ $< $(CFLAGS) $(CPPFLAGS)  .c.t: -	$(CC) -o $@ $< libhashsum.a $(CFLAGS) $(CPPFLAGS) +	$(CC) -o $@ $< libhashsum.a $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)  .o.t: -	$(CC) -o $@ $< libhashsum.a $(CFLAGS) $(CPPFLAGS) +	$(CC) -o $@ $< libhashsum.a $(LDFLAGS)  libhashsum.a: $(OBJ)  	@rm -f -- $@ @@ -1,6 +1,7 @@  /* See LICENSE file for copyright and license details. */  #include "libhashsum.h"  #include <errno.h> +#include <limits.h>  #include <string.h>  #ifdef TEST @@ -5,4 +5,4 @@ CC = c99  CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE  CFLAGS   = -LDFLAGS  = +LDFLAGS  = -lsha1 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; +} @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ +#define TEST +#include "common.h" + + +static struct testcase testcases[] = { +	{1, 0, "abc", "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880"} +}; + + +int +main(void) +{ +	TEST_MAIN("SHA0", SHA0); +} @@ -0,0 +1,27 @@ +/* See LICENSE file for copyright and license details. */ +#define TEST +#include "common.h" + + +static struct testcase testcases[] = { +	{1, 0, "\xff", "85e53271e14006f0265921d02d4d736cdc580b0b"}, +	{1, 0, "\xe5\xe0\x99\x24", "d1dffbc8a175dd8eebe0da87b1792b6dc1018e82"}, +	{56UL, 8, "\0", "9438e360f578e12c0e0e8ed28e2c125c1cefee16"}, +	{1000UL, 0, "Q", "49f1cfe3829963158e2b2b2cb5df086cee2e3bb0"}, +	{1000UL, 0, "A", "3ae3644d6777a1f56a1defeabc74af9c4b313e49"}, +	{1005UL, 0, "\x99", "18685d56c8bf67c3cee4443e9a78f65c30752f5d"}, +	{1, 0, "abc", "a9993e364706816aba3e25717850c26c9cd0d89d"}, +	{1, 0, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", +	 "84983e441c3bd26ebaae4aa1f95129e5e54670f1"}, +	{1000000UL, 8, "\0", "bef3595266a65a2ff36b700a75e8ed95c68210b6"}, +	{0x20000000UL, 0, "A", "df3f26fce8fa7bec2c61d0506749a320ac7dc942"}, +	{0x41000000UL, 8, "\0", "320c617b0b6ee1b6f9c3271eae135f40cae22c10"}, +	{0x6000003FUL, 0, "\x84", "b20aa99b62e6a480fd93b4d24b2c19ffac649bb8"} +}; + + +int +main(void) +{ +	TEST_MAIN("SHA1", SHA1); +} | 
