diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/llist.h | 14 | ||||
-rw-r--r-- | src/lib/sha2.c | 12 | ||||
-rw-r--r-- | src/lib/sha2.h | 6 | ||||
-rw-r--r-- | src/lib/test-hash-method.c | 35 | ||||
-rw-r--r-- | src/lib/test-llist.c | 41 |
5 files changed, 98 insertions, 10 deletions
diff --git a/src/lib/llist.h b/src/lib/llist.h index 8a52e87..5ad5d75 100644 --- a/src/lib/llist.h +++ b/src/lib/llist.h @@ -78,4 +78,18 @@ #define DLLIST2_REMOVE(head, tail, item) \ DLLIST2_REMOVE_FULL(head, tail, item, prev, next) +#define DLLIST2_JOIN_FULL(head1, tail1, head2, tail2, prev, next) STMT_START { \ + if (*(head1) == NULL) { \ + *(head1) = *(head2); \ + *(tail1) = *(tail2); \ + } else if (*(head2) != NULL) { \ + (*(tail1))->next = *(head2); \ + (*(head2))->prev = *(tail1); \ + (*tail1) = (*tail2); \ + } \ + } STMT_END + +#define DLLIST2_JOIN(head1, tail1, head2, tail2) \ + DLLIST2_JOIN_FULL(head1, tail1, head2, tail2, prev, next) + #endif diff --git a/src/lib/sha2.c b/src/lib/sha2.c index 93dddfb..b6bef47 100644 --- a/src/lib/sha2.c +++ b/src/lib/sha2.c @@ -287,7 +287,7 @@ void sha256_result(struct sha256_ctx *ctx, { size_t block_nb; size_t pm_len; - size_t len_b; + uint64_t len_b; int i; block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) @@ -298,7 +298,7 @@ void sha256_result(struct sha256_ctx *ctx, memset(ctx->block + ctx->len, 0, pm_len - ctx->len); ctx->block[ctx->len] = 0x80; - UNPACK32(len_b, ctx->block + pm_len - 4); + UNPACK64(len_b, ctx->block + pm_len - 8); sha256_transf(ctx, ctx->block, block_nb); @@ -414,7 +414,7 @@ void sha384_result(struct sha384_ctx *ctx, { unsigned int block_nb; unsigned int pm_len; - size_t len_b; + uint64_t len_b; int i; block_nb = 1 + ((SHA384_BLOCK_SIZE - 17) @@ -425,7 +425,7 @@ void sha384_result(struct sha384_ctx *ctx, memset(ctx->block + ctx->len, 0, pm_len - ctx->len); ctx->block[ctx->len] = 0x80; - UNPACK32(len_b, ctx->block + pm_len - 4); + UNPACK64(len_b, ctx->block + pm_len - 8); sha384_transf(ctx, ctx->block, block_nb); @@ -541,7 +541,7 @@ void sha512_result(struct sha512_ctx *ctx, { unsigned int block_nb; unsigned int pm_len; - size_t len_b; + uint64_t len_b; int i; block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) @@ -552,7 +552,7 @@ void sha512_result(struct sha512_ctx *ctx, memset(ctx->block + ctx->len, 0, pm_len - ctx->len); ctx->block[ctx->len] = 0x80; - UNPACK32(len_b, ctx->block + pm_len - 4); + UNPACK64(len_b, ctx->block + pm_len - 8); sha512_transf(ctx, ctx->block, block_nb); diff --git a/src/lib/sha2.h b/src/lib/sha2.h index 8c893eb..92bd2c7 100644 --- a/src/lib/sha2.h +++ b/src/lib/sha2.h @@ -38,21 +38,21 @@ #include "sha-common.h" struct sha256_ctx { - size_t tot_len; + uint64_t tot_len; size_t len; unsigned char block[2 * SHA256_BLOCK_SIZE]; uint32_t h[8]; }; struct sha384_ctx { - size_t tot_len; + uint64_t tot_len; size_t len; unsigned char block[2 * SHA384_BLOCK_SIZE]; uint64_t h[8]; }; struct sha512_ctx { - size_t tot_len; + uint64_t tot_len; size_t len; unsigned char block[2 * SHA512_BLOCK_SIZE]; uint64_t h[8]; diff --git a/src/lib/test-hash-method.c b/src/lib/test-hash-method.c index 0fd41e0..97db7c8 100644 --- a/src/lib/test-hash-method.c +++ b/src/lib/test-hash-method.c @@ -1,6 +1,7 @@ /* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */ #include "test-lib.h" +#include "hex-binary.h" #include "mmap-util.h" #include "hash-method.h" @@ -453,8 +454,42 @@ static void test_hash_methods_fips() { test_end(); } +static void test_hash_methods_large(void) +{ + struct { + const char *method; + const char *hash; + } tests[] = { + { "sha256", "1ad0598b790b3acb38876105cc8938c3365f3215fbee3412ac3cd5e96a7dad01" }, + { "sha384", "c187c084ffe516fea74b313340a540bc0bab306b1bdc564da21ecdc639e51f194460a0279c04aa40d65cec58698b10c0" }, + { "sha512", "556247cfeab056903a3f42cf8496019d9ad90911ded9aa1ede3046b803623e5e2cd2adbd0620e666a927436d125984de9199d643ff21ad1c76e29b116c13ffb2" }, + }; + unsigned char data[1024]; + unsigned int i; + + test_begin("hash method (large inputs)"); + for (i = 0; i < sizeof(data); i++) + data[i] = i & 0xFF; + + for (i = 0; i < N_ELEMENTS(tests); i++) { + const struct hash_method *method = + hash_method_lookup(tests[i].method); + unsigned char context[method->context_size]; + unsigned char result[method->digest_size]; + + method->init(context); + for (unsigned int j = 0; j < 600000; j++) + method->loop(context, data, sizeof(data)); + method->result(context, result); + test_assert_strcmp_idx(binary_to_hex(result, method->digest_size), + tests[i].hash, i); + } + test_end(); +} + void test_hash_method(void) { test_hash_method_boundary(); test_hash_methods_fips(); + test_hash_methods_large(); } diff --git a/src/lib/test-llist.c b/src/lib/test-llist.c index d57006c..e293eb6 100644 --- a/src/lib/test-llist.c +++ b/src/lib/test-llist.c @@ -71,7 +71,7 @@ static void test_dllist2(void) l2 = t_new(struct dllist, 1); l1 = t_new(struct dllist, 1); - test_begin("dllist"); + test_begin("dllist2"); /* prepend to empty */ DLLIST2_PREPEND(&head, &tail, l3); test_assert(head == l3 && tail == l3); @@ -131,8 +131,47 @@ static void test_dllist2(void) test_end(); } +static void test_dllist2_join(void) +{ + struct dllist *head, *tail, *elem[4]; + struct dllist *head2, *tail2, *elem2[N_ELEMENTS(elem)]; + + test_begin("dllist2 join"); + for (unsigned int i = 0; i < N_ELEMENTS(elem); i++) { + elem[i] = t_new(struct dllist, 1); + elem2[i] = t_new(struct dllist, 1); + } + for (unsigned int i = 0; i < N_ELEMENTS(elem); i++) { + for (unsigned int j = 0; j < N_ELEMENTS(elem2); j++) { + head = tail = head2 = tail2 = NULL; + for (unsigned int n = 0; n < i; n++) + DLLIST2_APPEND(&head, &tail, elem[n]); + for (unsigned int n = 0; n < j; n++) + DLLIST2_APPEND(&head2, &tail2, elem2[n]); + DLLIST2_JOIN(&head, &tail, &head2, &tail2); + + /* verify */ + struct dllist *tmp = head, *last = NULL; + for (unsigned int n = 0; n < i; n++) { + test_assert(tmp == elem[n]); + last = tmp; + tmp = tmp->next; + } + for (unsigned int n = 0; n < j; n++) { + test_assert(tmp == elem2[n]); + last = tmp; + tmp = tmp->next; + } + test_assert(tmp == NULL); + test_assert(tail == last); + } + } + test_end(); +} + void test_llist(void) { test_dllist(); test_dllist2(); + test_dllist2_join(); } |