aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-30 19:58:07 +0200
committerMattias Andrée <maandree@kth.se>2024-08-30 19:58:07 +0200
commit2c15891937c6fb9f3b5e70ae0f0d0d906e4c03ed (patch)
treed3d3cc3784207210fc7c63cca267257b20153b67
parentm + add man pages (diff)
downloadlibhashsum-2c15891937c6fb9f3b5e70ae0f0d0d906e4c03ed.tar.gz
libhashsum-2c15891937c6fb9f3b5e70ae0f0d0d906e4c03ed.tar.bz2
libhashsum-2c15891937c6fb9f3b5e70ae0f0d0d906e4c03ed.tar.xz
Add more tests + m fixes
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--Makefile3
-rw-r--r--common.h1
-rw-r--r--libhashsum.h12
-rw-r--r--libhashsum_get_algorithm_from_string.38
-rw-r--r--libhashsum_get_algorithm_from_string.c4
-rw-r--r--libhashsum_init_hasher_from_string.c28
-rw-r--r--misc-tests.c671
7 files changed, 708 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 8845858..28861b8 100644
--- a/Makefile
+++ b/Makefile
@@ -109,7 +109,8 @@ TEST =\
blake224.t\
blake256.t\
blake384.t\
- blake512.t
+ blake512.t\
+ misc-tests.t
LOBJ = $(OBJ:.o=.lo)
TOBJ = $(TEST:.t=.o)
diff --git a/common.h b/common.h
index 4c40a08..bd273be 100644
--- a/common.h
+++ b/common.h
@@ -229,7 +229,6 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz
#ifdef TEST_UNSUPPORTED
-# include <stdio.h>
# define TEST_MAIN(NAME, ID)\
struct libhashsum_hasher hasher;\
diff --git a/libhashsum.h b/libhashsum.h
index d1a9f64..b10f2f4 100644
--- a/libhashsum.h
+++ b/libhashsum.h
@@ -478,7 +478,7 @@ struct libhashsum_hasher {
* @since 1.0
*/
LIBHASHSUM_1_NONNULL_
-int libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algorithm algorithm); /* TODO test */
+int libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algorithm algorithm);
/**
* Inspect a hashing algorithm string to identify
@@ -492,7 +492,7 @@ int libhashsum_init_hasher(struct libhashsum_hasher *this, enum libhashsum_algor
* @since 1.0
*/
LIBHASHSUM_NONNULL_
-int libhashsum_get_algorithm_from_string(enum libhashsum_algorithm *algorithm_out, const char *algorithm); /* TODO test */
+int libhashsum_get_algorithm_from_string(enum libhashsum_algorithm *algorithm_out, const char *algorithm);
/**
* Create an initialised state for a hash algorithm
@@ -516,7 +516,7 @@ int libhashsum_get_algorithm_from_string(enum libhashsum_algorithm *algorithm_ou
* @since 1.0
*/
LIBHASHSUM_NONNULL_
-int libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm); /* TODO test */
+int libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm);
/**
* Create an initialised state for MD2
@@ -1097,7 +1097,7 @@ int libhashsum_init_blake256_hasher(struct libhashsum_hasher *this, const void *
* @since 1.0
*/
LIBHASHSUM_1_NONNULL_
-int libhashsum_init_blakes_hasher(struct libhashsum_hasher *this, size_t hashbits, const void *salt); /* TODO test */
+int libhashsum_init_blakes_hasher(struct libhashsum_hasher *this, size_t hashbits, const void *salt);
/**
* Create an initialised state for BLAKE384 (BLAKE, BLAKEb)
@@ -1144,7 +1144,7 @@ int libhashsum_init_blake512_hasher(struct libhashsum_hasher *this, const void *
* @since 1.0
*/
LIBHASHSUM_1_NONNULL_
-int libhashsum_init_blakeb_hasher(struct libhashsum_hasher *this, size_t hashbits, const void *salt); /* TODO test */
+int libhashsum_init_blakeb_hasher(struct libhashsum_hasher *this, size_t hashbits, const void *salt);
/**
* Create an initialised state for BLAKE
@@ -1166,7 +1166,7 @@ int libhashsum_init_blakeb_hasher(struct libhashsum_hasher *this, size_t hashbit
* @since 1.0
*/
LIBHASHSUM_1_NONNULL_
-int libhashsum_init_blake_hasher(struct libhashsum_hasher *this, size_t hashbits, const void *salt, size_t saltbytes); /* TODO test */
+int libhashsum_init_blake_hasher(struct libhashsum_hasher *this, size_t hashbits, const void *salt, size_t saltbytes);
#endif
diff --git a/libhashsum_get_algorithm_from_string.3 b/libhashsum_get_algorithm_from_string.3
index d7f338f..c039da6 100644
--- a/libhashsum_get_algorithm_from_string.3
+++ b/libhashsum_get_algorithm_from_string.3
@@ -315,7 +315,13 @@ The function
validate the parameters for the hash function
that may be places inside square brackets at
the end of
-.IR algorithm .
+.IR algorithm ,
+now will it check anything in
+.I algorithm
+after the opening bracket,
+however it will not recognise the hash function
+if brackets are used but the hash function does
+not have any parameters.
.PP
.I algorithm_out
and
diff --git a/libhashsum_get_algorithm_from_string.c b/libhashsum_get_algorithm_from_string.c
index 96892e1..9740510 100644
--- a/libhashsum_get_algorithm_from_string.c
+++ b/libhashsum_get_algorithm_from_string.c
@@ -20,7 +20,9 @@ equiv(const char *a, const char *b)
}
if (!strncasecmp(a, "sum", 3))
a = &a[3];
- return !*a && (!*b || *b == '[');
+ if (*b == '[')
+ return *a == '[' || !*a;
+ return !*a && !*b;
}
diff --git a/libhashsum_init_hasher_from_string.c b/libhashsum_init_hasher_from_string.c
index c4bf441..a1ee827 100644
--- a/libhashsum_init_hasher_from_string.c
+++ b/libhashsum_init_hasher_from_string.c
@@ -12,8 +12,9 @@ with_n(int (*initfunc)(struct libhashsum_hasher *, size_t), struct libhashsum_ha
if (!p || *++p == ']')
return initfunc(this, 0);
- if (*p++ != 'n' && *p++ != 'N')
+ if (*p != 'n' && *p != 'N')
goto einval;
+ p++;
if (*p++ != '=' || !('1' <= *p && *p <= '9'))
goto einval;
@@ -62,8 +63,11 @@ with_rcnz(int (*initfunc)(struct libhashsum_hasher *, size_t, size_t, size_t, si
size_t r = 0, c = 0, n = 0, z = 0;
p = strchr(algorithm, '[');
- if (!p || *++p == ']')
+ if (!p || *++p == ']') {
+ if (p && *p)
+ goto einval;
return initfunc(this, 0, 0, 0, 0);
+ }
for (;;) {
if (*p == 'r' || *p == 'R') {
@@ -111,16 +115,21 @@ with_salt(int (*initfunc)(struct libhashsum_hasher *, const void *),
if (!p || *++p == ']')
return initfunc(this, NULL);
- if (*p++ != 's' && *p++ != 'S')
+ if (*p != 's' && *p != 'S')
goto einval;
- if (*p++ != 'a' && *p++ != 'A')
+ p++;
+ if (*p != 'a' && *p != 'A')
goto einval;
- if (*p++ != 'l' && *p++ != 'L')
+ p++;
+ if (*p != 'l' && *p != 'L')
goto einval;
- if (*p++ != 't' && *p++ != 'T')
+ p++;
+ if (*p != 't' && *p != 'T')
goto einval;
- if (*p++ != '=')
+ p++;
+ if (*p != '=')
goto einval;
+ p++;
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
p = &p[2];
@@ -138,9 +147,10 @@ with_salt(int (*initfunc)(struct libhashsum_hasher *, const void *),
break;
}
- a = (uint8_t)(((*p & 15) + (*p > '9' ? 9 : 0)) << 4);
- b = (uint8_t)(((*p & 15) + (*p > '9' ? 9 : 0)) << 0);
+ a = (uint8_t)(((p[0] & 15) + (p[0] > '9' ? 9 : 0)) << 4);
+ b = (uint8_t)(((p[1] & 15) + (p[1] > '9' ? 9 : 0)) << 0);
salt[salti++] = (uint8_t)(a | b);
+ p = &p[2];
}
if (*p++ != ']' || *p)
diff --git a/misc-tests.c b/misc-tests.c
new file mode 100644
index 0000000..0144916
--- /dev/null
+++ b/misc-tests.c
@@ -0,0 +1,671 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+#define ASSERT(ASSERTION)\
+ do {\
+ if ((ASSERTION))\
+ break;\
+ fprintf(stderr, "assertion `%s` at line %i failed\n", #ASSERTION, __LINE__);\
+ exit(1);\
+ } while (0)
+
+
+static int
+eq(struct libhashsum_hasher *a, struct libhashsum_hasher *b)
+{
+ if (a->algorithm != b->algorithm ||
+ !a->algorithm_string != !b->algorithm_string ||
+ a->input_block_size != b->input_block_size ||
+ a->hash_size != b->hash_size ||
+ !a->hash_output != !b->hash_output ||
+ a->supports_non_whole_bytes != b->supports_non_whole_bytes ||
+ a->process != b->process ||
+ a->finalise_const != b->finalise_const ||
+ a->finalise != b->finalise ||
+ a->destroy != b->destroy)
+ return 0;
+ if (a->hash_output && memcpy(a->hash_output, b->hash_output, a->hash_size))
+ return 0;
+ if (a->algorithm_string && strcmp(a->algorithm_string, b->algorithm_string))
+ return 0;
+ switch (a->algorithm) {
+ case LIBHASHSUM_MD2:
+ case LIBHASHSUM_MD4:
+ case LIBHASHSUM_MD5:
+ case LIBHASHSUM_RIPEMD_128:
+ case LIBHASHSUM_RIPEMD_160:
+ case LIBHASHSUM_RIPEMD_256:
+ case LIBHASHSUM_RIPEMD_320:
+ case LIBHASHSUM_SHA0:
+ case LIBHASHSUM_SHA1:
+ case LIBHASHSUM_SHA_224:
+ case LIBHASHSUM_SHA_256:
+ case LIBHASHSUM_SHA_384:
+ case LIBHASHSUM_SHA_512:
+ case LIBHASHSUM_SHA_512_224:
+ case LIBHASHSUM_SHA_512_256:
+ case LIBHASHSUM_BLAKE224:
+ case LIBHASHSUM_BLAKE256:
+ case LIBHASHSUM_BLAKE384:
+ case LIBHASHSUM_BLAKE512:
+ if (memcmp(&a->state, &b->state, sizeof(a->state)))
+ return 0;
+ break;
+ case LIBHASHSUM_KECCAK:
+ case LIBHASHSUM_KECCAK_224:
+ case LIBHASHSUM_KECCAK_256:
+ case LIBHASHSUM_KECCAK_384:
+ case LIBHASHSUM_KECCAK_512:
+ case LIBHASHSUM_SHA3_224:
+ case LIBHASHSUM_SHA3_256:
+ case LIBHASHSUM_SHA3_384:
+ case LIBHASHSUM_SHA3_512:
+ case LIBHASHSUM_SHAKE128:
+ case LIBHASHSUM_SHAKE256:
+ case LIBHASHSUM_SHAKE512:
+ case LIBHASHSUM_RAWSHAKE128:
+ case LIBHASHSUM_RAWSHAKE256:
+ case LIBHASHSUM_RAWSHAKE512:
+#ifdef LIBHASHSUM_INCLUDE_LIBKECCAK_STATE
+ {
+ unsigned char *aM, *bM;
+ int r;
+ if (a->state.keccak.squeezes != b->state.keccak.squeezes ||
+ a->state.keccak.suffix != b->state.keccak.suffix ||
+ !a->state.keccak.s.M != !b->state.keccak.s.M)
+ return 0;
+ aM = a->state.keccak.s.M;
+ bM = b->state.keccak.s.M;
+ a->state.keccak.s.M = NULL;
+ b->state.keccak.s.M = NULL;
+ r = memcmp(&a->state.keccak.s, &b->state.keccak.s, sizeof(a->state.keccak.s));
+ a->state.keccak.s.M = aM;
+ b->state.keccak.s.M = bM;
+ if (r)
+ return 0;
+ }
+ break;
+#endif
+ default:
+ abort();
+ }
+ return 1;
+}
+
+
+static void
+test_blake(const char *salt)
+{
+ struct libhashsum_hasher hasher1, hasher2, hasher3;
+ int r;
+
+ memset(&hasher1, 0xBC, sizeof(hasher1));
+ memset(&hasher2, 0xBC, sizeof(hasher2));
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ errno = 0;
+ r = libhashsum_init_blake224_hasher(&hasher1, salt);
+ if (r) {
+ ASSERT(r == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 224, salt) == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 16) == -1);
+ ASSERT(errno == ENOSYS);
+ } else {
+ ASSERT(r == 0);
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 224, salt) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 16) == 0);
+ ASSERT(eq(&hasher1, &hasher2));
+ ASSERT(eq(&hasher1, &hasher3));
+ if (salt) {
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 0) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 8) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 17) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 32) == -1);
+ ASSERT(errno == EINVAL);
+ } else {
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 0) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 8) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 17) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 224, salt, 32) == 0);
+ ASSERT(eq(&hasher1, &hasher3));
+ }
+ }
+
+ memset(&hasher1, 0xBC, sizeof(hasher1));
+ memset(&hasher2, 0xBC, sizeof(hasher2));
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ errno = 0;
+ r = libhashsum_init_blake256_hasher(&hasher1, salt);
+ if (r) {
+ ASSERT(r == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 256, salt) == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 16) == -1);
+ ASSERT(errno == ENOSYS);
+ } else {
+ ASSERT(r == 0);
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 256, salt) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 16) == 0);
+ ASSERT(eq(&hasher1, &hasher2));
+ ASSERT(eq(&hasher1, &hasher3));
+ if (salt) {
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 0) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 8) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 17) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 32) == -1);
+ ASSERT(errno == EINVAL);
+ } else {
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 0) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 8) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 17) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 256, salt, 32) == 0);
+ ASSERT(eq(&hasher1, &hasher3));
+ }
+ }
+
+ memset(&hasher1, 0xBC, sizeof(hasher1));
+ memset(&hasher2, 0xBC, sizeof(hasher2));
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ errno = 0;
+ r = libhashsum_init_blake384_hasher(&hasher1, salt);
+ if (r) {
+ ASSERT(r == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 384, salt) == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 32) == -1);
+ ASSERT(errno == ENOSYS);
+ } else {
+ ASSERT(r == 0);
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 384, salt) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 32) == 0);
+ ASSERT(eq(&hasher1, &hasher2));
+ ASSERT(eq(&hasher1, &hasher3));
+ if (salt) {
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 0) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 16) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 33) == -1);
+ ASSERT(errno == EINVAL);
+ } else {
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 0) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 16) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 384, salt, 33) == 0);
+ ASSERT(eq(&hasher1, &hasher3));
+ }
+ }
+
+ memset(&hasher1, 0xBC, sizeof(hasher1));
+ memset(&hasher2, 0xBC, sizeof(hasher2));
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ errno = 0;
+ r = libhashsum_init_blake512_hasher(&hasher1, salt);
+ if (r) {
+ ASSERT(r == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 512, salt) == -1);
+ ASSERT(errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 32) == -1);
+ ASSERT(errno == ENOSYS);
+ } else {
+ ASSERT(r == 0);
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 512, salt) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 32) == 0);
+ ASSERT(eq(&hasher1, &hasher2));
+ ASSERT(eq(&hasher1, &hasher3));
+ if (salt) {
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 0) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 16) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 33) == -1);
+ ASSERT(errno == EINVAL);
+ } else {
+ memset(&hasher3, 0xBC, sizeof(hasher3));
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 0) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 16) == 0);
+ ASSERT(libhashsum_init_blake_hasher(&hasher3, 512, salt, 33) == 0);
+ ASSERT(eq(&hasher1, &hasher3));
+ }
+ }
+
+#ifdef SUPPORT_BLAKES
+ errno = 0;
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 128, salt) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 384, salt) == -1);
+ ASSERT(errno == EINVAL);
+#else
+ errno = 0;
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 128, salt) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakes_hasher(&hasher2, 384, salt) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+#endif
+
+#ifdef SUPPORT_BLAKEB
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 128, salt) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 256, salt) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 1024, salt) == -1);
+ ASSERT(errno == EINVAL);
+#else
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 128, salt) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 256, salt) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blakeb_hasher(&hasher2, 1024, salt) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+#endif
+
+#ifdef SUPPORT_BLAKE
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher2, 128, salt, 16) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher2, 400, salt, 32) == -1);
+ ASSERT(errno == EINVAL);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher2, 1024, salt, 32) == -1);
+ ASSERT(errno == EINVAL);
+#else
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher2, 128, salt, 16) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher2, 400, salt, 32) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+ errno = 0;
+ ASSERT(libhashsum_init_blake_hasher(&hasher2, 1024, salt, 32) == -1);
+ ASSERT(errno == EINVAL || errno == ENOSYS);
+#endif
+}
+
+
+int
+main(void)
+{
+ struct libhashsum_hasher hasher;
+ enum libhashsum_algorithm algo;
+
+ test_blake(NULL);
+ test_blake("1234567890abcdefhijkABCDEFHIJK,.1234567890abcdefhijkABCDEFHIJK,.");
+
+ errno = 0;
+ ASSERT(libhashsum_init_hasher(&hasher, -1) == -1);
+ ASSERT(errno == EINVAL);
+
+ errno = 0;
+ ASSERT(libhashsum_init_hasher(&hasher, 999) == -1);
+ ASSERT(errno == EINVAL);
+
+ errno = 0;
+ ASSERT(libhashsum_init_hasher(&hasher, LIBHASHSUM_KECCAK) == -1);
+ ASSERT(errno == EINVAL);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "X") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD55555") == 0);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD2") == 1 && algo == LIBHASHSUM_MD2);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD4") == 1 && algo == LIBHASHSUM_MD4);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD5") == 1 && algo == LIBHASHSUM_MD5);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak") == 1 && algo == LIBHASHSUM_KECCAK);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "md2") == 1 && algo == LIBHASHSUM_MD2);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "md4") == 1 && algo == LIBHASHSUM_MD4);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "md5") == 1 && algo == LIBHASHSUM_MD5);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "keccak") == 1 && algo == LIBHASHSUM_KECCAK);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "KECCAK") == 1 && algo == LIBHASHSUM_KECCAK);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD2[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD4[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "MD5[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak[]") == 1 && algo == LIBHASHSUM_KECCAK);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-128") == 1 && algo == LIBHASHSUM_RIPEMD_128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-160") == 1 && algo == LIBHASHSUM_RIPEMD_160);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-256") == 1 && algo == LIBHASHSUM_RIPEMD_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-320") == 1 && algo == LIBHASHSUM_RIPEMD_320);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-128") == 1 && algo == LIBHASHSUM_RIPEMD_128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-160") == 1 && algo == LIBHASHSUM_RIPEMD_160);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-256") == 1 && algo == LIBHASHSUM_RIPEMD_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-320") == 1 && algo == LIBHASHSUM_RIPEMD_320);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-0") == 1 && algo == LIBHASHSUM_SHA0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-1") == 1 && algo == LIBHASHSUM_SHA1);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-224") == 1 && algo == LIBHASHSUM_SHA_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-256") == 1 && algo == LIBHASHSUM_SHA_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-384") == 1 && algo == LIBHASHSUM_SHA_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512") == 1 && algo == LIBHASHSUM_SHA_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512/224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512/256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512-224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512-256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512_224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512_256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-224") == 1 && algo == LIBHASHSUM_SHA_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-256") == 1 && algo == LIBHASHSUM_SHA_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-384") == 1 && algo == LIBHASHSUM_SHA_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512") == 1 && algo == LIBHASHSUM_SHA_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512/224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512/256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512-224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512-256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512_224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512_256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-224") == 1 && algo == LIBHASHSUM_KECCAK_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-256") == 1 && algo == LIBHASHSUM_KECCAK_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-384") == 1 && algo == LIBHASHSUM_KECCAK_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-512") == 1 && algo == LIBHASHSUM_KECCAK_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-224") == 1 && algo == LIBHASHSUM_SHA3_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-256") == 1 && algo == LIBHASHSUM_SHA3_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-384") == 1 && algo == LIBHASHSUM_SHA3_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-512") == 1 && algo == LIBHASHSUM_SHA3_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE-128") == 1 && algo == LIBHASHSUM_SHAKE128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE-256") == 1 && algo == LIBHASHSUM_SHAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE-512") == 1 && algo == LIBHASHSUM_SHAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE-128") == 1 && algo == LIBHASHSUM_RAWSHAKE128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE-256") == 1 && algo == LIBHASHSUM_RAWSHAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE-512") == 1 && algo == LIBHASHSUM_RAWSHAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-224") == 1 && algo == LIBHASHSUM_BLAKE224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-256") == 1 && algo == LIBHASHSUM_BLAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-384") == 1 && algo == LIBHASHSUM_BLAKE384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-512") == 1 && algo == LIBHASHSUM_BLAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B-224") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B-256") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B-384") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B-512") == 0);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD128") == 1 && algo == LIBHASHSUM_RIPEMD_128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD160") == 1 && algo == LIBHASHSUM_RIPEMD_160);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD256") == 1 && algo == LIBHASHSUM_RIPEMD_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD320") == 1 && algo == LIBHASHSUM_RIPEMD_320);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD128") == 1 && algo == LIBHASHSUM_RIPEMD_128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD160") == 1 && algo == LIBHASHSUM_RIPEMD_160);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD256") == 1 && algo == LIBHASHSUM_RIPEMD_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD320") == 1 && algo == LIBHASHSUM_RIPEMD_320);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA0") == 1 && algo == LIBHASHSUM_SHA0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA1") == 1 && algo == LIBHASHSUM_SHA1);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-224") == 1 && algo == LIBHASHSUM_SHA_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-256") == 1 && algo == LIBHASHSUM_SHA_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-384") == 1 && algo == LIBHASHSUM_SHA_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512") == 1 && algo == LIBHASHSUM_SHA_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512/224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512/256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512-224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512-256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512_224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA2-512_256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA224") == 1 && algo == LIBHASHSUM_SHA_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA256") == 1 && algo == LIBHASHSUM_SHA_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA384") == 1 && algo == LIBHASHSUM_SHA_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512") == 1 && algo == LIBHASHSUM_SHA_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512/224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512/256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512-224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512-256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512_224") == 1 && algo == LIBHASHSUM_SHA_512_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA512_256") == 1 && algo == LIBHASHSUM_SHA_512_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak224") == 1 && algo == LIBHASHSUM_KECCAK_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak256") == 1 && algo == LIBHASHSUM_KECCAK_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak384") == 1 && algo == LIBHASHSUM_KECCAK_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak512") == 1 && algo == LIBHASHSUM_KECCAK_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA3-224") == 1 && algo == LIBHASHSUM_SHA3_224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA3-256") == 1 && algo == LIBHASHSUM_SHA3_256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA3-384") == 1 && algo == LIBHASHSUM_SHA3_384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA3-512") == 1 && algo == LIBHASHSUM_SHA3_512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE128") == 1 && algo == LIBHASHSUM_SHAKE128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE256") == 1 && algo == LIBHASHSUM_SHAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE512") == 1 && algo == LIBHASHSUM_SHAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE128") == 1 && algo == LIBHASHSUM_RAWSHAKE128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE256") == 1 && algo == LIBHASHSUM_RAWSHAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE512") == 1 && algo == LIBHASHSUM_RAWSHAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE224") == 1 && algo == LIBHASHSUM_BLAKE224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE256") == 1 && algo == LIBHASHSUM_BLAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE384") == 1 && algo == LIBHASHSUM_BLAKE384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE512") == 1 && algo == LIBHASHSUM_BLAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B224") == 1 && algo == LIBHASHSUM_BLAKE224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B256") == 1 && algo == LIBHASHSUM_BLAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B384") == 1 && algo == LIBHASHSUM_BLAKE384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B512") == 1 && algo == LIBHASHSUM_BLAKE512);
+
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-128[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-160[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RIPEMD-320[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-128[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-160[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RMD-320[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-0[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-1[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-384[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512/224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512/256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512-224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512_224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-2-512_256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-384[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512/224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512/256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512-224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512_224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-512_256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-384[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "Keccak-512[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-224[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-256[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-384[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHA-3-512[]") == 0);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE-128[]") == 1 && algo == LIBHASHSUM_SHAKE128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE-256[]") == 1 && algo == LIBHASHSUM_SHAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "SHAKE-512[]") == 1 && algo == LIBHASHSUM_SHAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE-128[]") == 1 && algo == LIBHASHSUM_RAWSHAKE128);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE-256[]") == 1 && algo == LIBHASHSUM_RAWSHAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "RawSHAKE-512[]") == 1 && algo == LIBHASHSUM_RAWSHAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-224[]") == 1 && algo == LIBHASHSUM_BLAKE224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-256[]") == 1 && algo == LIBHASHSUM_BLAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-384[]") == 1 && algo == LIBHASHSUM_BLAKE384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "BLAKE-512[]") == 1 && algo == LIBHASHSUM_BLAKE512);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B224[]") == 1 && algo == LIBHASHSUM_BLAKE224);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B256[]") == 1 && algo == LIBHASHSUM_BLAKE256);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B384[]") == 1 && algo == LIBHASHSUM_BLAKE384);
+ ASSERT(libhashsum_get_algorithm_from_string(&algo, "B512[]") == 1 && algo == LIBHASHSUM_BLAKE512);
+
+#define CHECK(ALGO, CALL)\
+ do {\
+ struct libhashsum_hasher hasher2;\
+ int r;\
+ memset(&hasher, 0xBC, sizeof(hasher));\
+ memset(&hasher2, 0xBC, sizeof(hasher2));\
+ r = CALL;\
+ if (r) {\
+ ASSERT(r == -1);\
+ ASSERT(errno == ENOSYS);\
+ errno = 0;\
+ ASSERT(libhashsum_init_hasher_from_string(&hasher2, ALGO) == -1);\
+ ASSERT(errno == ENOSYS);\
+ break;\
+ }\
+ ASSERT(libhashsum_init_hasher_from_string(&hasher2, ALGO) == 0);\
+ ASSERT(eq(&hasher, &hasher2));\
+ if (hasher.destroy) {\
+ hasher.destroy(&hasher);\
+ hasher2.destroy(&hasher2);\
+ }\
+ } while (0)
+
+#define SALT "\x01\x23\x45\x67\x89\xAb\xcd\xef\x01\x23\x45\x67\x89\xab\xcd\xef"
+
+ CHECK("MD2", libhashsum_init_md2_hasher(&hasher));
+ CHECK("MD4", libhashsum_init_md4_hasher(&hasher));
+ CHECK("MD5", libhashsum_init_md5_hasher(&hasher));
+ CHECK("RIPEMD128", libhashsum_init_ripemd_128_hasher(&hasher));
+ CHECK("RIPEMD160", libhashsum_init_ripemd_160_hasher(&hasher));
+ CHECK("RIPEMD256", libhashsum_init_ripemd_256_hasher(&hasher));
+ CHECK("RIPEMD320", libhashsum_init_ripemd_320_hasher(&hasher));
+ CHECK("RMD128", libhashsum_init_ripemd_128_hasher(&hasher));
+ CHECK("RMD160", libhashsum_init_ripemd_160_hasher(&hasher));
+ CHECK("RMD256", libhashsum_init_ripemd_256_hasher(&hasher));
+ CHECK("RMD320", libhashsum_init_ripemd_320_hasher(&hasher));
+ CHECK("SHA0", libhashsum_init_sha0_hasher(&hasher));
+ CHECK("SHA1", libhashsum_init_sha1_hasher(&hasher));
+ CHECK("SHA2-224", libhashsum_init_sha_224_hasher(&hasher));
+ CHECK("SHA2-256", libhashsum_init_sha_256_hasher(&hasher));
+ CHECK("SHA2-384", libhashsum_init_sha_384_hasher(&hasher));
+ CHECK("SHA2-512", libhashsum_init_sha_512_hasher(&hasher));
+ CHECK("SHA2-512/224", libhashsum_init_sha_512_224_hasher(&hasher));
+ CHECK("SHA2-512/256", libhashsum_init_sha_512_256_hasher(&hasher));
+ CHECK("SHA2-512-224", libhashsum_init_sha_512_224_hasher(&hasher));
+ CHECK("SHA2-512-256", libhashsum_init_sha_512_256_hasher(&hasher));
+ CHECK("SHA2-512_224", libhashsum_init_sha_512_224_hasher(&hasher));
+ CHECK("SHA2-512_256", libhashsum_init_sha_512_256_hasher(&hasher));
+ CHECK("SHA224", libhashsum_init_sha_224_hasher(&hasher));
+ CHECK("SHA256", libhashsum_init_sha_256_hasher(&hasher));
+ CHECK("SHA384", libhashsum_init_sha_384_hasher(&hasher));
+ CHECK("SHA512", libhashsum_init_sha_512_hasher(&hasher));
+ CHECK("SHA512/224", libhashsum_init_sha_512_224_hasher(&hasher));
+ CHECK("SHA512/256", libhashsum_init_sha_512_256_hasher(&hasher));
+ CHECK("SHA512-224", libhashsum_init_sha_512_224_hasher(&hasher));
+ CHECK("SHA512-256", libhashsum_init_sha_512_256_hasher(&hasher));
+ CHECK("SHA512_224", libhashsum_init_sha_512_224_hasher(&hasher));
+ CHECK("SHA512_256", libhashsum_init_sha_512_256_hasher(&hasher));
+ CHECK("Keccak224", libhashsum_init_keccak_224_hasher(&hasher));
+ CHECK("Keccak256", libhashsum_init_keccak_256_hasher(&hasher));
+ CHECK("Keccak384", libhashsum_init_keccak_384_hasher(&hasher));
+ CHECK("Keccak512", libhashsum_init_keccak_512_hasher(&hasher));
+ CHECK("SHA3-224", libhashsum_init_sha3_224_hasher(&hasher));
+ CHECK("SHA3-256", libhashsum_init_sha3_256_hasher(&hasher));
+ CHECK("SHA3-384", libhashsum_init_sha3_384_hasher(&hasher));
+ CHECK("SHA3-512", libhashsum_init_sha3_512_hasher(&hasher));
+ CHECK("SHAKE128", libhashsum_init_shake128_hasher(&hasher, 0));
+ CHECK("SHAKE256", libhashsum_init_shake256_hasher(&hasher, 0));
+ CHECK("SHAKE512", libhashsum_init_shake512_hasher(&hasher, 0));
+ CHECK("SHAKE128[]", libhashsum_init_shake128_hasher(&hasher, 0));
+ CHECK("SHAKE256[]", libhashsum_init_shake256_hasher(&hasher, 0));
+ CHECK("SHAKE512[]", libhashsum_init_shake512_hasher(&hasher, 0));
+ CHECK("SHAKE128[n=10000]", libhashsum_init_shake128_hasher(&hasher, 10000));
+ CHECK("SHAKE256[n=10000]", libhashsum_init_shake256_hasher(&hasher, 10000));
+ CHECK("SHAKE512[N=10000]", libhashsum_init_shake512_hasher(&hasher, 10000));
+ CHECK("RawSHAKE128", libhashsum_init_rawshake128_hasher(&hasher, 0));
+ CHECK("RawSHAKE256", libhashsum_init_rawshake256_hasher(&hasher, 0));
+ CHECK("RawSHAKE512", libhashsum_init_rawshake512_hasher(&hasher, 0));
+ CHECK("RawSHAKE128[]", libhashsum_init_rawshake128_hasher(&hasher, 0));
+ CHECK("RawSHAKE256[]", libhashsum_init_rawshake256_hasher(&hasher, 0));
+ CHECK("RawSHAKE512[]", libhashsum_init_rawshake512_hasher(&hasher, 0));
+ CHECK("RawSHAKE128[n=200]", libhashsum_init_rawshake128_hasher(&hasher, 200));
+ CHECK("RawSHAKE256[N=200]", libhashsum_init_rawshake256_hasher(&hasher, 200));
+ CHECK("RawSHAKE512[n=200]", libhashsum_init_rawshake512_hasher(&hasher, 200));
+ CHECK("BLAKE224", libhashsum_init_blake224_hasher(&hasher, NULL));
+ CHECK("BLAKE256", libhashsum_init_blake256_hasher(&hasher, NULL));
+ CHECK("BLAKE384", libhashsum_init_blake384_hasher(&hasher, NULL));
+ CHECK("BLAKE512", libhashsum_init_blake512_hasher(&hasher, NULL));
+ CHECK("BLAKE224[]", libhashsum_init_blake224_hasher(&hasher, NULL));
+ CHECK("BLAKE256[]", libhashsum_init_blake256_hasher(&hasher, NULL));
+ CHECK("BLAKE384[]", libhashsum_init_blake384_hasher(&hasher, NULL));
+ CHECK("BLAKE512[]", libhashsum_init_blake512_hasher(&hasher, NULL));
+ CHECK("BLAKE224[salt=00000000000000000000000000000000]", libhashsum_init_blake224_hasher(&hasher, NULL));
+ CHECK("BLAKE224[salt=0123456789abcdef0123456789ABCDEF]", libhashsum_init_blake224_hasher(&hasher, SALT));
+ CHECK("BLAKE256[SALT=0123456789abcdef0123456789ABCDEF]", libhashsum_init_blake256_hasher(&hasher, SALT));
+ CHECK("BLAKE384[Salt=0123456789abcdef0123456789abcdef0123456789ABCDEF0123456789ABCDEF]",
+ libhashsum_init_blake384_hasher(&hasher, SALT SALT));
+ CHECK("BLAKE512[SaLt=0123456789abcdef0123456789abcdef0123456789ABCDEF0123456789ABCDEF]",
+ libhashsum_init_blake512_hasher(&hasher, SALT SALT));
+ CHECK("BLAKE224[salt=0x0123456789Abcdef0123456789abcdef]", libhashsum_init_blake224_hasher(&hasher, SALT));
+ CHECK("BLAKE256[SALT=0X0123456789aBcdef0123456789abcdef]", libhashsum_init_blake256_hasher(&hasher, SALT));
+ CHECK("BLAKE384[Salt=0x0123456789abCdef0123456789abcdef0123456789abcdef0123456789abcdef]",
+ libhashsum_init_blake384_hasher(&hasher, SALT SALT));
+ CHECK("BLAKE512[SaLt=0X0123456789abcDef0123456789abcdef0123456789abcdef0123456789abcdef]",
+ libhashsum_init_blake512_hasher(&hasher, SALT SALT));
+ CHECK("Keccak", libhashsum_init_keccak_hasher(&hasher, 0, 0, 0, 0));
+ CHECK("Keccak[r=256]", libhashsum_init_keccak_hasher(&hasher, 256, 0, 0, 0));
+ CHECK("Keccak[R=256]", libhashsum_init_keccak_hasher(&hasher, 256, 0, 0, 0));
+ CHECK("Keccak[c=256]", libhashsum_init_keccak_hasher(&hasher, 0, 256, 0, 0));
+ CHECK("Keccak[C=256]", libhashsum_init_keccak_hasher(&hasher, 0, 256, 0, 0));
+ CHECK("Keccak[n=256]", libhashsum_init_keccak_hasher(&hasher, 0, 0, 256, 0));
+ CHECK("Keccak[N=256]", libhashsum_init_keccak_hasher(&hasher, 0, 0, 256, 0));
+ CHECK("Keccak[z=256]", libhashsum_init_keccak_hasher(&hasher, 0, 0, 0, 256));
+ CHECK("Keccak[Z=256]", libhashsum_init_keccak_hasher(&hasher, 0, 0, 0, 256));
+ CHECK("Keccak[r=1200,c=400,n=100,z=60]", libhashsum_init_keccak_hasher(&hasher, 1200, 400, 100, 60));
+ CHECK("Keccak[z=60,r=1200,c=400,n=100]", libhashsum_init_keccak_hasher(&hasher, 1200, 400, 100, 60));
+
+#undef CHECK
+#undef SALT
+
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "BLAKE256[salt=]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "BLAKE256[salt=00000000]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "BLAKE256[salt=1234567890abcdef1234567890abcdef"
+ "1234567890abcdef1234567890abcdef]"));
+ ASSERT(errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "SHAKE128[n]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "SHAKE128[n=]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "SHAKE128[n=0]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "SHAKE128[n=1000,]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "SHAKE128[,n=1000]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "X") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[]sum") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[n=256]sum") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[r=0]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[c=0]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[n=0]") == -1 && errno == EINVAL);
+ ASSERT(libhashsum_init_hasher_from_string(&hasher, "Keccak[z=0]") == -1 && errno == EINVAL);
+
+ return 0;
+}