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.
 
 
 
 
 
 

120 rivejä
4.7 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 <gtest/gtest.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/sha.h>
  17. #include "internal.h"
  18. #include "../../test/file_test.h"
  19. #include "../../test/test_util.h"
  20. TEST(CTRDRBGTest, Basic) {
  21. const uint8_t kSeed[CTR_DRBG_ENTROPY_LEN] = {
  22. 0xe4, 0xbc, 0x23, 0xc5, 0x08, 0x9a, 0x19, 0xd8, 0x6f, 0x41, 0x19, 0xcb,
  23. 0x3f, 0xa0, 0x8c, 0x0a, 0x49, 0x91, 0xe0, 0xa1, 0xde, 0xf1, 0x7e, 0x10,
  24. 0x1e, 0x4c, 0x14, 0xd9, 0xc3, 0x23, 0x46, 0x0a, 0x7c, 0x2f, 0xb5, 0x8e,
  25. 0x0b, 0x08, 0x6c, 0x6c, 0x57, 0xb5, 0x5f, 0x56, 0xca, 0xe2, 0x5b, 0xad,
  26. };
  27. CTR_DRBG_STATE drbg;
  28. ASSERT_TRUE(CTR_DRBG_init(&drbg, kSeed, nullptr, 0));
  29. const uint8_t kReseed[CTR_DRBG_ENTROPY_LEN] = {
  30. 0xfd, 0x85, 0xa8, 0x36, 0xbb, 0xa8, 0x50, 0x19, 0x88, 0x1e, 0x8c, 0x6b,
  31. 0xad, 0x23, 0xc9, 0x06, 0x1a, 0xdc, 0x75, 0x47, 0x76, 0x59, 0xac, 0xae,
  32. 0xa8, 0xe4, 0xa0, 0x1d, 0xfe, 0x07, 0xa1, 0x83, 0x2d, 0xad, 0x1c, 0x13,
  33. 0x6f, 0x59, 0xd7, 0x0f, 0x86, 0x53, 0xa5, 0xdc, 0x11, 0x86, 0x63, 0xd6,
  34. };
  35. ASSERT_TRUE(CTR_DRBG_reseed(&drbg, kReseed, nullptr, 0));
  36. uint8_t out[64];
  37. ASSERT_TRUE(CTR_DRBG_generate(&drbg, out, sizeof(out), nullptr, 0));
  38. ASSERT_TRUE(CTR_DRBG_generate(&drbg, out, sizeof(out), nullptr, 0));
  39. const uint8_t kExpected[64] = {
  40. 0xb2, 0xcb, 0x89, 0x05, 0xc0, 0x5e, 0x59, 0x50, 0xca, 0x31, 0x89,
  41. 0x50, 0x96, 0xbe, 0x29, 0xea, 0x3d, 0x5a, 0x3b, 0x82, 0xb2, 0x69,
  42. 0x49, 0x55, 0x54, 0xeb, 0x80, 0xfe, 0x07, 0xde, 0x43, 0xe1, 0x93,
  43. 0xb9, 0xe7, 0xc3, 0xec, 0xe7, 0x3b, 0x80, 0xe0, 0x62, 0xb1, 0xc1,
  44. 0xf6, 0x82, 0x02, 0xfb, 0xb1, 0xc5, 0x2a, 0x04, 0x0e, 0xa2, 0x47,
  45. 0x88, 0x64, 0x29, 0x52, 0x82, 0x23, 0x4a, 0xaa, 0xda,
  46. };
  47. EXPECT_EQ(Bytes(kExpected), Bytes(out));
  48. CTR_DRBG_clear(&drbg);
  49. }
  50. TEST(CTRDRBGTest, Large) {
  51. const uint8_t kSeed[CTR_DRBG_ENTROPY_LEN] = {0};
  52. CTR_DRBG_STATE drbg;
  53. ASSERT_TRUE(CTR_DRBG_init(&drbg, kSeed, nullptr, 0));
  54. std::unique_ptr<uint8_t[]> buf(new uint8_t[CTR_DRBG_MAX_GENERATE_LENGTH]);
  55. ASSERT_TRUE(CTR_DRBG_generate(&drbg, buf.get(), CTR_DRBG_MAX_GENERATE_LENGTH,
  56. nullptr, 0));
  57. uint8_t digest[SHA256_DIGEST_LENGTH];
  58. SHA256(buf.get(), CTR_DRBG_MAX_GENERATE_LENGTH, digest);
  59. const uint8_t kExpected[SHA256_DIGEST_LENGTH] = {
  60. 0x69, 0x78, 0x15, 0x96, 0xca, 0xc0, 0x3f, 0x6a, 0x6d, 0xed, 0x22,
  61. 0x1e, 0x26, 0xd0, 0x75, 0x49, 0xa0, 0x4b, 0x91, 0x58, 0x3c, 0xf4,
  62. 0xe3, 0x6d, 0xff, 0x41, 0xbf, 0xb9, 0xf8, 0xa8, 0x1c, 0x2b,
  63. };
  64. EXPECT_EQ(Bytes(kExpected), Bytes(digest));
  65. CTR_DRBG_clear(&drbg);
  66. }
  67. TEST(CTRDRBGTest, TestVectors) {
  68. FileTestGTest("crypto/fipsmodule/rand/ctrdrbg_vectors.txt", [](FileTest *t) {
  69. std::vector<uint8_t> seed, personalisation, reseed, ai_reseed, ai1, ai2,
  70. expected;
  71. ASSERT_TRUE(t->GetBytes(&seed, "EntropyInput"));
  72. ASSERT_TRUE(t->GetBytes(&personalisation, "PersonalizationString"));
  73. ASSERT_TRUE(t->GetBytes(&reseed, "EntropyInputReseed"));
  74. ASSERT_TRUE(t->GetBytes(&ai_reseed, "AdditionalInputReseed"));
  75. ASSERT_TRUE(t->GetBytes(&ai1, "AdditionalInput1"));
  76. ASSERT_TRUE(t->GetBytes(&ai2, "AdditionalInput2"));
  77. ASSERT_TRUE(t->GetBytes(&expected, "ReturnedBits"));
  78. ASSERT_EQ(static_cast<size_t>(CTR_DRBG_ENTROPY_LEN), seed.size());
  79. ASSERT_EQ(static_cast<size_t>(CTR_DRBG_ENTROPY_LEN), reseed.size());
  80. CTR_DRBG_STATE drbg;
  81. CTR_DRBG_init(&drbg, seed.data(),
  82. personalisation.empty() ? nullptr : personalisation.data(),
  83. personalisation.size());
  84. CTR_DRBG_reseed(&drbg, reseed.data(),
  85. ai_reseed.empty() ? nullptr : ai_reseed.data(),
  86. ai_reseed.size());
  87. std::vector<uint8_t> out;
  88. out.resize(expected.size());
  89. CTR_DRBG_generate(&drbg, out.data(), out.size(),
  90. ai1.empty() ? nullptr : ai1.data(), ai1.size());
  91. CTR_DRBG_generate(&drbg, out.data(), out.size(),
  92. ai2.empty() ? nullptr : ai2.data(), ai2.size());
  93. EXPECT_EQ(Bytes(expected), Bytes(out));
  94. });
  95. }