-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Morten Linderud <morten@linderud.pw>
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package tpm2test | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"testing" | ||
|
||
. "github.com/google/go-tpm/tpm2" | ||
"github.com/google/go-tpm/tpm2/transport/simulator" | ||
) | ||
|
||
func TestImport(t *testing.T) { | ||
thetpm, err := simulator.OpenSimulator() | ||
if err != nil { | ||
t.Fatalf("could not connect to TPM simulator: %v", err) | ||
} | ||
defer thetpm.Close() | ||
|
||
srkCreate := CreatePrimary{ | ||
PrimaryHandle: TPMRHOwner, | ||
InPublic: New2B(ECCSRKTemplate), | ||
} | ||
|
||
srkCreateRsp, err := srkCreate.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("could not generate SRK: %v", err) | ||
} | ||
defer func() { | ||
flush := FlushContext{ | ||
FlushHandle: srkCreateRsp.ObjectHandle, | ||
} | ||
_, err := flush.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("could not flush SRK: %v", err) | ||
} | ||
}() | ||
|
||
pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("failed to generate ecdsa key: %v", err) | ||
} | ||
|
||
sens2B := Marshal(TPMTSensitive{ | ||
SensitiveType: TPMAlgECC, | ||
Sensitive: NewTPMUSensitiveComposite( | ||
TPMAlgECC, | ||
&TPM2BECCParameter{Buffer: pk.D.FillBytes(make([]byte, 32))}, | ||
), | ||
}) | ||
|
||
l := Marshal(TPM2BPrivate{Buffer: sens2B}) | ||
|
||
_, err = Import{ | ||
ParentHandle: &AuthHandle{ | ||
Handle: srkCreateRsp.ObjectHandle, | ||
Name: srkCreateRsp.Name, | ||
Auth: PasswordAuth(nil), | ||
}, | ||
Duplicate: TPM2BPrivate{Buffer: l}, | ||
ObjectPublic: New2B(TPMTPublic{ | ||
Type: TPMAlgECC, | ||
NameAlg: TPMAlgSHA256, | ||
ObjectAttributes: TPMAObject{ | ||
SignEncrypt: true, | ||
SensitiveDataOrigin: false, | ||
EncryptedDuplication: false, | ||
}, | ||
Parameters: NewTPMUPublicParms( | ||
TPMAlgECC, | ||
&TPMSECCParms{ | ||
CurveID: TPMECCNistP256, | ||
Scheme: TPMTECCScheme{ | ||
Scheme: TPMAlgECDSA, | ||
Details: NewTPMUAsymScheme( | ||
TPMAlgECDSA, | ||
&TPMSSigSchemeECDSA{ | ||
HashAlg: TPMAlgSHA256, | ||
}, | ||
), | ||
}, | ||
}, | ||
), | ||
Unique: NewTPMUPublicID( | ||
TPMAlgECC, | ||
&TPMSECCPoint{ | ||
X: TPM2BECCParameter{ | ||
Buffer: pk.X.FillBytes(make([]byte, 32)), | ||
}, | ||
Y: TPM2BECCParameter{ | ||
Buffer: pk.Y.FillBytes(make([]byte, 32)), | ||
}, | ||
}, | ||
), | ||
}), | ||
}.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("could not import: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters