diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-27 21:02:25 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-27 21:02:25 +0200 |
commit | 31d9446dc092f1787a19734ae9c30286a9fdba2a (patch) | |
tree | 52f32de3f10992a7f7e079f3b0b517272c349151 /libhashsum | |
parent | m fix (diff) | |
download | libhashsum-31d9446dc092f1787a19734ae9c30286a9fdba2a.tar.gz libhashsum-31d9446dc092f1787a19734ae9c30286a9fdba2a.tar.bz2 libhashsum-31d9446dc092f1787a19734ae9c30286a9fdba2a.tar.xz |
m misc
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libhashsum.h | 29 | ||||
-rw-r--r-- | libhashsum_get_algorithm_string.c | 101 | ||||
-rw-r--r-- | libhashsum_init_hasher_from_string.c | 134 |
3 files changed, 171 insertions, 93 deletions
diff --git a/libhashsum.h b/libhashsum.h index 7f828c0..29a8f6f 100644 --- a/libhashsum.h +++ b/libhashsum.h @@ -61,7 +61,7 @@ enum libhashsum_algorithm { LIBHASHSUM_SHA3_256, /**< SHA3-256 */ LIBHASHSUM_SHA3_384, /**< SHA3-384 */ LIBHASHSUM_SHA3_512, /**< SHA3-512 */ - LIBHASHSUM_SHAKE128 , /**< SHAKE128 */ + LIBHASHSUM_SHAKE128, /**< SHAKE128 */ LIBHASHSUM_SHAKE256, /**< SHAKE256 */ LIBHASHSUM_SHAKE512, /**< SHAKE512 */ LIBHASHSUM_RAWSHAKE128, /**< RawSHAKE128 */ @@ -306,6 +306,12 @@ enum libhashsum_algorithm { #define LIBHASHSUM_BLAKE512_HASH_SIZE 64 +#if defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpadded" +#endif + + /** * Hash state * @@ -602,6 +608,11 @@ struct libhashsum_hasher { }; +#if defined(__GNUC__) +# pragma GCC diagnostic pop +#endif + + /** * Create an initialised state for a hash algorithm * and return hash functions and details @@ -629,6 +640,20 @@ LIBHASHSUM_1_NONNULL_ int libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algorithm algorithm); /** + * Inspect a hashing algorithm string to identify + * which algorithm it references + * + * @param algorithm_out Output parameter for the hashing algorithm + * identifier (set if 1 is returned) + * @param algorithm The hashing algorithm as a string + * @return 1 if the algorithm was recognised, 0 otherwise + * + * @since 1.0 + */ +LIBHASHSUM_NONNULL_ +int libhashsum_get_algorithm_string(enum libhashsum_algorithm *algorithm_out, const char *algorithm); + +/** * Create an initialised state for a hash algorithm * and return hash functions and details * @@ -1280,7 +1305,7 @@ int libhashsum_init_blakeb_hasher(struct libhashsum_hasher *this, size_t hashbit * @param this The output parameter for the functions, details, and state * @param hashbits Hash output size in bits * @param salt `NULL` (for all zeroes) or a salt - * @parma saltbytes The number of bytes in `salt` (ignored if `salt` is `NULL`), + * @param saltbytes The number of bytes in `salt` (ignored if `salt` is `NULL`), * shall be 16 for if `hashbits` is 224 or 256, and 32 if * `hashbits` is 384 or 512 * @return 0 on success, -1 on failure diff --git a/libhashsum_get_algorithm_string.c b/libhashsum_get_algorithm_string.c new file mode 100644 index 0000000..5f910be --- /dev/null +++ b/libhashsum_get_algorithm_string.c @@ -0,0 +1,101 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +#if defined(__GNUC__) +__attribute__((__pure__)) +#endif +static int +equiv(const char *a, const char *b) +{ + while (*a && *b && *b != '[') { + if (tolower(*a) == tolower(*b) || (*b == '/' && (*a == '-' || *a == '_'))) { + a++; + b++; + } else if (*b == '-' && !isdigit(b[-1])) { + b++; + } else { + return 0; + } + } + if (!strncasecmp(a, "sum", 3)) + a = &a[3]; + return !*a && (!*b || *b == '['); +} + + +int +libhashsum_get_algorithm_string(enum libhashsum_algorithm *algorithm_out, const char *algorithm) +{ + if (equiv(algorithm, "MD2")) + *algorithm_out = LIBHASHSUM_MD2; + else if (equiv(algorithm, "MD4")) + *algorithm_out = LIBHASHSUM_MD4; + else if (equiv(algorithm, "MD5")) + *algorithm_out = LIBHASHSUM_MD5; + else if (equiv(algorithm, "RIPEMD-128") || equiv(algorithm, "RMD-128")) + *algorithm_out = LIBHASHSUM_RIPEMD_128; + else if (equiv(algorithm, "RIPEMD-160") || equiv(algorithm, "RMD-160")) + *algorithm_out = LIBHASHSUM_RIPEMD_160; + else if (equiv(algorithm, "RIPEMD-256") || equiv(algorithm, "RMD-256")) + *algorithm_out = LIBHASHSUM_RIPEMD_256; + else if (equiv(algorithm, "RIPEMD-320") || equiv(algorithm, "RMD-320")) + *algorithm_out = LIBHASHSUM_RIPEMD_320; + else if (equiv(algorithm, "SHA-0")) + *algorithm_out = LIBHASHSUM_SHA0; + else if (equiv(algorithm, "SHA-1")) + *algorithm_out = LIBHASHSUM_SHA1; + else if (equiv(algorithm, "SHA-224") || equiv(algorithm, "SHA-2-224")) + *algorithm_out = LIBHASHSUM_SHA_224; + else if (equiv(algorithm, "SHA-256") || equiv(algorithm, "SHA-2-256")) + *algorithm_out = LIBHASHSUM_SHA_256; + else if (equiv(algorithm, "SHA-384") || equiv(algorithm, "SHA-2-384")) + *algorithm_out = LIBHASHSUM_SHA_384; + else if (equiv(algorithm, "SHA-512") || equiv(algorithm, "SHA-2-512")) + *algorithm_out = LIBHASHSUM_SHA_512; + else if (equiv(algorithm, "SHA-512/224") || equiv(algorithm, "SHA-2-512/224")) + *algorithm_out = LIBHASHSUM_SHA_512_224; + else if (equiv(algorithm, "SHA-512/256") || equiv(algorithm, "SHA-2-512/256")) + *algorithm_out = LIBHASHSUM_SHA_512_256; + else if (equiv(algorithm, "Keccak-224")) + *algorithm_out = LIBHASHSUM_KECCAK_224; + else if (equiv(algorithm, "Keccak-256")) + *algorithm_out = LIBHASHSUM_KECCAK_256; + else if (equiv(algorithm, "Keccak-384")) + *algorithm_out = LIBHASHSUM_KECCAK_384; + else if (equiv(algorithm, "Keccak-512")) + *algorithm_out = LIBHASHSUM_KECCAK_512; + else if (equiv(algorithm, "SHA-3-224")) + *algorithm_out = LIBHASHSUM_SHA3_224; + else if (equiv(algorithm, "SHA-3-256")) + *algorithm_out = LIBHASHSUM_SHA3_256; + else if (equiv(algorithm, "SHA-3-384")) + *algorithm_out = LIBHASHSUM_SHA3_384; + else if (equiv(algorithm, "SHA-3-512")) + *algorithm_out = LIBHASHSUM_SHA3_512; + else if (equiv(algorithm, "SHAKE-128[]")) + *algorithm_out = LIBHASHSUM_SHAKE128; + else if (equiv(algorithm, "SHAKE-256[]")) + *algorithm_out = LIBHASHSUM_SHAKE256; + else if (equiv(algorithm, "SHAKE-512[]")) + *algorithm_out = LIBHASHSUM_SHAKE512; + else if (equiv(algorithm, "RawSHAKE-128[]")) + *algorithm_out = LIBHASHSUM_RAWSHAKE128; + else if (equiv(algorithm, "RawSHAKE-256[]")) + *algorithm_out = LIBHASHSUM_RAWSHAKE256; + else if (equiv(algorithm, "RawSHAKE-512[]")) + *algorithm_out = LIBHASHSUM_RAWSHAKE512; + else if (equiv(algorithm, "Keccak[]")) + *algorithm_out = LIBHASHSUM_KECCAK; + else if (equiv(algorithm, "BLAKE-224[]") || equiv(algorithm, "B224[]")) + *algorithm_out = LIBHASHSUM_BLAKE224; + else if (equiv(algorithm, "BLAKE-256[]") || equiv(algorithm, "B256[]")) + *algorithm_out = LIBHASHSUM_BLAKE256; + else if (equiv(algorithm, "BLAKE-384[]") || equiv(algorithm, "B384[]")) + *algorithm_out = LIBHASHSUM_BLAKE384; + else if (equiv(algorithm, "BLAKE-512[]") || equiv(algorithm, "B512[]")) + *algorithm_out = LIBHASHSUM_BLAKE512; + else + return 0; + return 1; +} diff --git a/libhashsum_init_hasher_from_string.c b/libhashsum_init_hasher_from_string.c index 1bdc70d..55fe882 100644 --- a/libhashsum_init_hasher_from_string.c +++ b/libhashsum_init_hasher_from_string.c @@ -2,26 +2,6 @@ #include "common.h" -#if defined(__GNUC__) -__attribute__((__pure__)) -#endif -static int -equiv(const char *a, const char *b) -{ - while (*a && *b && *b != '[') { - if (tolower(*a) == tolower(*b)) { - a++; - b++; - } else if (*b == '-') { - b++; - } else { - return 0; - } - } - return !*a && (!*b || *b == '['); -} - - static int with_n(int (*initfunc)(struct libhashsum_hasher *, size_t), struct libhashsum_hasher *this, const char *algorithm) { @@ -175,75 +155,47 @@ einval: int libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm) { - if (!strcasecmp(algorithm, "MD2")) - return libhashsum_init_md2_hasher(this); - if (!strcasecmp(algorithm, "MD4")) - return libhashsum_init_md4_hasher(this); - if (!strcasecmp(algorithm, "MD5")) - return libhashsum_init_md5_hasher(this); - if (equiv(algorithm, "RIPEMD-128")) - return libhashsum_init_ripemd_128_hasher(this); - if (equiv(algorithm, "RIPEMD-160")) - return libhashsum_init_ripemd_160_hasher(this); - if (equiv(algorithm, "RIPEMD-256")) - return libhashsum_init_ripemd_256_hasher(this); - if (equiv(algorithm, "RIPEMD-320")) - return libhashsum_init_ripemd_320_hasher(this); - if (equiv(algorithm, "SHA-0")) - return libhashsum_init_sha0_hasher(this); - if (equiv(algorithm, "SHA-1")) - return libhashsum_init_sha1_hasher(this); - if (equiv(algorithm, "SHA-224") || !strcasecmp(algorithm, "SHA2-224")) - return libhashsum_init_sha_224_hasher(this); - if (equiv(algorithm, "SHA-256") || !strcasecmp(algorithm, "SHA2-256")) - return libhashsum_init_sha_256_hasher(this); - if (equiv(algorithm, "SHA-384") || !strcasecmp(algorithm, "SHA2-384")) - return libhashsum_init_sha_384_hasher(this); - if (equiv(algorithm, "SHA-512") || !strcasecmp(algorithm, "SHA2-512")) - return libhashsum_init_sha_512_hasher(this); - if (equiv(algorithm, "SHA-512/224") || !strcasecmp(algorithm, "SHA2-512/224")) - return libhashsum_init_sha_512_224_hasher(this); - if (equiv(algorithm, "SHA-512/256") || !strcasecmp(algorithm, "SHA2-512/256")) - return libhashsum_init_sha_512_256_hasher(this); - if (equiv(algorithm, "Keccak-224")) - return libhashsum_init_keccak_224_hasher(this); - if (equiv(algorithm, "Keccak-256")) - return libhashsum_init_keccak_256_hasher(this); - if (equiv(algorithm, "Keccak-384")) - return libhashsum_init_keccak_384_hasher(this); - if (equiv(algorithm, "Keccak-512")) - return libhashsum_init_keccak_512_hasher(this); - if (!strcasecmp(algorithm, "SHA3-224")) - return libhashsum_init_sha3_224_hasher(this); - if (!strcasecmp(algorithm, "SHA3-256")) - return libhashsum_init_sha3_256_hasher(this); - if (!strcasecmp(algorithm, "SHA3-384")) - return libhashsum_init_sha3_384_hasher(this); - if (!strcasecmp(algorithm, "SHA3-512")) - return libhashsum_init_sha3_512_hasher(this); - if (equiv(algorithm, "SHAKE-128[")) - return with_n(&libhashsum_init_shake128_hasher, this, algorithm); - if (equiv(algorithm, "SHAKE-256[")) - return with_n(&libhashsum_init_shake256_hasher, this, algorithm); - if (equiv(algorithm, "SHAKE-512[")) - return with_n(&libhashsum_init_shake512_hasher, this, algorithm); - if (equiv(algorithm, "RawSHAKE-128[")) - return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm); - if (equiv(algorithm, "RawSHAKE-256[")) - return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm); - if (equiv(algorithm, "RawSHAKE-512[")) - return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm); - if (equiv(algorithm, "Keccak[")) - return with_rcn(&libhashsum_init_keccak_hasher, this, algorithm); - if (equiv(algorithm, "BLAKE-224[")) - return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U); - if (equiv(algorithm, "BLAKE-256[")) - return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U); - if (equiv(algorithm, "BLAKE-384[")) - return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U); - if (equiv(algorithm, "BLAKE-512[")) - return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U); - - errno = EINVAL; - return -1; + enum libhashsum_algorithm algo; + if (!libhashsum_get_algorithm_string(&algo, algorithm)) { + errno = EINVAL; + return -1; + } + switch (algo) { + case LIBHASHSUM_MD2: return libhashsum_init_md2_hasher(this); + case LIBHASHSUM_MD4: return libhashsum_init_md4_hasher(this); + case LIBHASHSUM_MD5: return libhashsum_init_md5_hasher(this); + case LIBHASHSUM_RIPEMD_128: return libhashsum_init_ripemd_128_hasher(this); + case LIBHASHSUM_RIPEMD_160: return libhashsum_init_ripemd_160_hasher(this); + case LIBHASHSUM_RIPEMD_256: return libhashsum_init_ripemd_256_hasher(this); + case LIBHASHSUM_RIPEMD_320: return libhashsum_init_ripemd_320_hasher(this); + case LIBHASHSUM_SHA0: return libhashsum_init_sha0_hasher(this); + case LIBHASHSUM_SHA1: return libhashsum_init_sha1_hasher(this); + case LIBHASHSUM_SHA_224: return libhashsum_init_sha_224_hasher(this); + case LIBHASHSUM_SHA_256: return libhashsum_init_sha_256_hasher(this); + case LIBHASHSUM_SHA_384: return libhashsum_init_sha_384_hasher(this); + case LIBHASHSUM_SHA_512: return libhashsum_init_sha_512_hasher(this); + case LIBHASHSUM_SHA_512_224: return libhashsum_init_sha_512_224_hasher(this); + case LIBHASHSUM_SHA_512_256: return libhashsum_init_sha_512_256_hasher(this); + case LIBHASHSUM_KECCAK_224: return libhashsum_init_keccak_224_hasher(this); + case LIBHASHSUM_KECCAK_256: return libhashsum_init_keccak_256_hasher(this); + case LIBHASHSUM_KECCAK_384: return libhashsum_init_keccak_384_hasher(this); + case LIBHASHSUM_KECCAK_512: return libhashsum_init_keccak_512_hasher(this); + case LIBHASHSUM_SHA3_224: return libhashsum_init_sha3_224_hasher(this); + case LIBHASHSUM_SHA3_256: return libhashsum_init_sha3_256_hasher(this); + case LIBHASHSUM_SHA3_384: return libhashsum_init_sha3_384_hasher(this); + case LIBHASHSUM_SHA3_512: return libhashsum_init_sha3_512_hasher(this); + case LIBHASHSUM_SHAKE128: return with_n(&libhashsum_init_shake128_hasher, this, algorithm); + case LIBHASHSUM_SHAKE256: return with_n(&libhashsum_init_shake256_hasher, this, algorithm); + case LIBHASHSUM_SHAKE512: return with_n(&libhashsum_init_shake512_hasher, this, algorithm); + case LIBHASHSUM_RAWSHAKE128: return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm); + case LIBHASHSUM_RAWSHAKE256: return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm); + case LIBHASHSUM_RAWSHAKE512: return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm); + case LIBHASHSUM_KECCAK: return with_rcn(&libhashsum_init_keccak_hasher, this, algorithm); + case LIBHASHSUM_BLAKE224: return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U); + case LIBHASHSUM_BLAKE256: return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U); + case LIBHASHSUM_BLAKE384: return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U); + case LIBHASHSUM_BLAKE512: return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U); + default: + abort(); + } } |