Debian Codegraph @

simulator/simulator_test.go

raw
1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17 package simulator
18
19 import (
20 "crypto/rsa"
21 "io"
22 "math/big"
23 "testing"
24
25 "github.com/google/go-tpm-tools/client"
26 "github.com/google/go-tpm/legacy/tpm2"
27 )
28
29 func getSimulator(t *testing.T) *Simulator {
30 t.Helper()
31 simulator, err := Get()
32 if err != nil {
33 t.Fatal(err)
34 }
35 return simulator
36 }
37
38 func getEKModulus(t *testing.T, rwc io.ReadWriteCloser) *big.Int {
39 t.Helper()
40 ek, err := client.EndorsementKeyRSA(rwc)
41 if err != nil {
42 t.Fatal(err)
43 }
44 defer ek.Close()
45
46 return ek.PublicKey().(*rsa.PublicKey).N
47 }
48
49 func TestResetDoesntChangeEK(t *testing.T) {
50 s := getSimulator(t)
51 defer client.CheckedClose(t, s)
52
53 modulus1 := getEKModulus(t, s)
54 if err := s.Reset(); err != nil {
55 t.Fatal(err)
56 }
57 modulus2 := getEKModulus(t, s)
58
59 if modulus1.Cmp(modulus2) != 0 {
60 t.Fatal("Reset() should not change the EK")
61 }
62 }
63 func TestManufactureResetChangesEK(t *testing.T) {
64 s := getSimulator(t)
65 defer client.CheckedClose(t, s)
66
67 modulus1 := getEKModulus(t, s)
68 if err := s.ManufactureReset(); err != nil {
69 t.Fatal(err)
70 }
71 modulus2 := getEKModulus(t, s)
72
73 if modulus1.Cmp(modulus2) == 0 {
74 t.Fatal("ManufactureReset() should change the EK")
75 }
76 }
77
78 func TestGetRandom(t *testing.T) {
79 s := getSimulator(t)
80 defer client.CheckedClose(t, s)
81 result, err := tpm2.GetRandom(s, 10)
82 if err != nil {
83 t.Fatalf("GetRandom: %v", err)
84 }
85 t.Log(result)
86 }
87
88 // The default EK modulus returned by the simulator when using a seed of 0.
89 func zeroSeedModulus() *big.Int {
90 mod := new(big.Int)
91 mod.SetString("16916951631746795233120676661491589156159944041454533323301360736206690950055927665898258850365255777475324525235640153431219834851979041935421083247812345676551677241639541392158486693550125570954276972465867114995062336740464652481116557477039581976647612151813804384773839359390083864432536639577227083497558006614244043011423717921293964465162166865351126036685960128739613171620392174911624095420039156957292384191548425395162459332733115699189854006301807847331248289929021522087915411000598437989788501679617747304391662751900488011803826205901900186771991702576478232121332699862815915856148442279432061762451", 10)
92 return mod
93 }
94
95 func TestFixedSeedExpectedModulus(t *testing.T) {
96 s, err := GetWithFixedSeedInsecure(0)
97 if err != nil {
98 t.Fatal(err)
99 }
100 defer client.CheckedClose(t, s)
101
102 modulus := getEKModulus(t, s)
103 if modulus.Cmp(zeroSeedModulus()) != 0 {
104 t.Fatalf("getEKModulus() = %v, want %v", modulus, zeroSeedModulus())
105 }
106 }
107
108 func TestDifferentSeedDifferentModulus(t *testing.T) {
109 s, err := GetWithFixedSeedInsecure(1)
110 if err != nil {
111 t.Fatal(err)
112 }
113 defer client.CheckedClose(t, s)
114
115 modulus := getEKModulus(t, s)
116 if modulus.Cmp(zeroSeedModulus()) == 0 {
117 t.Fatalf("Moduli should not be equal when using different seeds")
118 }
119 }