drbg_cavs_test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2017-2019 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_local.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. OPENSSL_free(buff);
  93. RAND_DRBG_uninstantiate(drbg);
  94. RAND_DRBG_free(drbg);
  95. return failures == 0;
  96. }
  97. /*-
  98. * Do a single PR_FALSE KAT:
  99. *
  100. * Instantiate
  101. * Reseed
  102. * Generate Random Bits (pr=false)
  103. * Generate Random Bits (pr=false)
  104. * Uninstantiate
  105. *
  106. * Return 0 on failure.
  107. */
  108. static int single_kat_pr_false(const struct drbg_kat *td)
  109. {
  110. struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
  111. RAND_DRBG *drbg = NULL;
  112. unsigned char *buff = NULL;
  113. unsigned int flags = 0;
  114. int failures = 0;
  115. TEST_CTX t;
  116. if (td->df != USE_DF)
  117. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  118. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  119. return 0;
  120. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  121. kat_nonce, NULL))) {
  122. failures++;
  123. goto err;
  124. }
  125. memset(&t, 0, sizeof(t));
  126. t.entropy = data->entropyin;
  127. t.entropylen = td->entropyinlen;
  128. t.nonce = data->nonce;
  129. t.noncelen = td->noncelen;
  130. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  131. buff = OPENSSL_malloc(td->retbyteslen);
  132. if (buff == NULL)
  133. goto err;
  134. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  135. failures++;
  136. t.entropy = data->entropyinreseed;
  137. t.entropylen = td->entropyinlen;
  138. if (!TEST_true(RAND_DRBG_reseed(drbg, data->addinreseed, td->addinlen, 0))
  139. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  140. data->addin1, td->addinlen))
  141. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  142. data->addin2, td->addinlen))
  143. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  144. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  145. td->retbyteslen))
  146. failures++;
  147. err:
  148. OPENSSL_free(buff);
  149. RAND_DRBG_uninstantiate(drbg);
  150. RAND_DRBG_free(drbg);
  151. return failures == 0;
  152. }
  153. /*-
  154. * Do a single PR_TRUE KAT:
  155. *
  156. * Instantiate
  157. * Generate Random Bits (pr=true)
  158. * Generate Random Bits (pr=true)
  159. * Uninstantiate
  160. *
  161. * Return 0 on failure.
  162. */
  163. static int single_kat_pr_true(const struct drbg_kat *td)
  164. {
  165. struct drbg_kat_pr_true *data = (struct drbg_kat_pr_true *)td->t;
  166. RAND_DRBG *drbg = NULL;
  167. unsigned char *buff = NULL;
  168. unsigned int flags = 0;
  169. int failures = 0;
  170. TEST_CTX t;
  171. if (td->df != USE_DF)
  172. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  173. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  174. return 0;
  175. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  176. kat_nonce, NULL))) {
  177. failures++;
  178. goto err;
  179. }
  180. memset(&t, 0, sizeof(t));
  181. t.nonce = data->nonce;
  182. t.noncelen = td->noncelen;
  183. t.entropy = data->entropyin;
  184. t.entropylen = td->entropyinlen;
  185. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  186. buff = OPENSSL_malloc(td->retbyteslen);
  187. if (buff == NULL)
  188. goto err;
  189. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  190. failures++;
  191. t.entropy = data->entropyinpr1;
  192. t.entropylen = td->entropyinlen;
  193. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
  194. data->addin1, td->addinlen)))
  195. failures++;
  196. t.entropy = data->entropyinpr2;
  197. t.entropylen = td->entropyinlen;
  198. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
  199. data->addin2, td->addinlen))
  200. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  201. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  202. td->retbyteslen))
  203. failures++;
  204. err:
  205. OPENSSL_free(buff);
  206. RAND_DRBG_uninstantiate(drbg);
  207. RAND_DRBG_free(drbg);
  208. return failures == 0;
  209. }
  210. static int test_cavs_kats(int i)
  211. {
  212. const struct drbg_kat *td = drbg_test[i];
  213. int rv = 0;
  214. switch (td->type) {
  215. case NO_RESEED:
  216. if (!single_kat_no_reseed(td))
  217. goto err;
  218. break;
  219. case PR_FALSE:
  220. if (!single_kat_pr_false(td))
  221. goto err;
  222. break;
  223. case PR_TRUE:
  224. if (!single_kat_pr_true(td))
  225. goto err;
  226. break;
  227. default: /* cant happen */
  228. goto err;
  229. }
  230. rv = 1;
  231. err:
  232. return rv;
  233. }
  234. int setup_tests(void)
  235. {
  236. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  237. ADD_ALL_TESTS(test_cavs_kats, drbg_test_nelem);
  238. return 1;
  239. }