mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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