test_suite_cmac.function 10 KB

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