You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

74 lines
2.5 KiB

  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/rand.h>
  15. #include <openssl/crypto.h>
  16. #include "internal.h"
  17. #include "../test/test_util.h"
  18. #include "../test/file_test.h"
  19. static bool TestCTRDRBG(FileTest *t, void *arg) {
  20. std::vector<uint8_t> seed, personalisation, reseed, ai_reseed, ai1, ai2,
  21. expected;
  22. if (!t->GetBytes(&seed, "EntropyInput") ||
  23. !t->GetBytes(&personalisation, "PersonalizationString") ||
  24. !t->GetBytes(&reseed, "EntropyInputReseed") ||
  25. !t->GetBytes(&ai_reseed, "AdditionalInputReseed") ||
  26. !t->GetBytes(&ai1, "AdditionalInput1") ||
  27. !t->GetBytes(&ai2, "AdditionalInput2") ||
  28. !t->GetBytes(&expected, "ReturnedBits")) {
  29. t->PrintLine("missing value");
  30. return false;
  31. }
  32. if (seed.size() != CTR_DRBG_ENTROPY_LEN ||
  33. reseed.size() != CTR_DRBG_ENTROPY_LEN) {
  34. t->PrintLine("bad seed length");
  35. return false;
  36. }
  37. CTR_DRBG_STATE drbg;
  38. CTR_DRBG_init(&drbg, seed.data(),
  39. personalisation.size() > 0 ? personalisation.data() : nullptr,
  40. personalisation.size());
  41. CTR_DRBG_reseed(&drbg, reseed.data(),
  42. ai_reseed.size() > 0 ? ai_reseed.data() : nullptr,
  43. ai_reseed.size());
  44. std::vector<uint8_t> out;
  45. out.resize(expected.size());
  46. CTR_DRBG_generate(&drbg, out.data(), out.size(),
  47. ai1.size() > 0 ? ai1.data() : nullptr, ai1.size());
  48. CTR_DRBG_generate(&drbg, out.data(), out.size(),
  49. ai2.size() > 0 ? ai2.data() : nullptr, ai2.size());
  50. return t->ExpectBytesEqual(expected.data(), expected.size(), out.data(),
  51. out.size());
  52. }
  53. int main(int argc, char **argv) {
  54. CRYPTO_library_init();
  55. if (argc != 2) {
  56. fprintf(stderr, "%s <test file>\n", argv[0]);
  57. return 1;
  58. }
  59. return FileTestMain(TestCTRDRBG, nullptr, argv[1]);
  60. }