diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-24 01:05:44 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-24 01:05:44 +0200 |
commit | cf0f05362e71c189b8052bbfddd2f270f907041e (patch) | |
tree | 371ba96e6d0cdda94a5ba0f07628b1d1ea0c5960 | |
parent | Fix makefile (diff) | |
download | libhashsum-cf0f05362e71c189b8052bbfddd2f270f907041e.tar.gz libhashsum-cf0f05362e71c189b8052bbfddd2f270f907041e.tar.bz2 libhashsum-cf0f05362e71c189b8052bbfddd2f270f907041e.tar.xz |
Standardise how to interpret a partial byte
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | common.h | 6 | ||||
-rw-r--r-- | libhashsum.h | 14 | ||||
-rw-r--r-- | libhashsum_init_md4_hasher.c | 8 | ||||
-rw-r--r-- | libhashsum_init_md5_hasher.c | 5 | ||||
-rw-r--r-- | libhashsum_init_ripemd_128_hasher.c | 8 | ||||
-rw-r--r-- | libhashsum_init_ripemd_160_hasher.c | 8 | ||||
-rw-r--r-- | libhashsum_init_ripemd_256_hasher.c | 8 | ||||
-rw-r--r-- | libhashsum_init_ripemd_320_hasher.c | 8 | ||||
-rw-r--r-- | libhashsum_reverse_byte__.c | 12 |
10 files changed, 68 insertions, 12 deletions
@@ -33,7 +33,8 @@ OBJ =\ libhashsum_init_sha_512_hasher.o\ libhashsum_init_sha_512_224_hasher.o\ libhashsum_init_sha_512_256_hasher.o\ - libhashsum_init_sha2_hasher.o + libhashsum_init_sha2_hasher.o\ + libhashsum_reverse_byte__.o HDR =\ libhashsum.h\ @@ -11,6 +11,12 @@ #include <string.h> +#if defined(__GNUC__) +__attribute__((__const__)) +#endif +uint8_t libhashsum_reverse_byte__(uint8_t); + + #ifdef TEST # include <stdlib.h> # include <stdio.h> diff --git a/libhashsum.h b/libhashsum.h index 5fb71cf..0bfb48a 100644 --- a/libhashsum.h +++ b/libhashsum.h @@ -315,6 +315,13 @@ struct libhashsum_hasher { * Update the hash state given it's final * input data * + * Regardless of the algorithm's standard, the function + * will takes the lower bits from `data[bytes]`, if + * `extra_bits > 0` and use the for the additional bits; + * the least significant bit will be used as the first + * bit and the most significant bit will be used as the + * last bit + * * @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 @@ -333,6 +340,13 @@ struct libhashsum_hasher { * Update the hash state given it's final * input data * + * Regardless of the algorithm's standard, the function + * will takes the lower bits from `data[bytes]`, if + * `extra_bits > 0` and use the for the additional bits; + * the least significant bit will be used as the first + * bit and the most significant bit will be used as the + * last bit + * * @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 diff --git a/libhashsum_init_md4_hasher.c b/libhashsum_init_md4_hasher.c index c51ebf8..f3c26ac 100644 --- a/libhashsum_init_md4_hasher.c +++ b/libhashsum_init_md4_hasher.c @@ -98,10 +98,12 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign this->state.md4.count *= 8U; this->state.md4.count += (size_t)extra_bits; - memset(&m[bytes], 0, 64U - bytes); + if (extra_bits) + m[bytes] = libhashsum_reverse_byte__(m[bytes]); + memset(&m[bytes + 1U], 0, 63U - bytes); mask = (uint8_t)(1U << (7U - extra_bits)); m[bytes] |= mask; - m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits */ + m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits (original value was reversed) */ for (i = 0; i < 14; i++) this->state.md4.m.m32[i] = LETO32(&m[i * 4U]); @@ -143,6 +145,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; + this->state.md4.m.m8[bytes] = 0; memcpy(this->state.md4.m.m8, m, bytes + (size_t)(extra_bits > 0U)); return finalise_common(this, this->state.md4.m.m8, bytes, extra_bits); } @@ -161,6 +164,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (size < 64U) { + this->state.md4.m.m8[bytes] = 0; memcpy(this->state.md4.m.m8, m, bytes + (size_t)(extra_bits > 0U)); m = this->state.md4.m.m8; } diff --git a/libhashsum_init_md5_hasher.c b/libhashsum_init_md5_hasher.c index 292bd1e..7bf19af 100644 --- a/libhashsum_init_md5_hasher.c +++ b/libhashsum_init_md5_hasher.c @@ -109,9 +109,11 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign this->state.md5.count *= 8U; this->state.md5.count += (size_t)extra_bits; + if (extra_bits) + m[bytes] = libhashsum_reverse_byte__(m[bytes]); mask = (uint8_t)(1U << (7U - extra_bits)); m[bytes] |= mask; - m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits */ + m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits (original value was reversed) */ bytes++; if (bytes > 56U) { @@ -159,6 +161,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; + this->state.md5.m[bytes] = 0; memcpy(this->state.md5.m, m, bytes + (size_t)(extra_bits > 0U)); return finalise_common(this, this->state.md5.m, bytes, extra_bits); } diff --git a/libhashsum_init_ripemd_128_hasher.c b/libhashsum_init_ripemd_128_hasher.c index d4e818f..ef9f215 100644 --- a/libhashsum_init_ripemd_128_hasher.c +++ b/libhashsum_init_ripemd_128_hasher.c @@ -134,10 +134,12 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign this->state.ripemd_128.count *= 8U; this->state.ripemd_128.count += (size_t)extra_bits; - memset(&m[bytes], 0, 64U - bytes); + if (extra_bits) + m[bytes] = libhashsum_reverse_byte__(m[bytes]); + memset(&m[bytes + 1U], 0, 63U - bytes); mask = (uint8_t)(1U << (7U - extra_bits)); m[bytes] |= mask; - m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits */ + m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits (original value was reversed) */ for (i = 0; i < 14; i++) this->state.ripemd_128.m.m32[i] = LETO32(&m[i * 4U]); @@ -179,6 +181,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; + this->state.ripemd_128.m.m8[bytes] = 0; memcpy(this->state.ripemd_128.m.m8, m, bytes + (size_t)(extra_bits > 0U)); return finalise_common(this, this->state.ripemd_128.m.m8, bytes, extra_bits); } @@ -197,6 +200,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (size < 64U) { + this->state.ripemd_128.m.m8[bytes] = 0; memcpy(this->state.ripemd_128.m.m8, m, bytes + (size_t)(extra_bits > 0U)); m = this->state.ripemd_128.m.m8; } diff --git a/libhashsum_init_ripemd_160_hasher.c b/libhashsum_init_ripemd_160_hasher.c index 476fa97..001cbb8 100644 --- a/libhashsum_init_ripemd_160_hasher.c +++ b/libhashsum_init_ripemd_160_hasher.c @@ -133,10 +133,12 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign this->state.ripemd_160.count *= 8U; this->state.ripemd_160.count += (size_t)extra_bits; - memset(&m[bytes], 0, 64U - bytes); + if (extra_bits) + m[bytes] = libhashsum_reverse_byte__(m[bytes]); + memset(&m[bytes + 1U], 0, 63U - bytes); mask = (uint8_t)(1U << (7U - extra_bits)); m[bytes] |= mask; - m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits */ + m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits (original value was reversed) */ for (i = 0; i < 14; i++) this->state.ripemd_160.m.m32[i] = LETO32(&m[i * 4U]); @@ -182,6 +184,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; + this->state.ripemd_160.m.m8[bytes] = 0; memcpy(this->state.ripemd_160.m.m8, m, bytes + (size_t)(extra_bits > 0U)); return finalise_common(this, this->state.ripemd_160.m.m8, bytes, extra_bits); } @@ -200,6 +203,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (size < 64U) { + this->state.ripemd_160.m.m8[bytes] = 0; memcpy(this->state.ripemd_160.m.m8, m, bytes + (size_t)(extra_bits > 0U)); m = this->state.ripemd_160.m.m8; } diff --git a/libhashsum_init_ripemd_256_hasher.c b/libhashsum_init_ripemd_256_hasher.c index b1e9241..f08acf7 100644 --- a/libhashsum_init_ripemd_256_hasher.c +++ b/libhashsum_init_ripemd_256_hasher.c @@ -145,10 +145,12 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign this->state.ripemd_256.count *= 8U; this->state.ripemd_256.count += (size_t)extra_bits; - memset(&m[bytes], 0, 64U - bytes); + if (extra_bits) + m[bytes] = libhashsum_reverse_byte__(m[bytes]); + memset(&m[bytes + 1U], 0, 63U - bytes); mask = (uint8_t)(1U << (7U - extra_bits)); m[bytes] |= mask; - m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits */ + m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits (original value was reversed) */ for (i = 0; i < 14; i++) this->state.ripemd_256.m.m32[i] = LETO32(&m[i * 4U]); @@ -190,6 +192,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; + this->state.ripemd_256.m.m8[bytes] = 0; memcpy(this->state.ripemd_256.m.m8, m, bytes + (size_t)(extra_bits > 0U)); return finalise_common(this, this->state.ripemd_256.m.m8, bytes, extra_bits); } @@ -208,6 +211,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (size < 64U) { + this->state.ripemd_256.m.m8[bytes] = 0; memcpy(this->state.ripemd_256.m.m8, m, bytes + (size_t)(extra_bits > 0U)); m = this->state.ripemd_256.m.m8; } diff --git a/libhashsum_init_ripemd_320_hasher.c b/libhashsum_init_ripemd_320_hasher.c index d9ee4aa..2f09ce7 100644 --- a/libhashsum_init_ripemd_320_hasher.c +++ b/libhashsum_init_ripemd_320_hasher.c @@ -147,10 +147,12 @@ finalise_common(struct libhashsum_hasher *this, uint8_t *m, size_t bytes, unsign this->state.ripemd_320.count *= 8U; this->state.ripemd_320.count += (size_t)extra_bits; - memset(&m[bytes], 0, 64U - bytes); + if (extra_bits) + m[bytes] = libhashsum_reverse_byte__(m[bytes]); + memset(&m[bytes + 1U], 0, 63U - bytes); mask = (uint8_t)(1U << (7U - extra_bits)); m[bytes] |= mask; - m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits */ + m[bytes] &= (uint8_t)~(mask - 1U); /* keep high bits (original value was reversed) */ for (i = 0; i < 14; i++) this->state.ripemd_320.m.m32[i] = LETO32(&m[i * 4U]); @@ -196,6 +198,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; + this->state.ripemd_320.m.m8[bytes] = 0; memcpy(this->state.ripemd_320.m.m8, m, bytes + (size_t)(extra_bits > 0U)); return finalise_common(this, this->state.ripemd_320.m.m8, bytes, extra_bits); } @@ -214,6 +217,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (size < 64U) { + this->state.ripemd_320.m.m8[bytes] = 0; memcpy(this->state.ripemd_320.m.m8, m, bytes + (size_t)(extra_bits > 0U)); m = this->state.ripemd_320.m.m8; } diff --git a/libhashsum_reverse_byte__.c b/libhashsum_reverse_byte__.c new file mode 100644 index 0000000..c608eb0 --- /dev/null +++ b/libhashsum_reverse_byte__.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +uint8_t +libhashsum_reverse_byte__(uint8_t x) +{ + x = (uint8_t)(((x & 0xAAU) >> 1) | ((x << 1) & 0xAAU)); + x = (uint8_t)(((x & 0xCCU) >> 2) | ((x << 2) & 0xCCU)); + x = (uint8_t)(((x & 0xF0U) >> 4) | ((x << 4) & 0xF0U)); + return x; +} |