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