| 1 |
package tpm2test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
. "github.com/google/go-tpm/tpm2" |
| 7 |
"github.com/google/go-tpm/tpm2/transport" |
| 8 |
"github.com/google/go-tpm/tpm2/transport/simulator" |
| 9 |
) |
| 10 |
|
| 11 |
func getDeriver(t *testing.T, thetpm transport.TPM) NamedHandle { |
| 12 |
t.Helper() |
| 13 |
|
| 14 |
cl := CreateLoaded{ |
| 15 |
ParentHandle: TPMRHOwner, |
| 16 |
InPublic: New2BTemplate( |
| 17 |
&TPMTPublic{ |
| 18 |
Type: TPMAlgKeyedHash, |
| 19 |
NameAlg: TPMAlgSHA256, |
| 20 |
ObjectAttributes: TPMAObject{ |
| 21 |
SensitiveDataOrigin: true, |
| 22 |
UserWithAuth: true, |
| 23 |
Decrypt: true, |
| 24 |
Restricted: true, |
| 25 |
}, |
| 26 |
Parameters: NewTPMUPublicParms( |
| 27 |
TPMAlgKeyedHash, |
| 28 |
&TPMSKeyedHashParms{ |
| 29 |
Scheme: TPMTKeyedHashScheme{ |
| 30 |
Scheme: TPMAlgXOR, |
| 31 |
Details: NewTPMUSchemeKeyedHash( |
| 32 |
TPMAlgXOR, |
| 33 |
&TPMSSchemeXOR{ |
| 34 |
HashAlg: TPMAlgSHA256, |
| 35 |
KDF: TPMAlgKDF1SP800108, |
| 36 |
}, |
| 37 |
), |
| 38 |
}, |
| 39 |
}, |
| 40 |
), |
| 41 |
}), |
| 42 |
} |
| 43 |
rsp, err := cl.Execute(thetpm) |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("could not create derivation parent: %v:", err) |
| 46 |
} |
| 47 |
return NamedHandle{ |
| 48 |
Handle: rsp.ObjectHandle, |
| 49 |
Name: rsp.Name, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestCreateLoaded(t *testing.T) { |
| 54 |
thetpm, err := simulator.OpenSimulator() |
| 55 |
if err != nil { |
| 56 |
t.Fatalf("could not connect to TPM simulator: %v", err) |
| 57 |
} |
| 58 |
defer thetpm.Close() |
| 59 |
|
| 60 |
deriver := getDeriver(t, thetpm) |
| 61 |
|
| 62 |
derive := New2B( |
| 63 |
TPMSDerive{ |
| 64 |
Label: TPM2BLabel{ |
| 65 |
Buffer: []byte("label"), |
| 66 |
}, |
| 67 |
Context: TPM2BLabel{ |
| 68 |
Buffer: []byte("context"), |
| 69 |
}, |
| 70 |
}) |
| 71 |
|
| 72 |
createLoadeds := map[string]*CreateLoaded{ |
| 73 |
"PrimaryKey": { |
| 74 |
ParentHandle: TPMRHEndorsement, |
| 75 |
InPublic: New2BTemplate(&ECCEKTemplate), |
| 76 |
}, |
| 77 |
"NoParentPrimaryKey": { |
| 78 |
// Make the object in the null hierarchy and ensure that go-tpm supports not providing the parent handle at all. |
| 79 |
InPublic: New2BTemplate(&ECCEKTemplate), |
| 80 |
}, |
| 81 |
"OrdinaryKey": { |
| 82 |
ParentHandle: TPMRHOwner, |
| 83 |
InSensitive: TPM2BSensitiveCreate{ |
| 84 |
Sensitive: &TPMSSensitiveCreate{ |
| 85 |
UserAuth: TPM2BAuth{ |
| 86 |
Buffer: []byte("p@ssw0rd"), |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
InPublic: New2BTemplate( |
| 91 |
&TPMTPublic{ |
| 92 |
Type: TPMAlgECC, |
| 93 |
NameAlg: TPMAlgSHA256, |
| 94 |
ObjectAttributes: TPMAObject{ |
| 95 |
SensitiveDataOrigin: true, |
| 96 |
UserWithAuth: true, |
| 97 |
SignEncrypt: true, |
| 98 |
}, |
| 99 |
Parameters: NewTPMUPublicParms( |
| 100 |
TPMAlgECC, |
| 101 |
&TPMSECCParms{ |
| 102 |
CurveID: TPMECCNistP256, |
| 103 |
}, |
| 104 |
), |
| 105 |
}), |
| 106 |
}, |
| 107 |
"DataBlob": { |
| 108 |
ParentHandle: TPMRHOwner, |
| 109 |
InSensitive: TPM2BSensitiveCreate{ |
| 110 |
Sensitive: &TPMSSensitiveCreate{ |
| 111 |
UserAuth: TPM2BAuth{ |
| 112 |
Buffer: []byte("p@ssw0rd"), |
| 113 |
}, |
| 114 |
Data: NewTPMUSensitiveCreate(&TPM2BSensitiveData{ |
| 115 |
Buffer: []byte("secrets"), |
| 116 |
}), |
| 117 |
}, |
| 118 |
}, |
| 119 |
InPublic: New2BTemplate( |
| 120 |
&TPMTPublic{ |
| 121 |
Type: TPMAlgKeyedHash, |
| 122 |
NameAlg: TPMAlgSHA256, |
| 123 |
ObjectAttributes: TPMAObject{ |
| 124 |
UserWithAuth: true, |
| 125 |
}, |
| 126 |
}), |
| 127 |
}, |
| 128 |
"Derived": { |
| 129 |
ParentHandle: deriver, |
| 130 |
InSensitive: TPM2BSensitiveCreate{ |
| 131 |
Sensitive: &TPMSSensitiveCreate{ |
| 132 |
UserAuth: TPM2BAuth{ |
| 133 |
Buffer: []byte("p@ssw0rd"), |
| 134 |
}, |
| 135 |
Data: NewTPMUSensitiveCreate(&derive), |
| 136 |
}, |
| 137 |
}, |
| 138 |
InPublic: New2BTemplate( |
| 139 |
&TPMTPublic{ |
| 140 |
Type: TPMAlgECC, |
| 141 |
NameAlg: TPMAlgSHA256, |
| 142 |
ObjectAttributes: TPMAObject{ |
| 143 |
FixedParent: true, |
| 144 |
UserWithAuth: true, |
| 145 |
SignEncrypt: true, |
| 146 |
}, |
| 147 |
Parameters: NewTPMUPublicParms( |
| 148 |
TPMAlgECC, |
| 149 |
&TPMSECCParms{ |
| 150 |
CurveID: TPMECCNistP256, |
| 151 |
}, |
| 152 |
), |
| 153 |
}), |
| 154 |
}, |
| 155 |
} |
| 156 |
|
| 157 |
for name, createLoaded := range createLoadeds { |
| 158 |
t.Run(name, func(t *testing.T) { |
| 159 |
rsp, err := createLoaded.Execute(thetpm) |
| 160 |
if err != nil { |
| 161 |
t.Fatalf("error from CreateLoaded: %v", err) |
| 162 |
} |
| 163 |
if _, err = (FlushContext{FlushHandle: rsp.ObjectHandle}).Execute(thetpm); err != nil { |
| 164 |
t.Errorf("error from FlushContext: %v", err) |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
} |