• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/random/internal/randen_hwaes.h"
16 
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/base/internal/raw_logging.h"
20 #include "absl/random/internal/platform.h"
21 #include "absl/random/internal/randen_detect.h"
22 #include "absl/random/internal/randen_traits.h"
23 #include "absl/strings/str_format.h"
24 
25 namespace {
26 
27 using absl::random_internal::RandenHwAes;
28 using absl::random_internal::RandenTraits;
29 
30 // Local state parameters.
31 constexpr size_t kSeedBytes =
32     RandenTraits::kStateBytes - RandenTraits::kCapacityBytes;
33 constexpr size_t kStateSizeT = RandenTraits::kStateBytes / sizeof(uint64_t);
34 constexpr size_t kSeedSizeT = kSeedBytes / sizeof(uint32_t);
35 
36 struct alignas(16) randen {
37   uint64_t state[kStateSizeT];
38   uint32_t seed[kSeedSizeT];
39 };
40 
TEST(RandenHwAesTest,Default)41 TEST(RandenHwAesTest, Default) {
42   EXPECT_TRUE(absl::random_internal::CPUSupportsRandenHwAes());
43 
44   constexpr uint64_t kGolden[] = {
45       0x6c6534090ee6d3ee, 0x044e2b9b9d5333c6, 0xc3c14f134e433977,
46       0xdda9f47cd90410ee, 0x887bf3087fd8ca10, 0xf0b780f545c72912,
47       0x15dbb1d37696599f, 0x30ec63baff3c6d59, 0xb29f73606f7f20a6,
48       0x02808a316f49a54c, 0x3b8feaf9d5c8e50e, 0x9cbf605e3fd9de8a,
49       0xc970ae1a78183bbb, 0xd8b2ffd356301ed5, 0xf4b327fe0fc73c37,
50       0xcdfd8d76eb8f9a19, 0xc3a506eb91420c9d, 0xd5af05dd3eff9556,
51       0x48db1bb78f83c4a1, 0x7023920e0d6bfe8c, 0x58d3575834956d42,
52       0xed1ef4c26b87b840, 0x8eef32a23e0b2df3, 0x497cabf3431154fc,
53       0x4e24370570029a8b, 0xd88b5749f090e5ea, 0xc651a582a970692f,
54       0x78fcec2cbb6342f5, 0x463cb745612f55db, 0x352ee4ad1816afe3,
55       0x026ff374c101da7e, 0x811ef0821c3de851,
56   };
57 
58   alignas(16) randen d;
59   memset(d.state, 0, sizeof(d.state));
60   RandenHwAes::Generate(RandenHwAes::GetKeys(), d.state);
61 
62   uint64_t* id = d.state;
63   for (const auto& elem : kGolden) {
64     auto a = absl::StrFormat("%#x", elem);
65     auto b = absl::StrFormat("%#x", *id++);
66     EXPECT_EQ(a, b);
67   }
68 }
69 
70 }  // namespace
71 
main(int argc,char * argv[])72 int main(int argc, char* argv[]) {
73   testing::InitGoogleTest(&argc, argv);
74 
75   ABSL_RAW_LOG(INFO, "ABSL_HAVE_ACCELERATED_AES=%d", ABSL_HAVE_ACCELERATED_AES);
76   ABSL_RAW_LOG(INFO, "ABSL_RANDOM_INTERNAL_AES_DISPATCH=%d",
77                ABSL_RANDOM_INTERNAL_AES_DISPATCH);
78 
79 #if defined(ABSL_ARCH_X86_64)
80   ABSL_RAW_LOG(INFO, "ABSL_ARCH_X86_64");
81 #elif defined(ABSL_ARCH_X86_32)
82   ABSL_RAW_LOG(INFO, "ABSL_ARCH_X86_32");
83 #elif defined(ABSL_ARCH_AARCH64)
84   ABSL_RAW_LOG(INFO, "ABSL_ARCH_AARCH64");
85 #elif defined(ABSL_ARCH_ARM)
86   ABSL_RAW_LOG(INFO, "ABSL_ARCH_ARM");
87 #elif defined(ABSL_ARCH_PPC)
88   ABSL_RAW_LOG(INFO, "ABSL_ARCH_PPC");
89 #else
90   ABSL_RAW_LOG(INFO, "ARCH Unknown");
91 #endif
92 
93   int x = absl::random_internal::HasRandenHwAesImplementation();
94   ABSL_RAW_LOG(INFO, "HasRandenHwAesImplementation = %d", x);
95 
96   int y = absl::random_internal::CPUSupportsRandenHwAes();
97   ABSL_RAW_LOG(INFO, "CPUSupportsRandenHwAes = %d", x);
98 
99   if (!x || !y) {
100     ABSL_RAW_LOG(INFO, "Skipping Randen HWAES tests.");
101     return 0;
102   }
103   return RUN_ALL_TESTS();
104 }
105