blob: d5da2b6170680419b9b15b19446cb7a3f4ce9dba [file] [log] [blame]
Damjan Marionde3735f2021-12-06 12:48:46 +01001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
3 */
4
5#include <vppinfra/format.h>
Damjan Marionc3542e12023-03-15 11:42:06 +00006#include <vppinfra/test/test.h>
Damjan Marion04bd0ea2023-03-16 16:37:56 +00007#include <vppinfra/crypto/sha2.h>
Damjan Marionde3735f2021-12-06 12:48:46 +01008
9typedef struct
10{
11 const u8 *msg;
12 const u8 *key;
13 int tc;
14 u32 msg_len;
15 u32 key_len;
16 u8 digest_224[28];
17 u8 digest_256[32];
18 u8 digest_384[48];
19 u8 digest_512[64];
20 u8 digest_224_len;
21 u8 digest_256_len;
22 u8 digest_384_len;
23 u8 digest_512_len;
24} sha2_test_t;
25
26#ifndef CLIB_MARCH_VARIANT
27static const u8 key1[20] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
28 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
29 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
30static const u8 key2[4] = "Jefe";
31static const u8 key3[20] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
32 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
33 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
34static const u8 key4[25] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
35 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
36 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
37 0x16, 0x17, 0x18, 0x19 };
38static const u8 key5[20] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
39 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
40 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c };
41static const u8 key6[131] = {
42 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
43 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
44 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
45 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
46 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
47 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
48 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
49 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
50 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
51 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
52 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
53};
54
55static const u8 msg1[8] = "Hi There";
56static const u8 msg2[28] = "what do ya want for nothing?";
57static const u8 msg3[50] = {
58 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
59 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
60 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
61 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
62};
63static const u8 msg4[50] = {
64 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
65 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
66 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
67 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd
68};
69static const u8 msg6[54] =
70 "Test Using Larger Than Block-Size Key - Hash Key First";
71static const u8 msg7[153] =
72 "This is a test using a larger than block-size key and a larger than "
73 "block-size data. The key needs to be hashed before being used by the "
74 "HMAC algorithm.";
75
76const sha2_test_t sha2_tests[] = {
77 {
78 /* RFC4231 Test Case 1 */
79 .tc = 1,
80 .key = key1,
81 .key_len = sizeof (key1),
82 .msg = msg1,
83 .msg_len = sizeof (msg1),
84 .digest_224 = { 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19, 0x68, 0x32,
85 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f, 0x47, 0xb4, 0xb1, 0x16,
86 0x99, 0x12, 0xba, 0x4f, 0x53, 0x68, 0x4b, 0x22 },
87 .digest_256 = { 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
88 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
89 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
90 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 },
91 .digest_384 = { 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08,
92 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f, 0x15, 0xf9, 0xda, 0xdb,
93 0xe4, 0x10, 0x1e, 0xc6, 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb,
94 0xc5, 0x9c, 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
95 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6 },
96 .digest_512 = { 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0,
97 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2,
98 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1,
99 0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
100 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d,
101 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20,
102 0x3a, 0x12, 0x68, 0x54 },
103 },
104 {
105 /* RFC4231 Test Case 2 */
106 .tc = 2,
107 .key = key2,
108 .key_len = sizeof (key2),
109 .msg = msg2,
110 .msg_len = sizeof (msg2),
111 .digest_224 = { 0xa3, 0x0e, 0x01, 0x09, 0x8b, 0xc6, 0xdb, 0xbf, 0x45, 0x69,
112 0x0f, 0x3a, 0x7e, 0x9e, 0x6d, 0x0f, 0x8b, 0xbe, 0xa2, 0xa3,
113 0x9e, 0x61, 0x48, 0x00, 0x8f, 0xd0, 0x5e, 0x44 },
114 .digest_256 = { 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e,
115 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7,
116 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83,
117 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 },
118 .digest_384 = { 0xaf, 0x45, 0xd2, 0xe3, 0x76, 0x48, 0x40, 0x31, 0x61, 0x7f,
119 0x78, 0xd2, 0xb5, 0x8a, 0x6b, 0x1b, 0x9c, 0x7e, 0xf4, 0x64,
120 0xf5, 0xa0, 0x1b, 0x47, 0xe4, 0x2e, 0xc3, 0x73, 0x63, 0x22,
121 0x44, 0x5e, 0x8e, 0x22, 0x40, 0xca, 0x5e, 0x69, 0xe2, 0xc7,
122 0x8b, 0x32, 0x39, 0xec, 0xfa, 0xb2, 0x16, 0x49 },
123 .digest_512 = { 0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2, 0xe3, 0x95,
124 0xfb, 0xe7, 0x3b, 0x56, 0xe0, 0xa3, 0x87, 0xbd, 0x64, 0x22,
125 0x2e, 0x83, 0x1f, 0xd6, 0x10, 0x27, 0x0c, 0xd7, 0xea, 0x25,
126 0x05, 0x54, 0x97, 0x58, 0xbf, 0x75, 0xc0, 0x5a, 0x99, 0x4a,
127 0x6d, 0x03, 0x4f, 0x65, 0xf8, 0xf0, 0xe6, 0xfd, 0xca, 0xea,
128 0xb1, 0xa3, 0x4d, 0x4a, 0x6b, 0x4b, 0x63, 0x6e, 0x07, 0x0a,
129 0x38, 0xbc, 0xe7, 0x37 },
130 },
131 { /* RFC4231 Test Case 3 */
132 .tc = 3,
133 .key = key3,
134 .key_len = sizeof (key3),
135 .msg = msg3,
136 .msg_len = sizeof (msg3),
137 .digest_224 = { 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6, 0xff, 0xa9,
138 0x69, 0x4d, 0x7d, 0x6a, 0xd2, 0x64, 0x93, 0x65, 0xb0, 0xc1,
139 0xf6, 0x5d, 0x69, 0xd1, 0xec, 0x83, 0x33, 0xea },
140 .digest_256 = { 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46,
141 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7,
142 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22,
143 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe },
144 .digest_384 = { 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2,
145 0xac, 0xe0, 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9,
146 0x47, 0xac, 0x9f, 0xeb, 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66,
147 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, 0xc1, 0x38, 0x14, 0xb9,
148 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27 },
149 .digest_512 = { 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0,
150 0xf0, 0x75, 0x6c, 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd,
151 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, 0x3e, 0x33, 0xb2, 0x27,
152 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, 0xc8,
153 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46,
154 0xa3, 0x37, 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59,
155 0xe1, 0x32, 0x92, 0xfb } },
156 {
157 /* RFC4231 Test Case 4 */
158 .tc = 4,
159 .key = key4,
160 .key_len = sizeof (key4),
161 .msg = msg4,
162 .msg_len = sizeof (msg4),
163 .digest_224 = { 0x6c, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3c, 0xac, 0x6a, 0x2a,
164 0xbc, 0x1b, 0xb3, 0x82, 0x62, 0x7c, 0xec, 0x6a, 0x90, 0xd8,
165 0x6e, 0xfc, 0x01, 0x2d, 0xe7, 0xaf, 0xec, 0x5a },
166 .digest_256 = { 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e,
167 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a,
168 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07,
169 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b },
170 .digest_384 = { 0x3e, 0x8a, 0x69, 0xb7, 0x78, 0x3c, 0x25, 0x85, 0x19, 0x33,
171 0xab, 0x62, 0x90, 0xaf, 0x6c, 0xa7, 0x7a, 0x99, 0x81, 0x48,
172 0x08, 0x50, 0x00, 0x9c, 0xc5, 0x57, 0x7c, 0x6e, 0x1f, 0x57,
173 0x3b, 0x4e, 0x68, 0x01, 0xdd, 0x23, 0xc4, 0xa7, 0xd6, 0x79,
174 0xcc, 0xf8, 0xa3, 0x86, 0xc6, 0x74, 0xcf, 0xfb },
175 .digest_512 = { 0xb0, 0xba, 0x46, 0x56, 0x37, 0x45, 0x8c, 0x69, 0x90, 0xe5,
176 0xa8, 0xc5, 0xf6, 0x1d, 0x4a, 0xf7, 0xe5, 0x76, 0xd9, 0x7f,
177 0xf9, 0x4b, 0x87, 0x2d, 0xe7, 0x6f, 0x80, 0x50, 0x36, 0x1e,
178 0xe3, 0xdb, 0xa9, 0x1c, 0xa5, 0xc1, 0x1a, 0xa2, 0x5e, 0xb4,
179 0xd6, 0x79, 0x27, 0x5c, 0xc5, 0x78, 0x80, 0x63, 0xa5, 0xf1,
180 0x97, 0x41, 0x12, 0x0c, 0x4f, 0x2d, 0xe2, 0xad, 0xeb, 0xeb,
181 0x10, 0xa2, 0x98, 0xdd },
182 },
183 {
184 /* RFC4231 Test Case 5 */
185 .tc = 5,
186 .key = key5,
187 .key_len = sizeof (key5),
188 .msg = (u8 *) "Test With Truncation",
189 .msg_len = 20,
190 .digest_224 = { 0x0e, 0x2a, 0xea, 0x68, 0xa9, 0x0c, 0x8d, 0x37, 0xc9, 0x88,
191 0xbc, 0xdb, 0x9f, 0xca, 0x6f, 0xa8 },
192 .digest_224_len = 16,
193 .digest_256 = { 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, 0x6e, 0x0c,
194 0x79, 0x6c, 0x29, 0x55, 0x55, 0x2b },
195 .digest_256_len = 16,
196 .digest_384 = { 0x3a, 0xbf, 0x34, 0xc3, 0x50, 0x3b, 0x2a, 0x23, 0xa4, 0x6e,
197 0xfc, 0x61, 0x9b, 0xae, 0xf8, 0x97 },
198 .digest_384_len = 16,
199 .digest_512 = { 0x41, 0x5f, 0xad, 0x62, 0x71, 0x58, 0x0a, 0x53, 0x1d, 0x41,
200 0x79, 0xbc, 0x89, 0x1d, 0x87, 0xa6 },
201 .digest_512_len = 16,
202 },
203 { /* RFC4231 Test Case 6 */
204 .tc = 6,
205 .key = key6,
206 .key_len = sizeof (key6),
207 .msg = msg6,
208 .msg_len = sizeof (msg6),
209 .digest_224 = { 0x95, 0xe9, 0xa0, 0xdb, 0x96, 0x20, 0x95, 0xad, 0xae, 0xbe,
210 0x9b, 0x2d, 0x6f, 0x0d, 0xbc, 0xe2, 0xd4, 0x99, 0xf1, 0x12,
211 0xf2, 0xd2, 0xb7, 0x27, 0x3f, 0xa6, 0x87, 0x0e },
212 .digest_256 = { 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f,
213 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f,
214 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14,
215 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 },
216 .digest_384 = { 0x4e, 0xce, 0x08, 0x44, 0x85, 0x81, 0x3e, 0x90, 0x88, 0xd2,
217 0xc6, 0x3a, 0x04, 0x1b, 0xc5, 0xb4, 0x4f, 0x9e, 0xf1, 0x01,
218 0x2a, 0x2b, 0x58, 0x8f, 0x3c, 0xd1, 0x1f, 0x05, 0x03, 0x3a,
219 0xc4, 0xc6, 0x0c, 0x2e, 0xf6, 0xab, 0x40, 0x30, 0xfe, 0x82,
220 0x96, 0x24, 0x8d, 0xf1, 0x63, 0xf4, 0x49, 0x52 },
221 .digest_512 = { 0x80, 0xb2, 0x42, 0x63, 0xc7, 0xc1, 0xa3, 0xeb, 0xb7, 0x14,
222 0x93, 0xc1, 0xdd, 0x7b, 0xe8, 0xb4, 0x9b, 0x46, 0xd1, 0xf4,
223 0x1b, 0x4a, 0xee, 0xc1, 0x12, 0x1b, 0x01, 0x37, 0x83, 0xf8,
224 0xf3, 0x52, 0x6b, 0x56, 0xd0, 0x37, 0xe0, 0x5f, 0x25, 0x98,
225 0xbd, 0x0f, 0xd2, 0x21, 0x5d, 0x6a, 0x1e, 0x52, 0x95, 0xe6,
226 0x4f, 0x73, 0xf6, 0x3f, 0x0a, 0xec, 0x8b, 0x91, 0x5a, 0x98,
227 0x5d, 0x78, 0x65, 0x98 } },
228 {
229 /* RFC4231 Test Case 7 */
230 .tc = 7,
231 .key = key6,
232 .key_len = sizeof (key6),
233 .msg = msg7,
234 .msg_len = sizeof (msg7) - 1,
235 .digest_224 = { 0x3a, 0x85, 0x41, 0x66, 0xac, 0x5d, 0x9f, 0x02, 0x3f, 0x54,
236 0xd5, 0x17, 0xd0, 0xb3, 0x9d, 0xbd, 0x94, 0x67, 0x70, 0xdb,
237 0x9c, 0x2b, 0x95, 0xc9, 0xf6, 0xf5, 0x65, 0xd1 },
238 .digest_256 = { 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb,
239 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44,
240 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93,
241 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2 },
242 .digest_384 = { 0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d, 0x35, 0x1e,
243 0x2f, 0x25, 0x4e, 0x8f, 0xd3, 0x2c, 0x60, 0x24, 0x20, 0xfe,
244 0xb0, 0xb8, 0xfb, 0x9a, 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e,
245 0x99, 0xc5, 0xa6, 0x78, 0xcc, 0x31, 0xe7, 0x99, 0x17, 0x6d,
246 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e },
247 .digest_512 = { 0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba, 0xa4, 0xdf,
248 0xa9, 0xf9, 0x6e, 0x5e, 0x3f, 0xfd, 0xde, 0xbd, 0x71, 0xf8,
249 0x86, 0x72, 0x89, 0x86, 0x5d, 0xf5, 0xa3, 0x2d, 0x20, 0xcd,
250 0xc9, 0x44, 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, 0xb1,
251 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15, 0x13, 0x46,
252 0x76, 0xfb, 0x6d, 0xe0, 0x44, 0x60, 0x65, 0xc9, 0x74, 0x40,
253 0xfa, 0x8c, 0x6a, 0x58 },
254 },
255 {}
256};
257#else
258extern const sha2_test_t sha2_tests[];
259#endif
260
261static clib_error_t *
262check_digest (clib_error_t *err, int tc, u8 *calculated, const u8 *expected,
263 u8 len)
264{
265 if (memcmp (expected, calculated, len) != 0)
266 err = clib_error_return (err,
267 "Bad HMAC SHA%u digest for test case "
268 "%u:\nExpected:\n%U\nCalculated:\n%U\n",
269 len * 8, tc, format_hexdump, expected, len,
270 format_hexdump, calculated, len);
271 return err;
272}
273
274#define _(bits) \
275 static clib_error_t *test_clib_hmac_sha##bits (clib_error_t *err) \
276 { \
277 u8 digest[64]; \
278 const sha2_test_t *t = sha2_tests; \
279 \
280 while (t->key) \
281 { \
282 u8 digest_len = t->digest_##bits##_len; \
283 if (digest_len == 0) \
284 digest_len = sizeof (t->digest_##bits); \
285 clib_memset_u8 (digest, 0xfe, sizeof (digest)); \
286 clib_hmac_sha##bits (t->key, t->key_len, t->msg, t->msg_len, digest); \
287 if ((err = check_digest (err, t->tc, digest, t->digest_##bits, \
288 digest_len))) \
289 return err; \
290 t++; \
291 } \
292 \
293 return err; \
294 } \
295 \
Damjan Mariond5045e62022-04-06 21:16:37 +0200296 void __test_perf_fn perftest_sha##bits##_byte (test_perf_t *tp) \
Damjan Marionde3735f2021-12-06 12:48:46 +0100297 { \
298 volatile uword *np = &tp->n_ops; \
299 volatile uword *kl = &tp->arg0; \
300 ; \
301 u8 *key = test_mem_alloc_and_fill_inc_u8 (*kl, 32, 0); \
302 u8 *data = test_mem_alloc_and_fill_inc_u8 (*np, 0, 0); \
303 u8 *digest = test_mem_alloc (64); \
304 \
Damjan Mariond5045e62022-04-06 21:16:37 +0200305 test_perf_event_enable (tp); \
Damjan Marionde3735f2021-12-06 12:48:46 +0100306 clib_hmac_sha##bits (key, *kl, data, *np, digest); \
Damjan Mariond5045e62022-04-06 21:16:37 +0200307 test_perf_event_disable (tp); \
Damjan Marionde3735f2021-12-06 12:48:46 +0100308 } \
309 REGISTER_TEST (clib_hmac_sha##bits) = { \
310 .name = "clib_hmac_sha" #bits, \
311 .fn = test_clib_hmac_sha##bits, \
312 .perf_tests = PERF_TESTS ({ .name = "byte", \
Damjan Marionde3735f2021-12-06 12:48:46 +0100313 .n_ops = 16384, \
314 .arg0 = 20, \
315 .fn = perftest_sha##bits##_byte }) \
316 }
317
318_ (224);
319_ (256);
320_ (384);
321_ (512);
322#undef _