From cfc297ec764e3996548dee1e3e961e4dc47b6981 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 5 Sep 2024 18:17:36 +0200 Subject: Add standard_partial_byte_input_encoding and standard_partial_byte_output_encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libhashsum.h | 82 +++++++++++++++++++++++++++++++++++++ libhashsum_init_blake224_hasher.c | 2 + libhashsum_init_blake256_hasher.c | 2 + libhashsum_init_blake2b_hasher.c | 2 + libhashsum_init_blake2s_hasher.c | 2 + libhashsum_init_blake384_hasher.c | 2 + libhashsum_init_blake512_hasher.c | 2 + libhashsum_init_keccak__.c | 2 + libhashsum_init_md2_hasher.c | 2 + libhashsum_init_md4_hasher.c | 2 + libhashsum_init_md5_hasher.c | 2 + libhashsum_init_ripemd_128_hasher.c | 2 + libhashsum_init_ripemd_160_hasher.c | 2 + libhashsum_init_ripemd_256_hasher.c | 2 + libhashsum_init_ripemd_320_hasher.c | 2 + libhashsum_init_sha0_hasher.c | 2 + libhashsum_init_sha1_hasher.c | 2 + libhashsum_init_sha2_hasher.c | 2 + 18 files changed, 116 insertions(+) diff --git a/libhashsum.h b/libhashsum.h index 297c62c..cf13a2e 100644 --- a/libhashsum.h +++ b/libhashsum.h @@ -324,6 +324,19 @@ enum libhashsum_algorithm { }; +/** + * Which bits in non-whole octets are used + * + * @since 1.0 + */ +enum { + /* since 1.0 */ + LIBHASHSUM_UNSUPPORTED = 0, /**< Non-whole octets are unsupport */ + LIBHASHSUM_LEAST_SIGNIFICANT = 1, /**< The least significant bits of a non-whole octet are used */ + LIBHASHSUM_MOST_SIGNIFICANT = 2 /**< The most significant bits of a non-whole octet are used */ +}; + + /** * Message hash functions and state * @@ -383,6 +396,75 @@ struct libhashsum_hasher { */ unsigned char supports_non_whole_bytes; + /** + * libhashsum always uses the least significant bits of + * a non-whole octet, however every hash function that + * supports non-whole input octets specify their own standard + * for how non-whole octet are encoded. This field will be + * set to specify the standard used for the hash function. + * + * If this field is set to `LIBHASHSUM_UNSUPPORTED`, + * `.supports_non_whole_bytes` will be 0, and non-whole + * input bytes are unsupported. + * + * If this field is set to `LIBHASHSUM_LEAST_SIGNIFICANT`, + * `.supports_non_whole_bytes` will be 1, and the standard + * used by the hash function is the same as in libhashsum + * (the least significant bits are used), and no conversion + * between the hash function's standard and libhashsum's + * standard is required for input messages. + * + * If this field is set to `LIBHASHSUM_MOST_SIGNIFICANT`, + * `.supports_non_whole_bytes` will be 1, and the standard + * used by the hash function is the opposite of libhashsum's + * standard, thus if you have reference input message with + * a partial final byte (where the most significant bits are + * used, and the least significant bits are discarded), you + * have to reverse the bits in the last octet before + * inputing it to `*.finalise` or `*.finalise_const`. + * + * If you are creating your own message, you always store + * the used bits, in a non-whole octet, in the least + * significant part of the octet, when inputing it to + * `*.finalise` or `*.finalise_const`. + * + * @since 1.0 + */ + unsigned char standard_partial_byte_input_encoding; /* TODO man, test */ + + /** + * libhashsum always uses the least significant bits of + * a non-while octet, however every hash function that + * supports non-whole output octets specify their own standard + * for how non-whole octets are encoded. This field will be + * set to specify the standard used for the hash function. + * + * If this field is set to `LIBHASHSUM_UNSUPPORTED`, non-whole + * output bytees are unsupported. + * + * If this field is set to `LIBHASHSUM_LEAST_SIGNIFICANT`, + * the standard used by the hash function is the same as in + * libhashsum (the least significant bits are used), and no + * conversion between the hash function's standard and + * libhashsum's standard is required for hashes. + * + * If this field is set to `LIBHASHSUM_MOST_SIGNIFICANT`, + * the standard used by the hash function is the opposite of + * libhashsum's standard, thus if you have reference hash + * with a partial final byte (where the most significant bits + * are used, and the least significant bits are unused), you + * have to reverse the bits in the last octet before comparing + * the reference hash with the output of `*.finalise` and + * `*.finalise_const`. + * + * The text above assumes that the hash function's standard + * is to always clear unused bits; this is also always done + * by `*.finalise` and `*.finalise_const`. + * + * @since 1.0 + */ + unsigned char standard_partial_byte_output_encoding; /* TODO man, test */ + /** * Update the hash state given additional * input data diff --git a/libhashsum_init_blake224_hasher.c b/libhashsum_init_blake224_hasher.c index f4b0726..1bd9103 100644 --- a/libhashsum_init_blake224_hasher.c +++ b/libhashsum_init_blake224_hasher.c @@ -105,6 +105,8 @@ libhashsum_init_blake224_hasher(struct libhashsum_hasher *this, const void *salt this->hash_size = LIBHASHSUM_BLAKE224_HASH_SIZE; this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_blake256_hasher.c b/libhashsum_init_blake256_hasher.c index 3d00290..2dc9c8c 100644 --- a/libhashsum_init_blake256_hasher.c +++ b/libhashsum_init_blake256_hasher.c @@ -105,6 +105,8 @@ libhashsum_init_blake256_hasher(struct libhashsum_hasher *this, const void *salt this->hash_size = LIBHASHSUM_BLAKE256_HASH_SIZE; this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_blake2b_hasher.c b/libhashsum_init_blake2b_hasher.c index 06504d3..4172001 100644 --- a/libhashsum_init_blake2b_hasher.c +++ b/libhashsum_init_blake2b_hasher.c @@ -167,6 +167,8 @@ libhashsum_init_blake2b_hasher(struct libhashsum_hasher *this, size_t hashbits, this->hash_size = hashbits / 8U; this->hash_output = NULL; this->supports_non_whole_bytes = 0; + this->standard_partial_byte_input_encoding = LIBHASHSUM_UNSUPPORTED; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_blake2s_hasher.c b/libhashsum_init_blake2s_hasher.c index 689dc64..c2a2f37 100644 --- a/libhashsum_init_blake2s_hasher.c +++ b/libhashsum_init_blake2s_hasher.c @@ -167,6 +167,8 @@ libhashsum_init_blake2s_hasher(struct libhashsum_hasher *this, size_t hashbits, this->hash_size = hashbits / 8U; this->hash_output = NULL; this->supports_non_whole_bytes = 0; + this->standard_partial_byte_input_encoding = LIBHASHSUM_UNSUPPORTED; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_blake384_hasher.c b/libhashsum_init_blake384_hasher.c index a64d5ee..f1f32dc 100644 --- a/libhashsum_init_blake384_hasher.c +++ b/libhashsum_init_blake384_hasher.c @@ -105,6 +105,8 @@ libhashsum_init_blake384_hasher(struct libhashsum_hasher *this, const void *salt this->hash_size = LIBHASHSUM_BLAKE384_HASH_SIZE; this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_blake512_hasher.c b/libhashsum_init_blake512_hasher.c index 6407a7b..d9e3aca 100644 --- a/libhashsum_init_blake512_hasher.c +++ b/libhashsum_init_blake512_hasher.c @@ -105,6 +105,8 @@ libhashsum_init_blake512_hasher(struct libhashsum_hasher *this, const void *salt this->hash_size = LIBHASHSUM_BLAKE512_HASH_SIZE; this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_keccak__.c b/libhashsum_init_keccak__.c index 9d58958..6e1abf7 100644 --- a/libhashsum_init_keccak__.c +++ b/libhashsum_init_keccak__.c @@ -149,6 +149,8 @@ libhashsum_init_keccak__(struct libhashsum_hasher *this, size_t hashbits, void * this->hash_size += (size_t)!!(hashbits & 7U); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_LEAST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_LEAST_SIGNIFICANT; this->state.keccak.squeezes = squeezes; this->state.keccak.suffix = suffix; diff --git a/libhashsum_init_md2_hasher.c b/libhashsum_init_md2_hasher.c index b28a6ae..a30ede8 100644 --- a/libhashsum_init_md2_hasher.c +++ b/libhashsum_init_md2_hasher.c @@ -139,6 +139,8 @@ libhashsum_init_md2_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.md2.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 0; + this->standard_partial_byte_input_encoding = LIBHASHSUM_UNSUPPORTED; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_md4_hasher.c b/libhashsum_init_md4_hasher.c index abcc115..2b6e607 100644 --- a/libhashsum_init_md4_hasher.c +++ b/libhashsum_init_md4_hasher.c @@ -184,6 +184,8 @@ libhashsum_init_md4_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.md4.h.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_md5_hasher.c b/libhashsum_init_md5_hasher.c index 7eec6ed..00cb81a 100644 --- a/libhashsum_init_md5_hasher.c +++ b/libhashsum_init_md5_hasher.c @@ -199,6 +199,8 @@ libhashsum_init_md5_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.md5.h.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_ripemd_128_hasher.c b/libhashsum_init_ripemd_128_hasher.c index 02ac338..2eb08da 100644 --- a/libhashsum_init_ripemd_128_hasher.c +++ b/libhashsum_init_ripemd_128_hasher.c @@ -220,6 +220,8 @@ libhashsum_init_ripemd_128_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.ripemd_128.h.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_ripemd_160_hasher.c b/libhashsum_init_ripemd_160_hasher.c index 2e510fa..86d3340 100644 --- a/libhashsum_init_ripemd_160_hasher.c +++ b/libhashsum_init_ripemd_160_hasher.c @@ -223,6 +223,8 @@ libhashsum_init_ripemd_160_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.ripemd_160.h.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_ripemd_256_hasher.c b/libhashsum_init_ripemd_256_hasher.c index d9249b4..316cc39 100644 --- a/libhashsum_init_ripemd_256_hasher.c +++ b/libhashsum_init_ripemd_256_hasher.c @@ -231,6 +231,8 @@ libhashsum_init_ripemd_256_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.ripemd_256.h.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_ripemd_320_hasher.c b/libhashsum_init_ripemd_320_hasher.c index 80e4e23..dad69a0 100644 --- a/libhashsum_init_ripemd_320_hasher.c +++ b/libhashsum_init_ripemd_320_hasher.c @@ -237,6 +237,8 @@ libhashsum_init_ripemd_320_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.ripemd_320.h.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_MOST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; diff --git a/libhashsum_init_sha0_hasher.c b/libhashsum_init_sha0_hasher.c index b7d42a8..a860f77 100644 --- a/libhashsum_init_sha0_hasher.c +++ b/libhashsum_init_sha0_hasher.c @@ -65,6 +65,8 @@ libhashsum_init_sha0_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.sha0.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_LEAST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; 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 13020e2..bd514b3 100644 --- a/libhashsum_init_sha1_hasher.c +++ b/libhashsum_init_sha1_hasher.c @@ -65,6 +65,8 @@ libhashsum_init_sha1_hasher(struct libhashsum_hasher *this) this->hash_size = sizeof(this->state.sha1.sum); this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_LEAST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; 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 index 7e5aa38..ca455c3 100644 --- a/libhashsum_init_sha2_hasher.c +++ b/libhashsum_init_sha2_hasher.c @@ -94,6 +94,8 @@ libhashsum_init_sha2_hasher(struct libhashsum_hasher *this, unsigned algobits, s this->hash_size = hashbits >> 3; this->hash_output = NULL; this->supports_non_whole_bytes = 1; + this->standard_partial_byte_input_encoding = LIBHASHSUM_LEAST_SIGNIFICANT; + this->standard_partial_byte_output_encoding = LIBHASHSUM_UNSUPPORTED; this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; -- cgit v1.2.3-70-g09d2