summaryrefslogtreecommitdiffstats
path: root/test/psk.c
blob: 02cb6d813a653b95d690bcba4ab59f000eff6117 (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
// SPDX-License-Identifier: LGPL-2.1-or-later
/**
 * This file is part of libnvme.
 * Copyright (c) 2024 Daniel Wagner, SUSE Software Solutions
 */

#include "nvme/linux.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <ccan/array_size/array_size.h>

#include <libnvme.h>

static int test_rc;

struct test_data {
	const unsigned char configured_psk[48];
	size_t psk_length;
	unsigned char version;
	unsigned char hmac;
	const char *exported_psk;
};

static struct test_data test_data[] = {
	{ { 0x55, 0x12, 0xDB, 0xB6,
	    0x73, 0x7D, 0x01, 0x06,
	    0xF6, 0x59, 0x75, 0xB7,
	    0x73, 0xDF, 0xB0, 0x11,
	    0xFF, 0xC3, 0x44, 0xBC,
	    0xF4, 0x42, 0xE2, 0xDD,
	    0x6D, 0x8B, 0xC4, 0x87,
	    0x0B, 0x5D, 0x5B, 0x03},
	  32, 1, NVME_HMAC_ALG_NONE,
	  "NVMeTLSkey-1:00:VRLbtnN9AQb2WXW3c9+wEf/DRLz0QuLdbYvEhwtdWwNf9LrZ:" },
	{ { 0x55, 0x12, 0xDB, 0xB6,
	    0x73, 0x7D, 0x01, 0x06,
	    0xF6, 0x59, 0x75, 0xB7,
	    0x73, 0xDF, 0xB0, 0x11,
	    0xFF, 0xC3, 0x44, 0xBC,
	    0xF4, 0x42, 0xE2, 0xDD,
	    0x6D, 0x8B, 0xC4, 0x87,
	    0x0B, 0x5D, 0x5B, 0x03},
	  32, 1, NVME_HMAC_ALG_SHA2_256,
	  "NVMeTLSkey-1:01:VRLbtnN9AQb2WXW3c9+wEf/DRLz0QuLdbYvEhwtdWwNf9LrZ:" },
	{ { 0x55, 0x12, 0xDB, 0xB6,
	    0x73, 0x7D, 0x01, 0x06,
	    0xF6, 0x59, 0x75, 0xB7,
	    0x73, 0xDF, 0xB0, 0x11,
	    0xFF, 0xC3, 0x44, 0xBC,
	    0xF4, 0x42, 0xE2, 0xDD,
	    0x6D, 0x8B, 0xC4, 0x87,
	    0x0B, 0x5D, 0x5B, 0x03,
	    0xFF, 0xC3, 0x44, 0xBC,
	    0xF4, 0x42, 0xE2, 0xDD,
	    0x6D, 0x8B, 0xC4, 0x87,
	    0x0B, 0x5D, 0x5B, 0x03},
	  48, 1, NVME_HMAC_ALG_SHA2_384,
	  "NVMeTLSkey-1:02:VRLbtnN9AQb2WXW3c9+wEf/DRLz0QuLdbYvEhwtdWwP/w0S89ELi3W2LxIcLXVsDn8kXZQ==:" },
};

static void check_str(const char *exp, const char *res)
{
	if (!strcmp(res, exp))
		return;

	printf("ERROR: got '%s', expected '%s'\n", res, exp);

	test_rc = 1;
}

static void export_test(struct test_data *test)
{
	char *psk;

	if (test->version != 1 ||
	    !(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
	      test->hmac == NVME_HMAC_ALG_SHA2_384))
		return;

	printf("test nvme_export_tls_key hmac %d %s\n",
	       test->hmac, test->exported_psk);

	psk = nvme_export_tls_key(test->configured_psk, test->psk_length);
	if (!psk) {
		test_rc = 1;
		printf("ERROR: nvme_export_tls_key() failed with %d\n", errno);
		return;
	}
	check_str(test->exported_psk, psk);
	free(psk);
}

static void import_test(struct test_data *test)
{
	unsigned char *psk;
	int psk_length;
	unsigned int hmac;

	if (test->version != 1 ||
	    !(test->hmac == NVME_HMAC_ALG_SHA2_256 ||
	      test->hmac == NVME_HMAC_ALG_SHA2_384))
		return;

	printf("test nvme_import_tls_key hmac %d %s\n",
	       test->hmac, test->exported_psk);

	psk = nvme_import_tls_key(test->exported_psk, &psk_length, &hmac);
	if (!psk) {
		test_rc = 1;
		printf("ERROR: nvme_import_tls_key() failed with %d\n", errno);
		return;
	}

	if (test->hmac != hmac) {
		test_rc = 1;
		printf("ERROR: hmac parsing failed\n");
		goto out;
	}

	if (test->psk_length != psk_length) {
		test_rc = 1;
		printf("ERROR: length parsing failed\n");
		goto out;
	}
	if (memcmp(test->configured_psk, psk, psk_length)) {
		test_rc = 1;
		printf("ERROR: parsing psk failed\n");
	}

out:
	free(psk);
}

static void export_versioned_test(struct test_data *test)
{
	char *psk;

	if (test->version != 1)
		return;

	printf("test nvme_export_tls_key_versioned hmac %d %s\n",
	       test->hmac, test->exported_psk);

	psk = nvme_export_tls_key_versioned(test->version, test->hmac,
					    test->configured_psk,
					    test->psk_length);
	if (!psk) {
		test_rc = 1;
		printf("ERROR: nvme_export_tls_key_versioned() failed with %d\n",
		       errno);
		return;
	}

	check_str(test->exported_psk, psk);

	free(psk);
}

static void import_versioned_test(struct test_data *test)
{
	unsigned char *psk;
	unsigned char version;
	unsigned char hmac;
	size_t psk_length;

	if (test->version != 1)
		return;

	printf("test nvme_import_tls_key_versioned hmac %d %s\n",
	       test->hmac, test->exported_psk);

	psk = nvme_import_tls_key_versioned(test->exported_psk, &version,
					    &hmac, &psk_length);
	if (!psk) {
		test_rc = 1;
		printf("ERROR: nvme_import_tls_key_versioned() failed with %d\n",
		       errno);
		return;
	}

	if (test->version != version) {
		test_rc = 1;
		printf("ERROR: version parsing failed\n");
		goto out;
	}

	if (test->hmac != hmac) {
		test_rc = 1;
		printf("ERROR: hmac parsing failed\n");
		goto out;
	}

	if (test->psk_length != psk_length) {
		test_rc = 1;
		printf("ERROR: length parsing failed\n");
		goto out;
	}

	if (memcmp(test->configured_psk, psk, psk_length)) {
		test_rc = 1;
		printf("ERROR: parsing psk failed\n");
	}

out:
	free(psk);
}

int main(void)
{
	for (int i = 0; i < ARRAY_SIZE(test_data); i++)
		export_test(&test_data[i]);

	for (int i = 0; i < ARRAY_SIZE(test_data); i++)
		import_test(&test_data[i]);

	for (int i = 0; i < ARRAY_SIZE(test_data); i++)
		export_versioned_test(&test_data[i]);

	for (int i = 0; i < ARRAY_SIZE(test_data); i++)
		import_versioned_test(&test_data[i]);

	return test_rc ? EXIT_FAILURE : EXIT_SUCCESS;
}