test_suite_nist_kw.function 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/nist_kw.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_NIST_KW_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
  9. void mbedtls_nist_kw_self_test()
  10. {
  11. TEST_ASSERT(mbedtls_nist_kw_self_test(1) == 0);
  12. }
  13. /* END_CASE */
  14. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  15. void mbedtls_nist_kw_mix_contexts()
  16. {
  17. mbedtls_nist_kw_context ctx1, ctx2;
  18. unsigned char key[16];
  19. unsigned char plaintext[32];
  20. unsigned char ciphertext1[40];
  21. unsigned char ciphertext2[40];
  22. size_t output_len, i;
  23. memset(plaintext, 0, sizeof(plaintext));
  24. memset(ciphertext1, 0, sizeof(ciphertext1));
  25. memset(ciphertext2, 0, sizeof(ciphertext2));
  26. memset(key, 0, sizeof(key));
  27. /*
  28. * 1. Check wrap and unwrap with two separate contexts
  29. */
  30. mbedtls_nist_kw_init(&ctx1);
  31. mbedtls_nist_kw_init(&ctx2);
  32. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx1,
  33. MBEDTLS_CIPHER_ID_AES,
  34. key, sizeof(key) * 8,
  35. 1) == 0);
  36. TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx1, MBEDTLS_KW_MODE_KW,
  37. plaintext, sizeof(plaintext),
  38. ciphertext1, &output_len,
  39. sizeof(ciphertext1)) == 0);
  40. TEST_ASSERT(output_len == sizeof(ciphertext1));
  41. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx2,
  42. MBEDTLS_CIPHER_ID_AES,
  43. key, sizeof(key) * 8,
  44. 0) == 0);
  45. TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx2, MBEDTLS_KW_MODE_KW,
  46. ciphertext1, output_len,
  47. plaintext, &output_len,
  48. sizeof(plaintext)) == 0);
  49. TEST_ASSERT(output_len == sizeof(plaintext));
  50. for (i = 0; i < sizeof(plaintext); i++) {
  51. TEST_ASSERT(plaintext[i] == 0);
  52. }
  53. mbedtls_nist_kw_free(&ctx1);
  54. mbedtls_nist_kw_free(&ctx2);
  55. /*
  56. * 2. Check wrapping with two modes, on same context
  57. */
  58. mbedtls_nist_kw_init(&ctx1);
  59. mbedtls_nist_kw_init(&ctx2);
  60. output_len = sizeof(ciphertext1);
  61. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx1,
  62. MBEDTLS_CIPHER_ID_AES,
  63. key, sizeof(key) * 8,
  64. 1) == 0);
  65. TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx1, MBEDTLS_KW_MODE_KW,
  66. plaintext, sizeof(plaintext),
  67. ciphertext1, &output_len,
  68. sizeof(ciphertext1)) == 0);
  69. TEST_ASSERT(output_len == sizeof(ciphertext1));
  70. TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx1, MBEDTLS_KW_MODE_KWP,
  71. plaintext, sizeof(plaintext),
  72. ciphertext2, &output_len,
  73. sizeof(ciphertext2)) == 0);
  74. TEST_ASSERT(output_len == sizeof(ciphertext2));
  75. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx2,
  76. MBEDTLS_CIPHER_ID_AES,
  77. key, sizeof(key) * 8,
  78. 0) == 0);
  79. TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx2, MBEDTLS_KW_MODE_KW,
  80. ciphertext1, sizeof(ciphertext1),
  81. plaintext, &output_len,
  82. sizeof(plaintext)) == 0);
  83. TEST_ASSERT(output_len == sizeof(plaintext));
  84. for (i = 0; i < sizeof(plaintext); i++) {
  85. TEST_ASSERT(plaintext[i] == 0);
  86. }
  87. TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx2, MBEDTLS_KW_MODE_KWP,
  88. ciphertext2, sizeof(ciphertext2),
  89. plaintext, &output_len,
  90. sizeof(plaintext)) == 0);
  91. TEST_ASSERT(output_len == sizeof(plaintext));
  92. for (i = 0; i < sizeof(plaintext); i++) {
  93. TEST_ASSERT(plaintext[i] == 0);
  94. }
  95. exit:
  96. mbedtls_nist_kw_free(&ctx1);
  97. mbedtls_nist_kw_free(&ctx2);
  98. }
  99. /* END_CASE */
  100. /* BEGIN_CASE */
  101. void mbedtls_nist_kw_setkey(int cipher_id, int key_size,
  102. int is_wrap, int result)
  103. {
  104. mbedtls_nist_kw_context ctx;
  105. unsigned char key[32];
  106. int ret;
  107. mbedtls_nist_kw_init(&ctx);
  108. memset(key, 0x2A, sizeof(key));
  109. TEST_ASSERT((unsigned) key_size <= 8 * sizeof(key));
  110. ret = mbedtls_nist_kw_setkey(&ctx, cipher_id, key, key_size, is_wrap);
  111. TEST_ASSERT(ret == result);
  112. exit:
  113. mbedtls_nist_kw_free(&ctx);
  114. }
  115. /* END_CASE */
  116. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  117. void nist_kw_plaintext_lengths(int in_len, int out_len, int mode, int res)
  118. {
  119. mbedtls_nist_kw_context ctx;
  120. unsigned char key[16];
  121. unsigned char *plaintext = NULL;
  122. unsigned char *ciphertext = NULL;
  123. size_t output_len = out_len;
  124. mbedtls_nist_kw_init(&ctx);
  125. memset(key, 0, sizeof(key));
  126. if (in_len != 0) {
  127. plaintext = mbedtls_calloc(1, in_len);
  128. TEST_ASSERT(plaintext != NULL);
  129. }
  130. if (out_len != 0) {
  131. ciphertext = mbedtls_calloc(1, output_len);
  132. TEST_ASSERT(ciphertext != NULL);
  133. }
  134. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
  135. key, 8 * sizeof(key), 1) == 0);
  136. TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx, mode, plaintext, in_len,
  137. ciphertext, &output_len,
  138. output_len) == res);
  139. if (res == 0) {
  140. if (mode == MBEDTLS_KW_MODE_KWP) {
  141. TEST_ASSERT(output_len == (size_t) in_len + 8 -
  142. (in_len % 8) + 8);
  143. } else {
  144. TEST_ASSERT(output_len == (size_t) in_len + 8);
  145. }
  146. } else {
  147. TEST_ASSERT(output_len == 0);
  148. }
  149. exit:
  150. mbedtls_free(ciphertext);
  151. mbedtls_free(plaintext);
  152. mbedtls_nist_kw_free(&ctx);
  153. }
  154. /* END_CASE */
  155. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  156. void nist_kw_ciphertext_lengths(int in_len, int out_len, int mode, int res)
  157. {
  158. mbedtls_nist_kw_context ctx;
  159. unsigned char key[16];
  160. unsigned char *plaintext = NULL;
  161. unsigned char *ciphertext = NULL;
  162. int unwrap_ret;
  163. size_t output_len = out_len;
  164. mbedtls_nist_kw_init(&ctx);
  165. memset(key, 0, sizeof(key));
  166. if (out_len != 0) {
  167. plaintext = mbedtls_calloc(1, output_len);
  168. TEST_ASSERT(plaintext != NULL);
  169. }
  170. if (in_len != 0) {
  171. ciphertext = mbedtls_calloc(1, in_len);
  172. TEST_ASSERT(ciphertext != NULL);
  173. }
  174. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, MBEDTLS_CIPHER_ID_AES,
  175. key, 8 * sizeof(key), 0) == 0);
  176. unwrap_ret = mbedtls_nist_kw_unwrap(&ctx, mode, ciphertext, in_len,
  177. plaintext, &output_len,
  178. output_len);
  179. if (res == 0) {
  180. TEST_ASSERT(unwrap_ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
  181. } else {
  182. TEST_ASSERT(unwrap_ret == res);
  183. }
  184. TEST_ASSERT(output_len == 0);
  185. exit:
  186. mbedtls_free(ciphertext);
  187. mbedtls_free(plaintext);
  188. mbedtls_nist_kw_free(&ctx);
  189. }
  190. /* END_CASE */
  191. /* BEGIN_CASE */
  192. void mbedtls_nist_kw_wrap(int cipher_id, int mode, data_t *key, data_t *msg,
  193. data_t *expected_result)
  194. {
  195. unsigned char result[528];
  196. mbedtls_nist_kw_context ctx;
  197. size_t result_len, i, padlen;
  198. mbedtls_nist_kw_init(&ctx);
  199. memset(result, '+', sizeof(result));
  200. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, cipher_id,
  201. key->x, key->len * 8, 1) == 0);
  202. /* Test with input == output */
  203. TEST_ASSERT(mbedtls_nist_kw_wrap(&ctx, mode, msg->x, msg->len,
  204. result, &result_len, sizeof(result)) == 0);
  205. TEST_ASSERT(result_len == expected_result->len);
  206. TEST_ASSERT(memcmp(expected_result->x, result, result_len) == 0);
  207. padlen = (msg->len % 8 != 0) ? 8 - (msg->len % 8) : 0;
  208. /* Check that the function didn't write beyond the end of the buffer. */
  209. for (i = msg->len + 8 + padlen; i < sizeof(result); i++) {
  210. TEST_ASSERT(result[i] == '+');
  211. }
  212. exit:
  213. mbedtls_nist_kw_free(&ctx);
  214. }
  215. /* END_CASE */
  216. /* BEGIN_CASE */
  217. void mbedtls_nist_kw_unwrap(int cipher_id, int mode, data_t *key, data_t *msg,
  218. data_t *expected_result, int expected_ret)
  219. {
  220. unsigned char result[528];
  221. mbedtls_nist_kw_context ctx;
  222. size_t result_len, i;
  223. mbedtls_nist_kw_init(&ctx);
  224. memset(result, '+', sizeof(result));
  225. TEST_ASSERT(mbedtls_nist_kw_setkey(&ctx, cipher_id,
  226. key->x, key->len * 8, 0) == 0);
  227. /* Test with input == output */
  228. TEST_ASSERT(mbedtls_nist_kw_unwrap(&ctx, mode, msg->x, msg->len,
  229. result, &result_len, sizeof(result)) == expected_ret);
  230. if (expected_ret == 0) {
  231. TEST_ASSERT(result_len == expected_result->len);
  232. TEST_ASSERT(memcmp(expected_result->x, result, result_len) == 0);
  233. } else {
  234. TEST_ASSERT(result_len == 0);
  235. }
  236. /* Check that the function didn't write beyond the end of the buffer. */
  237. for (i = msg->len - 8; i < sizeof(result); i++) {
  238. TEST_ASSERT(result[i] == '+');
  239. }
  240. exit:
  241. mbedtls_nist_kw_free(&ctx);
  242. }
  243. /* END_CASE */