FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
rfc2202_hmac_sha1.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /* Test vectors published in RFC2202 */
17 
18 #include <vppinfra/clib.h>
19 #include <vnet/crypto/crypto.h>
20 #include <unittest/crypto/crypto.h>
21 
22 static u8 sha1_tc1_key[] = {
23  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
24  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
25  0x0b, 0x0b, 0x0b, 0x0b
26 };
27 
28 static char sha1_tc1_data[8] = "Hi There";
29 
30 static u8 sha1_tc1_digest[] = {
31  0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
32  0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
33  0xf1, 0x46, 0xbe, 0x00
34 };
35 
36 /* *INDENT-OFF* */
37 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc1) = {
38  .name = "RFC2202 HMAC-SHA-1 TC1",
39  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
40  .key = TEST_DATA (sha1_tc1_key),
41  .plaintext = TEST_DATA (sha1_tc1_data),
42  .digest = TEST_DATA (sha1_tc1_digest),
43 };
44 /* *INDENT-ON* */
45 
46 static char sha1_tc2_key[4] = "Jefe";
47 
48 static char sha1_tc2_data[28] = "what do ya want for nothing?";
49 
50 static u8 sha1_tc2_digest[] = {
51  0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2,
52  0xd2, 0x74, 0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c,
53  0x25, 0x9a, 0x7c, 0x79
54 };
55 
56 /* *INDENT-OFF* */
57 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc2) = {
58  .name = "RFC2202 HMAC-SHA-1 TC2",
59  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
60  .key = TEST_DATA (sha1_tc2_key),
61  .plaintext = TEST_DATA (sha1_tc2_data),
62  .digest = TEST_DATA (sha1_tc2_digest),
63 };
64 /* *INDENT-ON* */
65 
66 static u8 sha1_tc3_key[20] = {
67  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
68  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
69  0xaa, 0xaa, 0xaa, 0xaa
70 };
71 
72 static u8 sha1_tc3_data[50] = {
73  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
74  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
75  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
76  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
77  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
78  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
79  0xdd, 0xdd
80 };
81 
82 static u8 sha1_tc3_digest[] = {
83  0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac, 0x11, 0xcd,
84  0x91, 0xa3, 0x9a, 0xf4, 0x8a, 0xa1, 0x7b, 0x4f,
85  0x63, 0xf1, 0x75, 0xd3,
86 };
87 
88 /* *INDENT-OFF* */
89 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc3) = {
90  .name = "RFC2202 HMAC-SHA-1 TC3",
91  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
92  .key = TEST_DATA (sha1_tc3_key),
93  .plaintext = TEST_DATA (sha1_tc3_data),
94  .digest = TEST_DATA (sha1_tc3_digest),
95 };
96 /* *INDENT-ON* */
97 
98 static u8 sha1_tc4_key[25] = {
99  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
100  0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
101  0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
102  0x19
103 };
104 
105 static u8 sha1_tc4_data[50] = {
106  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
107  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
108  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
109  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
110  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
111  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
112  0xcd, 0xcd,
113 };
114 
115 static u8 sha1_tc4_digest[] = {
116  0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62, 0x50, 0xc6,
117  0xbc, 0x84, 0x14, 0xf9, 0xbf, 0x50, 0xc8, 0x6c,
118  0x2d, 0x72, 0x35, 0xda,
119 };
120 
121 /* *INDENT-OFF* */
122 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc4) = {
123  .name = "RFC2202 HMAC-SHA-1 TC4",
124  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
125  .key = TEST_DATA (sha1_tc4_key),
126  .plaintext = TEST_DATA (sha1_tc4_data),
127  .digest = TEST_DATA (sha1_tc4_digest),
128 };
129 /* *INDENT-ON* */
130 
131 static u8 sha1_tc5_key[20] = {
132  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
133  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
134  0x0c, 0x0c, 0x0c, 0x0c
135 };
136 
137 static char sha1_tc5_data[20] = "Test With Truncation";
138 
139 static u8 sha1_tc5_digest[] = {
140  0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f,
141  0xe7, 0xf2, 0x7b, 0xe1, 0xd5, 0x8b, 0xb9, 0x32,
142  0x4a, 0x9a, 0x5a, 0x04
143 };
144 
145 /* *INDENT-OFF* */
146 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc5) = {
147  .name = "RFC2202 HMAC-SHA-1 TC5",
148  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
149  .key = TEST_DATA (sha1_tc5_key),
150  .plaintext = TEST_DATA (sha1_tc5_data),
151  .digest = TEST_DATA (sha1_tc5_digest),
152 };
153 /* *INDENT-ON* */
154 
155 static u8 sha1_tc5_digest_96[12] = {
156  0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f,
157  0xe7, 0xf2, 0x7b, 0xe1
158 };
159 
160 /* *INDENT-OFF* */
161 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc5_trunc) = {
162  .name = "RFC2202 HMAC-SHA-1-96 TC5-trunc",
163  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
164  .key = TEST_DATA (sha1_tc5_key),
165  .plaintext = TEST_DATA (sha1_tc5_data),
166  .digest = TEST_DATA (sha1_tc5_digest_96),
167 };
168 /* *INDENT-ON* */
169 
170 static u8 sha1_tc6_key[80] = {
171  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
172  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
173  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
174  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
175  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
176  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
177  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
178  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
179  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
180  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
181 };
182 
183 static char sha1_tc6_data[54] =
184  "Test Using Larger Than Block-Size Key - Hash Key First";
185 
186 static u8 sha1_tc6_digest[] = {
187  0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0, 0x0e,
188  0x95, 0x70, 0x56, 0x37, 0xce, 0x8a, 0x3b, 0x55,
189  0xed, 0x40, 0x21, 0x12
190 };
191 
192 /* *INDENT-OFF* */
193 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc6) = {
194  .name = "RFC2202 HMAC-SHA-1 TC6",
195  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
196  .key = TEST_DATA (sha1_tc6_key),
197  .plaintext = TEST_DATA (sha1_tc6_data),
198  .digest = TEST_DATA (sha1_tc6_digest),
199 };
200 /* *INDENT-ON* */
201 
202 static char sha1_tc7_data[73] =
203  "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
204 
205 static u8 sha1_tc7_digest[20] = {
206  0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d, 0x78,
207  0x6d, 0x6b, 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x08,
208  0xbb, 0xff, 0x1a, 0x91
209 };
210 
211 /* *INDENT-OFF* */
212 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7) = {
213  .name = "RFC2202 HMAC-SHA-1 TC7",
214  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
215  .key = TEST_DATA (sha1_tc6_key),
216  .plaintext = TEST_DATA (sha1_tc7_data),
217  .digest = TEST_DATA (sha1_tc7_digest),
218 };
219 
220 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7_chained) = {
221  .name = "RFC2202 HMAC-SHA-1 TC7 [chained]",
222  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
223  .key = TEST_DATA (sha1_tc6_key),
224  .digest = TEST_DATA (sha1_tc7_digest),
225 
226  .is_chained = 1,
227  .pt_chunks = {
230  },
231 };
232 
233 UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7_inc) = {
234  .name = "HMAC-SHA-1 incremental (1024 B)",
235  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
236  .plaintext_incremental = 1024,
237  .key.length = 80,
238  .digest.length = 12,
239 };
240 /* *INDENT-ON* */
241 
242 /*
243  * fd.io coding-style-patch-verification: ON
244  *
245  * Local Variables:
246  * eval: (c-set-style "gnu")
247  * End:
248  */
static u8 sha1_tc3_data[50]
static u8 sha1_tc1_digest[]
static u8 sha1_tc4_key[25]
static u8 sha1_tc1_key[]
static u8 sha1_tc7_digest[20]
static char sha1_tc6_data[54]
static u8 sha1_tc2_digest[]
UNITTEST_REGISTER_CRYPTO_TEST(rfc_2202_sha1_tc1)
static u8 sha1_tc6_digest[]
unsigned char u8
Definition: types.h:56
static u8 sha1_tc3_key[20]
static char sha1_tc2_data[28]
static char sha1_tc2_key[4]
static char sha1_tc1_data[8]
static u8 sha1_tc6_key[80]
static char sha1_tc5_data[20]
static u8 sha1_tc4_digest[]
static char sha1_tc7_data[73]
static u8 sha1_tc3_digest[]
#define TEST_DATA(n)
Definition: crypto.h:63
#define TEST_DATA_CHUNK(s, off, n)
Definition: crypto.h:64
static u8 sha1_tc5_key[20]
static u8 sha1_tc4_data[50]
static u8 sha1_tc5_digest_96[12]
static u8 sha1_tc5_digest[]