| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "hexdecoct.h" |
| 4 |
#include "sha1.h" |
| 5 |
#include "tests.h" |
| 6 |
|
| 7 |
static void sha1_process_string(const char *key, struct sha1_ctx *ctx) { |
| 8 |
sha1_process_bytes(key, strlen(key), ctx); |
| 9 |
} |
| 10 |
|
| 11 |
static void test_sha1_one(const char *key, const char *expect) { |
| 12 |
uint8_t result[SHA1_DIGEST_SIZE + 3]; |
| 13 |
_cleanup_free_ char *str = NULL; |
| 14 |
struct sha1_ctx ctx; |
| 15 |
|
| 16 |
log_debug("\"%s\" → %s", key, expect); |
| 17 |
|
| 18 |
assert_se(str = new(char, strlen(key) + 4)); |
| 19 |
|
| 20 |
/* This tests unaligned buffers. */ |
| 21 |
|
| 22 |
for (size_t i = 0; i < 4; i++) { |
| 23 |
strcpy(str + i, key); |
| 24 |
|
| 25 |
for (size_t j = 0; j < 4; j++) { |
| 26 |
_cleanup_free_ char *hex_result = NULL; |
| 27 |
|
| 28 |
sha1_init_ctx(&ctx); |
| 29 |
sha1_process_string(str + i, &ctx); |
| 30 |
sha1_finish_ctx(&ctx, result + j); |
| 31 |
|
| 32 |
hex_result = hexmem(result + j, SHA1_DIGEST_SIZE); |
| 33 |
ASSERT_STREQ(hex_result, expect); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/* From https://datatracker.ietf.org/doc/html/rfc3174#section-7.3 */ |
| 39 |
#define TEST1 "abc" |
| 40 |
#define RESULT1 "a9993e364706816aba3e25717850c26c9cd0d89d" |
| 41 |
#define TEST2 "abcdbcdecdefdefgefghfghighijhi" "jkijkljklmklmnlmnomnopnopq" |
| 42 |
#define RESULT2 "84983e441c3bd26ebaae4aa1f95129e5e54670f1" |
| 43 |
#define TEST3 "a" |
| 44 |
#define RESULT3 "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8" |
| 45 |
#define TEST4 "01234567012345670123456701234567" "01234567012345670123456701234567" |
| 46 |
#define RESULT4 "e0c094e867ef46c350ef54a7f59dd60bed92ae83" |
| 47 |
|
| 48 |
TEST(sha1) { |
| 49 |
/* Results compared with output of 'echo -n "<input>" | sha1sum -' */ |
| 50 |
|
| 51 |
test_sha1_one(TEST1, RESULT1); |
| 52 |
test_sha1_one(TEST2, RESULT2); |
| 53 |
test_sha1_one(TEST3, RESULT3); |
| 54 |
test_sha1_one(TEST4, RESULT4); |
| 55 |
} |
| 56 |
|
| 57 |
DEFINE_TEST_MAIN(LOG_INFO); |