test_suite_hmac_drbg.function 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/hmac_drbg.h"
  3. #include "string.h"
  4. typedef struct {
  5. unsigned char *p;
  6. size_t len;
  7. } entropy_ctx;
  8. static int mbedtls_test_entropy_func(void *data, unsigned char *buf, size_t len)
  9. {
  10. entropy_ctx *ctx = (entropy_ctx *) data;
  11. if (len > ctx->len) {
  12. return -1;
  13. }
  14. memcpy(buf, ctx->p, len);
  15. ctx->p += len;
  16. ctx->len -= len;
  17. return 0;
  18. }
  19. /* END_HEADER */
  20. /* BEGIN_DEPENDENCIES
  21. * depends_on:MBEDTLS_HMAC_DRBG_C
  22. * END_DEPENDENCIES
  23. */
  24. /* BEGIN_CASE */
  25. void hmac_drbg_entropy_usage(int md_alg)
  26. {
  27. unsigned char out[16];
  28. unsigned char buf[1024];
  29. const mbedtls_md_info_t *md_info;
  30. mbedtls_hmac_drbg_context ctx;
  31. entropy_ctx entropy;
  32. size_t i, reps = 10;
  33. size_t default_entropy_len;
  34. size_t expected_consumed_entropy = 0;
  35. mbedtls_hmac_drbg_init(&ctx);
  36. memset(buf, 0, sizeof(buf));
  37. memset(out, 0, sizeof(out));
  38. entropy.len = sizeof(buf);
  39. entropy.p = buf;
  40. md_info = mbedtls_md_info_from_type(md_alg);
  41. TEST_ASSERT(md_info != NULL);
  42. if (mbedtls_md_get_size(md_info) <= 20) {
  43. default_entropy_len = 16;
  44. } else if (mbedtls_md_get_size(md_info) <= 28) {
  45. default_entropy_len = 24;
  46. } else {
  47. default_entropy_len = 32;
  48. }
  49. /* Set reseed interval before seed */
  50. mbedtls_hmac_drbg_set_reseed_interval(&ctx, 2 * reps);
  51. /* Init must use entropy */
  52. TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &entropy,
  53. NULL, 0) == 0);
  54. /* default_entropy_len of entropy, plus half as much for the nonce */
  55. expected_consumed_entropy += default_entropy_len * 3 / 2;
  56. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  57. /* By default, PR is off, and reseed interval was set to
  58. * 2 * reps so the next few calls should not use entropy */
  59. for (i = 0; i < reps; i++) {
  60. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
  61. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
  62. buf, 16) == 0);
  63. }
  64. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  65. /* While at it, make sure we didn't write past the requested length */
  66. TEST_ASSERT(out[sizeof(out) - 4] == 0);
  67. TEST_ASSERT(out[sizeof(out) - 3] == 0);
  68. TEST_ASSERT(out[sizeof(out) - 2] == 0);
  69. TEST_ASSERT(out[sizeof(out) - 1] == 0);
  70. /* There have been 2 * reps calls to random. The next call should reseed */
  71. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
  72. expected_consumed_entropy += default_entropy_len;
  73. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  74. /* Set reseed interval after seed */
  75. mbedtls_hmac_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
  76. /* The new few calls should not reseed */
  77. for (i = 0; i < (2 * reps); i++) {
  78. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
  79. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, out, sizeof(out),
  80. buf, 16) == 0);
  81. }
  82. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  83. /* Now enable PR, so the next few calls should all reseed */
  84. mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
  85. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
  86. expected_consumed_entropy += default_entropy_len;
  87. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  88. /* Finally, check setting entropy_len */
  89. mbedtls_hmac_drbg_set_entropy_len(&ctx, 42);
  90. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
  91. expected_consumed_entropy += 42;
  92. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  93. mbedtls_hmac_drbg_set_entropy_len(&ctx, 13);
  94. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
  95. expected_consumed_entropy += 13;
  96. TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
  97. exit:
  98. mbedtls_hmac_drbg_free(&ctx);
  99. }
  100. /* END_CASE */
  101. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  102. void hmac_drbg_seed_file(int md_alg, char *path, int ret)
  103. {
  104. const mbedtls_md_info_t *md_info;
  105. mbedtls_hmac_drbg_context ctx;
  106. mbedtls_hmac_drbg_init(&ctx);
  107. md_info = mbedtls_md_info_from_type(md_alg);
  108. TEST_ASSERT(md_info != NULL);
  109. TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info,
  110. mbedtls_test_rnd_std_rand, NULL,
  111. NULL, 0) == 0);
  112. TEST_ASSERT(mbedtls_hmac_drbg_write_seed_file(&ctx, path) == ret);
  113. TEST_ASSERT(mbedtls_hmac_drbg_update_seed_file(&ctx, path) == ret);
  114. exit:
  115. mbedtls_hmac_drbg_free(&ctx);
  116. }
  117. /* END_CASE */
  118. /* BEGIN_CASE */
  119. void hmac_drbg_buf(int md_alg)
  120. {
  121. unsigned char out[16];
  122. unsigned char buf[100];
  123. const mbedtls_md_info_t *md_info;
  124. mbedtls_hmac_drbg_context ctx;
  125. size_t i;
  126. mbedtls_hmac_drbg_init(&ctx);
  127. memset(buf, 0, sizeof(buf));
  128. memset(out, 0, sizeof(out));
  129. md_info = mbedtls_md_info_from_type(md_alg);
  130. TEST_ASSERT(md_info != NULL);
  131. TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info, buf, sizeof(buf)) == 0);
  132. /* Make sure it never tries to reseed (would segfault otherwise) */
  133. mbedtls_hmac_drbg_set_reseed_interval(&ctx, 3);
  134. mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
  135. for (i = 0; i < 30; i++) {
  136. TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
  137. }
  138. exit:
  139. mbedtls_hmac_drbg_free(&ctx);
  140. }
  141. /* END_CASE */
  142. /* BEGIN_CASE */
  143. void hmac_drbg_no_reseed(int md_alg, data_t *entropy,
  144. data_t *custom, data_t *add1,
  145. data_t *add2, data_t *output)
  146. {
  147. unsigned char data[1024];
  148. unsigned char my_output[512];
  149. entropy_ctx p_entropy;
  150. const mbedtls_md_info_t *md_info;
  151. mbedtls_hmac_drbg_context ctx;
  152. mbedtls_hmac_drbg_init(&ctx);
  153. p_entropy.p = entropy->x;
  154. p_entropy.len = entropy->len;
  155. md_info = mbedtls_md_info_from_type(md_alg);
  156. TEST_ASSERT(md_info != NULL);
  157. /* Test the simplified buffer-based variant */
  158. memcpy(data, entropy->x, p_entropy.len);
  159. memcpy(data + p_entropy.len, custom->x, custom->len);
  160. TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info,
  161. data, p_entropy.len + custom->len) == 0);
  162. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  163. add1->x, add1->len) == 0);
  164. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  165. add2->x, add2->len) == 0);
  166. /* Reset context for second run */
  167. mbedtls_hmac_drbg_free(&ctx);
  168. TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
  169. /* And now the normal entropy-based variant */
  170. TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
  171. custom->x, custom->len) == 0);
  172. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  173. add1->x, add1->len) == 0);
  174. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  175. add2->x, add2->len) == 0);
  176. TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
  177. exit:
  178. mbedtls_hmac_drbg_free(&ctx);
  179. }
  180. /* END_CASE */
  181. /* BEGIN_CASE */
  182. void hmac_drbg_nopr(int md_alg, data_t *entropy, data_t *custom,
  183. data_t *add1, data_t *add2, data_t *add3,
  184. data_t *output)
  185. {
  186. unsigned char my_output[512];
  187. entropy_ctx p_entropy;
  188. const mbedtls_md_info_t *md_info;
  189. mbedtls_hmac_drbg_context ctx;
  190. mbedtls_hmac_drbg_init(&ctx);
  191. p_entropy.p = entropy->x;
  192. p_entropy.len = entropy->len;
  193. md_info = mbedtls_md_info_from_type(md_alg);
  194. TEST_ASSERT(md_info != NULL);
  195. TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
  196. custom->x, custom->len) == 0);
  197. TEST_ASSERT(mbedtls_hmac_drbg_reseed(&ctx, add1->x, add1->len) == 0);
  198. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  199. add2->x, add2->len) == 0);
  200. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  201. add3->x, add3->len) == 0);
  202. TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
  203. exit:
  204. mbedtls_hmac_drbg_free(&ctx);
  205. }
  206. /* END_CASE */
  207. /* BEGIN_CASE */
  208. void hmac_drbg_pr(int md_alg, data_t *entropy, data_t *custom,
  209. data_t *add1, data_t *add2, data_t *output)
  210. {
  211. unsigned char my_output[512];
  212. entropy_ctx p_entropy;
  213. const mbedtls_md_info_t *md_info;
  214. mbedtls_hmac_drbg_context ctx;
  215. mbedtls_hmac_drbg_init(&ctx);
  216. p_entropy.p = entropy->x;
  217. p_entropy.len = entropy->len;
  218. md_info = mbedtls_md_info_from_type(md_alg);
  219. TEST_ASSERT(md_info != NULL);
  220. TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
  221. custom->x, custom->len) == 0);
  222. mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
  223. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  224. add1->x, add1->len) == 0);
  225. TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
  226. add2->x, add2->len) == 0);
  227. TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
  228. exit:
  229. mbedtls_hmac_drbg_free(&ctx);
  230. }
  231. /* END_CASE */
  232. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  233. void hmac_drbg_selftest()
  234. {
  235. TEST_ASSERT(mbedtls_hmac_drbg_self_test(1) == 0);
  236. }
  237. /* END_CASE */