blob: a41275e6316bd015eeff084c8b4de4e6bd60dd69 [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* test_hmac.c - TinyCrypt implementation of some HMAC tests */
2
3/*
Fabio Utzig3efe6b62017-09-22 16:03:24 -03004 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
David Brownfecda2d2017-09-07 10:20:34 -06005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 DESCRIPTION
35 This module tests the following HMAC routines:
36
37 Scenarios tested include:
38 - HMAC tests (RFC 4231 test vectors)
39*/
40
41#include <tinycrypt/hmac.h>
42#include <tinycrypt/sha256.h>
43#include <tinycrypt/constants.h>
44#include <test_utils.h>
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <stdint.h>
50
Fabio Utzig3efe6b62017-09-22 16:03:24 -030051unsigned int do_hmac_test(TCHmacState_t h, unsigned int testnum, const uint8_t *data,
52 size_t datalen, const uint8_t *expected,
53 size_t expectedlen)
David Brownfecda2d2017-09-07 10:20:34 -060054{
55 uint8_t digest[32];
Fabio Utzig3efe6b62017-09-22 16:03:24 -030056 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -060057
58 (void)tc_hmac_init(h);
59 (void)tc_hmac_update(h, data, datalen);
60 (void)tc_hmac_final(digest, TC_SHA256_DIGEST_SIZE, h);
61 result = check_result(testnum, expected, expectedlen,
62 digest, sizeof(digest));
63 return result;
64}
65
66/*
67 * NIST test vectors for encryption.
68 */
Fabio Utzig3efe6b62017-09-22 16:03:24 -030069unsigned int test_1(void)
David Brownfecda2d2017-09-07 10:20:34 -060070{
Fabio Utzig3efe6b62017-09-22 16:03:24 -030071 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -060072
73 TC_PRINT("HMAC %s:\n", __func__);
74
75 const uint8_t key[20] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -030076 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
77 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
David Brownfecda2d2017-09-07 10:20:34 -060078 };
79 const uint8_t data[8] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -030080 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
David Brownfecda2d2017-09-07 10:20:34 -060081 };
82 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -030083 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce,
84 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
85 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
David Brownfecda2d2017-09-07 10:20:34 -060086 };
87 struct tc_hmac_state_struct h;
88
89 (void)memset(&h, 0x00, sizeof(h));
90 (void)tc_hmac_set_key(&h, key, sizeof(key));
Fabio Utzig3efe6b62017-09-22 16:03:24 -030091 result = do_hmac_test(&h, 1, data, sizeof(data),expected,
92 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -060093 TC_END_RESULT(result);
94 return result;
95}
96
Fabio Utzig3efe6b62017-09-22 16:03:24 -030097unsigned int test_2(void)
David Brownfecda2d2017-09-07 10:20:34 -060098{
Fabio Utzig3efe6b62017-09-22 16:03:24 -030099 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600100 TC_PRINT("HMAC %s:\n", __func__);
101 const uint8_t key[4] = {
102 0x4a, 0x65, 0x66, 0x65
103 };
104 const uint8_t data[28] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300105 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x61, 0x20, 0x77,
106 0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68,
107 0x69, 0x6e, 0x67, 0x3f
David Brownfecda2d2017-09-07 10:20:34 -0600108 };
109 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300110 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26,
111 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83,
112 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43
David Brownfecda2d2017-09-07 10:20:34 -0600113 };
114 struct tc_hmac_state_struct h;
115
116 (void)memset(&h, 0x00, sizeof(h));
117 (void)tc_hmac_set_key(&h, key, sizeof(key));
118
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300119 result = do_hmac_test(&h, 2, data, sizeof(data), expected,
120 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -0600121 TC_END_RESULT(result);
122 return result;
123}
124
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300125unsigned int test_3(void)
David Brownfecda2d2017-09-07 10:20:34 -0600126{
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300127 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600128 TC_PRINT("HMAC %s:\n", __func__);
129 const uint8_t key[20] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300130 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
131 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
David Brownfecda2d2017-09-07 10:20:34 -0600132 };
133 const uint8_t data[50] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300134 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
135 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
136 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
137 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
138 0xdd, 0xdd
David Brownfecda2d2017-09-07 10:20:34 -0600139 };
140 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300141 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb,
142 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22,
143 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe
David Brownfecda2d2017-09-07 10:20:34 -0600144 };
145 struct tc_hmac_state_struct h;
146
147 (void)memset(&h, 0x00, sizeof(h));
148 (void)tc_hmac_set_key(&h, key, sizeof(key));
149
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300150 result = do_hmac_test(&h, 3, data, sizeof(data), expected,
151 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -0600152 TC_END_RESULT(result);
153 return result;
154}
155
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300156unsigned int test_4(void)
David Brownfecda2d2017-09-07 10:20:34 -0600157{
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300158 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600159 TC_PRINT("HMAC %s:\n", __func__);
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300160
David Brownfecda2d2017-09-07 10:20:34 -0600161 const uint8_t key[25] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300162 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
163 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
164 0x19
David Brownfecda2d2017-09-07 10:20:34 -0600165 };
166 const uint8_t data[50] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300167 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
168 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
169 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
170 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
171 0xcd, 0xcd
David Brownfecda2d2017-09-07 10:20:34 -0600172 };
173 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300174 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98,
175 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07,
176 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b
David Brownfecda2d2017-09-07 10:20:34 -0600177 };
178 struct tc_hmac_state_struct h;
179
180 (void)memset(&h, 0x00, sizeof(h));
181 (void)tc_hmac_set_key(&h, key, sizeof(key));
182
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300183 result = do_hmac_test(&h, 4, data, sizeof(data), expected,
184 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -0600185 TC_END_RESULT(result);
186 return result;
187}
188
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300189unsigned int test_5(void)
David Brownfecda2d2017-09-07 10:20:34 -0600190{
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300191 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600192 TC_PRINT("HMAC %s:\n", __func__);
193 const uint8_t key[20] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300194 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
195 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c
David Brownfecda2d2017-09-07 10:20:34 -0600196 };
197 const uint8_t data[20] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300198 0x54, 0x65, 0x73, 0x74, 0x20, 0x57, 0x69, 0x74, 0x68, 0x20, 0x54, 0x72,
199 0x75, 0x6e, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e
David Brownfecda2d2017-09-07 10:20:34 -0600200 };
201 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300202 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, 0x6e, 0x0c, 0x79, 0x6c,
203 0x29, 0x55, 0x55, 0x2b, 0xfa, 0x6f, 0x7c, 0x0a, 0x6a, 0x8a, 0xef, 0x8b,
204 0x93, 0xf8, 0x60, 0xaa, 0xb0, 0xcd, 0x20, 0xc5
David Brownfecda2d2017-09-07 10:20:34 -0600205 };
206 struct tc_hmac_state_struct h;
207
208 (void)memset(&h, 0x00, sizeof(h));
209 (void)tc_hmac_set_key(&h, key, sizeof(key));
210
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300211 result = do_hmac_test(&h, 5, data, sizeof(data), expected,
212 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -0600213 TC_END_RESULT(result);
214 return result;
215}
216
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300217unsigned int test_6(void)
David Brownfecda2d2017-09-07 10:20:34 -0600218{
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300219 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600220 TC_PRINT("HMAC %s:\n", __func__);
221 const uint8_t key[131] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300222 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
223 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
224 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
225 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
226 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
227 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
228 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
229 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
230 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
231 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
232 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
David Brownfecda2d2017-09-07 10:20:34 -0600233 };
234 const uint8_t data[54] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300235 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x4c,
236 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x42,
237 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x4b, 0x65,
238 0x79, 0x20, 0x2d, 0x20, 0x48, 0x61, 0x73, 0x68, 0x20, 0x4b, 0x65, 0x79,
239 0x20, 0x46, 0x69, 0x72, 0x73, 0x74
David Brownfecda2d2017-09-07 10:20:34 -0600240 };
241 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300242 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa,
243 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14,
244 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54
David Brownfecda2d2017-09-07 10:20:34 -0600245 };
246 struct tc_hmac_state_struct h;
247
248 (void)memset(&h, 0x00, sizeof(h));
249 (void)tc_hmac_set_key(&h, key, sizeof(key));
250
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300251 result = do_hmac_test(&h, 6, data, sizeof(data), expected,
252 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -0600253 TC_END_RESULT(result);
254 return result;
255}
256
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300257unsigned int test_7(void)
David Brownfecda2d2017-09-07 10:20:34 -0600258{
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300259 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600260 TC_PRINT("HMAC %s:\n", __func__);
261 const uint8_t key[131] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300262 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
263 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
264 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
265 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
266 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
267 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
268 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
269 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
270 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
271 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
272 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
David Brownfecda2d2017-09-07 10:20:34 -0600273 };
274 const uint8_t data[152] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300275 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
276 0x73, 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x6c,
277 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62,
278 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6b, 0x65,
279 0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20, 0x6c, 0x61, 0x72, 0x67,
280 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, 0x6c, 0x6f, 0x63,
281 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e,
282 0x20, 0x54, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x6e, 0x65, 0x65,
283 0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65, 0x20, 0x68, 0x61, 0x73,
284 0x68, 0x65, 0x64, 0x20, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x62,
285 0x65, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20, 0x62, 0x79,
286 0x20, 0x74, 0x68, 0x65, 0x20, 0x48, 0x4d, 0x41, 0x43, 0x20, 0x61, 0x6c,
287 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e
David Brownfecda2d2017-09-07 10:20:34 -0600288 };
289 const uint8_t expected[32] = {
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300290 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc,
291 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93,
292 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2
David Brownfecda2d2017-09-07 10:20:34 -0600293 };
294 struct tc_hmac_state_struct h;
295
296 (void)memset(&h, 0x00, sizeof(h));
297 (void)tc_hmac_set_key(&h, key, sizeof(key));
298
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300299 result = do_hmac_test(&h, 7, data, sizeof(data), expected,
300 sizeof(expected));
David Brownfecda2d2017-09-07 10:20:34 -0600301 TC_END_RESULT(result);
302 return result;
303}
304
305/*
306 * Main task to test AES
307 */
308int main(void)
309{
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300310 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -0600311
312 TC_START("Performing HMAC tests (RFC4231 test vectors):");
313
314 result = test_1();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300315 if (result == TC_FAIL) {
316 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600317 TC_ERROR("HMAC test #1 failed.\n");
318 goto exitTest;
319 }
320 result = test_2();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300321 if (result == TC_FAIL) {
322 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600323 TC_ERROR("HMAC test #2 failed.\n");
324 goto exitTest;
325 }
326 result = test_3();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300327 if (result == TC_FAIL) {
328 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600329 TC_ERROR("HMAC test #3 failed.\n");
330 goto exitTest;
331 }
332 result = test_4();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300333 if (result == TC_FAIL) {
334 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600335 TC_ERROR("HMAC test #4 failed.\n");
336 goto exitTest;
337 }
338 result = test_5();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300339 if (result == TC_FAIL) {
340 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600341 TC_ERROR("HMAC test #5 failed.\n");
342 goto exitTest;
343 }
344 result = test_6();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300345 if (result == TC_FAIL) {
346 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600347 TC_ERROR("HMAC #6 test failed.\n");
348 goto exitTest;
349 }
350 result = test_7();
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300351 if (result == TC_FAIL) {
352 /* terminate test */
David Brownfecda2d2017-09-07 10:20:34 -0600353 TC_ERROR("HMAC test #7 failed.\n");
354 goto exitTest;
355 }
356
357 TC_PRINT("All HMAC tests succeeded!\n");
358
359exitTest:
360 TC_END_RESULT(result);
361 TC_END_REPORT(result);
362}