Simon Butcher | d812fa6 | 2016-10-05 14:13:31 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/cipher.h" |
| 3 | #include "mbedtls/cmac.h" |
| 4 | /* END_HEADER */ |
| 5 | |
| 6 | /* BEGIN_DEPENDENCIES |
| 7 | * depends_on:MBEDTLS_CMAC_C |
| 8 | * END_DEPENDENCIES |
| 9 | */ |
| 10 | |
| 11 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 12 | void mbedtls_cmac_self_test( ) |
Simon Butcher | d812fa6 | 2016-10-05 14:13:31 +0100 | [diff] [blame] | 13 | { |
| 14 | TEST_ASSERT( mbedtls_cmac_self_test( 1 ) == 0 ); |
| 15 | } |
| 16 | /* END_CASE */ |
| 17 | |
| 18 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 19 | void mbedtls_cmac_null_args( ) |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 20 | { |
| 21 | mbedtls_cipher_context_t ctx; |
| 22 | const mbedtls_cipher_info_t *cipher_info; |
| 23 | unsigned char test_key[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 24 | unsigned char test_data[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 25 | unsigned char test_output[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 26 | |
| 27 | mbedtls_cipher_init( &ctx ); |
| 28 | |
| 29 | /* Test NULL cipher info */ |
| 30 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, test_data, 16 ) == |
| 31 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 32 | |
| 33 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB ); |
| 34 | TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 ); |
| 35 | |
| 36 | TEST_ASSERT( mbedtls_cipher_cmac_starts( NULL, test_key, 128 ) == |
| 37 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 38 | |
| 39 | TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx, NULL, 128 ) == |
| 40 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 41 | |
| 42 | TEST_ASSERT( mbedtls_cipher_cmac_update( NULL, test_data, 16 ) == |
| 43 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 44 | |
| 45 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, NULL, 16 ) == |
| 46 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 47 | |
| 48 | TEST_ASSERT( mbedtls_cipher_cmac_finish( NULL, test_output ) == |
| 49 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 50 | |
| 51 | TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, NULL ) == |
| 52 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 53 | |
| 54 | TEST_ASSERT( mbedtls_cipher_cmac_reset( NULL ) == |
| 55 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 56 | |
| 57 | TEST_ASSERT( mbedtls_cipher_cmac( NULL, |
| 58 | test_key, 128, |
| 59 | test_data, 16, |
| 60 | test_output ) == |
| 61 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 62 | |
| 63 | TEST_ASSERT( mbedtls_cipher_cmac( cipher_info, |
| 64 | NULL, 128, |
| 65 | test_data, 16, |
| 66 | test_output ) == |
| 67 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 68 | |
| 69 | TEST_ASSERT( mbedtls_cipher_cmac( cipher_info, |
| 70 | test_key, 128, |
| 71 | NULL, 16, |
| 72 | test_output ) == |
| 73 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 74 | |
| 75 | TEST_ASSERT( mbedtls_cipher_cmac( cipher_info, |
| 76 | test_key, 128, |
| 77 | test_data, 16, |
| 78 | NULL ) == |
| 79 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 80 | |
| 81 | TEST_ASSERT( mbedtls_aes_cmac_prf_128( NULL, 16, |
Simon Butcher | bd8d221 | 2016-10-11 12:05:51 +0100 | [diff] [blame] | 82 | test_data, 16, |
| 83 | test_output ) == |
| 84 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 85 | |
| 86 | TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16, |
| 87 | NULL, 16, |
| 88 | test_output ) == |
| 89 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 90 | |
| 91 | TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16, |
| 92 | test_data, 16, |
| 93 | NULL ) == |
| 94 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 95 | |
Andres AG | 8abc6b8 | 2016-10-11 15:41:40 +0100 | [diff] [blame] | 96 | exit: |
| 97 | mbedtls_cipher_free( &ctx ); |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 98 | } |
| 99 | /* END_CASE */ |
| 100 | |
| 101 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 102 | void mbedtls_cmac_setkey( int cipher_type, int key_size, int result ) |
Simon Butcher | d812fa6 | 2016-10-05 14:13:31 +0100 | [diff] [blame] | 103 | { |
| 104 | const mbedtls_cipher_info_t *cipher_info; |
| 105 | unsigned char key[32]; |
| 106 | unsigned char buf[16]; |
| 107 | unsigned char tmp[16]; |
| 108 | |
| 109 | memset( key, 0x2A, sizeof( key ) ); |
| 110 | TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) ); |
| 111 | |
| 112 | TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) ) |
| 113 | != NULL ); |
| 114 | |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 115 | memset( buf, 0x2A, sizeof( buf ) ); |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 116 | TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size, |
| 117 | buf, 16, tmp ) ) != 0 ); |
| 118 | } |
| 119 | /* END_CASE */ |
| 120 | |
| 121 | /* BEGIN_CASE */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 122 | void mbedtls_cmac_multiple_blocks( int cipher_type, HexParam_t * key, |
| 123 | int keybits, int block_size, |
| 124 | HexParam_t * block1, int block1_len, |
| 125 | HexParam_t * block2, int block2_len, |
| 126 | HexParam_t * block3, int block3_len, |
| 127 | HexParam_t * block4, int block4_len, |
| 128 | HexParam_t * expected_result ) |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 129 | { |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 130 | const mbedtls_cipher_info_t *cipher_info; |
| 131 | mbedtls_cipher_context_t ctx; |
| 132 | unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 133 | |
| 134 | /* Convert the test parameters to binary data */ |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 135 | |
Andres AG | 8abc6b8 | 2016-10-11 15:41:40 +0100 | [diff] [blame] | 136 | mbedtls_cipher_init( &ctx ); |
| 137 | |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 138 | /* Validate the test inputs */ |
| 139 | TEST_ASSERT( block1_len <= 100 ); |
| 140 | TEST_ASSERT( block2_len <= 100 ); |
| 141 | TEST_ASSERT( block3_len <= 100 ); |
| 142 | TEST_ASSERT( block4_len <= 100 ); |
| 143 | |
| 144 | /* Set up */ |
| 145 | TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) ) |
| 146 | != NULL ); |
| 147 | |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 148 | TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 ); |
| 149 | |
| 150 | TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 151 | (const unsigned char*)key->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 152 | keybits ) == 0 ); |
| 153 | |
| 154 | /* Multiple partial and complete blocks. A negative length means skip the |
| 155 | * update operation */ |
| 156 | if( block1_len >= 0) |
| 157 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 158 | (unsigned char*)block1->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 159 | block1_len ) == 0); |
| 160 | |
| 161 | if( block2_len >= 0 ) |
| 162 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 163 | (unsigned char*)block2->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 164 | block2_len ) == 0); |
| 165 | |
| 166 | if( block3_len >= 0 ) |
| 167 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 168 | (unsigned char*)block3->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 169 | block3_len ) == 0); |
| 170 | |
| 171 | if( block4_len >= 0 ) |
| 172 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 173 | (unsigned char*)block4->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 174 | block4_len ) == 0); |
| 175 | |
| 176 | TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 ); |
| 177 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 178 | TEST_ASSERT( memcmp( output, expected_result->x, block_size ) == 0 ); |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 179 | |
Simon Butcher | bd8d221 | 2016-10-11 12:05:51 +0100 | [diff] [blame] | 180 | exit: |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 181 | mbedtls_cipher_free( &ctx ); |
| 182 | } |
| 183 | /* END_CASE */ |
| 184 | |
| 185 | /* BEGIN_CASE */ |
| 186 | void mbedtls_cmac_multiple_operations_same_key( int cipher_type, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 187 | HexParam_t * key, int keybits, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 188 | int block_size, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 189 | HexParam_t * block_a1, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 190 | int block_a1_len, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 191 | HexParam_t * block_a2, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 192 | int block_a2_len, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 193 | HexParam_t * block_a3, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 194 | int block_a3_len, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 195 | HexParam_t * expected_result_a, |
| 196 | HexParam_t * block_b1, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 197 | int block_b1_len, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 198 | HexParam_t * block_b2, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 199 | int block_b2_len, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 200 | HexParam_t * block_b3, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 201 | int block_b3_len, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 202 | HexParam_t * expected_result_b |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 203 | ) |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 204 | { |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 205 | const mbedtls_cipher_info_t *cipher_info; |
| 206 | mbedtls_cipher_context_t ctx; |
| 207 | unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 208 | |
| 209 | /* Convert the test parameters to binary data */ |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 210 | |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 211 | |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 212 | |
Andres AG | 8abc6b8 | 2016-10-11 15:41:40 +0100 | [diff] [blame] | 213 | mbedtls_cipher_init( &ctx ); |
| 214 | |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 215 | /* Validate the test inputs */ |
| 216 | TEST_ASSERT( block_a1_len <= 100 ); |
| 217 | TEST_ASSERT( block_a2_len <= 100 ); |
| 218 | TEST_ASSERT( block_a3_len <= 100 ); |
| 219 | |
| 220 | TEST_ASSERT( block_b1_len <= 100 ); |
| 221 | TEST_ASSERT( block_b2_len <= 100 ); |
| 222 | TEST_ASSERT( block_b3_len <= 100 ); |
| 223 | |
| 224 | /* Set up */ |
| 225 | TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) ) |
| 226 | != NULL ); |
| 227 | |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 228 | TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 ); |
| 229 | |
| 230 | TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 231 | (const unsigned char*)key->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 232 | keybits ) == 0 ); |
| 233 | |
| 234 | /* Sequence A */ |
| 235 | |
| 236 | /* Multiple partial and complete blocks. A negative length means skip the |
| 237 | * update operation */ |
Simon Butcher | bd8d221 | 2016-10-11 12:05:51 +0100 | [diff] [blame] | 238 | if( block_a1_len >= 0 ) |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 239 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 240 | (unsigned char*)block_a1->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 241 | block_a1_len ) == 0); |
| 242 | |
| 243 | if( block_a2_len >= 0 ) |
| 244 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 245 | (unsigned char*)block_a2->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 246 | block_a2_len ) == 0); |
| 247 | |
| 248 | if( block_a3_len >= 0 ) |
| 249 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 250 | (unsigned char*)block_a3->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 251 | block_a3_len ) == 0); |
| 252 | |
| 253 | TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 ); |
| 254 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 255 | TEST_ASSERT( memcmp( output, expected_result_a->x, block_size ) == 0 ); |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 256 | |
| 257 | TEST_ASSERT( mbedtls_cipher_cmac_reset( &ctx ) == 0 ); |
| 258 | |
| 259 | /* Sequence B */ |
| 260 | |
| 261 | /* Multiple partial and complete blocks. A negative length means skip the |
| 262 | * update operation */ |
| 263 | if( block_b1_len >= 0) |
| 264 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 265 | (unsigned char*)block_b1->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 266 | block_b1_len ) == 0); |
| 267 | |
| 268 | if( block_b2_len >= 0 ) |
| 269 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 270 | (unsigned char*)block_b2->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 271 | block_b2_len ) == 0); |
| 272 | |
| 273 | if( block_b3_len >= 0 ) |
| 274 | TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 275 | (unsigned char*)block_b3->x, |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 276 | block_b3_len ) == 0); |
| 277 | |
| 278 | TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 ); |
| 279 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame^] | 280 | TEST_ASSERT( memcmp( output, expected_result_b->x, block_size ) == 0 ); |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 281 | |
Simon Butcher | bd8d221 | 2016-10-11 12:05:51 +0100 | [diff] [blame] | 282 | exit: |
Simon Butcher | 33183fd | 2016-10-10 21:41:03 +0100 | [diff] [blame] | 283 | mbedtls_cipher_free( &ctx ); |
Simon Butcher | d812fa6 | 2016-10-05 14:13:31 +0100 | [diff] [blame] | 284 | } |
| 285 | /* END_CASE */ |
| 286 | |