blob: 25cc49b8d2743e9e621dff464ca63980920dd822 [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. */
7static 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)
16{
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 */
24 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 ) );
31
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. */
35 ASSERT_ALLOC( output, n1 );
36 olen = 0xdeadbeef;
37 TEST_EQUAL( 0, mbedtls_ccm_update( ctx, input->x, n1, output, n1, &olen ) );
38 TEST_EQUAL( n1, olen );
39 ASSERT_COMPARE( output, olen, expected_output->x, n1 );
40 mbedtls_free( output );
41 output = NULL;
42
43 ASSERT_ALLOC( output, n2 );
44 olen = 0xdeadbeef;
45 TEST_EQUAL( 0, mbedtls_ccm_update( ctx, input->x + n1, n2, output, n2, &olen ) );
46 TEST_EQUAL( n2, olen );
47 ASSERT_COMPARE( output, olen, expected_output->x + n1, n2 );
48 mbedtls_free( output );
49 output = NULL;
50
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +020051 if( tag->len == 0 )
52 ASSERT_ALLOC( output, 16 );
53 else
54 ASSERT_ALLOC( output, tag->len );
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +020055 TEST_EQUAL( 0, mbedtls_ccm_finish( ctx, output, tag->len ) );
56 ASSERT_COMPARE( output, tag->len, tag->x, tag->len );
57 mbedtls_free( output );
58 output = NULL;
59
60 ok = 1;
61exit:
62 mbedtls_free( output );
63 return( ok );
64}
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020065/* END_HEADER */
66
67/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 * depends_on:MBEDTLS_CCM_C
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020069 * END_DEPENDENCIES
70 */
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010073void mbedtls_ccm_self_test( )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020074{
Andres AG93012e82016-09-09 09:10:28 +010075 TEST_ASSERT( mbedtls_ccm_self_test( 1 ) == 0 );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020076}
77/* END_CASE */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020078
79/* BEGIN_CASE */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020080void mbedtls_ccm_setkey( int cipher_id, int key_size, int result )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020083 unsigned char key[32];
84 int ret;
85
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020086 mbedtls_ccm_init( &ctx );
87
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020088 memset( key, 0x2A, sizeof( key ) );
89 TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
90
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020091 ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020092 TEST_ASSERT( ret == result );
93
Paul Bakkerbd51b262014-07-10 15:26:12 +020094exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020096}
97/* END_CASE */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200100void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res )
101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200103 unsigned char key[16];
104 unsigned char msg[10];
105 unsigned char iv[14];
Dave Rodgman2e680342020-10-15 14:00:40 +0100106 unsigned char *add = NULL;
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200107 unsigned char out[10];
108 unsigned char tag[18];
109 int decrypt_ret;
110
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200111 mbedtls_ccm_init( &ctx );
112
Dave Rodgman2e680342020-10-15 14:00:40 +0100113 ASSERT_ALLOC_WEAK( add, add_len );
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200114 memset( key, 0, sizeof( key ) );
115 memset( msg, 0, sizeof( msg ) );
116 memset( iv, 0, sizeof( iv ) );
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200117 memset( out, 0, sizeof( out ) );
118 memset( tag, 0, sizeof( tag ) );
119
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200120 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200121 key, 8 * sizeof( key ) ) == 0 );
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200124 msg, out, tag, tag_len ) == res );
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 decrypt_ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200127 msg, out, tag, tag_len );
128
129 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200131 else
132 TEST_ASSERT( decrypt_ret == res );
133
Paul Bakkerbd51b262014-07-10 15:26:12 +0200134exit:
Dave Rodgman2e680342020-10-15 14:00:40 +0100135 mbedtls_free( add );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +0200137}
138/* END_CASE */
139
Janos Follath95ab93d2018-05-14 14:32:41 +0100140/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
141void ccm_star_lengths( int msg_len, int iv_len, int add_len, int tag_len,
142 int res )
143{
144 mbedtls_ccm_context ctx;
145 unsigned char key[16];
146 unsigned char msg[10];
147 unsigned char iv[14];
148 unsigned char add[10];
149 unsigned char out[10];
150 unsigned char tag[18];
151 int decrypt_ret;
152
153 mbedtls_ccm_init( &ctx );
154
155 memset( key, 0, sizeof( key ) );
156 memset( msg, 0, sizeof( msg ) );
157 memset( iv, 0, sizeof( iv ) );
158 memset( add, 0, sizeof( add ) );
159 memset( out, 0, sizeof( out ) );
160 memset( tag, 0, sizeof( tag ) );
161
162 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
163 key, 8 * sizeof( key ) ) == 0 );
164
165 TEST_ASSERT( mbedtls_ccm_star_encrypt_and_tag( &ctx, msg_len, iv, iv_len,
166 add, add_len, msg, out, tag, tag_len ) == res );
167
168 decrypt_ret = mbedtls_ccm_star_auth_decrypt( &ctx, msg_len, iv, iv_len, add,
169 add_len, msg, out, tag, tag_len );
170
171 if( res == 0 && tag_len != 0 )
172 TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
173 else
174 TEST_ASSERT( decrypt_ret == res );
175
176exit:
177 mbedtls_ccm_free( &ctx );
178}
179/* END_CASE */
180
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200181/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100182void mbedtls_ccm_encrypt_and_tag( int cipher_id, data_t * key,
183 data_t * msg, data_t * iv,
184 data_t * add, data_t * result )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_ccm_context ctx;
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200187 size_t n1, n1_add;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200188 uint8_t* io_msg_buf = NULL;
189 uint8_t* tag_buf = NULL;
190 const size_t expected_tag_len = result->len - msg->len;
191 const uint8_t* expected_tag = result->x + msg->len;
192
193 /* Prepare input/output message buffer */
194 ASSERT_ALLOC( io_msg_buf, msg->len );
195 if( msg->len != 0 )
196 memcpy( io_msg_buf, msg->x, msg->len );
197
198 /* Prepare tag buffer */
199 ASSERT_ALLOC( tag_buf, expected_tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200200
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200201 mbedtls_ccm_init( &ctx );
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200202 TEST_EQUAL( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ), 0 );
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200203 /* Test with input == output */
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200204 TEST_EQUAL( mbedtls_ccm_encrypt_and_tag( &ctx, msg->len, iv->x, iv->len, add->x, add->len,
205 io_msg_buf, io_msg_buf, tag_buf, expected_tag_len ), 0);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200206
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200207 ASSERT_COMPARE( io_msg_buf, msg->len, result->x, msg->len );
208 ASSERT_COMPARE( tag_buf, expected_tag_len, expected_tag, expected_tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200209
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200210 /* Prepare data_t structers for multipart testing */
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200211 const data_t encrypted_expected = { .x = result->x,
212 .len = msg->len };
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200213 const data_t tag_expected = { .x = (uint8_t*) expected_tag, /* cast to conform with data_t x type */
214 .len = expected_tag_len };
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200215
216 for( n1 = 0; n1 <= msg->len; n1 += 1 )
217 {
218 for( n1_add = 0; n1_add <= add->len; n1_add += 1 )
219 {
220 mbedtls_test_set_step( n1 * 10000 + n1_add );
221 if( !check_multipart( &ctx, MBEDTLS_CCM_ENCRYPT,
222 iv, add, msg,
223 &encrypted_expected,
224 &tag_expected,
225 n1, n1_add ) )
226 goto exit;
227 }
228 }
229
Paul Bakkerbd51b262014-07-10 15:26:12 +0200230exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_ccm_free( &ctx );
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200232 mbedtls_free( io_msg_buf );
233 mbedtls_free( tag_buf );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200234}
235/* END_CASE */
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200236
237/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100238void mbedtls_ccm_auth_decrypt( int cipher_id, data_t * key,
239 data_t * msg, data_t * iv,
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200240 data_t * add, int expected_tag_len, int result,
Ronald Cronac6ae352020-06-26 14:33:03 +0200241 data_t * expected_msg )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ccm_context ctx;
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200244 size_t n1, n1_add;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200245
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200246 const size_t expected_msg_len = msg->len - expected_tag_len;
247 const uint8_t* expected_tag = msg->x + expected_msg_len;
248
249 /* Prepare input/output message buffer */
250 uint8_t* io_msg_buf = NULL;
251 ASSERT_ALLOC( io_msg_buf, expected_msg_len );
252 if( expected_msg_len )
253 memcpy( io_msg_buf, msg->x, expected_msg_len );
254
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200255 mbedtls_ccm_init( &ctx );
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200256 TEST_EQUAL( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ), 0 );
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200257 /* Test with input == output */
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200258 TEST_EQUAL( mbedtls_ccm_auth_decrypt( &ctx, expected_msg_len, iv->x, iv->len, add->x, add->len,
259 io_msg_buf, io_msg_buf, expected_tag, expected_tag_len ), result );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200260
Mohammad Azim Khancfd83422018-06-26 18:15:18 +0100261 if( result == 0 )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200262 {
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200263 ASSERT_COMPARE( io_msg_buf, expected_msg_len, expected_msg->x, expected_msg_len );
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200264
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200265 /* Prepare data_t structers for multipart testing */
266 const data_t encrypted = { .x = msg->x,
267 .len = expected_msg_len };
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200268
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200269 const data_t tag_expected = { .x = (uint8_t*) expected_tag,
270 .len = expected_tag_len };
271
272 for( n1 = 0; n1 <= expected_msg_len; n1 += 1 )
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200273 {
274 for( n1_add = 0; n1_add <= add->len; n1_add += 1 )
275 {
276 mbedtls_test_set_step( n1 * 10000 + n1_add );
277 if( !check_multipart( &ctx, MBEDTLS_CCM_DECRYPT,
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200278 iv, add, &encrypted,
Mateusz Starzyk25a3dfe2021-07-12 14:53:45 +0200279 expected_msg,
280 &tag_expected,
281 n1, n1_add ) )
282 goto exit;
283 }
284 }
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200285 }
286 else
287 {
288 size_t i;
289
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200290 for( i = 0; i < expected_msg_len; i++ )
291 TEST_EQUAL( io_msg_buf[i], 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200292 }
293
Paul Bakkerbd51b262014-07-10 15:26:12 +0200294exit:
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200295 mbedtls_free(io_msg_buf);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200297}
298/* END_CASE */
Darryl Green0daf4ca2018-05-29 14:12:26 +0100299
300/* BEGIN_CASE */
301void mbedtls_ccm_star_encrypt_and_tag( int cipher_id,
Ronald Cron9ed40732020-06-25 09:03:34 +0200302 data_t *key, data_t *msg,
303 data_t *source_address, data_t *frame_counter,
304 int sec_level, data_t *add,
305 data_t *expected_result, int output_ret )
Darryl Green0daf4ca2018-05-29 14:12:26 +0100306{
Darryl Green0daf4ca2018-05-29 14:12:26 +0100307 unsigned char iv[13];
Darryl Green0daf4ca2018-05-29 14:12:26 +0100308 mbedtls_ccm_context ctx;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200309 size_t iv_len, expected_tag_len;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200310 size_t n1, n1_add;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200311 uint8_t* io_msg_buf = NULL;
312 uint8_t* tag_buf = NULL;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100313
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200314 const uint8_t* expected_tag = expected_result->x + msg->len;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100315
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200316 /* Calculate tag length */
Darryl Green0daf4ca2018-05-29 14:12:26 +0100317 if( sec_level % 4 == 0)
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200318 expected_tag_len = 0;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100319 else
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200320 expected_tag_len = 1 << ( sec_level % 4 + 1);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100321
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200322 /* Prepare input/output message buffer */
323 ASSERT_ALLOC( io_msg_buf, msg->len );
324 if( msg->len )
325 memcpy( io_msg_buf, msg->x, msg->len );
326
327 /* Prepare tag buffer */
328 if( expected_tag_len == 0 )
329 ASSERT_ALLOC( tag_buf, 16 );
330 else
331 ASSERT_ALLOC( tag_buf, expected_tag_len );
332
333 /* Calculate iv */
Gilles Peskineb168c0d2021-02-09 12:00:13 +0100334 TEST_ASSERT( source_address->len == 8 );
335 TEST_ASSERT( frame_counter->len == 4 );
336 memcpy( iv, source_address->x, source_address->len );
337 memcpy( iv + source_address->len, frame_counter->x, frame_counter->len );
Ronald Cron9ed40732020-06-25 09:03:34 +0200338 iv[source_address->len + frame_counter->len] = sec_level;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100339 iv_len = sizeof( iv );
340
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200341 mbedtls_ccm_init( &ctx );
342 TEST_EQUAL( mbedtls_ccm_setkey( &ctx, cipher_id,
343 key->x, key->len * 8 ), 0 );
344 /* Test with input == output */
345 TEST_EQUAL( mbedtls_ccm_star_encrypt_and_tag( &ctx, msg->len, iv, iv_len,
346 add->x, add->len, io_msg_buf,
347 io_msg_buf, tag_buf, expected_tag_len), output_ret );
Darryl Green0daf4ca2018-05-29 14:12:26 +0100348
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200349 ASSERT_COMPARE( io_msg_buf, msg->len, expected_result->x, msg->len );
350 ASSERT_COMPARE( tag_buf, expected_tag_len, expected_tag, expected_tag_len );
Darryl Green0daf4ca2018-05-29 14:12:26 +0100351
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200352 if( output_ret == 0 )
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200353 {
354 const data_t iv_data = { .x = iv,
355 .len = iv_len };
356
357 const data_t encrypted_expected = { .x = expected_result->x,
358 .len = msg->len };
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200359 const data_t tag_expected = { .x = (uint8_t*)expected_tag,
360 .len = expected_tag_len };
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200361
362 for( n1 = 0; n1 <= msg->len; n1 += 1 )
363 {
364 for( n1_add = 0; n1_add <= add->len; n1_add += 1 )
365 {
366 mbedtls_test_set_step( n1 * 10000 + n1_add );
367 if( !check_multipart( &ctx, MBEDTLS_CCM_STAR_ENCRYPT,
368 &iv_data, add, msg,
369 &encrypted_expected,
370 &tag_expected,
371 n1, n1_add ) )
372 goto exit;
373 }
374 }
375 }
376
Darryl Green0daf4ca2018-05-29 14:12:26 +0100377exit:
378 mbedtls_ccm_free( &ctx );
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200379 mbedtls_free( io_msg_buf );
380 mbedtls_free( tag_buf );
Darryl Green0daf4ca2018-05-29 14:12:26 +0100381}
382/* END_CASE */
383
384/* BEGIN_CASE */
385void mbedtls_ccm_star_auth_decrypt( int cipher_id,
Ronald Cron9ed40732020-06-25 09:03:34 +0200386 data_t *key, data_t *msg,
387 data_t *source_address, data_t *frame_counter,
388 int sec_level, data_t *add,
389 data_t *expected_result, int output_ret )
Darryl Green0daf4ca2018-05-29 14:12:26 +0100390{
Darryl Green0daf4ca2018-05-29 14:12:26 +0100391 unsigned char iv[13];
Darryl Green0daf4ca2018-05-29 14:12:26 +0100392 mbedtls_ccm_context ctx;
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200393 size_t iv_len, expected_tag_len;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200394 size_t n1, n1_add;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100395
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200396 /* Calculate tag length */
Darryl Green0daf4ca2018-05-29 14:12:26 +0100397 if( sec_level % 4 == 0)
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200398 expected_tag_len = 0;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100399 else
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200400 expected_tag_len = 1 << ( sec_level % 4 + 1);
Darryl Green0daf4ca2018-05-29 14:12:26 +0100401
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200402 const size_t expected_msg_len = msg->len - expected_tag_len;
403 const uint8_t* expected_tag = msg->x + expected_msg_len;
404
405 /* Prepare input/output message buffer */
406 uint8_t* io_msg_buf = NULL;
407 ASSERT_ALLOC( io_msg_buf, expected_msg_len );
408 if( expected_msg_len )
409 memcpy( io_msg_buf, msg->x, expected_msg_len );
410
411 /* Calculate iv */
412 memset( iv, 0x00, sizeof( iv ) );
Gilles Peskineb168c0d2021-02-09 12:00:13 +0100413 TEST_ASSERT( source_address->len == 8 );
414 TEST_ASSERT( frame_counter->len == 4 );
415 memcpy( iv, source_address->x, source_address->len );
416 memcpy( iv + source_address->len, frame_counter->x, frame_counter->len );
Ronald Cron9ed40732020-06-25 09:03:34 +0200417 iv[source_address->len + frame_counter->len] = sec_level;
Darryl Green0daf4ca2018-05-29 14:12:26 +0100418 iv_len = sizeof( iv );
419
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200420 mbedtls_ccm_init( &ctx );
Ronald Cron9ed40732020-06-25 09:03:34 +0200421 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 );
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200422 /* Test with input == output */
423 TEST_EQUAL( mbedtls_ccm_star_auth_decrypt( &ctx, expected_msg_len, iv, iv_len,
424 add->x, add->len, io_msg_buf, io_msg_buf,
425 expected_tag, expected_tag_len ), output_ret );
Darryl Green0daf4ca2018-05-29 14:12:26 +0100426
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200427 ASSERT_COMPARE( io_msg_buf, expected_msg_len, expected_result->x, expected_msg_len );
Darryl Green0daf4ca2018-05-29 14:12:26 +0100428
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200429 if( output_ret == 0 )
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200430 {
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200431 const data_t iv_data = { .x = iv,
432 .len = iv_len };
433
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200434 const data_t encrypted = { .x = msg->x,
435 .len = expected_msg_len} ;
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200436
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200437 const data_t tag_expected = { .x = (uint8_t*) expected_tag,
438 .len = expected_tag_len };
439
440 for( n1 = 0; n1 <= expected_msg_len; n1 += 1 )
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200441 {
442 for( n1_add = 0; n1_add <= add->len; n1_add += 1 )
443 {
444 mbedtls_test_set_step( n1 * 10000 + n1_add );
445 if( !check_multipart( &ctx, MBEDTLS_CCM_STAR_DECRYPT,
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200446 &iv_data, add, &encrypted,
Mateusz Starzyk29ec75b2021-07-13 12:26:17 +0200447 expected_result,
448 &tag_expected,
449 n1, n1_add ) )
450 goto exit;
451 }
452 }
453 }
454
Darryl Green0daf4ca2018-05-29 14:12:26 +0100455exit:
456 mbedtls_ccm_free( &ctx );
Mateusz Starzyk27a1bef2021-07-13 15:33:19 +0200457 mbedtls_free( io_msg_buf );
Darryl Green0daf4ca2018-05-29 14:12:26 +0100458}
459/* END_CASE */