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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* mgmt-keywrap.h
* -----------
* Management CLI functions related to AES keywrap
*
* Copyright (c) 2018, NORDUnet A/S All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the NORDUnet nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define HAL_OK CMSIS_HAL_OK
#include "stm-init.h"
#include "stm-uart.h"
#include "mgmt-cli.h"
#include "mgmt-keywrap.h"
#undef HAL_OK
#define HAL_OK LIBHAL_OK
#include "hal.h"
#include "hal_internal.h"
#undef HAL_OK
#include <string.h>
/* test vectors and test code are from test-aes-key-wrap.c */
/*
* Test cases from RFC 5649 all use a 192-bit key, which our AES
* implementation doesn't support, so had to write our own.
*/
static const uint8_t Q[] = { /* Plaintext, 81 bytes */
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x20, 0x20, 0x4d, 0x79, 0x20, 0x6e,
0x61, 0x6d, 0x65, 0x20, 0x69, 0x73, 0x20, 0x49, 0x6e, 0x69, 0x67, 0x6f,
0x20, 0x4d, 0x6f, 0x6e, 0x74, 0x6f, 0x79, 0x61, 0x2e, 0x20, 0x20, 0x59,
0x6f, 0x75, 0x20, 0x62, 0x72, 0x6f, 0x6b, 0x65, 0x20, 0x6d, 0x79, 0x20,
0x41, 0x45, 0x53, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x77, 0x72, 0x61, 0x70,
0x70, 0x65, 0x72, 0x2e, 0x20, 0x20, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72,
0x65, 0x20, 0x74, 0x6f, 0x20, 0x64, 0x69, 0x65, 0x2e
};
static const uint8_t K_128[] = { /* 128-bit KEK, 16 bytes */
0xbc, 0x2a, 0xd8, 0x90, 0xd8, 0x91, 0x10, 0x65, 0xf0, 0x42, 0x10, 0x1b,
0x4a, 0x6b, 0xaf, 0x99
};
static const uint8_t K_256[] = { /* 256-bit KEK, 32 bytes */
0xe3, 0x97, 0x52, 0x81, 0x2b, 0x7e, 0xc2, 0xa4, 0x6a, 0xac, 0x50, 0x18,
0x0d, 0x10, 0xc6, 0x85, 0x2c, 0xcf, 0x86, 0x0a, 0xa9, 0x4f, 0x69, 0xab,
0x16, 0xa6, 0x4f, 0x3e, 0x96, 0xa0, 0xbd, 0x9e
};
static const uint8_t C_128[] = { /* Plaintext wrapped by 128-bit KEK, 96 bytes */
0xb0, 0x10, 0x91, 0x7b, 0xe7, 0x67, 0x9c, 0x10, 0x16, 0x64, 0xe7, 0x73,
0xd2, 0x68, 0xba, 0xed, 0x8c, 0x50, 0x49, 0x80, 0x16, 0x2f, 0x4e, 0x97,
0xe8, 0x45, 0x5c, 0x2f, 0x2b, 0x7a, 0x88, 0x0e, 0xd8, 0xef, 0xaa, 0x40,
0xb0, 0x2e, 0xb4, 0x50, 0xe7, 0x60, 0xf7, 0xbb, 0xed, 0x56, 0x79, 0x16,
0x65, 0xb7, 0x13, 0x9b, 0x4c, 0x66, 0x86, 0x5f, 0x4d, 0x53, 0x2d, 0xcd,
0x83, 0x41, 0x01, 0x35, 0x0d, 0x06, 0x39, 0x4e, 0x9e, 0xfe, 0x68, 0xc5,
0x2f, 0x37, 0x33, 0x99, 0xbb, 0x88, 0xf7, 0x76, 0x1e, 0x82, 0x48, 0xd6,
0xa2, 0xf3, 0x9b, 0x92, 0x01, 0x65, 0xcb, 0x48, 0x36, 0xf5, 0x42, 0xd3
};
static const uint8_t C_256[] = { /* Plaintext wrapped by 256-bit KEK, 96 bytes */
0x08, 0x00, 0xbc, 0x1b, 0x35, 0xe4, 0x2a, 0x69, 0x3f, 0x43, 0x07, 0x54,
0x31, 0xba, 0xb6, 0x89, 0x7c, 0x64, 0x9f, 0x03, 0x84, 0xc4, 0x4a, 0x71,
0xdb, 0xcb, 0xae, 0x55, 0x30, 0xdf, 0xb0, 0x2b, 0xc3, 0x91, 0x5d, 0x07,
0xa9, 0x24, 0xdb, 0xe7, 0xbe, 0x4d, 0x0d, 0x62, 0xd4, 0xf8, 0xb1, 0x94,
0xf1, 0xb9, 0x22, 0xb5, 0x94, 0xab, 0x7e, 0x0b, 0x15, 0x6a, 0xd9, 0x5f,
0x6c, 0x20, 0xb7, 0x7e, 0x13, 0x19, 0xfa, 0xc4, 0x70, 0xec, 0x0d, 0xbd,
0xf7, 0x01, 0xc6, 0xb3, 0x9a, 0x19, 0xaf, 0xf2, 0x47, 0x68, 0xea, 0x7e,
0x97, 0x7e, 0x52, 0x2e, 0xd4, 0x03, 0x31, 0xcb, 0x22, 0xb6, 0xfe, 0xf5
};
static int run_test(struct cli_def *cli,
const uint8_t * const K, const size_t K_len,
const uint8_t * const C, const size_t C_len)
{
#define TC_BUFSIZE 96 /* sizeof(C) */
const size_t Q_len = sizeof(Q);
uint8_t q[TC_BUFSIZE], c[TC_BUFSIZE];
size_t q_len = sizeof(q), c_len = sizeof(c);
hal_error_t err;
int ok1 = 1, ok2 = 1;
/*
* Wrap and compare results.
*/
cli_print(cli, "Wrapping with %lu-bit KEK...", (unsigned long) K_len * 8);
if ((err = hal_aes_keywrap(NULL, K, K_len, Q, Q_len, c, &c_len)) != LIBHAL_OK) {
cli_print(cli, "Couldn't wrap with %lu-bit KEK: %s",
(unsigned long) K_len * 8, hal_error_string(err));
ok1 = 0;
}
else if (C_len != c_len || memcmp(C, c, C_len) != 0) {
cli_print(cli, "Ciphertext mismatch:\n Want: ");
uart_send_hexdump(C, 0, C_len - 1);
cli_print(cli, "\n Got: ");
uart_send_hexdump(c, 0, c_len - 1);
cli_print(cli, "");
ok1 = 0;
}
else {
cli_print(cli, "OK");
}
/*
* Unwrap and compare results.
*/
cli_print(cli, "Unwrapping with %lu-bit KEK...", (unsigned long) K_len * 8);
if ((err = hal_aes_keyunwrap(NULL, K, K_len, C, C_len, q, &q_len)) != LIBHAL_OK) {
cli_print(cli, "Couldn't unwrap with %lu-bit KEK: %s",
(unsigned long) K_len * 8, hal_error_string(err));
ok2 = 0;
}
else if (Q_len != q_len || memcmp(Q, q, Q_len) != 0) {
cli_print(cli, "Plaintext mismatch:\n Want: ");
uart_send_hexdump(Q, 0, Q_len - 1);
cli_print(cli, "\n Got: ");
uart_send_hexdump(q, 0, q_len - 1);
cli_print(cli, "");
ok2 = 0;
}
else {
cli_print(cli, "OK");
}
return ok1 && ok2;
}
static int cmd_keywrap_test(struct cli_def *cli, const char *command, char *argv[], int argc)
{
command = command;
if (argc == 0) {
cli_print(cli, "1. Test vectors with software keywrap");
hal_aes_use_keywrap_core(0);
run_test(cli, K_128, sizeof(K_128), C_128, sizeof(C_128));
run_test(cli, K_256, sizeof(K_256), C_256, sizeof(C_256));
cli_print(cli, "\n2. Test vectors with keywrap core");
if (hal_aes_use_keywrap_core(1) == 0) {
cli_print(cli, "keywrap core not found, skipping");
}
else {
hal_aes_use_keywrap_core(1);
run_test(cli, K_128, sizeof(K_128), C_128, sizeof(C_128));
run_test(cli, K_256, sizeof(K_256), C_256, sizeof(C_256));
}
cli_print(cli, "\nFor more tests: keywrap test <keysize> <iterations>");
return CLI_OK;
}
hal_error_t err;
if (argc != 2) {
usage:
cli_print(cli, "Syntax: keywrap test <keysize> <iterations>");
return CLI_ERROR;
}
const int keysize = atoi(argv[0]);
const int iterations = atoi(argv[1]);
if (keysize <= 0 || iterations <= 0)
goto usage;
uint8_t Q[keysize + 8]; size_t Q_len;
uint8_t C[keysize + 8]; size_t C_len;
memset(C, 0, sizeof(C));
if ((err = hal_get_random(NULL, Q, keysize)) != LIBHAL_OK) {
cli_print(cli, "hal_get_random: %s", hal_error_string(err));
return CLI_ERROR;
}
cli_print(cli, "1. sanity test");
C_len = sizeof(C);
if ((err = hal_aes_keywrap(NULL, K_256, sizeof(K_256), Q, keysize, C, &C_len)) != LIBHAL_OK) {
cli_print(cli, "hal_aes_keywrap: %s", hal_error_string(err));
return CLI_ERROR;
}
for (int i = 0; i <= 1; ++i) {
if (!hal_aes_use_keywrap_core(i) && i) {
cli_print(cli, "keywrap core not found, skipping");
continue;
}
uint8_t q[keysize + 8];
size_t q_len = sizeof(q);
if ((err = hal_aes_keyunwrap(NULL, K_256, sizeof(K_256), C, C_len, q, &q_len)) != LIBHAL_OK) {
cli_print(cli, "hal_aes_keyunwrap: %s", hal_error_string(err));
return CLI_ERROR;
}
if (q_len != (size_t)keysize) {
cli_print(cli, "unwrap size mismatch: expected %d, got %d", (int)keysize, (int)q_len);
return CLI_ERROR;
}
if (memcmp(Q, q, q_len) != 0) {
cli_print(cli, "unwrap mismatch:\n Want: ");
uart_send_hexdump(Q, 0, Q_len - 1);
cli_print(cli, "\n Got: ");
uart_send_hexdump(q, 0, q_len - 1);
cli_print(cli, "");
return CLI_ERROR;
}
cli_print(cli, "with %s: OK", i ? "keywrap core" : "software keywrap");
}
cli_print(cli, "\n2. wrap timing with software keywrap");
hal_aes_use_keywrap_core(0);
uint32_t start = HAL_GetTick();
for (int i = 0; i < iterations; ++i) {
C_len = sizeof(C);
if ((err = hal_aes_keywrap(NULL, K_256, sizeof(K_256), Q, keysize, C, &C_len)) != LIBHAL_OK) {
cli_print(cli, "hal_aes_keywrap: %s", hal_error_string(err));
return CLI_ERROR;
}
}
uint32_t elapsed = HAL_GetTick() - start;
uint32_t per = 1000 * elapsed / iterations;
cli_print(cli, "%ld.%03lds total, %ld.%03ldms per wrap",
elapsed / 1000, elapsed % 1000, per / 1000, per % 1000);
cli_print(cli, "\n3. wrap timing with keywrap core");
if (hal_aes_use_keywrap_core(1) == 0) {
cli_print(cli, "keywrap core not found, skipping");
}
else {
start = HAL_GetTick();
for (int i = 0; i < iterations; ++i) {
C_len = sizeof(C);
if ((err = hal_aes_keywrap(NULL, K_256, sizeof(K_256), Q, keysize, C, &C_len)) != LIBHAL_OK) {
cli_print(cli, "hal_aes_keywrap: %s", hal_error_string(err));
return CLI_ERROR;
}
}
elapsed = HAL_GetTick() - start;
per = 1000 * elapsed / iterations;
cli_print(cli, "%ld.%03lds total, %ld.%03ldms per wrap",
elapsed / 1000, elapsed % 1000, per / 1000, per % 1000);
}
cli_print(cli, "\n4. unwrap timing with software keywrap");
hal_aes_use_keywrap_core(0);
start = HAL_GetTick();
for (int i = 0; i < iterations; ++i) {
Q_len = sizeof(Q);
if ((err = hal_aes_keyunwrap(NULL, K_256, sizeof(K_256), C, C_len, Q, &Q_len)) != LIBHAL_OK) {
cli_print(cli, "hal_aes_keyunwrap: %s", hal_error_string(err));
return CLI_ERROR;
}
}
elapsed = HAL_GetTick() - start;
per = 1000 * elapsed / iterations;
cli_print(cli, "%ld.%03lds total, %ld.%03ldms per wrap",
elapsed / 1000, elapsed % 1000, per / 1000, per % 1000);
cli_print(cli, "\n5. unwrap timing with keywrap core");
if (hal_aes_use_keywrap_core(1) == 0) {
cli_print(cli, "keywrap core not found, skipping");
}
else {
start = HAL_GetTick();
for (int i = 0; i < iterations; ++i) {
Q_len = sizeof(Q);
if ((err = hal_aes_keyunwrap(NULL, K_256, sizeof(K_256), C, C_len, Q, &Q_len)) != LIBHAL_OK) {
cli_print(cli, "hal_aes_keywrap: %s", hal_error_string(err));
return CLI_ERROR;
}
}
elapsed = HAL_GetTick() - start;
per = 1000 * elapsed / iterations;
cli_print(cli, "%ld.%03lds total, %ld.%03ldms per wrap",
elapsed / 1000, elapsed % 1000, per / 1000, per % 1000);
}
return CLI_OK;
}
void configure_cli_keywrap(struct cli_def *cli)
{
struct cli_command *c_keywrap = cli_register_command(cli, NULL, "keywrap", NULL, 0, 0, NULL);
/* keywrap test */
cli_register_command(cli, c_keywrap, "test", cmd_keywrap_test, 0, 0, "Test the keywrap core");
}
|