aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/unittest/crypto/rfc2202_hmac_sha1.c
blob: aa440625cc653b69554e0d214e0a4c6284ec5824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Test vectors published in RFC2202 */

#include <vppinfra/clib.h>
#include <vnet/crypto/crypto.h>
#include <unittest/crypto/crypto.h>

static u8 sha1_tc1_key[] = {
  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  0x0b, 0x0b, 0x0b, 0x0b
};

static char sha1_tc1_data[8] = "Hi There";

static u8 sha1_tc1_digest[] = {
  0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
  0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
  0xf1, 0x46, 0xbe, 0x00
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc1) = {
  .name = "RFC2202 HMAC-SHA-1 TC1",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc1_key),
  .plaintext = TEST_DATA (sha1_tc1_data),
  .digest = TEST_DATA (sha1_tc1_digest),
};

static char sha1_tc2_key[4] = "Jefe";

static char sha1_tc2_data[28] = "what do ya want for nothing?";

static u8 sha1_tc2_digest[] = {
  0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2,
  0xd2, 0x74, 0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c,
  0x25, 0x9a, 0x7c, 0x79
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc2) = {
  .name = "RFC2202 HMAC-SHA-1 TC2",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc2_key),
  .plaintext = TEST_DATA (sha1_tc2_data),
  .digest = TEST_DATA (sha1_tc2_digest),
};

static u8 sha1_tc3_key[20] = {
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa
};

static u8 sha1_tc3_data[50] = {
  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
  0xdd, 0xdd
};

static u8 sha1_tc3_digest[] = {
  0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac, 0x11, 0xcd,
  0x91, 0xa3, 0x9a, 0xf4, 0x8a, 0xa1, 0x7b, 0x4f,
  0x63, 0xf1, 0x75, 0xd3,
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc3) = {
  .name = "RFC2202 HMAC-SHA-1 TC3",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc3_key),
  .plaintext = TEST_DATA (sha1_tc3_data),
  .digest = TEST_DATA (sha1_tc3_digest),
};

static u8 sha1_tc4_key[25] = {
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
  0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  0x19
};

static u8 sha1_tc4_data[50] = {
  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
  0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
  0xcd, 0xcd,
};

static u8 sha1_tc4_digest[] = {
  0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62, 0x50, 0xc6,
  0xbc, 0x84, 0x14, 0xf9, 0xbf, 0x50, 0xc8, 0x6c,
  0x2d, 0x72, 0x35, 0xda,
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc4) = {
  .name = "RFC2202 HMAC-SHA-1 TC4",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc4_key),
  .plaintext = TEST_DATA (sha1_tc4_data),
  .digest = TEST_DATA (sha1_tc4_digest),
};

static u8 sha1_tc5_key[20] = {
  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  0x0c, 0x0c, 0x0c, 0x0c
};

static char sha1_tc5_data[20] = "Test With Truncation";

static u8 sha1_tc5_digest[] = {
  0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f,
  0xe7, 0xf2, 0x7b, 0xe1, 0xd5, 0x8b, 0xb9, 0x32,
  0x4a, 0x9a, 0x5a, 0x04
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc5) = {
  .name = "RFC2202 HMAC-SHA-1 TC5",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc5_key),
  .plaintext = TEST_DATA (sha1_tc5_data),
  .digest = TEST_DATA (sha1_tc5_digest),
};

static u8 sha1_tc5_digest_96[12] = {
  0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f,
  0xe7, 0xf2, 0x7b, 0xe1
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc5_trunc) = {
  .name = "RFC2202 HMAC-SHA-1-96 TC5-trunc",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc5_key),
  .plaintext = TEST_DATA (sha1_tc5_data),
  .digest = TEST_DATA (sha1_tc5_digest_96),
};

static u8 sha1_tc6_key[80] = {
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
};

static char sha1_tc6_data[54] =
  "Test Using Larger Than Block-Size Key - Hash Key First";

static u8 sha1_tc6_digest[] = {
  0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0, 0x0e,
  0x95, 0x70, 0x56, 0x37, 0xce, 0x8a, 0x3b, 0x55,
  0xed, 0x40, 0x21, 0x12
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc6) = {
  .name = "RFC2202 HMAC-SHA-1 TC6",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc6_key),
  .plaintext = TEST_DATA (sha1_tc6_data),
  .digest = TEST_DATA (sha1_tc6_digest),
};

static char sha1_tc7_data[73] =
  "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";

static u8 sha1_tc7_digest[20] = {
  0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d, 0x78,
  0x6d, 0x6b, 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x08,
  0xbb, 0xff, 0x1a, 0x91
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7) = {
  .name = "RFC2202 HMAC-SHA-1 TC7",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc6_key),
  .plaintext = TEST_DATA (sha1_tc7_data),
  .digest = TEST_DATA (sha1_tc7_digest),
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7_chained) = {
  .name = "RFC2202 HMAC-SHA-1 TC7 [chained]",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .key = TEST_DATA (sha1_tc6_key),
  .digest = TEST_DATA (sha1_tc7_digest),

  .is_chained = 1,
  .pt_chunks = {
    TEST_DATA_CHUNK (sha1_tc7_data, 0, 40),
    TEST_DATA_CHUNK (sha1_tc7_data, 40, 33)
  },
};

UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7_inc) = {
  .name = "HMAC-SHA-1 incremental (1024 B)",
  .alg = VNET_CRYPTO_ALG_HMAC_SHA1,
  .plaintext_incremental = 1024,
  .key.length = 80,
  .digest.length = 12,
};

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */