Skip to content

Commit 97c100a

Browse files
dwsuseigaw
authored andcommitted
test: add test case for importing/exporting PSKs
Test the pre-shared key interchange format import/export function. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 0e0f67b commit 97c100a

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

test/meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ if conf.get('HAVE_NETDB')
104104
test('util', test_util)
105105
endif
106106

107+
psk = executable(
108+
'test-psk',
109+
['psk.c'],
110+
dependencies: libnvme_dep,
111+
include_directories: [incdir, internal_incdir]
112+
)
113+
114+
test('psk', psk)
115+
107116
subdir('ioctl')
108117
subdir('nbft')
109118

test/psk.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 {
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 test_data[] = {
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+
static void check_str(const char *exp, const char *res)
64+
{
65+
if (!strcmp(res, exp))
66+
return;
67+
68+
printf("ERROR: got '%s', expected '%s'\n", res, exp);
69+
70+
test_rc = 1;
71+
}
72+
73+
static void export_test(struct test_data *test)
74+
{
75+
char *psk;
76+
77+
if (test->version != 1 ||
78+
!(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
79+
test->hmac == NVME_HMAC_ALG_SHA2_384))
80+
return;
81+
82+
printf("test nvme_export_tls_key hmac %d %s\n",
83+
test->hmac, test->exported_psk);
84+
85+
psk = nvme_export_tls_key(test->configured_psk, test->psk_length);
86+
if (!psk) {
87+
test_rc = 1;
88+
printf("ERROR: nvme_export_tls_key() failed with %d\n", errno);
89+
return;
90+
}
91+
check_str(test->exported_psk, psk);
92+
free(psk);
93+
}
94+
95+
static void import_test(struct test_data *test)
96+
{
97+
unsigned char *psk;
98+
int psk_length;
99+
unsigned int hmac;
100+
101+
if (test->version != 1 ||
102+
!(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
103+
test->hmac == NVME_HMAC_ALG_SHA2_384))
104+
return;
105+
106+
printf("test nvme_import_tls_key hmac %d %s\n",
107+
test->hmac, test->exported_psk);
108+
109+
psk = nvme_import_tls_key(test->exported_psk, &psk_length, &hmac);
110+
if (!psk) {
111+
test_rc = 1;
112+
printf("ERROR: nvme_import_tls_key() failed with %d\n", errno);
113+
return;
114+
}
115+
116+
if (test->hmac != hmac) {
117+
test_rc = 1;
118+
printf("ERROR: hmac parsing failed\n");
119+
goto out;
120+
}
121+
122+
if (test->psk_length != psk_length) {
123+
test_rc = 1;
124+
printf("ERROR: length parsing failed\n");
125+
goto out;
126+
}
127+
if (memcmp(test->configured_psk, psk, psk_length)) {
128+
test_rc = 1;
129+
printf("ERROR: parsing psk failed\n");
130+
}
131+
132+
out:
133+
free(psk);
134+
}
135+
136+
int main(void)
137+
{
138+
for (int i = 0; i < ARRAY_SIZE(test_data); i++)
139+
export_test(&test_data[i]);
140+
141+
for (int i = 0; i < ARRAY_SIZE(test_data); i++)
142+
import_test(&test_data[i]);
143+
144+
return test_rc ? EXIT_FAILURE : EXIT_SUCCESS;
145+
}

0 commit comments

Comments
 (0)