• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "epid/common-testhelper/epid2params_wrapper-testhelper.h"
23 #include "epid/common-testhelper/prng-testhelper.h"
24 #include "epid/member/tpm2/unittests/tpm2-testhelper.h"
25 
26 extern "C" {
27 #include "epid/member/tpm2/context.h"
28 #include "epid/member/tpm2/getrandom.h"
29 }
30 
31 namespace {
32 //////////////////////////////////////////////////////////////////////////
33 // Tpm2GetRandom Tests
34 
35 // Test if GetRandom can get deterministic data for known seed
TEST_F(EpidTpm2Test,GetRandomGetsExpectedDataForGivenSeed)36 TEST_F(EpidTpm2Test, GetRandomGetsExpectedDataForGivenSeed) {
37   std::vector<uint8_t> expected_digest = {
38       0x76, 0x90, 0x2c, 0x3b, 0xa5, 0x25, 0xd7, 0x1e, 0x66, 0x67, 0xaa, 0xb9,
39       0x80, 0x50, 0x9c, 0x17, 0x65, 0x19, 0x06, 0x2a, 0x53, 0x49, 0x7d, 0x1b,
40       0xe5, 0xf4, 0xec, 0xf3, 0xf0, 0x69, 0x81, 0xdc, 0x0f, 0x5a, 0x6f, 0x1c,
41       0xb3, 0x78, 0xa8, 0xea, 0x6b, 0xab, 0x1d, 0xc7, 0xd6, 0x1a, 0x10, 0x1a};
42   std::vector<uint8_t> output(48);
43   Prng my_prng;
44   my_prng.set_seed(0x1234);
45   Epid2ParamsObj epid2params;
46   int num_bits = (int)expected_digest.size() * 8;
47   Tpm2CtxObj tpm2(&Prng::Generate, &my_prng, NULL, epid2params);
48   my_prng.set_seed(0x1234);
49   EXPECT_EQ(kEpidNoErr, Tpm2GetRandom(tpm2, num_bits, output.data()));
50   EXPECT_EQ(expected_digest, output);
51 }
52 
53 }  // namespace
54