Debian Codegraph @

test/psk.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2024 Daniel Wagner, SUSE Software Solutions
5 */
6
7 #include "nvme/linux.h"
8 #include <string.h>
9 #include <stdlib.h>
10 #include <errno.h>
11
12 #include <ccan/array_size/array_size.h>
13
14 #include <libnvme.h>
15
16 static int test_rc;
17
18 struct test_data_psk {
19 const unsigned char configured_psk[48];
20 size_t psk_length;
21 unsigned char version;
22 unsigned char hmac;
23 const char *exported_psk;
24 };
25
26 static struct test_data_psk test_data_psk[] = {
27 { { 0x55, 0x12, 0xDB, 0xB6,
28 0x73, 0x7D, 0x01, 0x06,
29 0xF6, 0x59, 0x75, 0xB7,
30 0x73, 0xDF, 0xB0, 0x11,
31 0xFF, 0xC3, 0x44, 0xBC,
32 0xF4, 0x42, 0xE2, 0xDD,
33 0x6D, 0x8B, 0xC4, 0x87,
34 0x0B, 0x5D, 0x5B, 0x03},
35 32, 1, NVME_HMAC_ALG_NONE,
36 "NVMeTLSkey-1:00:VRLbtnN9AQb2WXW3c9+wEf/DRLz0QuLdbYvEhwtdWwNf9LrZ:" },
37 { { 0x55, 0x12, 0xDB, 0xB6,
38 0x73, 0x7D, 0x01, 0x06,
39 0xF6, 0x59, 0x75, 0xB7,
40 0x73, 0xDF, 0xB0, 0x11,
41 0xFF, 0xC3, 0x44, 0xBC,
42 0xF4, 0x42, 0xE2, 0xDD,
43 0x6D, 0x8B, 0xC4, 0x87,
44 0x0B, 0x5D, 0x5B, 0x03},
45 32, 1, NVME_HMAC_ALG_SHA2_256,
46 "NVMeTLSkey-1:01:VRLbtnN9AQb2WXW3c9+wEf/DRLz0QuLdbYvEhwtdWwNf9LrZ:" },
47 { { 0x55, 0x12, 0xDB, 0xB6,
48 0x73, 0x7D, 0x01, 0x06,
49 0xF6, 0x59, 0x75, 0xB7,
50 0x73, 0xDF, 0xB0, 0x11,
51 0xFF, 0xC3, 0x44, 0xBC,
52 0xF4, 0x42, 0xE2, 0xDD,
53 0x6D, 0x8B, 0xC4, 0x87,
54 0x0B, 0x5D, 0x5B, 0x03,
55 0xFF, 0xC3, 0x44, 0xBC,
56 0xF4, 0x42, 0xE2, 0xDD,
57 0x6D, 0x8B, 0xC4, 0x87,
58 0x0B, 0x5D, 0x5B, 0x03},
59 48, 1, NVME_HMAC_ALG_SHA2_384,
60 "NVMeTLSkey-1:02:VRLbtnN9AQb2WXW3c9+wEf/DRLz0QuLdbYvEhwtdWwP/w0S89ELi3W2LxIcLXVsDn8kXZQ==:" },
61 };
62
63 struct test_data_identity {
64 const unsigned char configured_psk[48];
65 size_t psk_length;
66 unsigned char version;
67 unsigned char hmac;
68 const char *hostnqn;
69 const char *subsysnqn;
70 const char *identity;
71 };
72
73 static struct test_data_identity test_data_identity[] = {
74 { { 0x55, 0x12, 0xDB, 0xB6,
75 0x73, 0x7D, 0x01, 0x06,
76 0xF6, 0x59, 0x75, 0xB7,
77 0x73, 0xDF, 0xB0, 0x11,
78 0xFF, 0xC3, 0x44, 0xBC,
79 0xF4, 0x42, 0xE2, 0xDD,
80 0x6D, 0x8B, 0xC4, 0x87,
81 0x0B, 0x5D, 0x5B, 0x03},
82 32, 1, NVME_HMAC_ALG_SHA2_256,
83 "nqn.psk-test-host", "nqn.psk-test-subsys",
84 "NVMe1R01 nqn.psk-test-host nqn.psk-test-subsys iSbjiStwJ/1TrTvDlt2fjFmzvsytOJelidNnA+X5lEU=" },
85 { { 0x55, 0x12, 0xDB, 0xB6,
86 0x73, 0x7D, 0x01, 0x06,
87 0xF6, 0x59, 0x75, 0xB7,
88 0x73, 0xDF, 0xB0, 0x11,
89 0xFF, 0xC3, 0x44, 0xBC,
90 0xF4, 0x42, 0xE2, 0xDD,
91 0x6D, 0x8B, 0xC4, 0x87,
92 0x0B, 0x5D, 0x5B, 0x03,
93 0xFF, 0xC3, 0x44, 0xBC,
94 0xF4, 0x42, 0xE2, 0xDD,
95 0x6D, 0x8B, 0xC4, 0x87,
96 0x0B, 0x5D, 0x5B, 0x03},
97 48, 1, NVME_HMAC_ALG_SHA2_384,
98 "nqn.psk-test-host", "nqn.psk-test-subsys",
99 "NVMe1R02 nqn.psk-test-host nqn.psk-test-subsys QhW2+Rp6RzHlNtCslyRxMnwJ11tKKhz8JCAQpQ+XUD8f9td1VeH5h53yz2wKJG1a" },
100 };
101
102 static struct test_data_identity test_data_identity_compat[] = {
103 { { 0x55, 0x12, 0xDB, 0xB6,
104 0x73, 0x7D, 0x01, 0x06,
105 0xF6, 0x59, 0x75, 0xB7,
106 0x73, 0xDF, 0xB0, 0x11,
107 0xFF, 0xC3, 0x44, 0xBC,
108 0xF4, 0x42, 0xE2, 0xDD,
109 0x6D, 0x8B, 0xC4, 0x87,
110 0x0B, 0x5D, 0x5B, 0x03},
111 32, 1, NVME_HMAC_ALG_SHA2_256,
112 "nqn.psk-test-host", "nqn.psk-test-subsys",
113 "NVMe1R01 nqn.psk-test-host nqn.psk-test-subsys 66GuqV08TsAGII39teWUfwQwizjv06Jy8jOcX3NAAzM=" },
114 { { 0x55, 0x12, 0xDB, 0xB6,
115 0x73, 0x7D, 0x01, 0x06,
116 0xF6, 0x59, 0x75, 0xB7,
117 0x73, 0xDF, 0xB0, 0x11,
118 0xFF, 0xC3, 0x44, 0xBC,
119 0xF4, 0x42, 0xE2, 0xDD,
120 0x6D, 0x8B, 0xC4, 0x87,
121 0x0B, 0x5D, 0x5B, 0x03,
122 0xFF, 0xC3, 0x44, 0xBC,
123 0xF4, 0x42, 0xE2, 0xDD,
124 0x6D, 0x8B, 0xC4, 0x87,
125 0x0B, 0x5D, 0x5B, 0x03},
126 48, 1, NVME_HMAC_ALG_SHA2_384,
127 "nqn.psk-test-host", "nqn.psk-test-subsys",
128 "NVMe1R02 nqn.psk-test-host nqn.psk-test-subsys RsKmYJ3nAn1ApjjMloJFbAkLPivONDAX/xW327YBUsn2eGShXSjCZvBaOxscLqmz" },
129 };
130
131 static void check_str(const char *exp, const char *res)
132 {
133 if (!strcmp(res, exp))
134 return;
135
136 printf("ERROR: got '%s', expected '%s'\n", res, exp);
137
138 test_rc = 1;
139 }
140
141 static void export_test(struct test_data_psk *test)
142 {
143 char *psk;
144
145 if (test->version != 1 ||
146 !(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
147 test->hmac == NVME_HMAC_ALG_SHA2_384))
148 return;
149
150 printf("test nvme_export_tls_key hmac %d %s\n",
151 test->hmac, test->exported_psk);
152
153 psk = nvme_export_tls_key(test->configured_psk, test->psk_length);
154 if (!psk) {
155 test_rc = 1;
156 printf("ERROR: nvme_export_tls_key() failed with %d\n", errno);
157 return;
158 }
159 check_str(test->exported_psk, psk);
160 free(psk);
161 }
162
163 static void import_test(struct test_data_psk *test)
164 {
165 unsigned char *psk;
166 int psk_length;
167 unsigned int hmac;
168
169 if (test->version != 1 ||
170 !(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
171 test->hmac == NVME_HMAC_ALG_SHA2_384))
172 return;
173
174 printf("test nvme_import_tls_key hmac %d %s\n",
175 test->hmac, test->exported_psk);
176
177 psk = nvme_import_tls_key(test->exported_psk, &psk_length, &hmac);
178 if (!psk) {
179 test_rc = 1;
180 printf("ERROR: nvme_import_tls_key() failed with %d\n", errno);
181 return;
182 }
183
184 if (test->hmac != hmac) {
185 test_rc = 1;
186 printf("ERROR: hmac parsing failed\n");
187 goto out;
188 }
189
190 if (test->psk_length != psk_length) {
191 test_rc = 1;
192 printf("ERROR: length parsing failed\n");
193 goto out;
194 }
195 if (memcmp(test->configured_psk, psk, psk_length)) {
196 test_rc = 1;
197 printf("ERROR: parsing psk failed\n");
198 }
199
200 out:
201 free(psk);
202 }
203
204 static void export_versioned_test(struct test_data_psk *test)
205 {
206 char *psk;
207
208 if (test->version != 1)
209 return;
210
211 printf("test nvme_export_tls_key_versioned hmac %d %s\n",
212 test->hmac, test->exported_psk);
213
214 psk = nvme_export_tls_key_versioned(test->version, test->hmac,
215 test->configured_psk,
216 test->psk_length);
217 if (!psk) {
218 test_rc = 1;
219 printf("ERROR: nvme_export_tls_key_versioned() failed with %d\n",
220 errno);
221 return;
222 }
223
224 check_str(test->exported_psk, psk);
225
226 free(psk);
227 }
228
229 static void import_versioned_test(struct test_data_psk *test)
230 {
231 unsigned char *psk;
232 unsigned char version;
233 unsigned char hmac;
234 size_t psk_length;
235
236 if (test->version != 1)
237 return;
238
239 printf("test nvme_import_tls_key_versioned hmac %d %s\n",
240 test->hmac, test->exported_psk);
241
242 psk = nvme_import_tls_key_versioned(test->exported_psk, &version,
243 &hmac, &psk_length);
244 if (!psk) {
245 test_rc = 1;
246 printf("ERROR: nvme_import_tls_key_versioned() failed with %d\n",
247 errno);
248 return;
249 }
250
251 if (test->version != version) {
252 test_rc = 1;
253 printf("ERROR: version parsing failed\n");
254 goto out;
255 }
256
257 if (test->hmac != hmac) {
258 test_rc = 1;
259 printf("ERROR: hmac parsing failed\n");
260 goto out;
261 }
262
263 if (test->psk_length != psk_length) {
264 test_rc = 1;
265 printf("ERROR: length parsing failed\n");
266 goto out;
267 }
268
269 if (memcmp(test->configured_psk, psk, psk_length)) {
270 test_rc = 1;
271 printf("ERROR: parsing psk failed\n");
272 }
273
274 out:
275 free(psk);
276 }
277
278 static void identity_test(struct test_data_identity *test)
279 {
280 char *id;
281
282 if (test->version != 1 ||
283 !(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
284 test->hmac == NVME_HMAC_ALG_SHA2_384))
285 return;
286
287 printf("test nvme_generate_tls_key_identity host %s subsys %s hmac %d %s\n",
288 test->hostnqn, test->subsysnqn, test->hmac, test->identity);
289
290 id = nvme_generate_tls_key_identity(test->hostnqn, test->subsysnqn,
291 test->version, test->hmac,
292 (unsigned char *)test->configured_psk,
293 test->psk_length);
294 if (!id) {
295 if (errno == ENOTSUP)
296 return;
297 test_rc = 1;
298 printf("ERROR: nvme_generate_tls_key_identity() failed with %d\n", errno);
299 return;
300 }
301 check_str(test->identity, id);
302 free(id);
303 }
304
305 static void identity_test_compat(struct test_data_identity *test)
306 {
307 char *id;
308
309 if (test->version != 1 ||
310 !(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
311 test->hmac == NVME_HMAC_ALG_SHA2_384))
312 return;
313
314 printf("test nvme_generate_tls_key_identity_compat host %s subsys %s hmac %d %s\n",
315 test->hostnqn, test->subsysnqn, test->hmac, test->identity);
316
317 id = nvme_generate_tls_key_identity_compat(test->hostnqn,
318 test->subsysnqn,
319 test->version, test->hmac,
320 (unsigned char *)test->configured_psk,
321 test->psk_length);
322 if (!id) {
323 if (errno == ENOTSUP)
324 return;
325 test_rc = 1;
326 printf("ERROR: nvme_generate_tls_key_identity_compat() failed with %d\n", errno);
327 return;
328 }
329 check_str(test->identity, id);
330 free(id);
331 }
332
333 int main(void)
334 {
335 for (int i = 0; i < ARRAY_SIZE(test_data_psk); i++)
336 export_test(&test_data_psk[i]);
337
338 for (int i = 0; i < ARRAY_SIZE(test_data_psk); i++)
339 import_test(&test_data_psk[i]);
340
341 for (int i = 0; i < ARRAY_SIZE(test_data_psk); i++)
342 export_versioned_test(&test_data_psk[i]);
343
344 for (int i = 0; i < ARRAY_SIZE(test_data_psk); i++)
345 import_versioned_test(&test_data_psk[i]);
346
347 for (int i = 0; i < ARRAY_SIZE(test_data_identity); i++)
348 identity_test(&test_data_identity[i]);
349
350 for (int i = 0; i < ARRAY_SIZE(test_data_identity_compat); i++)
351 identity_test_compat(&test_data_identity_compat[i]);
352
353 return test_rc ? EXIT_FAILURE : EXIT_SUCCESS;
354 }