test_suite_cmac.function 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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,
  81. int result )
  82. {
  83. const mbedtls_cipher_info_t *cipher_info;
  84. unsigned char key[32];
  85. unsigned char buf[16];
  86. unsigned char tmp[16];
  87. memset( key, 0x2A, sizeof( key ) );
  88. TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
  89. TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
  90. != NULL );
  91. memset( buf, 0x2A, sizeof( buf ) );
  92. TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
  93. buf, 16, tmp ) ) != 0 );
  94. }
  95. /* END_CASE */
  96. /* BEGIN_CASE */
  97. void mbedtls_cmac_multiple_blocks( int cipher_type,
  98. char *key_string, int keybits,
  99. int block_size,
  100. char *block1_string, int block1_len,
  101. char *block2_string, int block2_len,
  102. char *block3_string, int block3_len,
  103. char *block4_string, int block4_len,
  104. char *expected_result_string )
  105. {
  106. unsigned char key[100];
  107. unsigned char block1[100];
  108. unsigned char block2[100];
  109. unsigned char block3[100];
  110. unsigned char block4[100];
  111. unsigned char expected_result[100];
  112. const mbedtls_cipher_info_t *cipher_info;
  113. mbedtls_cipher_context_t ctx;
  114. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  115. /* Convert the test parameters to binary data */
  116. unhexify( key, key_string );
  117. unhexify( block1, block1_string );
  118. unhexify( block2, block2_string );
  119. unhexify( block3, block3_string );
  120. unhexify( block4, block4_string );
  121. unhexify( expected_result, expected_result_string );
  122. mbedtls_cipher_init( &ctx );
  123. /* Validate the test inputs */
  124. TEST_ASSERT( block1_len <= 100 );
  125. TEST_ASSERT( block2_len <= 100 );
  126. TEST_ASSERT( block3_len <= 100 );
  127. TEST_ASSERT( block4_len <= 100 );
  128. /* Set up */
  129. TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
  130. != NULL );
  131. TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
  132. TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
  133. (const unsigned char*)key,
  134. keybits ) == 0 );
  135. /* Multiple partial and complete blocks. A negative length means skip the
  136. * update operation */
  137. if( block1_len >= 0)
  138. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  139. (unsigned char*)block1,
  140. block1_len ) == 0);
  141. if( block2_len >= 0 )
  142. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  143. (unsigned char*)block2,
  144. block2_len ) == 0);
  145. if( block3_len >= 0 )
  146. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  147. (unsigned char*)block3,
  148. block3_len ) == 0);
  149. if( block4_len >= 0 )
  150. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  151. (unsigned char*)block4,
  152. block4_len ) == 0);
  153. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
  154. TEST_ASSERT( memcmp( output, expected_result, block_size ) == 0 );
  155. exit:
  156. mbedtls_cipher_free( &ctx );
  157. }
  158. /* END_CASE */
  159. /* BEGIN_CASE */
  160. void mbedtls_cmac_multiple_operations_same_key( int cipher_type,
  161. char *key_string, int keybits,
  162. int block_size,
  163. char *block_a1_string, int block_a1_len,
  164. char *block_a2_string, int block_a2_len,
  165. char *block_a3_string, int block_a3_len,
  166. char *expected_result_a_string,
  167. char *block_b1_string, int block_b1_len,
  168. char *block_b2_string, int block_b2_len,
  169. char *block_b3_string, int block_b3_len,
  170. char *expected_result_b_string )
  171. {
  172. unsigned char key[100];
  173. unsigned char block_a1[100];
  174. unsigned char block_a2[100];
  175. unsigned char block_a3[100];
  176. unsigned char block_b1[100];
  177. unsigned char block_b2[100];
  178. unsigned char block_b3[100];
  179. unsigned char expected_result_a[100], expected_result_b[100];
  180. const mbedtls_cipher_info_t *cipher_info;
  181. mbedtls_cipher_context_t ctx;
  182. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  183. /* Convert the test parameters to binary data */
  184. unhexify( key, key_string );
  185. unhexify( block_a1, block_a1_string );
  186. unhexify( block_a2, block_a2_string );
  187. unhexify( block_a3, block_a3_string );
  188. unhexify( block_b1, block_b1_string );
  189. unhexify( block_b2, block_b2_string );
  190. unhexify( block_b3, block_b3_string );
  191. unhexify( expected_result_a, expected_result_a_string );
  192. unhexify( expected_result_b, expected_result_b_string );
  193. mbedtls_cipher_init( &ctx );
  194. /* Validate the test inputs */
  195. TEST_ASSERT( block_a1_len <= 100 );
  196. TEST_ASSERT( block_a2_len <= 100 );
  197. TEST_ASSERT( block_a3_len <= 100 );
  198. TEST_ASSERT( block_b1_len <= 100 );
  199. TEST_ASSERT( block_b2_len <= 100 );
  200. TEST_ASSERT( block_b3_len <= 100 );
  201. /* Set up */
  202. TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
  203. != NULL );
  204. TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
  205. TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
  206. (const unsigned char*)key,
  207. keybits ) == 0 );
  208. /* Sequence A */
  209. /* Multiple partial and complete blocks. A negative length means skip the
  210. * update operation */
  211. if( block_a1_len >= 0 )
  212. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  213. (unsigned char*)block_a1,
  214. block_a1_len ) == 0);
  215. if( block_a2_len >= 0 )
  216. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  217. (unsigned char*)block_a2,
  218. block_a2_len ) == 0);
  219. if( block_a3_len >= 0 )
  220. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  221. (unsigned char*)block_a3,
  222. block_a3_len ) == 0);
  223. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
  224. TEST_ASSERT( memcmp( output, expected_result_a, block_size ) == 0 );
  225. TEST_ASSERT( mbedtls_cipher_cmac_reset( &ctx ) == 0 );
  226. /* Sequence B */
  227. /* Multiple partial and complete blocks. A negative length means skip the
  228. * update operation */
  229. if( block_b1_len >= 0)
  230. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  231. (unsigned char*)block_b1,
  232. block_b1_len ) == 0);
  233. if( block_b2_len >= 0 )
  234. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  235. (unsigned char*)block_b2,
  236. block_b2_len ) == 0);
  237. if( block_b3_len >= 0 )
  238. TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
  239. (unsigned char*)block_b3,
  240. block_b3_len ) == 0);
  241. TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
  242. TEST_ASSERT( memcmp( output, expected_result_b, block_size ) == 0 );
  243. exit:
  244. mbedtls_cipher_free( &ctx );
  245. }
  246. /* END_CASE */