aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-24 10:04:37 +0200
committerMattias Andrée <maandree@kth.se>2024-08-24 10:04:37 +0200
commitcbe3b01daad36d3ba8f3d2e4c1e0d5645aa44010 (patch)
tree128aa07e1a8e8bb2738b4b3db40d455d2151952c
parentStandardise how to interpret a partial byte (diff)
downloadlibhashsum-cbe3b01daad36d3ba8f3d2e4c1e0d5645aa44010.tar.gz
libhashsum-cbe3b01daad36d3ba8f3d2e4c1e0d5645aa44010.tar.bz2
libhashsum-cbe3b01daad36d3ba8f3d2e4c1e0d5645aa44010.tar.xz
Test partial byte support in SHA1 and SHA-224
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--common.h10
-rw-r--r--libhashsum_init_sha0_hasher.c2
-rw-r--r--libhashsum_init_sha1_hasher.c2
-rw-r--r--sha1.c9
-rw-r--r--sha_224.c9
5 files changed, 27 insertions, 5 deletions
diff --git a/common.h b/common.h
index e70d922..3d0972c 100644
--- a/common.h
+++ b/common.h
@@ -126,9 +126,11 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz
return 2;
}
for (j = 0; j < testcases[i].input_repeat; j++) {
- p = memcpy(p, testcases[i].input, input_string_len);
+ memcpy(p, testcases[i].input, input_string_len);
p = &p[input_string_len];
}
+ if (bits)
+ *p = testcases[i].input[input_string_len];
if (hasher.finalise(&hasher, input, input_total_len, (unsigned)bits, input_size)) {
perror("hasher.finalise");
return 2;
@@ -160,6 +162,12 @@ run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_siz
if (testcases[i].input_repeat == 1)
printf("[\033[1;%s\033[m] %s(\"%s\"%s) = %s\n",
caseok ? "32mPASS" : "31mFAIL", name, input, bitstr, hexsum);
+ else if (!testcases[i].input_repeat && *bitstr)
+ printf("[\033[1;%s\033[m] %s(%s) = %s\n",
+ caseok ? "32mPASS" : "31mFAIL", name, &bitstr[3], hexsum);
+ else if (!testcases[i].input_repeat)
+ printf("[\033[1;%s\033[m] %s(\"\") = %s\n",
+ caseok ? "32mPASS" : "31mFAIL", name, hexsum);
else
printf("[\033[1;%s\033[m] %s(%zu * \"%s\"%s) = %s\n",
caseok ? "32mPASS" : "31mFAIL", name, testcases[i].input_repeat, input, bitstr, hexsum);
diff --git a/libhashsum_init_sha0_hasher.c b/libhashsum_init_sha0_hasher.c
index e9c0ea4..8f5854a 100644
--- a/libhashsum_init_sha0_hasher.c
+++ b/libhashsum_init_sha0_hasher.c
@@ -37,7 +37,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u
m = &m[r];
bytes -= r;
- libsha1_digest(&this->state.sha0.s, data, (bytes << 3) | extra_bits, this->state.sha0.sum);
+ libsha1_digest(&this->state.sha0.s, m, (bytes << 3) | (size_t)extra_bits, this->state.sha0.sum);
memset(&this->state.sha0.s, 0, sizeof(this->state.sha0.s));
this->hash_output = this->state.sha0.sum;
return 0;
diff --git a/libhashsum_init_sha1_hasher.c b/libhashsum_init_sha1_hasher.c
index 25c7496..8287429 100644
--- a/libhashsum_init_sha1_hasher.c
+++ b/libhashsum_init_sha1_hasher.c
@@ -37,7 +37,7 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u
m = &m[r];
bytes -= r;
- libsha1_digest(&this->state.sha1.s, data, (bytes << 3) | extra_bits, this->state.sha1.sum);
+ libsha1_digest(&this->state.sha1.s, m, (bytes << 3) | (size_t)extra_bits, this->state.sha1.sum);
memset(&this->state.sha1.s, 0, sizeof(this->state.sha1.s));
this->hash_output = this->state.sha1.sum;
return 0;
diff --git a/sha1.c b/sha1.c
index 3a465e8..faeea45 100644
--- a/sha1.c
+++ b/sha1.c
@@ -19,7 +19,14 @@ static struct testcase testcases[] = {
{0x41000000UL, 8, "\0", "320c617b0b6ee1b6f9c3271eae135f40cae22c10"},
{0x6000003FUL, 0, "\x84", "b20aa99b62e6a480fd93b4d24b2c19ffac649bb8"},
#endif
- {1000000UL, 8, "\0", "bef3595266a65a2ff36b700a75e8ed95c68210b6"}
+ {1000000UL, 8, "\0", "bef3595266a65a2ff36b700a75e8ed95c68210b6"},
+ {0, 1, "\x00", "bb6b3e18f0115b57925241676f5b1ae88747b08a"},
+ {0, 2, "\x01", "ec6b39952e1a3ec3ab3507185cf756181c84bbe2"},
+ {0, 3, "\x04", "a37596ec13a0d2f9e6c0b8b96f9112823aa6d961"},
+ {0, 4, "\x0d", "ba582f5967911beb91599684c2eb2baeefb78da7"},
+ {0, 5, "\x09", "3320540d1c28b96ddd03eee1b186a8f2ae883fbe"},
+ {0, 6, "\x08", "b372bd120957ebc3392cd060e131699d1fee6059"},
+ {0, 7, "\x22", "04f31807151181ad0db278a1660526b0aeef64c2"}
};
diff --git a/sha_224.c b/sha_224.c
index fea9405..bffca4c 100644
--- a/sha_224.c
+++ b/sha_224.c
@@ -19,7 +19,14 @@ static struct testcase testcases[] = {
#endif
{1, 0, "abc", "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"},
{1, 0, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
- "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"}
+ "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"},
+ {0, 1, "\x01", "0d05096bca2a4a77a2b47a05a59618d01174b37892376135c1b6e957"},
+ {0, 2, "\x02", "ef9c947a47bb9311a0f2b8939cfc12090554868b3b64d8f71e6442f3"},
+ {0, 3, "\x04", "4f2ec61c914dce56c3fe5067aa184125ab126c39edb8bf64f58bdccd"},
+ {0, 4, "\x05", "b04c423c9091ff5bb32ea4b0063e98814633350c1bc2bd974f776fd2"},
+ {0, 5, "\x0d", "e3b048552c3c387bcab37f6eb06bb79b96a4aee5ff27f51531a9551c"},
+ {0, 6, "\x2b", "44b64a6dbd91d49df5af0c9f8e001b1378e1dc29c4b891350e5d7bd9"},
+ {0, 7, "\x0c", "20f25c1fe299cf337ff7ff9cc4b5b5afac076759720174a29ba79db6"}
};