forked from google/go-tpm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented and Tested TPM2_Sign (google#282)
* Implemented and Tested TPM2_Sign * Fixed nits from PR Co-authored-by: Matthew Tsai <matthewtsai@google.com>
- Loading branch information
Showing
5 changed files
with
211 additions
and
5 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
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
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
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,161 @@ | ||
package tpm2 | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/google/go-tpm/direct/helpers" | ||
"github.com/google/go-tpm/direct/structures/tpm" | ||
"github.com/google/go-tpm/direct/structures/tpm2b" | ||
"github.com/google/go-tpm/direct/structures/tpma" | ||
"github.com/google/go-tpm/direct/structures/tpml" | ||
"github.com/google/go-tpm/direct/structures/tpms" | ||
"github.com/google/go-tpm/direct/structures/tpmt" | ||
"github.com/google/go-tpm/direct/structures/tpmu" | ||
"github.com/google/go-tpm/direct/transport/simulator" | ||
"github.com/google/go-tpm/tpmutil" | ||
) | ||
|
||
func CreatePCRSelection(s []int) ([]byte, error) { | ||
|
||
const sizeOfPCRSelect = 3 | ||
|
||
PCRs := make(tpmutil.RawBytes, sizeOfPCRSelect) | ||
|
||
for _, n := range s { | ||
if n >= 8*sizeOfPCRSelect { | ||
return nil, fmt.Errorf("PCR index %d is out of range (exceeds maximum value %d)", n, 8*sizeOfPCRSelect-1) | ||
} | ||
byteNum := n / 8 | ||
bytePos := byte(1 << (n % 8)) | ||
PCRs[byteNum] |= bytePos | ||
} | ||
|
||
return PCRs, nil | ||
} | ||
|
||
func TestCreatePCRSelection(t *testing.T) { | ||
|
||
emptyTest, err := CreatePCRSelection([]int{}) | ||
if err != nil { | ||
t.Fatalf("Failed to create PCRSelection") | ||
} | ||
|
||
if !cmp.Equal(emptyTest, []byte{0, 0, 0}) { | ||
t.Fatalf("emptyTest does not return valid PCRs") | ||
} | ||
|
||
filledTest, err := CreatePCRSelection([]int{0, 1, 2}) | ||
if err != nil { | ||
t.Fatalf("Failed to create PCRSelection") | ||
} | ||
|
||
if !cmp.Equal(filledTest, []byte{7, 0, 0}) { | ||
t.Fatalf("filledTest does not return valid PCRs") | ||
} | ||
} | ||
|
||
func TestSign(t *testing.T) { | ||
|
||
thetpm, err := simulator.OpenSimulator() | ||
if err != nil { | ||
t.Fatalf("could not connect to TPM simulator: %v", err) | ||
} | ||
defer thetpm.Close() | ||
|
||
PCR7, err := CreatePCRSelection([]int{7}) | ||
if err != nil { | ||
t.Fatalf("Failed to create PCRSelection") | ||
} | ||
|
||
createPrimary := CreatePrimary{ | ||
PrimaryHandle: tpm.RHOwner, | ||
|
||
InPublic: tpm2b.Public{ | ||
PublicArea: tpmt.Public{ | ||
Type: tpm.AlgRSA, | ||
NameAlg: tpm.AlgSHA256, | ||
ObjectAttributes: tpma.Object{ | ||
SignEncrypt: true, | ||
FixedTPM: true, | ||
FixedParent: true, | ||
SensitiveDataOrigin: true, | ||
UserWithAuth: true, | ||
}, | ||
Parameters: tpmu.PublicParms{ | ||
RSADetail: &tpms.RSAParms{ | ||
Scheme: tpmt.RSAScheme{ | ||
Scheme: tpm.AlgRSASSA, | ||
Details: tpmu.AsymScheme{ | ||
RSASSA: &tpms.SigSchemeRSASSA{ | ||
HashAlg: tpm.AlgSHA256, | ||
}, | ||
}, | ||
}, | ||
KeyBits: 2048, | ||
}, | ||
}, | ||
}, | ||
}, | ||
CreationPCR: tpml.PCRSelection{ | ||
PCRSelections: []tpms.PCRSelection{ | ||
{ | ||
Hash: tpm.AlgSHA1, | ||
PCRSelect: PCR7, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
rspCP, err := createPrimary.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("could not create key: %v", err) | ||
} | ||
|
||
flushContext := FlushContext{FlushHandle: rspCP.ObjectHandle} | ||
defer flushContext.Execute(thetpm) | ||
|
||
digest := sha256.Sum256([]byte("migrationpains")) | ||
|
||
sign := Sign{ | ||
KeyHandle: NamedHandle{ | ||
Handle: rspCP.ObjectHandle, | ||
Name: rspCP.Name, | ||
}, | ||
Digest: tpm2b.Digest{ | ||
Buffer: digest[:], | ||
}, | ||
InScheme: tpmt.SigScheme{ | ||
Scheme: tpm.AlgRSASSA, | ||
Details: tpmu.SigScheme{ | ||
RSASSA: &tpms.SchemeHash{ | ||
HashAlg: tpm.AlgSHA256, | ||
}, | ||
}, | ||
}, | ||
Validation: tpmt.TKHashCheck{ | ||
Tag: tpm.STHashCheck, | ||
}, | ||
} | ||
|
||
rspSign, err := sign.Execute(thetpm) | ||
if err != nil { | ||
t.Fatalf("Failed to Sign Digest: %v", err) | ||
} | ||
|
||
pub := rspCP.OutPublic.PublicArea | ||
rsaPub, err := helpers.RSAPub(pub.Parameters.RSADetail, pub.Unique.RSA) | ||
if err != nil { | ||
t.Fatalf("Failed to retrive Public Key: %v", err) | ||
} | ||
|
||
if err := rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, digest[:], rspSign.Signature.Signature.RSASSA.Sig.Buffer); err != nil { | ||
t.Errorf("Signature verification failed: %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