]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hmac.c
Merge pull request #31648 from neighbourhoodie/review-content
[thirdparty/systemd.git] / src / test / test-hmac.c
CommitLineData
8d39bff4
LB
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include "hexdecoct.h"
4#include "hmac.h"
5#include "string-util.h"
6#include "tests.h"
7
8static void hmac_sha256_by_string(const char *key, const char *value, uint8_t res[static SHA256_DIGEST_SIZE]) {
9 hmac_sha256(key, strlen(key), value, strlen(value), res);
10}
11
4f7452a8 12TEST(hmac) {
93d13a7d
YW
13 uint8_t result[SHA256_DIGEST_SIZE];
14 char *hex_result = NULL;
8d39bff4 15
93d13a7d 16 /* Results compared with output of 'echo -n "<input>" | openssl dgst -sha256 -hmac "<key>"' */
8d39bff4 17
93d13a7d
YW
18 hmac_sha256_by_string("waldo",
19 "",
20 result);
21 hex_result = hexmem(result, sizeof(result));
5f0e4d2f 22 ASSERT_STREQ(hex_result, "cadd5e42114351181f3abff477641d88efb57d2b5641a1e5c6d623363a6d3bad");
93d13a7d 23 hex_result = mfree(hex_result);
8d39bff4 24
93d13a7d
YW
25 hmac_sha256_by_string("waldo",
26 "baldohaldo",
27 result);
28 hex_result = hexmem(result, sizeof(result));
c79e88b3 29 ASSERT_STREQ(hex_result, "c47ad5031ba21605e52c6ca68090d66a2dd5ccf84efa4bace15361a8cba63cda");
93d13a7d 30 hex_result = mfree(hex_result);
8d39bff4 31
93d13a7d
YW
32 hmac_sha256_by_string("waldo",
33 "baldo haldo",
34 result);
35 hex_result = hexmem(result, sizeof(result));
c79e88b3 36 ASSERT_STREQ(hex_result, "4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69");
93d13a7d 37 hex_result = mfree(hex_result);
8d39bff4 38
93d13a7d
YW
39 hmac_sha256_by_string("waldo",
40 "baldo 4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69 haldo",
41 result);
42 hex_result = hexmem(result, sizeof(result));
c79e88b3 43 ASSERT_STREQ(hex_result, "039f3df430b19753ffb493e5b90708f75c5210b63c6bcbef3374eb3f0a3f97f7");
93d13a7d 44 hex_result = mfree(hex_result);
8d39bff4 45
93d13a7d
YW
46 hmac_sha256_by_string("4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69",
47 "baldo haldo",
48 result);
49 hex_result = hexmem(result, sizeof(result));
c79e88b3 50 ASSERT_STREQ(hex_result, "c4cfaf48077cbb0bbd177a09e59ec4c248f4ca771503410f5b54b98d88d2f47b");
93d13a7d 51 hex_result = mfree(hex_result);
8d39bff4 52
93d13a7d
YW
53 hmac_sha256_by_string("4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69",
54 "supercalifragilisticexpialidocious",
55 result);
56 hex_result = hexmem(result, sizeof(result));
c79e88b3 57 ASSERT_STREQ(hex_result, "2c059e7a63c4c3b23f47966a65fd2f8a2f5d7161e2e90d78ff68866b5c375cb7");
93d13a7d 58 hex_result = mfree(hex_result);
8d39bff4 59
93d13a7d
YW
60 hmac_sha256_by_string("4e8974ad6c08b98cc2519cd1e27aa7195769fcf86db1dd7ceaab4d44c490ad69c47ad5031ba21605e52c6ca68090d66a2dd5ccf84efa4bace15361a8cba63cda",
61 "supercalifragilisticexpialidocious",
62 result);
63 hex_result = hexmem(result, sizeof(result));
c79e88b3 64 ASSERT_STREQ(hex_result, "1dd1d1d45b9d9f9673dc9983c968c46ff3168e03cfeb4156a219eba1af4cff5f");
93d13a7d 65 hex_result = mfree(hex_result);
8d39bff4
LB
66}
67
4f7452a8 68DEFINE_TEST_MAIN(LOG_INFO);