randen_hwaes_test.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "absl/random/internal/randen_hwaes.h"
  15. #include "gmock/gmock.h"
  16. #include "gtest/gtest.h"
  17. #include "absl/base/internal/raw_logging.h"
  18. #include "absl/random/internal/platform.h"
  19. #include "absl/random/internal/randen_detect.h"
  20. #include "absl/random/internal/randen_traits.h"
  21. #include "absl/strings/str_format.h"
  22. namespace {
  23. using absl::random_internal::RandenHwAes;
  24. using absl::random_internal::RandenTraits;
  25. TEST(RandenHwAesTest, Default) {
  26. EXPECT_TRUE(absl::random_internal::CPUSupportsRandenHwAes());
  27. constexpr uint8_t kGolden[] = {
  28. 0xee, 0xd3, 0xe6, 0x0e, 0x09, 0x34, 0x65, 0x6c, 0xc6, 0x33, 0x53, 0x9d,
  29. 0x9b, 0x2b, 0x4e, 0x04, 0x77, 0x39, 0x43, 0x4e, 0x13, 0x4f, 0xc1, 0xc3,
  30. 0xee, 0x10, 0x04, 0xd9, 0x7c, 0xf4, 0xa9, 0xdd, 0x10, 0xca, 0xd8, 0x7f,
  31. 0x08, 0xf3, 0x7b, 0x88, 0x12, 0x29, 0xc7, 0x45, 0xf5, 0x80, 0xb7, 0xf0,
  32. 0x9f, 0x59, 0x96, 0x76, 0xd3, 0xb1, 0xdb, 0x15, 0x59, 0x6d, 0x3c, 0xff,
  33. 0xba, 0x63, 0xec, 0x30, 0xa6, 0x20, 0x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
  48. 0x7e, 0xda, 0x01, 0xc1, 0x74, 0xf3, 0x6f, 0x02, 0x51, 0xe8, 0x3d, 0x1c,
  49. 0x82, 0xf0, 0x1e, 0x81,
  50. };
  51. alignas(16) uint8_t state[RandenTraits::kStateBytes];
  52. std::memset(state, 0, sizeof(state));
  53. RandenHwAes::Generate(RandenHwAes::GetKeys(), state);
  54. EXPECT_EQ(0, std::memcmp(state, kGolden, sizeof(state)));
  55. }
  56. } // namespace
  57. int main(int argc, char* argv[]) {
  58. testing::InitGoogleTest(&argc, argv);
  59. ABSL_RAW_LOG(INFO, "ABSL_HAVE_ACCELERATED_AES=%d", ABSL_HAVE_ACCELERATED_AES);
  60. ABSL_RAW_LOG(INFO, "ABSL_RANDOM_INTERNAL_AES_DISPATCH=%d",
  61. ABSL_RANDOM_INTERNAL_AES_DISPATCH);
  62. #if defined(ABSL_ARCH_X86_64)
  63. ABSL_RAW_LOG(INFO, "ABSL_ARCH_X86_64");
  64. #elif defined(ABSL_ARCH_X86_32)
  65. ABSL_RAW_LOG(INFO, "ABSL_ARCH_X86_32");
  66. #elif defined(ABSL_ARCH_AARCH64)
  67. ABSL_RAW_LOG(INFO, "ABSL_ARCH_AARCH64");
  68. #elif defined(ABSL_ARCH_ARM)
  69. ABSL_RAW_LOG(INFO, "ABSL_ARCH_ARM");
  70. #elif defined(ABSL_ARCH_PPC)
  71. ABSL_RAW_LOG(INFO, "ABSL_ARCH_PPC");
  72. #else
  73. ABSL_RAW_LOG(INFO, "ARCH Unknown");
  74. #endif
  75. int x = absl::random_internal::HasRandenHwAesImplementation();
  76. ABSL_RAW_LOG(INFO, "HasRandenHwAesImplementation = %d", x);
  77. int y = absl::random_internal::CPUSupportsRandenHwAes();
  78. ABSL_RAW_LOG(INFO, "CPUSupportsRandenHwAes = %d", x);
  79. if (!x || !y) {
  80. ABSL_RAW_LOG(INFO, "Skipping Randen HWAES tests.");
  81. return 0;
  82. }
  83. return RUN_ALL_TESTS();
  84. }