test_suite_cmac.function 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/cipher.h"
  3. #include "mbedtls/cmac.h"
  4. /* END_HEADER */
  5. /* BEGIN_DEPENDENCIES
  6. * depends_on:MBEDTLS_CMAC_C
  7. * END_DEPENDENCIES
  8. */
  9. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  10. void mbedtls_cmac_self_test( )
  11. {
  12. TEST_ASSERT( mbedtls_cmac_self_test( 1 ) == 0 );
  13. }
  14. /* END_CASE */
  15. /* BEGIN_CASE */
  16. void mbedtls_cmac_null_args( )
  17. {
  18. mbedtls_cipher_context_t ctx;
  19. const mbedtls_cipher_info_t *cipher_info;
  20. unsigned char test_key[MBEDTLS_CIPHER_BLKSIZE_MAX];
  21. unsigned char test_data[MBEDTLS_CIPHER_BLKSIZE_MAX];
  22. unsigned char test_output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  23. mbedtls_cipher_init( &ctx );
  24. /* Test NULL cipher info */
  25. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, test_data, 16 ) ==
  26. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  27. cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
  28. TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
  29. TEST_ASSERT( mbedtls_cipher_cmac_starts( NULL, test_key, 128 ) ==
  30. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  31. TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx, NULL, 128 ) ==
  32. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  33. TEST_ASSERT( mbedtls_cipher_cmac_update( NULL, test_data, 16 ) ==
  34. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  35. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, NULL, 16 ) ==
  36. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  37. TEST_ASSERT( mbedtls_cipher_cmac_finish( NULL, test_output ) ==
  38. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  39. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, NULL ) ==
  40. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  41. TEST_ASSERT( mbedtls_cipher_cmac_reset( NULL ) ==
  42. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  43. TEST_ASSERT( mbedtls_cipher_cmac( NULL,
  44. test_key, 128,
  45. test_data, 16,
  46. test_output ) ==
  47. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  48. TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
  49. NULL, 128,
  50. test_data, 16,
  51. test_output ) ==
  52. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  53. TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
  54. test_key, 128,
  55. NULL, 16,
  56. test_output ) ==
  57. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  58. TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
  59. test_key, 128,
  60. test_data, 16,
  61. NULL ) ==
  62. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  63. TEST_ASSERT( mbedtls_aes_cmac_prf_128( NULL, 16,
  64. test_data, 16,
  65. test_output ) ==
  66. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  67. TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16,
  68. NULL, 16,
  69. test_output ) ==
  70. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  71. TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16,
  72. test_data, 16,
  73. NULL ) ==
  74. MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  75. exit:
  76. mbedtls_cipher_free( &ctx );
  77. }
  78. /* END_CASE */
  79. /* BEGIN_CASE */
  80. void mbedtls_cmac_setkey( int cipher_type, int key_size, int result )
  81. {
  82. const mbedtls_cipher_info_t *cipher_info;
  83. unsigned char key[32];
  84. unsigned char buf[16];
  85. unsigned char tmp[16];
  86. memset( key, 0x2A, sizeof( key ) );
  87. TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
  88. TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
  89. != NULL );
  90. memset( buf, 0x2A, sizeof( buf ) );
  91. TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
  92. buf, 16, tmp ) ) != 0 );
  93. }
  94. /* END_CASE */
  95. /* BEGIN_CASE */
  96. void mbedtls_cmac_multiple_blocks( int cipher_type, data_t * key,
  97. int keybits, int block_size,
  98. data_t * block1, int block1_len,
  99. data_t * block2, int block2_len,
  100. data_t * block3, int block3_len,
  101. data_t * block4, int block4_len,
  102. data_t * expected_result )
  103. {
  104. const mbedtls_cipher_info_t *cipher_info;
  105. mbedtls_cipher_context_t ctx;
  106. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  107. /* Convert the test parameters to binary data */
  108. mbedtls_cipher_init( &ctx );
  109. /* Validate the test inputs */
  110. TEST_ASSERT( block1_len <= 100 );
  111. TEST_ASSERT( block2_len <= 100 );
  112. TEST_ASSERT( block3_len <= 100 );
  113. TEST_ASSERT( block4_len <= 100 );
  114. /* Set up */
  115. TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
  116. != NULL );
  117. TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
  118. TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
  119. (const unsigned char*)key->x,
  120. keybits ) == 0 );
  121. /* Multiple partial and complete blocks. A negative length means skip the
  122. * update operation */
  123. if( block1_len >= 0)
  124. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  125. (unsigned char*)block1->x,
  126. block1_len ) == 0);
  127. if( block2_len >= 0 )
  128. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  129. (unsigned char*)block2->x,
  130. block2_len ) == 0);
  131. if( block3_len >= 0 )
  132. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  133. (unsigned char*)block3->x,
  134. block3_len ) == 0);
  135. if( block4_len >= 0 )
  136. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  137. (unsigned char*)block4->x,
  138. block4_len ) == 0);
  139. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
  140. TEST_ASSERT( memcmp( output, expected_result->x, block_size ) == 0 );
  141. exit:
  142. mbedtls_cipher_free( &ctx );
  143. }
  144. /* END_CASE */
  145. /* BEGIN_CASE */
  146. void mbedtls_cmac_multiple_operations_same_key( int cipher_type,
  147. data_t * key, int keybits,
  148. int block_size,
  149. data_t * block_a1,
  150. int block_a1_len,
  151. data_t * block_a2,
  152. int block_a2_len,
  153. data_t * block_a3,
  154. int block_a3_len,
  155. data_t * expected_result_a,
  156. data_t * block_b1,
  157. int block_b1_len,
  158. data_t * block_b2,
  159. int block_b2_len,
  160. data_t * block_b3,
  161. int block_b3_len,
  162. data_t * expected_result_b
  163. )
  164. {
  165. const mbedtls_cipher_info_t *cipher_info;
  166. mbedtls_cipher_context_t ctx;
  167. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  168. /* Convert the test parameters to binary data */
  169. mbedtls_cipher_init( &ctx );
  170. /* Validate the test inputs */
  171. TEST_ASSERT( block_a1_len <= 100 );
  172. TEST_ASSERT( block_a2_len <= 100 );
  173. TEST_ASSERT( block_a3_len <= 100 );
  174. TEST_ASSERT( block_b1_len <= 100 );
  175. TEST_ASSERT( block_b2_len <= 100 );
  176. TEST_ASSERT( block_b3_len <= 100 );
  177. /* Set up */
  178. TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
  179. != NULL );
  180. TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
  181. TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
  182. (const unsigned char*)key->x,
  183. keybits ) == 0 );
  184. /* Sequence A */
  185. /* Multiple partial and complete blocks. A negative length means skip the
  186. * update operation */
  187. if( block_a1_len >= 0 )
  188. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  189. (unsigned char*)block_a1->x,
  190. block_a1_len ) == 0);
  191. if( block_a2_len >= 0 )
  192. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  193. (unsigned char*)block_a2->x,
  194. block_a2_len ) == 0);
  195. if( block_a3_len >= 0 )
  196. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  197. (unsigned char*)block_a3->x,
  198. block_a3_len ) == 0);
  199. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
  200. TEST_ASSERT( memcmp( output, expected_result_a->x, block_size ) == 0 );
  201. TEST_ASSERT( mbedtls_cipher_cmac_reset( &ctx ) == 0 );
  202. /* Sequence B */
  203. /* Multiple partial and complete blocks. A negative length means skip the
  204. * update operation */
  205. if( block_b1_len >= 0)
  206. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  207. (unsigned char*)block_b1->x,
  208. block_b1_len ) == 0);
  209. if( block_b2_len >= 0 )
  210. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  211. (unsigned char*)block_b2->x,
  212. block_b2_len ) == 0);
  213. if( block_b3_len >= 0 )
  214. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  215. (unsigned char*)block_b3->x,
  216. block_b3_len ) == 0);
  217. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
  218. TEST_ASSERT( memcmp( output, expected_result_b->x, block_size ) == 0 );
  219. exit:
  220. mbedtls_cipher_free( &ctx );
  221. }
  222. /* END_CASE */