drbg_cavs_test.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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->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. OPENSSL_free(buff);
  95. RAND_DRBG_uninstantiate(drbg);
  96. RAND_DRBG_free(drbg);
  97. return failures == 0;
  98. }
  99. /*-
  100. * Do a single PR_FALSE KAT:
  101. *
  102. * Instantiate
  103. * Reseed
  104. * Generate Random Bits (pr=false)
  105. * Generate Random Bits (pr=false)
  106. * Uninstantiate
  107. *
  108. * Return 0 on failure.
  109. */
  110. static int single_kat_pr_false(const struct drbg_kat *td)
  111. {
  112. struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
  113. RAND_DRBG *drbg = NULL;
  114. unsigned char *buff = NULL;
  115. unsigned int flags = 0;
  116. int failures = 0;
  117. TEST_CTX t;
  118. if ((td->flags & USE_DF) == 0)
  119. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  120. if ((td->flags & USE_HMAC) != 0)
  121. flags |= RAND_DRBG_FLAG_HMAC;
  122. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  123. return 0;
  124. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  125. kat_nonce, NULL))) {
  126. failures++;
  127. goto err;
  128. }
  129. memset(&t, 0, sizeof(t));
  130. t.entropy = data->entropyin;
  131. t.entropylen = td->entropyinlen;
  132. t.nonce = data->nonce;
  133. t.noncelen = td->noncelen;
  134. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  135. buff = OPENSSL_malloc(td->retbyteslen);
  136. if (buff == NULL)
  137. goto err;
  138. if (!TEST_true(RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)))
  139. failures++;
  140. t.entropy = data->entropyinreseed;
  141. t.entropylen = td->entropyinlen;
  142. if (!TEST_true(RAND_DRBG_reseed(drbg, data->addinreseed, td->addinlen, 0))
  143. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  144. data->addin1, td->addinlen))
  145. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
  146. data->addin2, td->addinlen))
  147. || !TEST_true(RAND_DRBG_uninstantiate(drbg))
  148. || !TEST_mem_eq(data->retbytes, td->retbyteslen, buff,
  149. td->retbyteslen))
  150. failures++;
  151. err:
  152. OPENSSL_free(buff);
  153. RAND_DRBG_uninstantiate(drbg);
  154. RAND_DRBG_free(drbg);
  155. return failures == 0;
  156. }
  157. /*-
  158. * Do a single PR_TRUE KAT:
  159. *
  160. * Instantiate
  161. * Generate Random Bits (pr=true)
  162. * Generate Random Bits (pr=true)
  163. * Uninstantiate
  164. *
  165. * Return 0 on failure.
  166. */
  167. static int single_kat_pr_true(const struct drbg_kat *td)
  168. {
  169. struct drbg_kat_pr_true *data = (struct drbg_kat_pr_true *)td->t;
  170. RAND_DRBG *drbg = NULL;
  171. unsigned char *buff = NULL;
  172. unsigned int flags = 0;
  173. int failures = 0;
  174. TEST_CTX t;
  175. if ((td->flags & USE_DF) == 0)
  176. flags |= RAND_DRBG_FLAG_CTR_NO_DF;
  177. if ((td->flags & USE_HMAC) != 0)
  178. flags |= RAND_DRBG_FLAG_HMAC;
  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. OPENSSL_free(buff);
  212. RAND_DRBG_uninstantiate(drbg);
  213. RAND_DRBG_free(drbg);
  214. return failures == 0;
  215. }
  216. static int test_cavs_kats(const struct drbg_kat *test[], int i)
  217. {
  218. const struct drbg_kat *td = test[i];
  219. int rv = 0;
  220. #ifdef FIPS_MODULE
  221. /* FIPS mode doesn't support instantiating without a derivation function */
  222. if ((td->flags & USE_DF) == 0)
  223. return TEST_skip("instantiating without derivation function "
  224. "is not supported in FIPS mode");
  225. #endif
  226. switch (td->type) {
  227. case NO_RESEED:
  228. if (!single_kat_no_reseed(td))
  229. goto err;
  230. break;
  231. case PR_FALSE:
  232. if (!single_kat_pr_false(td))
  233. goto err;
  234. break;
  235. case PR_TRUE:
  236. if (!single_kat_pr_true(td))
  237. goto err;
  238. break;
  239. default: /* cant happen */
  240. goto err;
  241. }
  242. rv = 1;
  243. err:
  244. return rv;
  245. }
  246. static int test_cavs_ctr(int i)
  247. {
  248. return test_cavs_kats(drbg_ctr_test, i);
  249. }
  250. static int test_cavs_hmac(int i)
  251. {
  252. return test_cavs_kats(drbg_hmac_test, i);
  253. }
  254. static int test_cavs_hash(int i)
  255. {
  256. return test_cavs_kats(drbg_hash_test, i);
  257. }
  258. int setup_tests(void)
  259. {
  260. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  261. ADD_ALL_TESTS(test_cavs_ctr, drbg_ctr_nelem);
  262. ADD_ALL_TESTS(test_cavs_hmac, drbg_hmac_nelem);
  263. ADD_ALL_TESTS(test_cavs_hash, drbg_hash_nelem);
  264. return 1;
  265. }