drbg_cavs_test.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include "internal/nelem.h"
  11. #include <openssl/crypto.h>
  12. #include <openssl/err.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/obj_mac.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/aes.h>
  17. #include "../crypto/rand/rand_lcl.h"
  18. #include "testutil.h"
  19. #include "drbg_cavs_data.h"
  20. static int app_data_index;
  21. typedef struct test_ctx_st {
  22. const unsigned char *entropy;
  23. size_t entropylen;
  24. int entropycnt;
  25. const unsigned char *nonce;
  26. size_t noncelen;
  27. int noncecnt;
  28. } TEST_CTX;
  29. static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
  30. int entropy, size_t min_len, size_t max_len,
  31. int prediction_resistance)
  32. {
  33. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  34. t->entropycnt++;
  35. *pout = (unsigned char *)t->entropy;
  36. return t->entropylen;
  37. }
  38. static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
  39. int entropy, size_t min_len, size_t max_len)
  40. {
  41. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  42. t->noncecnt++;
  43. *pout = (unsigned char *)t->nonce;
  44. return t->noncelen;
  45. }
  46. /*
  47. * Do a single NO_RESEED KAT:
  48. *
  49. * Instantiate
  50. * Generate Random Bits (pr=false)
  51. * Generate Random Bits (pr=false)
  52. * Uninstantiate
  53. *
  54. * Return 0 on failure.
  55. */
  56. static int single_kat_no_reseed(const struct drbg_kat *td)
  57. {
  58. struct drbg_kat_no_reseed *data = (struct drbg_kat_no_reseed *)td->t;
  59. RAND_DRBG *drbg = NULL;
  60. unsigned char *buff = NULL;
  61. unsigned int flags = 0;
  62. int failures = 0;
  63. TEST_CTX t;
  64. if (td->df != USE_DF)
  65. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  66. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  67. return 0;
  68. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  69. kat_nonce, NULL))) {
  70. failures++;
  71. goto err;
  72. }
  73. memset(&t, 0, sizeof(t));
  74. t.entropy = data->entropyin;
  75. t.entropylen = td->entropyinlen;
  76. t.nonce = data->nonce;
  77. t.noncelen = td->noncelen;
  78. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  79. buff = OPENSSL_malloc(td->retbyteslen);
  80. if (buff == NULL)
  81. goto err;
  82. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen))
  83. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  84. data->addin1, td->addinlen))
  85. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  86. data->addin2, td->addinlen))
  87. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  88. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  89. td->retbyteslen))
  90. failures++;
  91. err:
  92. if (buff != NULL)
  93. OPENSSL_free(buff);
  94. if (drbg != NULL) {
  95. RAND_DRBG_uninstantiate(drbg);
  96. RAND_DRBG_free(drbg);
  97. }
  98. return failures == 0;
  99. }
  100. /*-
  101. * Do a single PR_FALSE KAT:
  102. *
  103. * Instantiate
  104. * Reseed
  105. * Generate Random Bits (pr=false)
  106. * Generate Random Bits (pr=false)
  107. * Uninstantiate
  108. *
  109. * Return 0 on failure.
  110. */
  111. static int single_kat_pr_false(const struct drbg_kat *td)
  112. {
  113. struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
  114. RAND_DRBG *drbg = NULL;
  115. unsigned char *buff = NULL;
  116. unsigned int flags = 0;
  117. int failures = 0;
  118. TEST_CTX t;
  119. if (td->df != USE_DF)
  120. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  121. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  122. return 0;
  123. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  124. kat_nonce, NULL))) {
  125. failures++;
  126. goto err;
  127. }
  128. memset(&t, 0, sizeof(t));
  129. t.entropy = data->entropyin;
  130. t.entropylen = td->entropyinlen;
  131. t.nonce = data->nonce;
  132. t.noncelen = td->noncelen;
  133. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  134. buff = OPENSSL_malloc(td->retbyteslen);
  135. if (buff == NULL)
  136. goto err;
  137. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  138. failures++;
  139. t.entropy = data->entropyinreseed;
  140. t.entropylen = td->entropyinlen;
  141. if (!TEST_true(RAND_DRBG_reseed(drbg, data->addinreseed, td->addinlen, 0))
  142. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  143. data->addin1, td->addinlen))
  144. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  145. data->addin2, td->addinlen))
  146. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  147. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  148. td->retbyteslen))
  149. failures++;
  150. err:
  151. if (buff != NULL)
  152. OPENSSL_free(buff);
  153. if (drbg != NULL) {
  154. RAND_DRBG_uninstantiate(drbg);
  155. RAND_DRBG_free(drbg);
  156. }
  157. return failures == 0;
  158. }
  159. /*-
  160. * Do a single PR_TRUE KAT:
  161. *
  162. * Instantiate
  163. * Generate Random Bits (pr=true)
  164. * Generate Random Bits (pr=true)
  165. * Uninstantiate
  166. *
  167. * Return 0 on failure.
  168. */
  169. static int single_kat_pr_true(const struct drbg_kat *td)
  170. {
  171. struct drbg_kat_pr_true *data = (struct drbg_kat_pr_true *)td->t;
  172. RAND_DRBG *drbg = NULL;
  173. unsigned char *buff = NULL;
  174. unsigned int flags = 0;
  175. int failures = 0;
  176. TEST_CTX t;
  177. if (td->df != USE_DF)
  178. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  179. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  180. return 0;
  181. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  182. kat_nonce, NULL))) {
  183. failures++;
  184. goto err;
  185. }
  186. memset(&t, 0, sizeof(t));
  187. t.nonce = data->nonce;
  188. t.noncelen = td->noncelen;
  189. t.entropy = data->entropyin;
  190. t.entropylen = td->entropyinlen;
  191. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  192. buff = OPENSSL_malloc(td->retbyteslen);
  193. if (buff == NULL)
  194. goto err;
  195. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  196. failures++;
  197. t.entropy = data->entropyinpr1;
  198. t.entropylen = td->entropyinlen;
  199. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
  200. data->addin1, td->addinlen)))
  201. failures++;
  202. t.entropy = data->entropyinpr2;
  203. t.entropylen = td->entropyinlen;
  204. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
  205. data->addin2, td->addinlen))
  206. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  207. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  208. td->retbyteslen))
  209. failures++;
  210. err:
  211. if (buff != NULL)
  212. OPENSSL_free(buff);
  213. if (drbg != NULL) {
  214. RAND_DRBG_uninstantiate(drbg);
  215. RAND_DRBG_free(drbg);
  216. }
  217. return failures == 0;
  218. }
  219. static int test_cavs_kats(int i)
  220. {
  221. const struct drbg_kat *td = drbg_test[i];
  222. int rv = 0;
  223. switch (td->type) {
  224. case NO_RESEED:
  225. if (!single_kat_no_reseed(td))
  226. goto err;
  227. break;
  228. case PR_FALSE:
  229. if (!single_kat_pr_false(td))
  230. goto err;
  231. break;
  232. case PR_TRUE:
  233. if (!single_kat_pr_true(td))
  234. goto err;
  235. break;
  236. default: /* cant happen */
  237. goto err;
  238. }
  239. rv = 1;
  240. err:
  241. return rv;
  242. }
  243. int setup_tests(void)
  244. {
  245. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  246. ADD_ALL_TESTS(test_cavs_kats, drbg_test_nelem);
  247. return 1;
  248. }