blob: 798be7701393e5f9a69ce3ee50d97ba9e9e5e7f2 [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;
Gilles Peskineefe30762024-10-07 11:11:32 +020082 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020083 unsigned char key[32];
84 int ret;
85
Valerio Setti45c84fe2023-12-20 09:54:39 +010086 BLOCK_CIPHER_PSA_INIT();
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 memset(key, 0x2A, sizeof(key));
89 TEST_ASSERT((unsigned) key_size <= 8 * sizeof(key));
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 ret = mbedtls_ccm_setkey(&ctx, cipher_id, key, key_size);
92 TEST_ASSERT(ret == result);
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020093
Paul Bakkerbd51b262014-07-10 15:26:12 +020094exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_ccm_free(&ctx);
Valerio Setti45c84fe2023-12-20 09:54:39 +010096 BLOCK_CIPHER_PSA_DONE();
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020097}
98/* END_CASE */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020099
Valerio Setti689c0f72023-12-20 09:53:39 +0100100/* BEGIN_CASE depends_on:MBEDTLS_CCM_GCM_CAN_AES */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101void 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 +0200102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200104 mbedtls_ccm_init(&ctx);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200105 unsigned char key[16];
106 unsigned char msg[10];
107 unsigned char iv[14];
Dave Rodgman2e680342020-10-15 14:00:40 +0100108 unsigned char *add = NULL;
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200109 unsigned char out[10];
110 unsigned char tag[18];
111 int decrypt_ret;
112
Valerio Setti10e9aa22023-12-12 11:54:20 +0100113 BLOCK_CIPHER_PSA_INIT();
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200114
Tom Cosgrove412a8132023-07-20 16:55:14 +0100115 TEST_CALLOC_OR_SKIP(add, add_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 memset(key, 0, sizeof(key));
117 memset(msg, 0, sizeof(msg));
118 memset(iv, 0, sizeof(iv));
119 memset(out, 0, sizeof(out));
120 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 TEST_ASSERT(mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
123 key, 8 * sizeof(key)) == 0);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 TEST_ASSERT(mbedtls_ccm_encrypt_and_tag(&ctx, msg_len, iv, iv_len, add, add_len,
126 msg, out, tag, tag_len) == res);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 decrypt_ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len, iv, iv_len, add, add_len,
129 msg, out, tag, tag_len);
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (res == 0) {
132 TEST_ASSERT(decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED);
133 } else {
134 TEST_ASSERT(decrypt_ret == res);
135 }
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200136
Paul Bakkerbd51b262014-07-10 15:26:12 +0200137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_free(add);
139 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100140 BLOCK_CIPHER_PSA_DONE();
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200141}
142/* END_CASE */
143
Valerio Setti45c84fe2023-12-20 09:54:39 +0100144/* BEGIN_CASE depends_on:MBEDTLS_CCM_GCM_CAN_AES */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void ccm_star_lengths(int msg_len, int iv_len, int add_len, int tag_len,
146 int res)
Janos Follath95ab93d2018-05-14 14:32:41 +0100147{
148 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200149 mbedtls_ccm_init(&ctx);
Janos Follath95ab93d2018-05-14 14:32:41 +0100150 unsigned char key[16];
151 unsigned char msg[10];
152 unsigned char iv[14];
153 unsigned char add[10];
154 unsigned char out[10];
155 unsigned char tag[18];
156 int decrypt_ret;
157
Valerio Setti10e9aa22023-12-12 11:54:20 +0100158 BLOCK_CIPHER_PSA_INIT();
Janos Follath95ab93d2018-05-14 14:32:41 +0100159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 memset(key, 0, sizeof(key));
161 memset(msg, 0, sizeof(msg));
162 memset(iv, 0, sizeof(iv));
163 memset(add, 0, sizeof(add));
164 memset(out, 0, sizeof(out));
165 memset(tag, 0, sizeof(tag));
Janos Follath95ab93d2018-05-14 14:32:41 +0100166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 TEST_ASSERT(mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
168 key, 8 * sizeof(key)) == 0);
Janos Follath95ab93d2018-05-14 14:32:41 +0100169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 TEST_ASSERT(mbedtls_ccm_star_encrypt_and_tag(&ctx, msg_len, iv, iv_len,
171 add, add_len, msg, out, tag, tag_len) == res);
Janos Follath95ab93d2018-05-14 14:32:41 +0100172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 decrypt_ret = mbedtls_ccm_star_auth_decrypt(&ctx, msg_len, iv, iv_len, add,
174 add_len, msg, out, tag, tag_len);
Janos Follath95ab93d2018-05-14 14:32:41 +0100175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (res == 0 && tag_len != 0) {
177 TEST_ASSERT(decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED);
178 } else {
179 TEST_ASSERT(decrypt_ret == res);
180 }
Janos Follath95ab93d2018-05-14 14:32:41 +0100181
182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100184 BLOCK_CIPHER_PSA_DONE();
Janos Follath95ab93d2018-05-14 14:32:41 +0100185}
186/* END_CASE */
187
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200188/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189void mbedtls_ccm_encrypt_and_tag(int cipher_id, data_t *key,
190 data_t *msg, data_t *iv,
191 data_t *add, data_t *result)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200194 mbedtls_ccm_init(&ctx);
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200195 size_t n1, n1_add;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 uint8_t *io_msg_buf = NULL;
197 uint8_t *tag_buf = NULL;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200198 const size_t expected_tag_len = result->len - msg->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 const uint8_t *expected_tag = result->x + msg->len;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200200
201 /* Prepare input/output message buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100202 TEST_CALLOC(io_msg_buf, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (msg->len != 0) {
204 memcpy(io_msg_buf, msg->x, msg->len);
205 }
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200206
207 /* Prepare tag buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100208 TEST_CALLOC(tag_buf, expected_tag_len);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200209
Valerio Setti10e9aa22023-12-12 11:54:20 +0100210 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200212 /* Test with input == output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 TEST_EQUAL(mbedtls_ccm_encrypt_and_tag(&ctx, msg->len, iv->x, iv->len, add->x, add->len,
214 io_msg_buf, io_msg_buf, tag_buf, expected_tag_len), 0);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200215
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100216 TEST_MEMORY_COMPARE(io_msg_buf, msg->len, result->x, msg->len);
217 TEST_MEMORY_COMPARE(tag_buf, expected_tag_len, expected_tag, expected_tag_len);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200218
Mateusz Starzykceb5bc62021-07-30 14:36:22 +0200219 /* Prepare data_t structures for multipart testing */
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200220 const data_t encrypted_expected = { .x = result->x,
221 .len = msg->len };
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 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 +0200223 .len = expected_tag_len };
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 for (n1 = 0; n1 <= msg->len; n1 += 1) {
226 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
227 mbedtls_test_set_step(n1 * 10000 + n1_add);
228 if (!check_multipart(&ctx, MBEDTLS_CCM_ENCRYPT,
229 iv, add, msg,
230 &encrypted_expected,
231 &tag_expected,
232 n1, n1_add)) {
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200233 goto exit;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200234 }
235 }
236 }
237
Darryl Green0daf4ca2018-05-29 14:12:26 +0100238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_ccm_free(&ctx);
240 mbedtls_free(io_msg_buf);
241 mbedtls_free(tag_buf);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100242 BLOCK_CIPHER_PSA_DONE();
Darryl Green0daf4ca2018-05-29 14:12:26 +0100243}
244/* END_CASE */
245
246/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247void mbedtls_ccm_star_no_tag(int cipher_id, int mode, data_t *key,
248 data_t *msg, data_t *iv, data_t *result)
249{
250 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200251 mbedtls_ccm_init(&ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 uint8_t *output = NULL;
253 size_t olen;
254
Valerio Setti10e9aa22023-12-12 11:54:20 +0100255 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
257 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
258 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, msg->len, 0));
259
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100260 TEST_CALLOC(output, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
262 TEST_EQUAL(result->len, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100263 TEST_MEMORY_COMPARE(output, olen, result->x, result->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100264
265 TEST_EQUAL(0, mbedtls_ccm_finish(&ctx, NULL, 0));
266exit:
267 mbedtls_free(output);
268 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100269 BLOCK_CIPHER_PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +0100270}
271/* END_CASE */
272
273/* BEGIN_CASE */
274void mbedtls_ccm_auth_decrypt(int cipher_id, data_t *key,
275 data_t *msg, data_t *iv,
276 data_t *add, int expected_tag_len, int result,
277 data_t *expected_msg)
278{
279 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200280 mbedtls_ccm_init(&ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 size_t n1, n1_add;
282
283 const size_t expected_msg_len = msg->len - expected_tag_len;
284 const uint8_t *expected_tag = msg->x + expected_msg_len;
285
286 /* Prepare input/output message buffer */
287 uint8_t *io_msg_buf = NULL;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100288 TEST_CALLOC(io_msg_buf, expected_msg_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (expected_msg_len) {
290 memcpy(io_msg_buf, msg->x, expected_msg_len);
291 }
292
Valerio Setti10e9aa22023-12-12 11:54:20 +0100293 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
295 /* Test with input == output */
296 TEST_EQUAL(mbedtls_ccm_auth_decrypt(&ctx, expected_msg_len, iv->x, iv->len, add->x, add->len,
297 io_msg_buf, io_msg_buf, expected_tag, expected_tag_len),
298 result);
299
300 if (result == 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100301 TEST_MEMORY_COMPARE(io_msg_buf, expected_msg_len, expected_msg->x, expected_msg_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100302
303 /* Prepare data_t structures for multipart testing */
304 const data_t encrypted = { .x = msg->x,
305 .len = expected_msg_len };
306
307 const data_t tag_expected = { .x = (uint8_t *) expected_tag,
308 .len = expected_tag_len };
309
310 for (n1 = 0; n1 <= expected_msg_len; n1 += 1) {
311 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
312 mbedtls_test_set_step(n1 * 10000 + n1_add);
313 if (!check_multipart(&ctx, MBEDTLS_CCM_DECRYPT,
314 iv, add, &encrypted,
315 expected_msg,
316 &tag_expected,
317 n1, n1_add)) {
318 goto exit;
319 }
320 }
321 }
322 } else {
323 size_t i;
324
325 for (i = 0; i < expected_msg_len; i++) {
326 TEST_EQUAL(io_msg_buf[i], 0);
327 }
328 }
329
330exit:
331 mbedtls_free(io_msg_buf);
332 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100333 BLOCK_CIPHER_PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +0100334}
335/* END_CASE */
336
337/* BEGIN_CASE */
338void mbedtls_ccm_star_encrypt_and_tag(int cipher_id,
339 data_t *key, data_t *msg,
340 data_t *source_address, data_t *frame_counter,
341 int sec_level, data_t *add,
342 data_t *expected_result, int output_ret)
343{
344 unsigned char iv[13];
345 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200346 mbedtls_ccm_init(&ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 size_t iv_len, expected_tag_len;
348 size_t n1, n1_add;
349 uint8_t *io_msg_buf = NULL;
350 uint8_t *tag_buf = NULL;
351
352 const uint8_t *expected_tag = expected_result->x + msg->len;
353
354 /* Calculate tag length */
355 if (sec_level % 4 == 0) {
356 expected_tag_len = 0;
357 } else {
358 expected_tag_len = 1 << (sec_level % 4 + 1);
359 }
360
361 /* Prepare input/output message buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100362 TEST_CALLOC(io_msg_buf, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (msg->len) {
364 memcpy(io_msg_buf, msg->x, msg->len);
365 }
366
367 /* Prepare tag buffer */
368 if (expected_tag_len == 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100369 TEST_CALLOC(tag_buf, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 } else {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100371 TEST_CALLOC(tag_buf, expected_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 }
373
374 /* Calculate iv */
375 TEST_ASSERT(source_address->len == 8);
376 TEST_ASSERT(frame_counter->len == 4);
377 memcpy(iv, source_address->x, source_address->len);
378 memcpy(iv + source_address->len, frame_counter->x, frame_counter->len);
379 iv[source_address->len + frame_counter->len] = sec_level;
380 iv_len = sizeof(iv);
381
Valerio Setti10e9aa22023-12-12 11:54:20 +0100382 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id,
384 key->x, key->len * 8), 0);
385 /* Test with input == output */
386 TEST_EQUAL(mbedtls_ccm_star_encrypt_and_tag(&ctx, msg->len, iv, iv_len,
387 add->x, add->len, io_msg_buf,
388 io_msg_buf, tag_buf, expected_tag_len), output_ret);
389
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100390 TEST_MEMORY_COMPARE(io_msg_buf, msg->len, expected_result->x, msg->len);
391 TEST_MEMORY_COMPARE(tag_buf, expected_tag_len, expected_tag, expected_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100392
393 if (output_ret == 0) {
394 const data_t iv_data = { .x = iv,
395 .len = iv_len };
396
397 const data_t encrypted_expected = { .x = expected_result->x,
398 .len = msg->len };
399 const data_t tag_expected = { .x = (uint8_t *) expected_tag,
400 .len = expected_tag_len };
401
402 for (n1 = 0; n1 <= msg->len; n1 += 1) {
403 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
404 mbedtls_test_set_step(n1 * 10000 + n1_add);
405 if (!check_multipart(&ctx, MBEDTLS_CCM_STAR_ENCRYPT,
406 &iv_data, add, msg,
407 &encrypted_expected,
408 &tag_expected,
409 n1, n1_add)) {
410 goto exit;
411 }
412 }
413 }
414 }
415
416exit:
417 mbedtls_ccm_free(&ctx);
418 mbedtls_free(io_msg_buf);
419 mbedtls_free(tag_buf);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100420 BLOCK_CIPHER_PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +0100421}
422/* END_CASE */
423
424/* BEGIN_CASE */
425void mbedtls_ccm_star_auth_decrypt(int cipher_id,
426 data_t *key, data_t *msg,
427 data_t *source_address, data_t *frame_counter,
428 int sec_level, data_t *add,
429 data_t *expected_result, int output_ret)
Darryl Green0daf4ca2018-05-29 14:12:26 +0100430{
Darryl Green0daf4ca2018-05-29 14:12:26 +0100431 unsigned char iv[13];
Darryl Green0daf4ca2018-05-29 14:12:26 +0100432 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200433 mbedtls_ccm_init(&ctx);
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200434 size_t iv_len, expected_tag_len;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200435 size_t n1, n1_add;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100436
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200437 /* Calculate tag length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (sec_level % 4 == 0) {
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200439 expected_tag_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 } else {
441 expected_tag_len = 1 << (sec_level % 4 + 1);
442 }
Darryl Green0daf4ca2018-05-29 14:12:26 +0100443
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200444 const size_t expected_msg_len = msg->len - expected_tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 const uint8_t *expected_tag = msg->x + expected_msg_len;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200446
447 /* Prepare input/output message buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 uint8_t *io_msg_buf = NULL;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100449 TEST_CALLOC(io_msg_buf, expected_msg_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (expected_msg_len) {
451 memcpy(io_msg_buf, msg->x, expected_msg_len);
452 }
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200453
454 /* Calculate iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 memset(iv, 0x00, sizeof(iv));
456 TEST_ASSERT(source_address->len == 8);
457 TEST_ASSERT(frame_counter->len == 4);
458 memcpy(iv, source_address->x, source_address->len);
459 memcpy(iv + source_address->len, frame_counter->x, frame_counter->len);
Ronald Cron9ed40732020-06-25 09:03:34 +0200460 iv[source_address->len + frame_counter->len] = sec_level;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 iv_len = sizeof(iv);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100462
Valerio Setti10e9aa22023-12-12 11:54:20 +0100463 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 TEST_ASSERT(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8) == 0);
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200465 /* Test with input == output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 TEST_EQUAL(mbedtls_ccm_star_auth_decrypt(&ctx, expected_msg_len, iv, iv_len,
467 add->x, add->len, io_msg_buf, io_msg_buf,
468 expected_tag, expected_tag_len), output_ret);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100469
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100470 TEST_MEMORY_COMPARE(io_msg_buf, expected_msg_len, expected_result->x, expected_msg_len);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (output_ret == 0) {
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200473 const data_t iv_data = { .x = iv,
474 .len = iv_len };
475
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200476 const data_t encrypted = { .x = msg->x,
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 .len = expected_msg_len };
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 const data_t tag_expected = { .x = (uint8_t *) expected_tag,
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200480 .len = expected_tag_len };
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 for (n1 = 0; n1 <= expected_msg_len; n1 += 1) {
483 for (n1_add = 0; n1_add <= add->len; n1_add += 1) {
484 mbedtls_test_set_step(n1 * 10000 + n1_add);
485 if (!check_multipart(&ctx, MBEDTLS_CCM_STAR_DECRYPT,
486 &iv_data, add, &encrypted,
487 expected_result,
488 &tag_expected,
489 n1, n1_add)) {
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200490 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 }
492 }
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200493 }
494 }
495
Darryl Green0daf4ca2018-05-29 14:12:26 +0100496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_ccm_free(&ctx);
498 mbedtls_free(io_msg_buf);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100499 BLOCK_CIPHER_PSA_DONE();
Darryl Green0daf4ca2018-05-29 14:12:26 +0100500}
501/* END_CASE */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200502
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200503/* Skip auth data, provide full text */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505void mbedtls_ccm_skip_ad(int cipher_id, int mode,
506 data_t *key, data_t *msg, data_t *iv,
507 data_t *result, data_t *tag)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200508{
509 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200510 mbedtls_ccm_init(&ctx);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200511 uint8_t *output = NULL;
512 size_t olen;
513
514 /* Sanity checks on the test data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 TEST_EQUAL(msg->len, result->len);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200516
Valerio Setti10e9aa22023-12-12 11:54:20 +0100517 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
519 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
520 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, msg->len, tag->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200521
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100522 TEST_CALLOC(output, result->len);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200523 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, result->len, &olen));
525 TEST_EQUAL(result->len, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100526 TEST_MEMORY_COMPARE(output, olen, result->x, result->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_free(output);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200528 output = NULL;
529
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100530 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 TEST_EQUAL(0, mbedtls_ccm_finish(&ctx, output, tag->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100532 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 mbedtls_free(output);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200534 output = NULL;
535
536exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 mbedtls_free(output);
538 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100539 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200540}
541/* END_CASE */
542
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200543/* Provide auth data, skip full text */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200544/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545void mbedtls_ccm_skip_update(int cipher_id, int mode,
546 data_t *key, data_t *iv, data_t *add,
547 data_t *tag)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200548{
549 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200550 mbedtls_ccm_init(&ctx);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200551 uint8_t *output = NULL;
552
Valerio Setti10e9aa22023-12-12 11:54:20 +0100553 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
555 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
556 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, tag->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200559
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100560 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 TEST_EQUAL(0, mbedtls_ccm_finish(&ctx, output, tag->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100562 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_free(output);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200564 output = NULL;
565
566exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_free(output);
568 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100569 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200570}
571/* END_CASE */
572
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200573/* Provide too much auth data */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200574/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575void mbedtls_ccm_overflow_ad(int cipher_id, int mode,
576 data_t *key, data_t *iv,
577 data_t *add)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200578{
579 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200580 mbedtls_ccm_init(&ctx);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200581
Gilles Peskine42919e02024-10-07 11:12:17 +0200582 /* This test can't be run with empty additional data */
583 TEST_LE_U(1, add->len);
584
Valerio Setti10e9aa22023-12-12 11:54:20 +0100585 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
587 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200588 // use hardcoded values for msg length and tag length. They are not a part of this test
Mateusz Starzyk3050f052021-09-02 12:38:51 +0200589 // subtract 1 from configured auth data length to provoke an overflow
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len - 1, 16, 16));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100595 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200596}
597/* END_CASE */
598
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200599/* Provide unexpected auth data */
600/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601void mbedtls_ccm_unexpected_ad(int cipher_id, int mode,
602 data_t *key, data_t *iv,
603 data_t *add)
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200604{
605 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200606 mbedtls_ccm_init(&ctx);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200607
Valerio Setti10e9aa22023-12-12 11:54:20 +0100608 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
610 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200611 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, 16, 16));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100617 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200618}
619/* END_CASE */
620
621/* Provide unexpected plaintext/ciphertext data */
622/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623void mbedtls_ccm_unexpected_text(int cipher_id, int mode,
624 data_t *key, data_t *msg, data_t *iv,
625 data_t *add)
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200626{
627 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200628 mbedtls_ccm_init(&ctx);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200629 uint8_t *output = NULL;
630 size_t olen;
631
Valerio Setti10e9aa22023-12-12 11:54:20 +0100632 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
634 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200635 // use hardcoded value for tag length. It is not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, 16));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200639
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100640 TEST_CALLOC(output, msg->len);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200641 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT,
643 mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200644exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 mbedtls_free(output);
646 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100647 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200648}
649/* END_CASE */
650
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200651/* Provide incomplete auth data and finish */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200652/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653void mbedtls_ccm_incomplete_ad(int cipher_id, int mode,
654 data_t *key, data_t *iv, data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200655{
656 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200657 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200658 uint8_t *output = NULL;
659
Gilles Peskine42919e02024-10-07 11:12:17 +0200660 /* This test can't be run with empty additional data */
661 TEST_LE_U(1, add->len);
662
Valerio Setti10e9aa22023-12-12 11:54:20 +0100663 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
665 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200666 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len - 1));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200670
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100671 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200673
674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 mbedtls_free(output);
676 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100677 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200678}
679/* END_CASE */
680
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200681/* Provide complete auth data on first update_ad.
682 * Provide unexpected auth data on second update_ad */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684void mbedtls_ccm_full_ad_and_overflow(int cipher_id, int mode,
685 data_t *key, data_t *iv,
686 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200687{
688 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200689 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200690
Valerio Setti10e9aa22023-12-12 11:54:20 +0100691 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
693 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200694 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 16, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200696
697 // pass full auth data
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200699 // pass 1 extra byte
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, 1));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100703 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200704}
705/* END_CASE */
706
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200707/* Provide incomplete auth data on first update_ad.
708 * Provide too much auth data on second update_ad */
709/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710void mbedtls_ccm_incomplete_ad_and_overflow(int cipher_id, int mode,
711 data_t *key, data_t *iv,
712 data_t *add)
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200713{
714 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200715 mbedtls_ccm_init(&ctx);
Ronald Cron133740b2021-09-17 09:38:07 +0200716 uint8_t add_second_buffer[2];
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200717
Gilles Peskine42919e02024-10-07 11:12:17 +0200718 /* This test can't be run with empty additional data */
719 TEST_LE_U(1, add->len);
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 add_second_buffer[0] = add->x[add->len - 1];
Ronald Cron133740b2021-09-17 09:38:07 +0200722 add_second_buffer[1] = 0xAB; // some magic value
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200723
Valerio Setti10e9aa22023-12-12 11:54:20 +0100724 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
726 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200727 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 16, 16));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200729
730 // pass incomplete auth data
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len - 1));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200732 // pass 2 extra bytes (1 missing byte from previous incomplete pass, and 1 unexpected byte)
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add_second_buffer, 2));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200734exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100736 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200737}
738/* END_CASE */
739
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200740/* Provide too much plaintext/ciphertext */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200741/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742void mbedtls_ccm_overflow_update(int cipher_id, int mode,
743 data_t *key, data_t *msg, data_t *iv,
744 data_t *add)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200745{
746 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200747 mbedtls_ccm_init(&ctx);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200748 uint8_t *output = NULL;
749 size_t olen;
750
Gilles Peskine42919e02024-10-07 11:12:17 +0200751 /* This test can't be run with an empty message */
752 TEST_LE_U(1, msg->len);
753
Valerio Setti10e9aa22023-12-12 11:54:20 +0100754 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
756 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200757 // use hardcoded value for tag length. It is a not a part of this test
Mateusz Starzyk3050f052021-09-02 12:38:51 +0200758 // subtract 1 from configured msg length to provoke an overflow
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len - 1, 16));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200762
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100763 TEST_CALLOC(output, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
765 mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200766exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 mbedtls_free(output);
768 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100769 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200770}
771/* END_CASE */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200772
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200773/* Provide incomplete plaintext/ciphertext and finish */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200774/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775void mbedtls_ccm_incomplete_update(int cipher_id, int mode,
776 data_t *key, data_t *msg, data_t *iv,
777 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200778{
779 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200780 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200781 uint8_t *output = NULL;
782 size_t olen;
783
Gilles Peskine42919e02024-10-07 11:12:17 +0200784 /* This test can't be run with an empty message */
785 TEST_LE_U(1, msg->len);
786
Valerio Setti10e9aa22023-12-12 11:54:20 +0100787 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
789 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200790 // use hardcoded value for tag length. It is not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200794
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100795 TEST_CALLOC(output, msg->len);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200796 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len - 1, output, msg->len, &olen));
798 mbedtls_free(output);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200799 output = NULL;
800
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100801 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200803
804exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 mbedtls_free(output);
806 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100807 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200808}
809/* END_CASE */
810
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200811/* Provide full plaintext/ciphertext of first update
812 * Provide unexpected plaintext/ciphertext on second update */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200813/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814void mbedtls_ccm_full_update_and_overflow(int cipher_id, int mode,
815 data_t *key, data_t *msg, data_t *iv,
816 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200817{
818 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200819 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200820 uint8_t *output = NULL;
821 size_t olen;
822
Valerio Setti10e9aa22023-12-12 11:54:20 +0100823 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
825 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200826 // use hardcoded value for tag length. It is a not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200830
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100831 TEST_CALLOC(output, msg->len);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200832 // pass full text
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200834 // pass 1 extra byte
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
836 mbedtls_ccm_update(&ctx, msg->x, 1, output, 1, &olen));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200837exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 mbedtls_free(output);
839 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100840 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200841}
842/* END_CASE */
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200843
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200844/* Provide incomplete plaintext/ciphertext of first update
845 * Provide too much plaintext/ciphertext on second update */
846/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847void mbedtls_ccm_incomplete_update_overflow(int cipher_id, int mode,
848 data_t *key, data_t *msg, data_t *iv,
849 data_t *add)
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200850{
851 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200852 mbedtls_ccm_init(&ctx);
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200853 uint8_t *output = NULL;
854 size_t olen;
Ronald Cron133740b2021-09-17 09:38:07 +0200855 uint8_t msg_second_buffer[2];
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200856
Gilles Peskine42919e02024-10-07 11:12:17 +0200857 /* This test can't be run with an empty message */
858 TEST_LE_U(1, msg->len);
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 msg_second_buffer[0] = msg->x[msg->len - 1];
Ronald Cron133740b2021-09-17 09:38:07 +0200861 msg_second_buffer[1] = 0xAB; // some magic value
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200862
Valerio Setti10e9aa22023-12-12 11:54:20 +0100863 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
865 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200866 // use hardcoded value for tag length. It is a not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200870
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100871 TEST_CALLOC(output, msg->len + 1);
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200872 // pass incomplete text
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len - 1, output, msg->len + 1, &olen));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200874 // pass 2 extra bytes (1 missing byte from previous incomplete pass, and 1 unexpected byte)
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
876 mbedtls_ccm_update(&ctx, msg_second_buffer, 2, output + msg->len - 1, 2, &olen));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200877exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 mbedtls_free(output);
879 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100880 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200881}
882/* END_CASE */
883
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200884/* Finish without passing any auth data or plaintext/ciphertext input */
885/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886void mbedtls_ccm_instant_finish(int cipher_id, int mode,
887 data_t *key, data_t *iv)
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200888{
889 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200890 mbedtls_ccm_init(&ctx);
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200891 uint8_t *output = NULL;
892
Valerio Setti10e9aa22023-12-12 11:54:20 +0100893 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
895 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200896 // use hardcoded values for add length, msg length and tag length.
897 // They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 16, 16, 16));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200899
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100900 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200902
903exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 mbedtls_free(output);
905 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100906 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200907}
908/* END_CASE */