blob: 9831666c3e960453932f8770b5038db489fa5d72 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/ccm.h"
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +02003
4/* Use the multipart interface to process the encrypted data in two parts
5 * and check that the output matches the expected output.
6 * The context must have been set up with the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007static int check_multipart(mbedtls_ccm_context *ctx,
8 int mode,
9 const data_t *iv,
10 const data_t *add,
11 const data_t *input,
12 const data_t *expected_output,
13 const data_t *tag,
14 size_t n1,
15 size_t n1_add)
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020016{
17 int ok = 0;
18 uint8_t *output = NULL;
19 size_t n2 = input->len - n1;
20 size_t n2_add = add->len - n1_add;
21 size_t olen;
22
23 /* Sanity checks on the test data */
Gilles Peskine449bd832023-01-11 14:50:10 +010024 TEST_ASSERT(n1 <= input->len);
25 TEST_ASSERT(n1_add <= add->len);
26 TEST_EQUAL(input->len, expected_output->len);
27 TEST_EQUAL(0, mbedtls_ccm_starts(ctx, mode, iv->x, iv->len));
28 TEST_EQUAL(0, mbedtls_ccm_set_lengths(ctx, add->len, input->len, tag->len));
29 TEST_EQUAL(0, mbedtls_ccm_update_ad(ctx, add->x, n1_add));
30 TEST_EQUAL(0, mbedtls_ccm_update_ad(ctx, add->x + n1_add, n2_add));
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020031
32 /* Allocate a tight buffer for each update call. This way, if the function
33 * tries to write beyond the advertised required buffer size, this will
34 * count as an overflow for memory sanitizers and static checkers. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +010035 TEST_CALLOC(output, n1);
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020036 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +010037 TEST_EQUAL(0, mbedtls_ccm_update(ctx, input->x, n1, output, n1, &olen));
38 TEST_EQUAL(n1, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010039 TEST_MEMORY_COMPARE(output, olen, expected_output->x, n1);
Gilles Peskine449bd832023-01-11 14:50:10 +010040 mbedtls_free(output);
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020041 output = NULL;
42
Tom Cosgrove05b2a872023-07-21 11:31:13 +010043 TEST_CALLOC(output, n2);
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020044 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +010045 TEST_EQUAL(0, mbedtls_ccm_update(ctx, input->x + n1, n2, output, n2, &olen));
46 TEST_EQUAL(n2, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010047 TEST_MEMORY_COMPARE(output, olen, expected_output->x + n1, n2);
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_free(output);
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020049 output = NULL;
50
Tom Cosgrove05b2a872023-07-21 11:31:13 +010051 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +010052 TEST_EQUAL(0, mbedtls_ccm_finish(ctx, output, tag->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010053 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_free(output);
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020055 output = NULL;
56
57 ok = 1;
58exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_free(output);
60 return ok;
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020061}
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020062/* END_HEADER */
63
64/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 * depends_on:MBEDTLS_CCM_C
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020066 * END_DEPENDENCIES
67 */
68
Valerio Setti689c0f72023-12-20 09:53:39 +010069/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_CCM_GCM_CAN_AES */
Gilles Peskine449bd832023-01-11 14:50:10 +010070void mbedtls_ccm_self_test()
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020071{
Valerio Setti10e9aa22023-12-12 11:54:20 +010072 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +010073 TEST_ASSERT(mbedtls_ccm_self_test(1) == 0);
Valerio Setti10e9aa22023-12-12 11:54:20 +010074 BLOCK_CIPHER_PSA_DONE();
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020075}
76/* END_CASE */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020077
78/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010079void mbedtls_ccm_setkey(int cipher_id, int key_size, int result)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020082 unsigned char key[32];
83 int ret;
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 memset(key, 0x2A, sizeof(key));
88 TEST_ASSERT((unsigned) key_size <= 8 * sizeof(key));
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 ret = mbedtls_ccm_setkey(&ctx, cipher_id, key, key_size);
91 TEST_ASSERT(ret == result);
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020092
Paul Bakkerbd51b262014-07-10 15:26:12 +020093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_ccm_free(&ctx);
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020095}
96/* END_CASE */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020097
Valerio Setti689c0f72023-12-20 09:53:39 +010098/* BEGIN_CASE depends_on:MBEDTLS_CCM_GCM_CAN_AES */
Gilles Peskine449bd832023-01-11 14:50:10 +010099void ccm_lengths(int msg_len, int iv_len, int add_len, int tag_len, int res)
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200102 unsigned char key[16];
103 unsigned char msg[10];
104 unsigned char iv[14];
Dave Rodgman2e680342020-10-15 14:00:40 +0100105 unsigned char *add = NULL;
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200106 unsigned char out[10];
107 unsigned char tag[18];
108 int decrypt_ret;
109
Valerio Setti10e9aa22023-12-12 11:54:20 +0100110 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200112
Tom Cosgrove412a8132023-07-20 16:55:14 +0100113 TEST_CALLOC_OR_SKIP(add, add_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 memset(key, 0, sizeof(key));
115 memset(msg, 0, sizeof(msg));
116 memset(iv, 0, sizeof(iv));
117 memset(out, 0, sizeof(out));
118 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_ASSERT(mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
121 key, 8 * sizeof(key)) == 0);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 TEST_ASSERT(mbedtls_ccm_encrypt_and_tag(&ctx, msg_len, iv, iv_len, add, add_len,
124 msg, out, tag, tag_len) == res);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 decrypt_ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len, iv, iv_len, add, add_len,
127 msg, out, tag, tag_len);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (res == 0) {
130 TEST_ASSERT(decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED);
131 } else {
132 TEST_ASSERT(decrypt_ret == res);
133 }
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200134
Paul Bakkerbd51b262014-07-10 15:26:12 +0200135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_free(add);
137 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100138 BLOCK_CIPHER_PSA_DONE();
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200139}
140/* END_CASE */
141
Janos Follath95ab93d2018-05-14 14:32:41 +0100142/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143void ccm_star_lengths(int msg_len, int iv_len, int add_len, int tag_len,
144 int res)
Janos Follath95ab93d2018-05-14 14:32:41 +0100145{
146 mbedtls_ccm_context ctx;
147 unsigned char key[16];
148 unsigned char msg[10];
149 unsigned char iv[14];
150 unsigned char add[10];
151 unsigned char out[10];
152 unsigned char tag[18];
153 int decrypt_ret;
154
Valerio Setti10e9aa22023-12-12 11:54:20 +0100155 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_ccm_init(&ctx);
Janos Follath95ab93d2018-05-14 14:32:41 +0100157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 memset(key, 0, sizeof(key));
159 memset(msg, 0, sizeof(msg));
160 memset(iv, 0, sizeof(iv));
161 memset(add, 0, sizeof(add));
162 memset(out, 0, sizeof(out));
163 memset(tag, 0, sizeof(tag));
Janos Follath95ab93d2018-05-14 14:32:41 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 TEST_ASSERT(mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
166 key, 8 * sizeof(key)) == 0);
Janos Follath95ab93d2018-05-14 14:32:41 +0100167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 TEST_ASSERT(mbedtls_ccm_star_encrypt_and_tag(&ctx, msg_len, iv, iv_len,
169 add, add_len, msg, out, tag, tag_len) == res);
Janos Follath95ab93d2018-05-14 14:32:41 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 decrypt_ret = mbedtls_ccm_star_auth_decrypt(&ctx, msg_len, iv, iv_len, add,
172 add_len, msg, out, tag, tag_len);
Janos Follath95ab93d2018-05-14 14:32:41 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (res == 0 && tag_len != 0) {
175 TEST_ASSERT(decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED);
176 } else {
177 TEST_ASSERT(decrypt_ret == res);
178 }
Janos Follath95ab93d2018-05-14 14:32:41 +0100179
180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100182 BLOCK_CIPHER_PSA_DONE();
Janos Follath95ab93d2018-05-14 14:32:41 +0100183}
184/* END_CASE */
185
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200186/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187void mbedtls_ccm_encrypt_and_tag(int cipher_id, data_t *key,
188 data_t *msg, data_t *iv,
189 data_t *add, data_t *result)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200190{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_ccm_context ctx;
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200192 size_t n1, n1_add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 uint8_t *io_msg_buf = NULL;
194 uint8_t *tag_buf = NULL;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200195 const size_t expected_tag_len = result->len - msg->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 const uint8_t *expected_tag = result->x + msg->len;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200197
198 /* Prepare input/output message buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100199 TEST_CALLOC(io_msg_buf, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (msg->len != 0) {
201 memcpy(io_msg_buf, msg->x, msg->len);
202 }
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200203
204 /* Prepare tag buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100205 TEST_CALLOC(tag_buf, expected_tag_len);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200206
Valerio Setti10e9aa22023-12-12 11:54:20 +0100207 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 mbedtls_ccm_init(&ctx);
209 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200210 /* Test with input == output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 TEST_EQUAL(mbedtls_ccm_encrypt_and_tag(&ctx, msg->len, iv->x, iv->len, add->x, add->len,
212 io_msg_buf, io_msg_buf, tag_buf, expected_tag_len), 0);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200213
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100214 TEST_MEMORY_COMPARE(io_msg_buf, msg->len, result->x, msg->len);
215 TEST_MEMORY_COMPARE(tag_buf, expected_tag_len, expected_tag, expected_tag_len);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200216
Mateusz Starzykceb5bc62021-07-30 14:36:22 +0200217 /* Prepare data_t structures for multipart testing */
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200218 const data_t encrypted_expected = { .x = result->x,
219 .len = msg->len };
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 const data_t tag_expected = { .x = (uint8_t *) expected_tag, /* cast to conform with data_t x type */
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200221 .len = expected_tag_len };
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 for (n1 = 0; n1 <= msg->len; n1 += 1) {
224 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
225 mbedtls_test_set_step(n1 * 10000 + n1_add);
226 if (!check_multipart(&ctx, MBEDTLS_CCM_ENCRYPT,
227 iv, add, msg,
228 &encrypted_expected,
229 &tag_expected,
230 n1, n1_add)) {
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200231 goto exit;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200232 }
233 }
234 }
235
Darryl Green0daf4ca2018-05-29 14:12:26 +0100236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_ccm_free(&ctx);
238 mbedtls_free(io_msg_buf);
239 mbedtls_free(tag_buf);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100240 BLOCK_CIPHER_PSA_DONE();
Darryl Green0daf4ca2018-05-29 14:12:26 +0100241}
242/* END_CASE */
243
244/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245void mbedtls_ccm_star_no_tag(int cipher_id, int mode, data_t *key,
246 data_t *msg, data_t *iv, data_t *result)
247{
248 mbedtls_ccm_context ctx;
249 uint8_t *output = NULL;
250 size_t olen;
251
Valerio Setti10e9aa22023-12-12 11:54:20 +0100252 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_ccm_init(&ctx);
254 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
255 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
256 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, msg->len, 0));
257
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100258 TEST_CALLOC(output, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
260 TEST_EQUAL(result->len, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100261 TEST_MEMORY_COMPARE(output, olen, result->x, result->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100262
263 TEST_EQUAL(0, mbedtls_ccm_finish(&ctx, NULL, 0));
264exit:
265 mbedtls_free(output);
266 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100267 BLOCK_CIPHER_PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +0100268}
269/* END_CASE */
270
271/* BEGIN_CASE */
272void mbedtls_ccm_auth_decrypt(int cipher_id, data_t *key,
273 data_t *msg, data_t *iv,
274 data_t *add, int expected_tag_len, int result,
275 data_t *expected_msg)
276{
277 mbedtls_ccm_context ctx;
278 size_t n1, n1_add;
279
280 const size_t expected_msg_len = msg->len - expected_tag_len;
281 const uint8_t *expected_tag = msg->x + expected_msg_len;
282
283 /* Prepare input/output message buffer */
284 uint8_t *io_msg_buf = NULL;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100285 TEST_CALLOC(io_msg_buf, expected_msg_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (expected_msg_len) {
287 memcpy(io_msg_buf, msg->x, expected_msg_len);
288 }
289
Valerio Setti10e9aa22023-12-12 11:54:20 +0100290 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_ccm_init(&ctx);
292 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
293 /* Test with input == output */
294 TEST_EQUAL(mbedtls_ccm_auth_decrypt(&ctx, expected_msg_len, iv->x, iv->len, add->x, add->len,
295 io_msg_buf, io_msg_buf, expected_tag, expected_tag_len),
296 result);
297
298 if (result == 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100299 TEST_MEMORY_COMPARE(io_msg_buf, expected_msg_len, expected_msg->x, expected_msg_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100300
301 /* Prepare data_t structures for multipart testing */
302 const data_t encrypted = { .x = msg->x,
303 .len = expected_msg_len };
304
305 const data_t tag_expected = { .x = (uint8_t *) expected_tag,
306 .len = expected_tag_len };
307
308 for (n1 = 0; n1 <= expected_msg_len; n1 += 1) {
309 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
310 mbedtls_test_set_step(n1 * 10000 + n1_add);
311 if (!check_multipart(&ctx, MBEDTLS_CCM_DECRYPT,
312 iv, add, &encrypted,
313 expected_msg,
314 &tag_expected,
315 n1, n1_add)) {
316 goto exit;
317 }
318 }
319 }
320 } else {
321 size_t i;
322
323 for (i = 0; i < expected_msg_len; i++) {
324 TEST_EQUAL(io_msg_buf[i], 0);
325 }
326 }
327
328exit:
329 mbedtls_free(io_msg_buf);
330 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100331 BLOCK_CIPHER_PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +0100332}
333/* END_CASE */
334
335/* BEGIN_CASE */
336void mbedtls_ccm_star_encrypt_and_tag(int cipher_id,
337 data_t *key, data_t *msg,
338 data_t *source_address, data_t *frame_counter,
339 int sec_level, data_t *add,
340 data_t *expected_result, int output_ret)
341{
342 unsigned char iv[13];
343 mbedtls_ccm_context ctx;
344 size_t iv_len, expected_tag_len;
345 size_t n1, n1_add;
346 uint8_t *io_msg_buf = NULL;
347 uint8_t *tag_buf = NULL;
348
349 const uint8_t *expected_tag = expected_result->x + msg->len;
350
351 /* Calculate tag length */
352 if (sec_level % 4 == 0) {
353 expected_tag_len = 0;
354 } else {
355 expected_tag_len = 1 << (sec_level % 4 + 1);
356 }
357
358 /* Prepare input/output message buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100359 TEST_CALLOC(io_msg_buf, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (msg->len) {
361 memcpy(io_msg_buf, msg->x, msg->len);
362 }
363
364 /* Prepare tag buffer */
365 if (expected_tag_len == 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100366 TEST_CALLOC(tag_buf, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 } else {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100368 TEST_CALLOC(tag_buf, expected_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 }
370
371 /* Calculate iv */
372 TEST_ASSERT(source_address->len == 8);
373 TEST_ASSERT(frame_counter->len == 4);
374 memcpy(iv, source_address->x, source_address->len);
375 memcpy(iv + source_address->len, frame_counter->x, frame_counter->len);
376 iv[source_address->len + frame_counter->len] = sec_level;
377 iv_len = sizeof(iv);
378
Valerio Setti10e9aa22023-12-12 11:54:20 +0100379 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_ccm_init(&ctx);
381 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id,
382 key->x, key->len * 8), 0);
383 /* Test with input == output */
384 TEST_EQUAL(mbedtls_ccm_star_encrypt_and_tag(&ctx, msg->len, iv, iv_len,
385 add->x, add->len, io_msg_buf,
386 io_msg_buf, tag_buf, expected_tag_len), output_ret);
387
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100388 TEST_MEMORY_COMPARE(io_msg_buf, msg->len, expected_result->x, msg->len);
389 TEST_MEMORY_COMPARE(tag_buf, expected_tag_len, expected_tag, expected_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100390
391 if (output_ret == 0) {
392 const data_t iv_data = { .x = iv,
393 .len = iv_len };
394
395 const data_t encrypted_expected = { .x = expected_result->x,
396 .len = msg->len };
397 const data_t tag_expected = { .x = (uint8_t *) expected_tag,
398 .len = expected_tag_len };
399
400 for (n1 = 0; n1 <= msg->len; n1 += 1) {
401 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
402 mbedtls_test_set_step(n1 * 10000 + n1_add);
403 if (!check_multipart(&ctx, MBEDTLS_CCM_STAR_ENCRYPT,
404 &iv_data, add, msg,
405 &encrypted_expected,
406 &tag_expected,
407 n1, n1_add)) {
408 goto exit;
409 }
410 }
411 }
412 }
413
414exit:
415 mbedtls_ccm_free(&ctx);
416 mbedtls_free(io_msg_buf);
417 mbedtls_free(tag_buf);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100418 BLOCK_CIPHER_PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +0100419}
420/* END_CASE */
421
422/* BEGIN_CASE */
423void mbedtls_ccm_star_auth_decrypt(int cipher_id,
424 data_t *key, data_t *msg,
425 data_t *source_address, data_t *frame_counter,
426 int sec_level, data_t *add,
427 data_t *expected_result, int output_ret)
Darryl Green0daf4ca2018-05-29 14:12:26 +0100428{
Darryl Green0daf4ca2018-05-29 14:12:26 +0100429 unsigned char iv[13];
Darryl Green0daf4ca2018-05-29 14:12:26 +0100430 mbedtls_ccm_context ctx;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200431 size_t iv_len, expected_tag_len;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200432 size_t n1, n1_add;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100433
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200434 /* Calculate tag length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (sec_level % 4 == 0) {
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200436 expected_tag_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else {
438 expected_tag_len = 1 << (sec_level % 4 + 1);
439 }
Darryl Green0daf4ca2018-05-29 14:12:26 +0100440
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200441 const size_t expected_msg_len = msg->len - expected_tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 const uint8_t *expected_tag = msg->x + expected_msg_len;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200443
444 /* Prepare input/output message buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 uint8_t *io_msg_buf = NULL;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100446 TEST_CALLOC(io_msg_buf, expected_msg_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (expected_msg_len) {
448 memcpy(io_msg_buf, msg->x, expected_msg_len);
449 }
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200450
451 /* Calculate iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 memset(iv, 0x00, sizeof(iv));
453 TEST_ASSERT(source_address->len == 8);
454 TEST_ASSERT(frame_counter->len == 4);
455 memcpy(iv, source_address->x, source_address->len);
456 memcpy(iv + source_address->len, frame_counter->x, frame_counter->len);
Ronald Cron9ed40732020-06-25 09:03:34 +0200457 iv[source_address->len + frame_counter->len] = sec_level;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 iv_len = sizeof(iv);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100459
Valerio Setti10e9aa22023-12-12 11:54:20 +0100460 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 mbedtls_ccm_init(&ctx);
462 TEST_ASSERT(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8) == 0);
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200463 /* Test with input == output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 TEST_EQUAL(mbedtls_ccm_star_auth_decrypt(&ctx, expected_msg_len, iv, iv_len,
465 add->x, add->len, io_msg_buf, io_msg_buf,
466 expected_tag, expected_tag_len), output_ret);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100467
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100468 TEST_MEMORY_COMPARE(io_msg_buf, expected_msg_len, expected_result->x, expected_msg_len);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (output_ret == 0) {
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200471 const data_t iv_data = { .x = iv,
472 .len = iv_len };
473
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200474 const data_t encrypted = { .x = msg->x,
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 .len = expected_msg_len };
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 const data_t tag_expected = { .x = (uint8_t *) expected_tag,
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200478 .len = expected_tag_len };
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 for (n1 = 0; n1 <= expected_msg_len; n1 += 1) {
481 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
482 mbedtls_test_set_step(n1 * 10000 + n1_add);
483 if (!check_multipart(&ctx, MBEDTLS_CCM_STAR_DECRYPT,
484 &iv_data, add, &encrypted,
485 expected_result,
486 &tag_expected,
487 n1, n1_add)) {
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200488 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 }
490 }
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200491 }
492 }
493
Darryl Green0daf4ca2018-05-29 14:12:26 +0100494exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 mbedtls_ccm_free(&ctx);
496 mbedtls_free(io_msg_buf);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100497 BLOCK_CIPHER_PSA_DONE();
Darryl Green0daf4ca2018-05-29 14:12:26 +0100498}
499/* END_CASE */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200500
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200501/* Skip auth data, provide full text */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200502/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503void mbedtls_ccm_skip_ad(int cipher_id, int mode,
504 data_t *key, data_t *msg, data_t *iv,
505 data_t *result, data_t *tag)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200506{
507 mbedtls_ccm_context ctx;
508 uint8_t *output = NULL;
509 size_t olen;
510
511 /* Sanity checks on the test data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 TEST_EQUAL(msg->len, result->len);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200513
Valerio Setti10e9aa22023-12-12 11:54:20 +0100514 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_ccm_init(&ctx);
516 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
517 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
518 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, msg->len, tag->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200519
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100520 TEST_CALLOC(output, result->len);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200521 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, result->len, &olen));
523 TEST_EQUAL(result->len, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100524 TEST_MEMORY_COMPARE(output, olen, result->x, result->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_free(output);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200526 output = NULL;
527
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100528 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 TEST_EQUAL(0, mbedtls_ccm_finish(&ctx, output, tag->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100530 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 mbedtls_free(output);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200532 output = NULL;
533
534exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 mbedtls_free(output);
536 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100537 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200538}
539/* END_CASE */
540
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200541/* Provide auth data, skip full text */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200542/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543void mbedtls_ccm_skip_update(int cipher_id, int mode,
544 data_t *key, data_t *iv, data_t *add,
545 data_t *tag)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200546{
547 mbedtls_ccm_context ctx;
548 uint8_t *output = NULL;
549
Valerio Setti10e9aa22023-12-12 11:54:20 +0100550 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_ccm_init(&ctx);
552 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
553 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
554 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, tag->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200557
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100558 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 TEST_EQUAL(0, mbedtls_ccm_finish(&ctx, output, tag->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100560 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_free(output);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200562 output = NULL;
563
564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_free(output);
566 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100567 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200568}
569/* END_CASE */
570
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200571/* Provide too much auth data */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200572/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573void mbedtls_ccm_overflow_ad(int cipher_id, int mode,
574 data_t *key, data_t *iv,
575 data_t *add)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200576{
577 mbedtls_ccm_context ctx;
578
Valerio Setti10e9aa22023-12-12 11:54:20 +0100579 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_ccm_init(&ctx);
581 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
582 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200583 // use hardcoded values for msg length and tag length. They are not a part of this test
Mateusz Starzyk3050f052021-09-02 12:38:51 +0200584 // subtract 1 from configured auth data length to provoke an overflow
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len - 1, 16, 16));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200588exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100590 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200591}
592/* END_CASE */
593
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200594/* Provide unexpected auth data */
595/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100596void mbedtls_ccm_unexpected_ad(int cipher_id, int mode,
597 data_t *key, data_t *iv,
598 data_t *add)
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200599{
600 mbedtls_ccm_context ctx;
601
Valerio Setti10e9aa22023-12-12 11:54:20 +0100602 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 mbedtls_ccm_init(&ctx);
604 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
605 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200606 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, 16, 16));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200610exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100612 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200613}
614/* END_CASE */
615
616/* Provide unexpected plaintext/ciphertext data */
617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618void mbedtls_ccm_unexpected_text(int cipher_id, int mode,
619 data_t *key, data_t *msg, data_t *iv,
620 data_t *add)
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200621{
622 mbedtls_ccm_context ctx;
623 uint8_t *output = NULL;
624 size_t olen;
625
Valerio Setti10e9aa22023-12-12 11:54:20 +0100626 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 mbedtls_ccm_init(&ctx);
628 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
629 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200630 // use hardcoded value for tag length. It is not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, 16));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200634
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100635 TEST_CALLOC(output, msg->len);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200636 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT,
638 mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200639exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 mbedtls_free(output);
641 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100642 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200643}
644/* END_CASE */
645
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200646/* Provide incomplete auth data and finish */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200647/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648void mbedtls_ccm_incomplete_ad(int cipher_id, int mode,
649 data_t *key, data_t *iv, data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200650{
651 mbedtls_ccm_context ctx;
652 uint8_t *output = NULL;
653
Valerio Setti10e9aa22023-12-12 11:54:20 +0100654 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 mbedtls_ccm_init(&ctx);
656 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
657 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200658 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len - 1));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200662
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100663 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200665
666exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 mbedtls_free(output);
668 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100669 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200670}
671/* END_CASE */
672
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200673/* Provide complete auth data on first update_ad.
674 * Provide unexpected auth data on second update_ad */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200675/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100676void mbedtls_ccm_full_ad_and_overflow(int cipher_id, int mode,
677 data_t *key, data_t *iv,
678 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200679{
680 mbedtls_ccm_context ctx;
681
Valerio Setti10e9aa22023-12-12 11:54:20 +0100682 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_ccm_init(&ctx);
684 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
685 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200686 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 16, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200688
689 // pass full auth data
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200691 // pass 1 extra byte
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, 1));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100695 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200696}
697/* END_CASE */
698
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200699/* Provide incomplete auth data on first update_ad.
700 * Provide too much auth data on second update_ad */
701/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702void mbedtls_ccm_incomplete_ad_and_overflow(int cipher_id, int mode,
703 data_t *key, data_t *iv,
704 data_t *add)
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200705{
706 mbedtls_ccm_context ctx;
Ronald Cron133740b2021-09-17 09:38:07 +0200707 uint8_t add_second_buffer[2];
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 add_second_buffer[0] = add->x[add->len - 1];
Ronald Cron133740b2021-09-17 09:38:07 +0200710 add_second_buffer[1] = 0xAB; // some magic value
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200711
Valerio Setti10e9aa22023-12-12 11:54:20 +0100712 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 mbedtls_ccm_init(&ctx);
714 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
715 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200716 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 16, 16));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200718
719 // pass incomplete auth data
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len - 1));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200721 // pass 2 extra bytes (1 missing byte from previous incomplete pass, and 1 unexpected byte)
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add_second_buffer, 2));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200723exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100725 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200726}
727/* END_CASE */
728
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200729/* Provide too much plaintext/ciphertext */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200730/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731void mbedtls_ccm_overflow_update(int cipher_id, int mode,
732 data_t *key, data_t *msg, data_t *iv,
733 data_t *add)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200734{
735 mbedtls_ccm_context ctx;
736 uint8_t *output = NULL;
737 size_t olen;
738
Valerio Setti10e9aa22023-12-12 11:54:20 +0100739 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 mbedtls_ccm_init(&ctx);
741 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
742 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200743 // use hardcoded value for tag length. It is a not a part of this test
Mateusz Starzyk3050f052021-09-02 12:38:51 +0200744 // subtract 1 from configured msg length to provoke an overflow
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len - 1, 16));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200748
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100749 TEST_CALLOC(output, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
751 mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 mbedtls_free(output);
754 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100755 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200756}
757/* END_CASE */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200758
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200759/* Provide incomplete plaintext/ciphertext and finish */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200760/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761void mbedtls_ccm_incomplete_update(int cipher_id, int mode,
762 data_t *key, data_t *msg, data_t *iv,
763 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200764{
765 mbedtls_ccm_context ctx;
766 uint8_t *output = NULL;
767 size_t olen;
768
Valerio Setti10e9aa22023-12-12 11:54:20 +0100769 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 mbedtls_ccm_init(&ctx);
771 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
772 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200773 // use hardcoded value for tag length. It is not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200777
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100778 TEST_CALLOC(output, msg->len);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200779 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len - 1, output, msg->len, &olen));
781 mbedtls_free(output);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200782 output = NULL;
783
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100784 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200786
787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 mbedtls_free(output);
789 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100790 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200791}
792/* END_CASE */
793
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200794/* Provide full plaintext/ciphertext of first update
795 * Provide unexpected plaintext/ciphertext on second update */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200796/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797void mbedtls_ccm_full_update_and_overflow(int cipher_id, int mode,
798 data_t *key, data_t *msg, data_t *iv,
799 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200800{
801 mbedtls_ccm_context ctx;
802 uint8_t *output = NULL;
803 size_t olen;
804
Valerio Setti10e9aa22023-12-12 11:54:20 +0100805 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_ccm_init(&ctx);
807 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
808 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200809 // use hardcoded value for tag length. It is a not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200813
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100814 TEST_CALLOC(output, msg->len);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200815 // pass full text
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200817 // pass 1 extra byte
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
819 mbedtls_ccm_update(&ctx, msg->x, 1, output, 1, &olen));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200820exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_free(output);
822 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100823 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200824}
825/* END_CASE */
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200826
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200827/* Provide incomplete plaintext/ciphertext of first update
828 * Provide too much plaintext/ciphertext on second update */
829/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830void mbedtls_ccm_incomplete_update_overflow(int cipher_id, int mode,
831 data_t *key, data_t *msg, data_t *iv,
832 data_t *add)
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200833{
834 mbedtls_ccm_context ctx;
835 uint8_t *output = NULL;
836 size_t olen;
Ronald Cron133740b2021-09-17 09:38:07 +0200837 uint8_t msg_second_buffer[2];
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 msg_second_buffer[0] = msg->x[msg->len - 1];
Ronald Cron133740b2021-09-17 09:38:07 +0200840 msg_second_buffer[1] = 0xAB; // some magic value
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200841
Valerio Setti10e9aa22023-12-12 11:54:20 +0100842 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 mbedtls_ccm_init(&ctx);
844 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
845 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200846 // use hardcoded value for tag length. It is a not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200850
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100851 TEST_CALLOC(output, msg->len + 1);
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200852 // pass incomplete text
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len - 1, output, msg->len + 1, &olen));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200854 // pass 2 extra bytes (1 missing byte from previous incomplete pass, and 1 unexpected byte)
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
856 mbedtls_ccm_update(&ctx, msg_second_buffer, 2, output + msg->len - 1, 2, &olen));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200857exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 mbedtls_free(output);
859 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100860 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200861}
862/* END_CASE */
863
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200864/* Finish without passing any auth data or plaintext/ciphertext input */
865/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866void mbedtls_ccm_instant_finish(int cipher_id, int mode,
867 data_t *key, data_t *iv)
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200868{
869 mbedtls_ccm_context ctx;
870 uint8_t *output = NULL;
871
Valerio Setti10e9aa22023-12-12 11:54:20 +0100872 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_ccm_init(&ctx);
874 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
875 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200876 // use hardcoded values for add length, msg length and tag length.
877 // They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 16, 16, 16));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200879
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100880 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200882
883exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 mbedtls_free(output);
885 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100886 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200887}
888/* END_CASE */