drbg_cavs_test.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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->flags & USE_DF) == 0)
  65. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  66. if ((td->flags & USE_HMAC) != 0)
  67. flags |= RAND_DRBG_FLAG_HMAC;
  68. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  69. return 0;
  70. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  71. kat_nonce, NULL))) {
  72. failures++;
  73. goto err;
  74. }
  75. memset(&t, 0, sizeof(t));
  76. t.entropy = data->entropyin;
  77. t.entropylen = td->entropyinlen;
  78. t.nonce = data->nonce;
  79. t.noncelen = td->noncelen;
  80. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  81. buff = OPENSSL_malloc(td->retbyteslen);
  82. if (buff == NULL)
  83. goto err;
  84. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen))
  85. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  86. data->addin1, td->addinlen))
  87. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  88. data->addin2, td->addinlen))
  89. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  90. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  91. td->retbyteslen))
  92. failures++;
  93. err:
  94. if (buff != NULL)
  95. OPENSSL_free(buff);
  96. if (drbg != NULL) {
  97. RAND_DRBG_uninstantiate(drbg);
  98. RAND_DRBG_free(drbg);
  99. }
  100. return failures == 0;
  101. }
  102. /*-
  103. * Do a single PR_FALSE KAT:
  104. *
  105. * Instantiate
  106. * Reseed
  107. * Generate Random Bits (pr=false)
  108. * Generate Random Bits (pr=false)
  109. * Uninstantiate
  110. *
  111. * Return 0 on failure.
  112. */
  113. static int single_kat_pr_false(const struct drbg_kat *td)
  114. {
  115. struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
  116. RAND_DRBG *drbg = NULL;
  117. unsigned char *buff = NULL;
  118. unsigned int flags = 0;
  119. int failures = 0;
  120. TEST_CTX t;
  121. if ((td->flags & USE_DF) == 0)
  122. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  123. if ((td->flags & USE_HMAC) != 0)
  124. flags |= RAND_DRBG_FLAG_HMAC;
  125. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  126. return 0;
  127. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  128. kat_nonce, NULL))) {
  129. failures++;
  130. goto err;
  131. }
  132. memset(&t, 0, sizeof(t));
  133. t.entropy = data->entropyin;
  134. t.entropylen = td->entropyinlen;
  135. t.nonce = data->nonce;
  136. t.noncelen = td->noncelen;
  137. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  138. buff = OPENSSL_malloc(td->retbyteslen);
  139. if (buff == NULL)
  140. goto err;
  141. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  142. failures++;
  143. t.entropy = data->entropyinreseed;
  144. t.entropylen = td->entropyinlen;
  145. if (!TEST_true(RAND_DRBG_reseed(drbg, data->addinreseed, td->addinlen, 0))
  146. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  147. data->addin1, td->addinlen))
  148. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  149. data->addin2, td->addinlen))
  150. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  151. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  152. td->retbyteslen))
  153. failures++;
  154. err:
  155. if (buff != NULL)
  156. OPENSSL_free(buff);
  157. if (drbg != NULL) {
  158. RAND_DRBG_uninstantiate(drbg);
  159. RAND_DRBG_free(drbg);
  160. }
  161. return failures == 0;
  162. }
  163. /*-
  164. * Do a single PR_TRUE KAT:
  165. *
  166. * Instantiate
  167. * Generate Random Bits (pr=true)
  168. * Generate Random Bits (pr=true)
  169. * Uninstantiate
  170. *
  171. * Return 0 on failure.
  172. */
  173. static int single_kat_pr_true(const struct drbg_kat *td)
  174. {
  175. struct drbg_kat_pr_true *data = (struct drbg_kat_pr_true *)td->t;
  176. RAND_DRBG *drbg = NULL;
  177. unsigned char *buff = NULL;
  178. unsigned int flags = 0;
  179. int failures = 0;
  180. TEST_CTX t;
  181. if ((td->flags & USE_DF) == 0)
  182. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  183. if ((td->flags & USE_HMAC) != 0)
  184. flags |= RAND_DRBG_FLAG_HMAC;
  185. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  186. return 0;
  187. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  188. kat_nonce, NULL))) {
  189. failures++;
  190. goto err;
  191. }
  192. memset(&t, 0, sizeof(t));
  193. t.nonce = data->nonce;
  194. t.noncelen = td->noncelen;
  195. t.entropy = data->entropyin;
  196. t.entropylen = td->entropyinlen;
  197. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  198. buff = OPENSSL_malloc(td->retbyteslen);
  199. if (buff == NULL)
  200. goto err;
  201. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  202. failures++;
  203. t.entropy = data->entropyinpr1;
  204. t.entropylen = td->entropyinlen;
  205. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
  206. data->addin1, td->addinlen)))
  207. failures++;
  208. t.entropy = data->entropyinpr2;
  209. t.entropylen = td->entropyinlen;
  210. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
  211. data->addin2, td->addinlen))
  212. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  213. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  214. td->retbyteslen))
  215. failures++;
  216. err:
  217. if (buff != NULL)
  218. OPENSSL_free(buff);
  219. if (drbg != NULL) {
  220. RAND_DRBG_uninstantiate(drbg);
  221. RAND_DRBG_free(drbg);
  222. }
  223. return failures == 0;
  224. }
  225. static int test_cavs_kats(const struct drbg_kat *test[], int i)
  226. {
  227. const struct drbg_kat *td = test[i];
  228. int rv = 0;
  229. switch (td->type) {
  230. case NO_RESEED:
  231. if (!single_kat_no_reseed(td))
  232. goto err;
  233. break;
  234. case PR_FALSE:
  235. if (!single_kat_pr_false(td))
  236. goto err;
  237. break;
  238. case PR_TRUE:
  239. if (!single_kat_pr_true(td))
  240. goto err;
  241. break;
  242. default: /* cant happen */
  243. goto err;
  244. }
  245. rv = 1;
  246. err:
  247. return rv;
  248. }
  249. static int test_cavs_ctr(int i)
  250. {
  251. return test_cavs_kats(drbg_ctr_test, i);
  252. }
  253. static int test_cavs_hmac(int i)
  254. {
  255. return test_cavs_kats(drbg_hmac_test, i);
  256. }
  257. static int test_cavs_hash(int i)
  258. {
  259. return test_cavs_kats(drbg_hash_test, i);
  260. }
  261. int setup_tests(void)
  262. {
  263. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  264. ADD_ALL_TESTS(test_cavs_ctr, drbg_ctr_nelem);
  265. ADD_ALL_TESTS(test_cavs_hmac, drbg_hmac_nelem);
  266. ADD_ALL_TESTS(test_cavs_hash, drbg_hash_nelem);
  267. return 1;
  268. }