aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 15:58:27 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 15:58:27 +0100
commit6b7209128ee0a7aa22126ed7f4cc3d0c2fee267b (patch)
tree05bea4079bdd0ebc5f0dba72ac77dd568f40d8f4
parentMinor fixes and tests (diff)
downloadlibsha2-6b7209128ee0a7aa22126ed7f4cc3d0c2fee267b.tar.gz
libsha2-6b7209128ee0a7aa22126ed7f4cc3d0c2fee267b.tar.bz2
libsha2-6b7209128ee0a7aa22126ed7f4cc3d0c2fee267b.tar.xz
Use lowest bits rather than highest bits in complete byte, document this, and add tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--digest.c1
-rw-r--r--libsha2_digest.38
-rw-r--r--test.c17
3 files changed, 26 insertions, 0 deletions
diff --git a/digest.c b/digest.c
index 5dc1600..0b4f474 100644
--- a/digest.c
+++ b/digest.c
@@ -24,6 +24,7 @@ libsha2_digest(struct libsha2_state *restrict state, const char *message, size_t
off = (state->message_size / 8) % state->chunk_size;
if (msglen) {
state->chunk[off] = (unsigned char)*message;
+ state->chunk[off] <<= 8 - msglen;
state->chunk[off] |= (unsigned char)(1 << (7 - msglen));
state->chunk[off] &= (unsigned char)~((1 << (7 - msglen)) - 1);
state->message_size += msglen;
diff --git a/libsha2_digest.3 b/libsha2_digest.3
index c31025c..dac48b4 100644
--- a/libsha2_digest.3
+++ b/libsha2_digest.3
@@ -31,6 +31,14 @@ least the return value of the
.BR libsha2_state_output_size (3)
function.
.PP
+If
+.I msglen
+is not a multiple of 8, the lowest
+.I msglen%8
+bits from the last by in
+.I message
+is used as the complete byte.
+.PP
The
.BR libsha2_behex_lower (3)
and
diff --git a/test.c b/test.c
index 8cb0d97..6cd664e 100644
--- a/test.c
+++ b/test.c
@@ -62,6 +62,15 @@
test_str(str, EXPECTED);\
} while (0)
+#define test_bits(S, N, ALGO, EXPECTED)\
+ do {\
+ libsha2_unhex(buf, S);\
+ test(!libsha2_init(&s, ALGO));\
+ libsha2_digest(&s, buf, N, buf);\
+ libsha2_behex_lower(str, buf, libsha2_state_output_size(&s));\
+ test_str(str, EXPECTED);\
+ } while (0)
+
int
main(int argc, char *argv[])
@@ -306,6 +315,14 @@ main(int argc, char *argv[])
libsha2_behex_lower(str, buf, libsha2_algorithm_output_size(LIBSHA2_256));
test_str(str, "c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4");
+ test_bits("01", 1, LIBSHA2_224, "0d05096bca2a4a77a2b47a05a59618d01174b37892376135c1b6e957");
+ test_bits("02", 2, LIBSHA2_224, "ef9c947a47bb9311a0f2b8939cfc12090554868b3b64d8f71e6442f3");
+ test_bits("04", 3, LIBSHA2_224, "4f2ec61c914dce56c3fe5067aa184125ab126c39edb8bf64f58bdccd");
+ test_bits("05", 4, LIBSHA2_224, "b04c423c9091ff5bb32ea4b0063e98814633350c1bc2bd974f776fd2");
+ test_bits("0d", 5, LIBSHA2_224, "e3b048552c3c387bcab37f6eb06bb79b96a4aee5ff27f51531a9551c");
+ test_bits("2b", 6, LIBSHA2_224, "44b64a6dbd91d49df5af0c9f8e001b1378e1dc29c4b891350e5d7bd9");
+ test_bits("0c", 7, LIBSHA2_224, "20f25c1fe299cf337ff7ff9cc4b5b5afac076759720174a29ba79db6");
+
test(!errno);
return 0;