blob: a35e6b084ac54db7f4091ac9147729617626f1a9 [file] [log] [blame]
Robert Sloan572a4e22017-04-17 10:52:19 -07001/* 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"
Robert Sloan9254e682017-04-24 09:42:06 -070021#include "../../test/test_util.h"
Robert Sloan572a4e22017-04-17 10:52:19 -070022
23
24TEST(CTRDRBGTest, Basic) {
25 const uint8_t kSeed[CTR_DRBG_ENTROPY_LEN] = {
26 0xe4, 0xbc, 0x23, 0xc5, 0x08, 0x9a, 0x19, 0xd8, 0x6f, 0x41, 0x19, 0xcb,
27 0x3f, 0xa0, 0x8c, 0x0a, 0x49, 0x91, 0xe0, 0xa1, 0xde, 0xf1, 0x7e, 0x10,
28 0x1e, 0x4c, 0x14, 0xd9, 0xc3, 0x23, 0x46, 0x0a, 0x7c, 0x2f, 0xb5, 0x8e,
29 0x0b, 0x08, 0x6c, 0x6c, 0x57, 0xb5, 0x5f, 0x56, 0xca, 0xe2, 0x5b, 0xad,
30 };
31
32 CTR_DRBG_STATE drbg;
33 ASSERT_TRUE(CTR_DRBG_init(&drbg, kSeed, nullptr, 0));
34
35 const uint8_t kReseed[CTR_DRBG_ENTROPY_LEN] = {
36 0xfd, 0x85, 0xa8, 0x36, 0xbb, 0xa8, 0x50, 0x19, 0x88, 0x1e, 0x8c, 0x6b,
37 0xad, 0x23, 0xc9, 0x06, 0x1a, 0xdc, 0x75, 0x47, 0x76, 0x59, 0xac, 0xae,
38 0xa8, 0xe4, 0xa0, 0x1d, 0xfe, 0x07, 0xa1, 0x83, 0x2d, 0xad, 0x1c, 0x13,
39 0x6f, 0x59, 0xd7, 0x0f, 0x86, 0x53, 0xa5, 0xdc, 0x11, 0x86, 0x63, 0xd6,
40 };
41
42 ASSERT_TRUE(CTR_DRBG_reseed(&drbg, kReseed, nullptr, 0));
43
44 uint8_t out[64];
45 ASSERT_TRUE(CTR_DRBG_generate(&drbg, out, sizeof(out), nullptr, 0));
46 ASSERT_TRUE(CTR_DRBG_generate(&drbg, out, sizeof(out), nullptr, 0));
47
48 const uint8_t kExpected[64] = {
49 0xb2, 0xcb, 0x89, 0x05, 0xc0, 0x5e, 0x59, 0x50, 0xca, 0x31, 0x89,
50 0x50, 0x96, 0xbe, 0x29, 0xea, 0x3d, 0x5a, 0x3b, 0x82, 0xb2, 0x69,
51 0x49, 0x55, 0x54, 0xeb, 0x80, 0xfe, 0x07, 0xde, 0x43, 0xe1, 0x93,
52 0xb9, 0xe7, 0xc3, 0xec, 0xe7, 0x3b, 0x80, 0xe0, 0x62, 0xb1, 0xc1,
53 0xf6, 0x82, 0x02, 0xfb, 0xb1, 0xc5, 0x2a, 0x04, 0x0e, 0xa2, 0x47,
54 0x88, 0x64, 0x29, 0x52, 0x82, 0x23, 0x4a, 0xaa, 0xda,
55 };
56
57 EXPECT_EQ(Bytes(kExpected), Bytes(out));
58
59 CTR_DRBG_clear(&drbg);
60}
61
62TEST(CTRDRBGTest, Large) {
63 const uint8_t kSeed[CTR_DRBG_ENTROPY_LEN] = {0};
64
65 CTR_DRBG_STATE drbg;
66 ASSERT_TRUE(CTR_DRBG_init(&drbg, kSeed, nullptr, 0));
67
68 std::unique_ptr<uint8_t[]> buf(new uint8_t[CTR_DRBG_MAX_GENERATE_LENGTH]);
69 ASSERT_TRUE(CTR_DRBG_generate(&drbg, buf.get(), CTR_DRBG_MAX_GENERATE_LENGTH,
70 nullptr, 0));
71
72 uint8_t digest[SHA256_DIGEST_LENGTH];
73 SHA256(buf.get(), CTR_DRBG_MAX_GENERATE_LENGTH, digest);
74
75 const uint8_t kExpected[SHA256_DIGEST_LENGTH] = {
76 0x69, 0x78, 0x15, 0x96, 0xca, 0xc0, 0x3f, 0x6a, 0x6d, 0xed, 0x22,
77 0x1e, 0x26, 0xd0, 0x75, 0x49, 0xa0, 0x4b, 0x91, 0x58, 0x3c, 0xf4,
78 0xe3, 0x6d, 0xff, 0x41, 0xbf, 0xb9, 0xf8, 0xa8, 0x1c, 0x2b,
79 };
80 EXPECT_EQ(Bytes(kExpected), Bytes(digest));
81
82 CTR_DRBG_clear(&drbg);
83}