drbg_extra_test.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 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_extra_test.h"
  20. static unsigned char zerobuff[32];
  21. static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
  22. int entropy, size_t min_len, size_t max_len,
  23. int prediction_resistance)
  24. {
  25. *pout = zerobuff;
  26. return sizeof(zerobuff);
  27. }
  28. static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
  29. int entropy, size_t min_len, size_t max_len)
  30. {
  31. *pout = zerobuff;
  32. return sizeof(zerobuff);
  33. }
  34. static int run_extra_kat(const struct drbg_extra_kat *td)
  35. {
  36. unsigned long long i;
  37. RAND_DRBG *drbg = NULL;
  38. unsigned char buff[BUFFSIZE];
  39. unsigned int flags = 0;
  40. int failures = 0;
  41. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, flags, NULL)))
  42. return 0;
  43. /* Set deterministic entropy callback. */
  44. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  45. kat_nonce, NULL))) {
  46. failures++;
  47. goto err;
  48. }
  49. /* Set fixed reseed intervall. */
  50. if (!TEST_true(RAND_DRBG_set_reseed_interval(drbg, RESEEDINTERVAL))) {
  51. failures++;
  52. goto err;
  53. }
  54. if (!TEST_true(RAND_DRBG_instantiate(drbg, NULL, 0)))
  55. failures++;
  56. for (i = 0; i < td->ngen; i++) {
  57. if(!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL,
  58. 0)))
  59. failures++;
  60. }
  61. if (!TEST_true(RAND_DRBG_uninstantiate(drbg))
  62. || !TEST_mem_eq(td->expected, sizeof(buff), buff, sizeof(buff)))
  63. failures++;
  64. err:
  65. RAND_DRBG_uninstantiate(drbg);
  66. RAND_DRBG_free(drbg);
  67. return failures == 0;
  68. }
  69. static int test_extra_kats(int i)
  70. {
  71. return run_extra_kat(drbg_extra_test[i]);
  72. }
  73. int setup_tests(void)
  74. {
  75. ADD_ALL_TESTS(test_extra_kats, OSSL_NELEM(drbg_extra_test));
  76. return 1;
  77. }