blob: f2b794441aa21b91f0eda599565a4825834a531a [file] [log] [blame]
Ron Eldor8dd03cd2018-07-15 09:37:28 +03001/* BEGIN_HEADER */
2#include "mbedtls/nist_kw.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_NIST_KW_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010011void mbedtls_nist_kw_self_test()
Ron Eldor8dd03cd2018-07-15 09:37:28 +030012{
Gilles Peskine449bd832023-01-11 14:50:10 +010013 TEST_ASSERT(mbedtls_nist_kw_self_test(1) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030014}
15/* END_CASE */
16
17/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010018void mbedtls_nist_kw_mix_contexts()
Ron Eldor8dd03cd2018-07-15 09:37:28 +030019{
20 mbedtls_nist_kw_context ctx1, ctx2;
21 unsigned char key[16];
22 unsigned char plaintext[32];
23 unsigned char ciphertext1[40];
24 unsigned char ciphertext2[40];
25 size_t output_len, i;
26
Gilles Peskine449bd832023-01-11 14:50:10 +010027 memset(plaintext, 0, sizeof(plaintext));
28 memset(ciphertext1, 0, sizeof(ciphertext1));
29 memset(ciphertext2, 0, sizeof(ciphertext2));
30 memset(key, 0, sizeof(key));
Ron Eldor8dd03cd2018-07-15 09:37:28 +030031
32 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +010033 * 1. Check wrap and unwrap with two separate contexts
Ron Eldor8dd03cd2018-07-15 09:37:28 +030034 */
Gilles Peskine449bd832023-01-11 14:50:10 +010035 mbedtls_nist_kw_init(&ctx1);
36 mbedtls_nist_kw_init(&ctx2);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx1,
39 MBEDTLS_CIPHER_ID_AES,
40 key, sizeof(key) * 8,
41 1) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx1, MBEDTLS_KW_MODE_KW,
44 plaintext, sizeof(plaintext),
45 ciphertext1, &output_len,
46 sizeof(ciphertext1)) == 0);
47 TEST_ASSERT(output_len == sizeof(ciphertext1));
Ron Eldor8dd03cd2018-07-15 09:37:28 +030048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx2,
50 MBEDTLS_CIPHER_ID_AES,
51 key, sizeof(key) * 8,
52 0) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx2, MBEDTLS_KW_MODE_KW,
55 ciphertext1, output_len,
56 plaintext, &output_len,
57 sizeof(plaintext)) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 TEST_ASSERT(output_len == sizeof(plaintext));
60 for (i = 0; i < sizeof(plaintext); i++) {
61 TEST_ASSERT(plaintext[i] == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030062 }
Gilles Peskine449bd832023-01-11 14:50:10 +010063 mbedtls_nist_kw_free(&ctx1);
64 mbedtls_nist_kw_free(&ctx2);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030065
66 /*
67 * 2. Check wrapping with two modes, on same context
68 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_nist_kw_init(&ctx1);
70 mbedtls_nist_kw_init(&ctx2);
71 output_len = sizeof(ciphertext1);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx1,
74 MBEDTLS_CIPHER_ID_AES,
75 key, sizeof(key) * 8,
76 1) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx1, MBEDTLS_KW_MODE_KW,
79 plaintext, sizeof(plaintext),
80 ciphertext1, &output_len,
81 sizeof(ciphertext1)) == 0);
82 TEST_ASSERT(output_len == sizeof(ciphertext1));
Ron Eldor8dd03cd2018-07-15 09:37:28 +030083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx1, MBEDTLS_KW_MODE_KWP,
85 plaintext, sizeof(plaintext),
86 ciphertext2, &output_len,
87 sizeof(ciphertext2)) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 TEST_ASSERT(output_len == sizeof(ciphertext2));
Ron Eldor8dd03cd2018-07-15 09:37:28 +030090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx2,
92 MBEDTLS_CIPHER_ID_AES,
93 key, sizeof(key) * 8,
94 0) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +030095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx2, MBEDTLS_KW_MODE_KW,
97 ciphertext1, sizeof(ciphertext1),
98 plaintext, &output_len,
99 sizeof(plaintext)) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 TEST_ASSERT(output_len == sizeof(plaintext));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 for (i = 0; i < sizeof(plaintext); i++) {
104 TEST_ASSERT(plaintext[i] == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx2, MBEDTLS_KW_MODE_KWP,
108 ciphertext2, sizeof(ciphertext2),
109 plaintext, &output_len,
110 sizeof(plaintext)) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_ASSERT(output_len == sizeof(plaintext));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 for (i = 0; i < sizeof(plaintext); i++) {
115 TEST_ASSERT(plaintext[i] == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300116 }
117
118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_nist_kw_free(&ctx1);
120 mbedtls_nist_kw_free(&ctx2);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300121}
122/* END_CASE */
123
124/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125void mbedtls_nist_kw_setkey(int cipher_id, int key_size,
126 int is_wrap, int result)
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300127{
128 mbedtls_nist_kw_context ctx;
129 unsigned char key[32];
130 int ret;
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_nist_kw_init(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 memset(key, 0x2A, sizeof(key));
135 TEST_ASSERT((unsigned) key_size <= 8 * sizeof(key));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 ret = mbedtls_nist_kw_setkey(&ctx, cipher_id, key, key_size, is_wrap);
138 TEST_ASSERT(ret == result);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300139
140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_nist_kw_free(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300142}
143/* END_CASE */
144
145/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146void nist_kw_plaintext_lengths(int in_len, int out_len, int mode, int res)
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300147{
148 mbedtls_nist_kw_context ctx;
149 unsigned char key[16];
150 unsigned char *plaintext = NULL;
151 unsigned char *ciphertext = NULL;
152 size_t output_len = out_len;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_nist_kw_init(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 memset(key, 0, sizeof(key));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (in_len != 0) {
159 plaintext = mbedtls_calloc(1, in_len);
160 TEST_ASSERT(plaintext != NULL);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300161 }
Ron Eldor446227a2018-08-13 14:46:45 +0300162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (out_len != 0) {
164 ciphertext = mbedtls_calloc(1, output_len);
165 TEST_ASSERT(ciphertext != NULL);
Ron Eldor446227a2018-08-13 14:46:45 +0300166 }
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
169 key, 8 * sizeof(key), 1) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx, mode, plaintext, in_len,
172 ciphertext, &output_len,
173 output_len) == res);
174 if (res == 0) {
175 if (mode == MBEDTLS_KW_MODE_KWP) {
176 TEST_ASSERT(output_len == (size_t) in_len + 8 -
177 (in_len % 8) + 8);
178 } else {
179 TEST_ASSERT(output_len == (size_t) in_len + 8);
180 }
181 } else {
182 TEST_ASSERT(output_len == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300183 }
184
185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_free(ciphertext);
187 mbedtls_free(plaintext);
188 mbedtls_nist_kw_free(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300189}
190/* END_CASE */
191
192/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void nist_kw_ciphertext_lengths(int in_len, int out_len, int mode, int res)
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300194{
195 mbedtls_nist_kw_context ctx;
196 unsigned char key[16];
197 unsigned char *plaintext = NULL;
198 unsigned char *ciphertext = NULL;
199 int unwrap_ret;
200 size_t output_len = out_len;
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_nist_kw_init(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 memset(key, 0, sizeof(key));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (out_len != 0) {
207 plaintext = mbedtls_calloc(1, output_len);
208 TEST_ASSERT(plaintext != NULL);
Ron Eldor446227a2018-08-13 14:46:45 +0300209 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (in_len != 0) {
211 ciphertext = mbedtls_calloc(1, in_len);
212 TEST_ASSERT(ciphertext != NULL);
Ron Eldor446227a2018-08-13 14:46:45 +0300213 }
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
216 key, 8 * sizeof(key), 0) == 0);
217 unwrap_ret = mbedtls_nist_kw_unwrap(&ctx, mode, ciphertext, in_len,
218 plaintext, &output_len,
219 output_len);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (res == 0) {
222 TEST_ASSERT(unwrap_ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
223 } else {
224 TEST_ASSERT(unwrap_ret == res);
225 }
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(output_len == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300228
229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_free(ciphertext);
231 mbedtls_free(plaintext);
232 mbedtls_nist_kw_free(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300233}
234/* END_CASE */
235
236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237void mbedtls_nist_kw_wrap(int cipher_id, int mode, data_t *key, data_t *msg,
238 data_t *expected_result)
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300239{
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300240 unsigned char result[528];
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300241 mbedtls_nist_kw_context ctx;
Ronald Cron9ed40732020-06-25 09:03:34 +0200242 size_t result_len, i, padlen;
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_nist_kw_init(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 memset(result, '+', sizeof(result));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, cipher_id,
249 key->x, key->len * 8, 1) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300250
251 /* Test with input == output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx, mode, msg->x, msg->len,
253 result, &result_len, sizeof(result)) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 TEST_ASSERT(result_len == expected_result->len);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 TEST_ASSERT(memcmp(expected_result->x, result, result_len) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 padlen = (msg->len % 8 != 0) ? 8 - (msg->len % 8) : 0;
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300260 /* Check that the function didn't write beyond the end of the buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 for (i = msg->len + 8 + padlen; i < sizeof(result); i++) {
262 TEST_ASSERT(result[i] == '+');
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300263 }
264
265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 mbedtls_nist_kw_free(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300267}
268/* END_CASE */
269
270/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271void mbedtls_nist_kw_unwrap(int cipher_id, int mode, data_t *key, data_t *msg,
272 data_t *expected_result, int expected_ret)
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300273{
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300274 unsigned char result[528];
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300275 mbedtls_nist_kw_context ctx;
Ronald Cron9ed40732020-06-25 09:03:34 +0200276 size_t result_len, i;
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_nist_kw_init(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 memset(result, '+', sizeof(result));
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, cipher_id,
283 key->x, key->len * 8, 0) == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300284
285 /* Test with input == output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx, mode, msg->x, msg->len,
287 result, &result_len, sizeof(result)) == expected_ret);
288 if (expected_ret == 0) {
289 TEST_ASSERT(result_len == expected_result->len);
290 TEST_ASSERT(memcmp(expected_result->x, result, result_len) == 0);
291 } else {
292 TEST_ASSERT(result_len == 0);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300293 }
294
295 /* Check that the function didn't write beyond the end of the buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 for (i = msg->len - 8; i < sizeof(result); i++) {
297 TEST_ASSERT(result[i] == '+');
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300298 }
299
300exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_nist_kw_free(&ctx);
Ron Eldor8dd03cd2018-07-15 09:37:28 +0300302}
303/* END_CASE */