aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum.h
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-23 22:36:41 +0200
committerMattias Andrée <maandree@kth.se>2024-08-23 22:40:29 +0200
commit2fd94aae29d53d9859bfc3f53777970def38e253 (patch)
treee45573486f44271d75ee22284953e5dcde303ca5 /libhashsum.h
parentFirst commit (diff)
downloadlibhashsum-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 'libhashsum.h')
-rw-r--r--libhashsum.h72
1 files changed, 59 insertions, 13 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