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" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
| 4 | |
| 5 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6 | * depends_on:MBEDTLS_CCM_C |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
| 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 11 | void mbedtls_ccm_self_test( ) |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 12 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 13 | TEST_ASSERT( mbedtls_ccm_self_test( 1 ) == 0 ); |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 14 | } |
| 15 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 16 | |
| 17 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 18 | 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] | 19 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | mbedtls_ccm_context ctx; |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 21 | unsigned char key[32]; |
| 22 | int ret; |
| 23 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 24 | mbedtls_ccm_init( &ctx ); |
| 25 | |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 26 | memset( key, 0x2A, sizeof( key ) ); |
| 27 | TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) ); |
| 28 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 29 | ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 30 | TEST_ASSERT( ret == result ); |
| 31 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 32 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 34 | } |
| 35 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 38 | void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res ) |
| 39 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_ccm_context ctx; |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 41 | 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é-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 49 | mbedtls_ccm_init( &ctx ); |
| 50 | |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 51 | 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é-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 58 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 59 | key, 8 * sizeof( key ) ) == 0 ); |
| 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | 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] | 62 | msg, out, tag, tag_len ) == res ); |
| 63 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | 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] | 65 | msg, out, tag, tag_len ); |
| 66 | |
| 67 | if( res == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED ); |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 69 | else |
| 70 | TEST_ASSERT( decrypt_ret == res ); |
| 71 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 72 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 74 | } |
| 75 | /* END_CASE */ |
| 76 | |
Janos Follath | 95ab93d | 2018-05-14 14:32:41 +0100 | [diff] [blame] | 77 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
| 78 | void 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 | |
| 113 | exit: |
| 114 | mbedtls_ccm_free( &ctx ); |
| 115 | } |
| 116 | /* END_CASE */ |
| 117 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 118 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 119 | void mbedtls_ccm_encrypt_and_tag( int cipher_id, data_t * key, |
| 120 | data_t * msg, data_t * iv, |
| 121 | data_t * add, data_t * result ) |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 122 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | mbedtls_ccm_context ctx; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 124 | size_t tag_len; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 125 | uint8_t * msg_n_tag = (uint8_t *)malloc( result->len + 2 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 126 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 127 | mbedtls_ccm_init( &ctx ); |
| 128 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 129 | memset( msg_n_tag, 0, result->len + 2 ); |
| 130 | memcpy( msg_n_tag, msg->x, msg->len ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 131 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 132 | tag_len = result->len - msg->len; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 133 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 134 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 135 | |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 136 | /* Test with input == output */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 137 | TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg->len, iv->x, iv->len, add->x, add->len, |
| 138 | msg_n_tag, msg_n_tag, msg_n_tag + msg->len, tag_len ) == 0 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 139 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 140 | TEST_ASSERT( memcmp( msg_n_tag, result->x, result->len ) == 0 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 141 | |
| 142 | /* Check we didn't write past the end */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 143 | TEST_ASSERT( msg_n_tag[result->len] == 0 && msg_n_tag[result->len + 1] == 0 ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 144 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 145 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | mbedtls_ccm_free( &ctx ); |
Azim Khan | 46c9b1f | 2017-05-31 20:46:35 +0100 | [diff] [blame] | 147 | free( msg_n_tag ); |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 148 | } |
| 149 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 150 | |
| 151 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 152 | void mbedtls_ccm_auth_decrypt( int cipher_id, data_t * key, |
| 153 | data_t * msg, data_t * iv, |
| 154 | data_t * add, int tag_len, int result, |
| 155 | data_t * hex_msg ) |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 156 | { |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 157 | unsigned char tag[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_ccm_context ctx; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 159 | |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 160 | mbedtls_ccm_init( &ctx ); |
| 161 | |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 162 | memset( tag, 0x00, sizeof( tag ) ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 163 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 164 | msg->len -= tag_len; |
| 165 | memcpy( tag, msg->x + msg->len, tag_len ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 166 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 167 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 168 | |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 169 | /* Test with input == output */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 170 | TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg->len, iv->x, iv->len, add->x, add->len, |
Mohammad Azim Khan | cfd8342 | 2018-06-26 18:15:18 +0100 | [diff] [blame] | 171 | msg->x, msg->x, msg->x + msg->len, tag_len ) == result ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 172 | |
Mohammad Azim Khan | cfd8342 | 2018-06-26 18:15:18 +0100 | [diff] [blame] | 173 | if( result == 0 ) |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 174 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 175 | TEST_ASSERT( memcmp( msg->x, hex_msg->x, hex_msg->len ) == 0 ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
| 179 | size_t i; |
| 180 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 181 | for( i = 0; i < msg->len; i++ ) |
| 182 | TEST_ASSERT( msg->x[i] == 0 ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Manuel Pégourié-Gonnard | 0f6b66d | 2014-05-07 14:43:46 +0200 | [diff] [blame] | 185 | /* Check we didn't write past the end (where the original tag is) */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 186 | TEST_ASSERT( memcmp( msg->x + msg->len, tag, tag_len ) == 0 ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 187 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 188 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | mbedtls_ccm_free( &ctx ); |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 190 | } |
| 191 | /* END_CASE */ |
Darryl Green | 0daf4ca | 2018-05-29 14:12:26 +0100 | [diff] [blame] | 192 | |
| 193 | /* BEGIN_CASE */ |
| 194 | void mbedtls_ccm_star_encrypt_and_tag( int cipher_id, |
| 195 | char *key_hex, char *msg_hex, |
| 196 | char *source_address_hex, char *frame_counter_hex, |
| 197 | int sec_level, char *add_hex, |
| 198 | char *result_hex, int output_ret ) |
| 199 | { |
| 200 | unsigned char key[32]; |
| 201 | unsigned char msg[50]; |
| 202 | unsigned char iv[13]; |
| 203 | unsigned char add[32]; |
| 204 | unsigned char result[50]; |
| 205 | unsigned char source_address[8]; |
| 206 | unsigned char frame_counter[4]; |
| 207 | mbedtls_ccm_context ctx; |
| 208 | size_t i, key_len, msg_len, iv_len, add_len, result_len, source_address_len, frame_counter_len, tag_len; |
| 209 | int ret; |
| 210 | |
| 211 | mbedtls_ccm_init( &ctx ); |
| 212 | |
| 213 | memset( key, 0x00, sizeof( key ) ); |
| 214 | memset( msg, 0x00, sizeof( msg ) ); |
| 215 | memset( iv, 0x00, sizeof( iv ) ); |
| 216 | memset( add, 0x00, sizeof( add ) ); |
| 217 | memset( result, 0x00, sizeof( result ) ); |
| 218 | memset( source_address, 0x00, sizeof( source_address ) ); |
| 219 | memset( frame_counter, 0x00, sizeof( frame_counter ) ); |
| 220 | |
| 221 | key_len = unhexify( key, key_hex ); |
| 222 | msg_len = unhexify( msg, msg_hex ); |
| 223 | add_len = unhexify( add, add_hex ); |
| 224 | result_len = unhexify( result, result_hex ); |
| 225 | source_address_len = unhexify( source_address, source_address_hex ); |
| 226 | frame_counter_len = unhexify( frame_counter, frame_counter_hex ); |
| 227 | |
| 228 | if( sec_level % 4 == 0) |
| 229 | tag_len = 0; |
| 230 | else |
| 231 | tag_len = 1 << ( sec_level % 4 + 1); |
| 232 | |
| 233 | for( i = 0; i < source_address_len; i++ ) |
| 234 | iv[i] = source_address[i]; |
| 235 | |
| 236 | for( i = 0; i < frame_counter_len; i++ ) |
| 237 | iv[source_address_len + i] = frame_counter[i]; |
| 238 | |
| 239 | iv[source_address_len + frame_counter_len] = sec_level; |
| 240 | iv_len = sizeof( iv ); |
| 241 | |
| 242 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 ); |
| 243 | |
| 244 | ret = mbedtls_ccm_star_encrypt_and_tag( &ctx, msg_len, iv, iv_len, |
| 245 | add, add_len, msg, msg, msg + msg_len, tag_len ); |
| 246 | |
| 247 | TEST_ASSERT( ret == output_ret ); |
| 248 | |
| 249 | TEST_ASSERT( memcmp( msg, result, result_len ) == 0 ); |
| 250 | |
| 251 | /* Check we didn't write past the end */ |
| 252 | TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 ); |
| 253 | |
| 254 | exit: |
| 255 | mbedtls_ccm_free( &ctx ); |
| 256 | } |
| 257 | /* END_CASE */ |
| 258 | |
| 259 | /* BEGIN_CASE */ |
| 260 | void mbedtls_ccm_star_auth_decrypt( int cipher_id, |
| 261 | char *key_hex, char *msg_hex, |
| 262 | char *source_address_hex, char *frame_counter_hex, |
| 263 | int sec_level, char *add_hex, |
| 264 | char *result_hex, int output_ret ) |
| 265 | { |
| 266 | unsigned char key[32]; |
| 267 | unsigned char msg[50]; |
| 268 | unsigned char iv[13]; |
| 269 | unsigned char add[32]; |
| 270 | unsigned char tag[16]; |
| 271 | unsigned char result[50]; |
| 272 | unsigned char source_address[8]; |
| 273 | unsigned char frame_counter[4]; |
| 274 | mbedtls_ccm_context ctx; |
| 275 | size_t i, key_len, msg_len, iv_len, add_len, tag_len, result_len, source_address_len, frame_counter_len; |
| 276 | int ret; |
| 277 | |
| 278 | mbedtls_ccm_init( &ctx ); |
| 279 | |
| 280 | memset( key, 0x00, sizeof( key ) ); |
| 281 | memset( msg, 0x00, sizeof( msg ) ); |
| 282 | memset( iv, 0x00, sizeof( iv ) ); |
| 283 | memset( add, 0x00, sizeof( add ) ); |
| 284 | memset( result, 0x00, sizeof( result ) ); |
| 285 | memset( source_address, 0x00, sizeof( source_address ) ); |
| 286 | memset( frame_counter, 0x00, sizeof( frame_counter ) ); |
| 287 | memset( tag, 0x00, sizeof( tag ) ); |
| 288 | |
| 289 | key_len = unhexify( key, key_hex ); |
| 290 | msg_len = unhexify( msg, msg_hex ); |
| 291 | add_len = unhexify( add, add_hex ); |
| 292 | result_len = unhexify( result, result_hex ); |
| 293 | source_address_len = unhexify( source_address, source_address_hex ); |
| 294 | frame_counter_len = unhexify( frame_counter, frame_counter_hex ); |
| 295 | |
| 296 | if( sec_level % 4 == 0) |
| 297 | tag_len = 0; |
| 298 | else |
| 299 | tag_len = 1 << ( sec_level % 4 + 1); |
| 300 | |
| 301 | for( i = 0; i < source_address_len; i++ ) |
| 302 | iv[i] = source_address[i]; |
| 303 | |
| 304 | for( i = 0; i < frame_counter_len; i++ ) |
| 305 | iv[source_address_len + i] = frame_counter[i]; |
| 306 | |
| 307 | iv[source_address_len + frame_counter_len] = sec_level; |
| 308 | iv_len = sizeof( iv ); |
| 309 | |
| 310 | msg_len -= tag_len; |
| 311 | memcpy( tag, msg + msg_len, tag_len ); |
| 312 | |
| 313 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 ); |
| 314 | |
| 315 | ret = mbedtls_ccm_star_auth_decrypt( &ctx, msg_len, iv, iv_len, |
| 316 | add, add_len, msg, msg, msg + msg_len, tag_len ); |
| 317 | |
| 318 | TEST_ASSERT( ret == output_ret ); |
| 319 | |
| 320 | TEST_ASSERT( memcmp( msg, result, result_len ) == 0 ); |
| 321 | |
| 322 | /* Check we didn't write past the end (where the original tag is) */ |
| 323 | TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 ); |
| 324 | |
| 325 | exit: |
| 326 | mbedtls_ccm_free( &ctx ); |
| 327 | } |
| 328 | /* END_CASE */ |