blob: 5dbc837e428c79895abe70655561b6b3ad7eeef2 [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"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02003/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_CCM_C
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02007 * END_DEPENDENCIES
8 */
9
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void mbedtls_ccm_self_test( )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020012{
Andres AG93012e82016-09-09 09:10:28 +010013 TEST_ASSERT( mbedtls_ccm_self_test( 1 ) == 0 );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020014}
15/* END_CASE */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020016
17/* BEGIN_CASE */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020018void mbedtls_ccm_setkey( int cipher_id, int key_size, int result )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020021 unsigned char key[32];
22 int ret;
23
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020024 mbedtls_ccm_init( &ctx );
25
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020026 memset( key, 0x2A, sizeof( key ) );
27 TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
28
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020029 ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020030 TEST_ASSERT( ret == result );
31
Paul Bakkerbd51b262014-07-10 15:26:12 +020032exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020034}
35/* END_CASE */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020038void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res )
39{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020041 unsigned char key[16];
42 unsigned char msg[10];
43 unsigned char iv[14];
44 unsigned char add[10];
45 unsigned char out[10];
46 unsigned char tag[18];
47 int decrypt_ret;
48
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020049 mbedtls_ccm_init( &ctx );
50
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020051 memset( key, 0, sizeof( key ) );
52 memset( msg, 0, sizeof( msg ) );
53 memset( iv, 0, sizeof( iv ) );
54 memset( add, 0, sizeof( add ) );
55 memset( out, 0, sizeof( out ) );
56 memset( tag, 0, sizeof( tag ) );
57
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020058 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020059 key, 8 * sizeof( key ) ) == 0 );
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 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 +020062 msg, out, tag, tag_len ) == res );
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 decrypt_ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020065 msg, out, tag, tag_len );
66
67 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020069 else
70 TEST_ASSERT( decrypt_ret == res );
71
Paul Bakkerbd51b262014-07-10 15:26:12 +020072exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020074}
75/* END_CASE */
76
Janos Follath95ab93d2018-05-14 14:32:41 +010077/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
78void ccm_star_lengths( int msg_len, int iv_len, int add_len, int tag_len,
79 int res )
80{
81 mbedtls_ccm_context ctx;
82 unsigned char key[16];
83 unsigned char msg[10];
84 unsigned char iv[14];
85 unsigned char add[10];
86 unsigned char out[10];
87 unsigned char tag[18];
88 int decrypt_ret;
89
90 mbedtls_ccm_init( &ctx );
91
92 memset( key, 0, sizeof( key ) );
93 memset( msg, 0, sizeof( msg ) );
94 memset( iv, 0, sizeof( iv ) );
95 memset( add, 0, sizeof( add ) );
96 memset( out, 0, sizeof( out ) );
97 memset( tag, 0, sizeof( tag ) );
98
99 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
100 key, 8 * sizeof( key ) ) == 0 );
101
102 TEST_ASSERT( mbedtls_ccm_star_encrypt_and_tag( &ctx, msg_len, iv, iv_len,
103 add, add_len, msg, out, tag, tag_len ) == res );
104
105 decrypt_ret = mbedtls_ccm_star_auth_decrypt( &ctx, msg_len, iv, iv_len, add,
106 add_len, msg, out, tag, tag_len );
107
108 if( res == 0 && tag_len != 0 )
109 TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
110 else
111 TEST_ASSERT( decrypt_ret == res );
112
113exit:
114 mbedtls_ccm_free( &ctx );
115}
116/* END_CASE */
117
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200118/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100119void mbedtls_ccm_encrypt_and_tag( int cipher_id, uint8_t * key,
120 uint32_t key_len, uint8_t * msg,
121 uint32_t msg_len, uint8_t * iv,
122 uint32_t iv_len, uint8_t * add,
123 uint32_t add_len, uint8_t * result,
124 uint32_t result_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ccm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100127 size_t tag_len;
Azim Khan46c9b1f2017-05-31 20:46:35 +0100128 uint8_t * msg_n_tag = (uint8_t *)malloc( result_len + 2 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200129
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200130 mbedtls_ccm_init( &ctx );
131
Azim Khan46c9b1f2017-05-31 20:46:35 +0100132 memset( msg_n_tag, 0, result_len + 2 );
133 memcpy( msg_n_tag, msg, msg_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200134
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200135 tag_len = result_len - msg_len;
136
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200137 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200138
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200139 /* Test with input == output */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
Azim Khan46c9b1f2017-05-31 20:46:35 +0100141 msg_n_tag, msg_n_tag, msg_n_tag + msg_len, tag_len ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200142
Azim Khan46c9b1f2017-05-31 20:46:35 +0100143 TEST_ASSERT( memcmp( msg_n_tag, result, result_len ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200144
145 /* Check we didn't write past the end */
Azim Khan46c9b1f2017-05-31 20:46:35 +0100146 TEST_ASSERT( msg_n_tag[result_len] == 0 && msg_n_tag[result_len + 1] == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200147
Paul Bakkerbd51b262014-07-10 15:26:12 +0200148exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_ccm_free( &ctx );
Azim Khan46c9b1f2017-05-31 20:46:35 +0100150 free( msg_n_tag );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200151}
152/* END_CASE */
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200153
154/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100155void mbedtls_ccm_auth_decrypt( int cipher_id, uint8_t * key, uint32_t key_len,
156 uint8_t * msg, uint32_t msg_len, uint8_t * iv,
157 uint32_t iv_len, uint8_t * add,
158 uint32_t add_len, int tag_len,
Azim Khan46c9b1f2017-05-31 20:46:35 +0100159 char * result, uint8_t * hex_msg,
160 uint32_t hex_msg_len )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200161{
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200162 unsigned char tag[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200164 int ret;
165
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200166 mbedtls_ccm_init( &ctx );
167
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200168 memset( tag, 0x00, sizeof( tag ) );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200169
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200170 msg_len -= tag_len;
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200171 memcpy( tag, msg + msg_len, tag_len );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200172
Azim Khan46c9b1f2017-05-31 20:46:35 +0100173 if( strcmp( "FAIL", result ) == 0 )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ret = MBEDTLS_ERR_CCM_AUTH_FAILED;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200176 }
177 else
178 {
179 ret = 0;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200180 }
181
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200182 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200183
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200184 /* Test with input == output */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200186 msg, msg, msg + msg_len, tag_len ) == ret );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200187
188 if( ret == 0 )
189 {
Azim Khan46c9b1f2017-05-31 20:46:35 +0100190 TEST_ASSERT( memcmp( msg, hex_msg, hex_msg_len ) == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200191 }
192 else
193 {
194 size_t i;
195
196 for( i = 0; i < msg_len; i++ )
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200197 TEST_ASSERT( msg[i] == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200198 }
199
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200200 /* Check we didn't write past the end (where the original tag is) */
201 TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200202
Paul Bakkerbd51b262014-07-10 15:26:12 +0200203exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200205}
206/* END_CASE */
Darryl Green0daf4ca2018-05-29 14:12:26 +0100207
208/* BEGIN_CASE */
209void mbedtls_ccm_star_encrypt_and_tag( int cipher_id,
210 char *key_hex, char *msg_hex,
211 char *source_address_hex, char *frame_counter_hex,
212 int sec_level, char *add_hex,
213 char *result_hex, int output_ret )
214{
215 unsigned char key[32];
216 unsigned char msg[50];
217 unsigned char iv[13];
218 unsigned char add[32];
219 unsigned char result[50];
220 unsigned char source_address[8];
221 unsigned char frame_counter[4];
222 mbedtls_ccm_context ctx;
223 size_t i, key_len, msg_len, iv_len, add_len, result_len, source_address_len, frame_counter_len, tag_len;
224 int ret;
225
226 mbedtls_ccm_init( &ctx );
227
228 memset( key, 0x00, sizeof( key ) );
229 memset( msg, 0x00, sizeof( msg ) );
230 memset( iv, 0x00, sizeof( iv ) );
231 memset( add, 0x00, sizeof( add ) );
232 memset( result, 0x00, sizeof( result ) );
233 memset( source_address, 0x00, sizeof( source_address ) );
234 memset( frame_counter, 0x00, sizeof( frame_counter ) );
235
236 key_len = unhexify( key, key_hex );
237 msg_len = unhexify( msg, msg_hex );
238 add_len = unhexify( add, add_hex );
239 result_len = unhexify( result, result_hex );
240 source_address_len = unhexify( source_address, source_address_hex );
241 frame_counter_len = unhexify( frame_counter, frame_counter_hex );
242
243 if( sec_level % 4 == 0)
244 tag_len = 0;
245 else
246 tag_len = 1 << ( sec_level % 4 + 1);
247
248 for( i = 0; i < source_address_len; i++ )
249 iv[i] = source_address[i];
250
251 for( i = 0; i < frame_counter_len; i++ )
252 iv[source_address_len + i] = frame_counter[i];
253
254 iv[source_address_len + frame_counter_len] = sec_level;
255 iv_len = sizeof( iv );
256
257 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
258
259 ret = mbedtls_ccm_star_encrypt_and_tag( &ctx, msg_len, iv, iv_len,
260 add, add_len, msg, msg, msg + msg_len, tag_len );
261
262 TEST_ASSERT( ret == output_ret );
263
264 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
265
266 /* Check we didn't write past the end */
267 TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 );
268
269exit:
270 mbedtls_ccm_free( &ctx );
271}
272/* END_CASE */
273
274/* BEGIN_CASE */
275void mbedtls_ccm_star_auth_decrypt( int cipher_id,
276 char *key_hex, char *msg_hex,
277 char *source_address_hex, char *frame_counter_hex,
278 int sec_level, char *add_hex,
279 char *result_hex, int output_ret )
280{
281 unsigned char key[32];
282 unsigned char msg[50];
283 unsigned char iv[13];
284 unsigned char add[32];
285 unsigned char tag[16];
286 unsigned char result[50];
287 unsigned char source_address[8];
288 unsigned char frame_counter[4];
289 mbedtls_ccm_context ctx;
290 size_t i, key_len, msg_len, iv_len, add_len, tag_len, result_len, source_address_len, frame_counter_len;
291 int ret;
292
293 mbedtls_ccm_init( &ctx );
294
295 memset( key, 0x00, sizeof( key ) );
296 memset( msg, 0x00, sizeof( msg ) );
297 memset( iv, 0x00, sizeof( iv ) );
298 memset( add, 0x00, sizeof( add ) );
299 memset( result, 0x00, sizeof( result ) );
300 memset( source_address, 0x00, sizeof( source_address ) );
301 memset( frame_counter, 0x00, sizeof( frame_counter ) );
302 memset( tag, 0x00, sizeof( tag ) );
303
304 key_len = unhexify( key, key_hex );
305 msg_len = unhexify( msg, msg_hex );
306 add_len = unhexify( add, add_hex );
307 result_len = unhexify( result, result_hex );
308 source_address_len = unhexify( source_address, source_address_hex );
309 frame_counter_len = unhexify( frame_counter, frame_counter_hex );
310
311 if( sec_level % 4 == 0)
312 tag_len = 0;
313 else
314 tag_len = 1 << ( sec_level % 4 + 1);
315
316 for( i = 0; i < source_address_len; i++ )
317 iv[i] = source_address[i];
318
319 for( i = 0; i < frame_counter_len; i++ )
320 iv[source_address_len + i] = frame_counter[i];
321
322 iv[source_address_len + frame_counter_len] = sec_level;
323 iv_len = sizeof( iv );
324
325 msg_len -= tag_len;
326 memcpy( tag, msg + msg_len, tag_len );
327
328 TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
329
330 ret = mbedtls_ccm_star_auth_decrypt( &ctx, msg_len, iv, iv_len,
331 add, add_len, msg, msg, msg + msg_len, tag_len );
332
333 TEST_ASSERT( ret == output_ret );
334
335 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
336
337 /* Check we didn't write past the end (where the original tag is) */
338 TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
339
340exit:
341 mbedtls_ccm_free( &ctx );
342}
343/* END_CASE */