Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/ccm.h" |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 3 | |
| 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. */ |
| 7 | static 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 Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 51 | if( tag->len == 0 ) |
| 52 | ASSERT_ALLOC( output, 16 ); |
| 53 | else |
| 54 | ASSERT_ALLOC( output, tag->len ); |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 55 | 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; |
| 61 | exit: |
| 62 | mbedtls_free( output ); |
| 63 | return( ok ); |
| 64 | } |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 65 | /* END_HEADER */ |
| 66 | |
| 67 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | * depends_on:MBEDTLS_CCM_C |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 69 | * END_DEPENDENCIES |
| 70 | */ |
| 71 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 73 | void mbedtls_ccm_self_test( ) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 74 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 75 | TEST_ASSERT( mbedtls_ccm_self_test( 1 ) == 0 ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 76 | } |
| 77 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 78 | |
| 79 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 80 | void mbedtls_ccm_setkey( int cipher_id, int key_size, int result ) |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 81 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | mbedtls_ccm_context ctx; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 83 | unsigned char key[32]; |
| 84 | int ret; |
| 85 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 86 | mbedtls_ccm_init( &ctx ); |
| 87 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 88 | memset( key, 0x2A, sizeof( key ) ); |
| 89 | TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) ); |
| 90 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 91 | ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 92 | TEST_ASSERT( ret == result ); |
| 93 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 94 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 96 | } |
| 97 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 100 | void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res ) |
| 101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | mbedtls_ccm_context ctx; |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 103 | unsigned char key[16]; |
| 104 | unsigned char msg[10]; |
| 105 | unsigned char iv[14]; |
Dave Rodgman | 2e68034 | 2020-10-15 14:00:40 +0100 | [diff] [blame] | 106 | unsigned char *add = NULL; |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 107 | unsigned char out[10]; |
| 108 | unsigned char tag[18]; |
| 109 | int decrypt_ret; |
| 110 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 111 | mbedtls_ccm_init( &ctx ); |
| 112 | |
Dave Rodgman | 2e68034 | 2020-10-15 14:00:40 +0100 | [diff] [blame] | 113 | ASSERT_ALLOC_WEAK( add, add_len ); |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 114 | memset( key, 0, sizeof( key ) ); |
| 115 | memset( msg, 0, sizeof( msg ) ); |
| 116 | memset( iv, 0, sizeof( iv ) ); |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 117 | memset( out, 0, sizeof( out ) ); |
| 118 | memset( tag, 0, sizeof( tag ) ); |
| 119 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 120 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 121 | key, 8 * sizeof( key ) ) == 0 ); |
| 122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len, |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 124 | msg, out, tag, tag_len ) == res ); |
| 125 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | decrypt_ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len, |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 127 | msg, out, tag, tag_len ); |
| 128 | |
| 129 | if( res == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED ); |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 131 | else |
| 132 | TEST_ASSERT( decrypt_ret == res ); |
| 133 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 134 | exit: |
Dave Rodgman | 2e68034 | 2020-10-15 14:00:40 +0100 | [diff] [blame] | 135 | mbedtls_free( add ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 137 | } |
| 138 | /* END_CASE */ |
| 139 | |
Janos Follath | 95ab93d | 2018-05-14 14:32:41 +0100 | [diff] [blame] | 140 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
| 141 | void 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 | |
| 176 | exit: |
| 177 | mbedtls_ccm_free( &ctx ); |
| 178 | } |
| 179 | /* END_CASE */ |
| 180 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 181 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 182 | void 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é-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | mbedtls_ccm_context ctx; |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 187 | size_t n1, n1_add; |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 188 | 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é-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 201 | mbedtls_ccm_init( &ctx ); |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 202 | TEST_EQUAL( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ), 0 ); |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 203 | /* Test with input == output */ |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 204 | 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é-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 206 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 207 | 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é-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 209 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 210 | /* Prepare data_t structers for multipart testing */ |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 211 | const data_t encrypted_expected = { .x = result->x, |
| 212 | .len = msg->len }; |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 213 | const data_t tag_expected = { .x = (uint8_t*) expected_tag, /* cast to conform with data_t x type */ |
| 214 | .len = expected_tag_len }; |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 215 | |
| 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 Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 230 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | mbedtls_ccm_free( &ctx ); |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 232 | mbedtls_free( io_msg_buf ); |
| 233 | mbedtls_free( tag_buf ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 234 | } |
| 235 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 236 | |
| 237 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 238 | void mbedtls_ccm_auth_decrypt( int cipher_id, data_t * key, |
| 239 | data_t * msg, data_t * iv, |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 240 | data_t * add, int expected_tag_len, int result, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 241 | data_t * expected_msg ) |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 242 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | mbedtls_ccm_context ctx; |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 244 | size_t n1, n1_add; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 245 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 246 | 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é-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 255 | mbedtls_ccm_init( &ctx ); |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 256 | TEST_EQUAL( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ), 0 ); |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 257 | /* Test with input == output */ |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 258 | 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é-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 260 | |
Mohammad Azim Khan | cfd8342 | 2018-06-26 18:15:18 +0100 | [diff] [blame] | 261 | if( result == 0 ) |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 262 | { |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 263 | ASSERT_COMPARE( io_msg_buf, expected_msg_len, expected_msg->x, expected_msg_len ); |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 264 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 265 | /* Prepare data_t structers for multipart testing */ |
| 266 | const data_t encrypted = { .x = msg->x, |
| 267 | .len = expected_msg_len }; |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 268 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 269 | 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 Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 273 | { |
| 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 Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 278 | iv, add, &encrypted, |
Mateusz Starzyk | 25a3dfe | 2021-07-12 14:53:45 +0200 | [diff] [blame] | 279 | expected_msg, |
| 280 | &tag_expected, |
| 281 | n1, n1_add ) ) |
| 282 | goto exit; |
| 283 | } |
| 284 | } |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 285 | } |
| 286 | else |
| 287 | { |
| 288 | size_t i; |
| 289 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 290 | for( i = 0; i < expected_msg_len; i++ ) |
| 291 | TEST_EQUAL( io_msg_buf[i], 0 ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 294 | exit: |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 295 | mbedtls_free(io_msg_buf); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 297 | } |
| 298 | /* END_CASE */ |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 299 | |
| 300 | /* BEGIN_CASE */ |
| 301 | void mbedtls_ccm_star_encrypt_and_tag( int cipher_id, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 302 | 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 Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 306 | { |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 307 | unsigned char iv[13]; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 308 | mbedtls_ccm_context ctx; |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 309 | size_t iv_len, expected_tag_len; |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 310 | size_t n1, n1_add; |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 311 | uint8_t* io_msg_buf = NULL; |
| 312 | uint8_t* tag_buf = NULL; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 313 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 314 | const uint8_t* expected_tag = expected_result->x + msg->len; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 315 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 316 | /* Calculate tag length */ |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 317 | if( sec_level % 4 == 0) |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 318 | expected_tag_len = 0; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 319 | else |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 320 | expected_tag_len = 1 << ( sec_level % 4 + 1); |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 321 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 322 | /* 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 Peskine | b168c0d | 2021-02-09 12:00:13 +0100 | [diff] [blame] | 334 | 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 Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 338 | iv[source_address->len + frame_counter->len] = sec_level; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 339 | iv_len = sizeof( iv ); |
| 340 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 341 | 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 Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 348 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 349 | 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 Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 351 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 352 | if( output_ret == 0 ) |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 353 | { |
| 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 Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 359 | const data_t tag_expected = { .x = (uint8_t*)expected_tag, |
| 360 | .len = expected_tag_len }; |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 361 | |
| 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 Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 377 | exit: |
| 378 | mbedtls_ccm_free( &ctx ); |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 379 | mbedtls_free( io_msg_buf ); |
| 380 | mbedtls_free( tag_buf ); |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 381 | } |
| 382 | /* END_CASE */ |
| 383 | |
| 384 | /* BEGIN_CASE */ |
| 385 | void mbedtls_ccm_star_auth_decrypt( int cipher_id, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 386 | 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 Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 390 | { |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 391 | unsigned char iv[13]; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 392 | mbedtls_ccm_context ctx; |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 393 | size_t iv_len, expected_tag_len; |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 394 | size_t n1, n1_add; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 395 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 396 | /* Calculate tag length */ |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 397 | if( sec_level % 4 == 0) |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 398 | expected_tag_len = 0; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 399 | else |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 400 | expected_tag_len = 1 << ( sec_level % 4 + 1); |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 401 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 402 | 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 Peskine | b168c0d | 2021-02-09 12:00:13 +0100 | [diff] [blame] | 413 | 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 Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 417 | iv[source_address->len + frame_counter->len] = sec_level; |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 418 | iv_len = sizeof( iv ); |
| 419 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 420 | mbedtls_ccm_init( &ctx ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 421 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 ); |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 422 | /* 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 Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 426 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 427 | ASSERT_COMPARE( io_msg_buf, expected_msg_len, expected_result->x, expected_msg_len ); |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 428 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 429 | if( output_ret == 0 ) |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 430 | { |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 431 | const data_t iv_data = { .x = iv, |
| 432 | .len = iv_len }; |
| 433 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 434 | const data_t encrypted = { .x = msg->x, |
| 435 | .len = expected_msg_len} ; |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 436 | |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 437 | 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 Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 441 | { |
| 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 Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 446 | &iv_data, add, &encrypted, |
Mateusz Starzyk | 29ec75b | 2021-07-13 12:26:17 +0200 | [diff] [blame] | 447 | expected_result, |
| 448 | &tag_expected, |
| 449 | n1, n1_add ) ) |
| 450 | goto exit; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 455 | exit: |
| 456 | mbedtls_ccm_free( &ctx ); |
Mateusz Starzyk | 27a1bef | 2021-07-13 15:33:19 +0200 | [diff] [blame] | 457 | mbedtls_free( io_msg_buf ); |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 458 | } |
| 459 | /* END_CASE */ |