1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/hex.h>
15 #include <gmssl/hmac.h>
16
17
18 // FIXME: md5, sha1, sm3 test vectors
19
20
21 struct {
22 char *key;
23 char *data;
24 char *hmac_sha224;
25 char *hmac_sha256;
26 char *hmac_sha384;
27 char *hmac_sha512;
28 } hmac_tests[] = {
29
30 // rfc 4231 test vectors
31 {
32 "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
33 "0b0b0b0b",
34 "4869205468657265",
35 "896fb1128abbdf196832107cd49df33f"
36 "47b4b1169912ba4f53684b22",
37 "b0344c61d8db38535ca8afceaf0bf12b"
38 "881dc200c9833da726e9376c2e32cff7",
39 "afd03944d84895626b0825f4ab46907f"
40 "15f9dadbe4101ec682aa034c7cebc59c"
41 "faea9ea9076ede7f4af152e8b2fa9cb6",
42 "87aa7cdea5ef619d4ff0b4241a1d6cb0"
43 "2379f4e2ce4ec2787ad0b30545e17cde"
44 "daa833b7d6b8a702038b274eaea3f4e4"
45 "be9d914eeb61f1702e696c203a126854",
46 },
47 {
48 "4a656665",
49 "7768617420646f2079612077616e7420"
50 "666f72206e6f7468696e673f",
51 "a30e01098bc6dbbf45690f3a7e9e6d0f"
52 "8bbea2a39e6148008fd05e44",
53 "5bdcc146bf60754e6a042426089575c7"
54 "5a003f089d2739839dec58b964ec3843",
55 "af45d2e376484031617f78d2b58a6b1b"
56 "9c7ef464f5a01b47e42ec3736322445e"
57 "8e2240ca5e69e2c78b3239ecfab21649",
58 "164b7a7bfcf819e2e395fbe73b56e0a3"
59 "87bd64222e831fd610270cd7ea250554"
60 "9758bf75c05a994a6d034f65f8f0e6fd"
61 "caeab1a34d4a6b4b636e070a38bce737",
62 },
63 {
64 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
65 "aaaaaaaa",
66 "dddddddddddddddddddddddddddddddd"
67 "dddddddddddddddddddddddddddddddd"
68 "dddddddddddddddddddddddddddddddd"
69 "dddd",
70 "7fb3cb3588c6c1f6ffa9694d7d6ad264"
71 "9365b0c1f65d69d1ec8333ea",
72 "773ea91e36800e46854db8ebd09181a7"
73 "2959098b3ef8c122d9635514ced565fe",
74 "88062608d3e6ad8a0aa2ace014c8a86f"
75 "0aa635d947ac9febe83ef4e55966144b"
76 "2a5ab39dc13814b94e3ab6e101a34f27",
77 "fa73b0089d56a284efb0f0756c890be9"
78 "b1b5dbdd8ee81a3655f83e33b2279d39"
79 "bf3e848279a722c806b485a47e67c807"
80 "b946a337bee8942674278859e13292fb",
81 },
82 };
83
test_hmac(const DIGEST * digest,const char * key_hex,const char * data_hex,const char * hmac_hex)84 int test_hmac(const DIGEST *digest, const char *key_hex, const char *data_hex, const char *hmac_hex)
85 {
86 HMAC_CTX ctx;
87 uint8_t key[strlen(key_hex)/2];
88 uint8_t data[strlen(data_hex)/2];
89 uint8_t hmac[strlen(hmac_hex)/2];
90 uint8_t buf[64];
91 size_t len;
92
93 hex_to_bytes(key_hex, strlen(key_hex), key, &len);
94 hex_to_bytes(data_hex, strlen(data_hex), data, &len);
95 hex_to_bytes(hmac_hex, strlen(hmac_hex), hmac, &len);
96
97 hmac_init(&ctx, digest, key, sizeof(key));
98 hmac_update(&ctx, data, sizeof(data));
99 hmac_finish(&ctx, buf, &len);
100
101 if (len != sizeof(hmac) || memcmp(buf, hmac, sizeof(hmac)) != 0) {
102 printf("failed\n");
103 return 0;
104 }
105 printf("ok\n");
106 return 1;
107 }
108
main(void)109 int main(void)
110 {
111 int i;
112 for (i = 0; i < sizeof(hmac_tests)/sizeof(hmac_tests[0]); i++) {
113 test_hmac(DIGEST_sha224(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha224);
114 test_hmac(DIGEST_sha256(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha256);
115 test_hmac(DIGEST_sha384(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha384);
116 test_hmac(DIGEST_sha512(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha512);
117 };
118
119 return 0;
120 };
121