getrandom-simulator-test.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /// TPM GetRandom unit tests.
  17. /*! \file */
  18. #include <vector>
  19. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  20. #include "gtest/gtest.h"
  21. #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
  22. #include "epid/common-testhelper/prng-testhelper.h"
  23. #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
  24. extern "C" {
  25. #include "epid/member/tpm2/context.h"
  26. #include "epid/member/tpm2/getrandom.h"
  27. }
  28. namespace {
  29. //////////////////////////////////////////////////////////////////////////
  30. // Tpm2GetRandom Tests
  31. // Test if GetRandom can get deterministic data for known seed
  32. TEST_F(EpidTpm2Test, GetRandomGetsExpectedDataForGivenSeed) {
  33. std::vector<uint8_t> expected_digest = {
  34. 0x76, 0x90, 0x2c, 0x3b, 0xa5, 0x25, 0xd7, 0x1e, 0x66, 0x67, 0xaa, 0xb9,
  35. 0x80, 0x50, 0x9c, 0x17, 0x65, 0x19, 0x06, 0x2a, 0x53, 0x49, 0x7d, 0x1b,
  36. 0xe5, 0xf4, 0xec, 0xf3, 0xf0, 0x69, 0x81, 0xdc, 0x0f, 0x5a, 0x6f, 0x1c,
  37. 0xb3, 0x78, 0xa8, 0xea, 0x6b, 0xab, 0x1d, 0xc7, 0xd6, 0x1a, 0x10, 0x1a};
  38. std::vector<uint8_t> output(48);
  39. Prng my_prng;
  40. my_prng.set_seed(0x1234);
  41. Epid2ParamsObj epid2params;
  42. int num_bits = (int)expected_digest.size() * 8;
  43. Tpm2CtxObj tpm2(&Prng::Generate, &my_prng, NULL, epid2params);
  44. my_prng.set_seed(0x1234);
  45. EXPECT_EQ(kEpidNoErr, Tpm2GetRandom(tpm2, num_bits, output.data()));
  46. EXPECT_EQ(expected_digest, output);
  47. }
  48. } // namespace