blob: 8c39d27a114557bbac40e097049b15fae6f2ce7c [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
Valerio Setti10e9aa22023-12-12 11:54:20 +0100582 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
584 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200585 // use hardcoded values for msg length and tag length. They are not a part of this test
Mateusz Starzyk3050f052021-09-02 12:38:51 +0200586 // subtract 1 from configured auth data length to provoke an overflow
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len - 1, 16, 16));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100592 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200593}
594/* END_CASE */
595
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200596/* Provide unexpected auth data */
597/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598void mbedtls_ccm_unexpected_ad(int cipher_id, int mode,
599 data_t *key, data_t *iv,
600 data_t *add)
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200601{
602 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200603 mbedtls_ccm_init(&ctx);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200604
Valerio Setti10e9aa22023-12-12 11:54:20 +0100605 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
607 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200608 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 0, 16, 16));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200612exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100614 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200615}
616/* END_CASE */
617
618/* Provide unexpected plaintext/ciphertext data */
619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620void mbedtls_ccm_unexpected_text(int cipher_id, int mode,
621 data_t *key, data_t *msg, data_t *iv,
622 data_t *add)
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200623{
624 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200625 mbedtls_ccm_init(&ctx);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200626 uint8_t *output = NULL;
627 size_t olen;
628
Valerio Setti10e9aa22023-12-12 11:54:20 +0100629 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
631 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200632 // use hardcoded value for tag length. It is not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, 16));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200636
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100637 TEST_CALLOC(output, msg->len);
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200638 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT,
640 mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_free(output);
643 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100644 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk83e4c122021-09-03 14:07:21 +0200645}
646/* END_CASE */
647
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200648/* Provide incomplete auth data and finish */
Mateusz Starzyk87889062021-07-29 14:08:18 +0200649/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100650void mbedtls_ccm_incomplete_ad(int cipher_id, int mode,
651 data_t *key, data_t *iv, data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200652{
653 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200654 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200655 uint8_t *output = NULL;
656
Valerio Setti10e9aa22023-12-12 11:54:20 +0100657 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
659 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200660 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 0, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len - 1));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200664
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100665 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200667
668exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 mbedtls_free(output);
670 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100671 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200672}
673/* END_CASE */
674
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200675/* Provide complete auth data on first update_ad.
676 * Provide unexpected auth data on second update_ad */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200677/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678void mbedtls_ccm_full_ad_and_overflow(int cipher_id, int mode,
679 data_t *key, data_t *iv,
680 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200681{
682 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200683 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200684
Valerio Setti10e9aa22023-12-12 11:54:20 +0100685 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
687 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200688 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 16, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200690
691 // pass full auth data
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200693 // pass 1 extra byte
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add->x, 1));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100697 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200698}
699/* END_CASE */
700
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200701/* Provide incomplete auth data on first update_ad.
702 * Provide too much auth data on second update_ad */
703/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704void mbedtls_ccm_incomplete_ad_and_overflow(int cipher_id, int mode,
705 data_t *key, data_t *iv,
706 data_t *add)
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200707{
708 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200709 mbedtls_ccm_init(&ctx);
Ronald Cron133740b2021-09-17 09:38:07 +0200710 uint8_t add_second_buffer[2];
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 add_second_buffer[0] = add->x[add->len - 1];
Ronald Cron133740b2021-09-17 09:38:07 +0200713 add_second_buffer[1] = 0xAB; // some magic value
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200714
Valerio Setti10e9aa22023-12-12 11:54:20 +0100715 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
717 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200718 // use hardcoded values for msg length and tag length. They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, 16, 16));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200720
721 // pass incomplete auth data
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len - 1));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200723 // pass 2 extra bytes (1 missing byte from previous incomplete pass, and 1 unexpected byte)
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_update_ad(&ctx, add_second_buffer, 2));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100727 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200728}
729/* END_CASE */
730
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200731/* Provide too much plaintext/ciphertext */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733void mbedtls_ccm_overflow_update(int cipher_id, int mode,
734 data_t *key, data_t *msg, data_t *iv,
735 data_t *add)
Mateusz Starzyk87889062021-07-29 14:08:18 +0200736{
737 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200738 mbedtls_ccm_init(&ctx);
Mateusz Starzyk87889062021-07-29 14:08:18 +0200739 uint8_t *output = NULL;
740 size_t olen;
741
Valerio Setti10e9aa22023-12-12 11:54:20 +0100742 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
744 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200745 // use hardcoded value for tag length. It is a not a part of this test
Mateusz Starzyk3050f052021-09-02 12:38:51 +0200746 // subtract 1 from configured msg length to provoke an overflow
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len - 1, 16));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200750
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100751 TEST_CALLOC(output, msg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
753 mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzyk87889062021-07-29 14:08:18 +0200754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 mbedtls_free(output);
756 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100757 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk87889062021-07-29 14:08:18 +0200758}
759/* END_CASE */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200760
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200761/* Provide incomplete plaintext/ciphertext and finish */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200762/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763void mbedtls_ccm_incomplete_update(int cipher_id, int mode,
764 data_t *key, data_t *msg, data_t *iv,
765 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200766{
767 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200768 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200769 uint8_t *output = NULL;
770 size_t olen;
771
Valerio Setti10e9aa22023-12-12 11:54:20 +0100772 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
774 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200775 // use hardcoded value for tag length. It is not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200779
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100780 TEST_CALLOC(output, msg->len);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200781 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len - 1, output, msg->len, &olen));
783 mbedtls_free(output);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200784 output = NULL;
785
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100786 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200788
789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 mbedtls_free(output);
791 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100792 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200793}
794/* END_CASE */
795
Mateusz Starzyk8fb17542021-08-10 13:45:19 +0200796/* Provide full plaintext/ciphertext of first update
797 * Provide unexpected plaintext/ciphertext on second update */
Mateusz Starzykf442de62021-08-10 13:36:43 +0200798/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799void mbedtls_ccm_full_update_and_overflow(int cipher_id, int mode,
800 data_t *key, data_t *msg, data_t *iv,
801 data_t *add)
Mateusz Starzykf442de62021-08-10 13:36:43 +0200802{
803 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200804 mbedtls_ccm_init(&ctx);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200805 uint8_t *output = NULL;
806 size_t olen;
807
Valerio Setti10e9aa22023-12-12 11:54:20 +0100808 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
810 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200811 // use hardcoded value for tag length. It is a not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200815
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100816 TEST_CALLOC(output, msg->len);
Mateusz Starzykf442de62021-08-10 13:36:43 +0200817 // pass full text
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len, output, msg->len, &olen));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200819 // pass 1 extra byte
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
821 mbedtls_ccm_update(&ctx, msg->x, 1, output, 1, &olen));
Mateusz Starzykf442de62021-08-10 13:36:43 +0200822exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_free(output);
824 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100825 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykf442de62021-08-10 13:36:43 +0200826}
827/* END_CASE */
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200828
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200829/* Provide incomplete plaintext/ciphertext of first update
830 * Provide too much plaintext/ciphertext on second update */
831/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832void mbedtls_ccm_incomplete_update_overflow(int cipher_id, int mode,
833 data_t *key, data_t *msg, data_t *iv,
834 data_t *add)
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200835{
836 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200837 mbedtls_ccm_init(&ctx);
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200838 uint8_t *output = NULL;
839 size_t olen;
Ronald Cron133740b2021-09-17 09:38:07 +0200840 uint8_t msg_second_buffer[2];
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 msg_second_buffer[0] = msg->x[msg->len - 1];
Ronald Cron133740b2021-09-17 09:38:07 +0200843 msg_second_buffer[1] = 0xAB; // some magic value
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200844
Valerio Setti10e9aa22023-12-12 11:54:20 +0100845 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
847 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200848 // use hardcoded value for tag length. It is a not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, add->len, msg->len, 16));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 TEST_EQUAL(0, mbedtls_ccm_update_ad(&ctx, add->x, add->len));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200852
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100853 TEST_CALLOC(output, msg->len + 1);
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200854 // pass incomplete text
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 TEST_EQUAL(0, mbedtls_ccm_update(&ctx, msg->x, msg->len - 1, output, msg->len + 1, &olen));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200856 // pass 2 extra bytes (1 missing byte from previous incomplete pass, and 1 unexpected byte)
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, \
858 mbedtls_ccm_update(&ctx, msg_second_buffer, 2, output + msg->len - 1, 2, &olen));
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 mbedtls_free(output);
861 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100862 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykcd975e42021-09-02 13:25:19 +0200863}
864/* END_CASE */
865
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200866/* Finish without passing any auth data or plaintext/ciphertext input */
867/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868void mbedtls_ccm_instant_finish(int cipher_id, int mode,
869 data_t *key, data_t *iv)
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200870{
871 mbedtls_ccm_context ctx;
Gilles Peskineefe30762024-10-07 11:11:32 +0200872 mbedtls_ccm_init(&ctx);
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200873 uint8_t *output = NULL;
874
Valerio Setti10e9aa22023-12-12 11:54:20 +0100875 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 TEST_EQUAL(mbedtls_ccm_setkey(&ctx, cipher_id, key->x, key->len * 8), 0);
877 TEST_EQUAL(0, mbedtls_ccm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200878 // use hardcoded values for add length, msg length and tag length.
879 // They are not a part of this test
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 TEST_EQUAL(0, mbedtls_ccm_set_lengths(&ctx, 16, 16, 16));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200881
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100882 TEST_CALLOC(output, 16);
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 TEST_EQUAL(MBEDTLS_ERR_CCM_BAD_INPUT, mbedtls_ccm_finish(&ctx, output, 16));
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200884
885exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_free(output);
887 mbedtls_ccm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100888 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyke0f52272021-08-10 13:55:47 +0200889}
890/* END_CASE */