Skip to content

Commit

Permalink
use secure enclave public key, tweaks (#35)
Browse files Browse the repository at this point in the history
* use secure enclave public key, tweaks
  • Loading branch information
James-Pickett authored Dec 29, 2023
1 parent 5859599 commit db516b7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/challenge/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type OuterChallenge struct {
}

func (o *OuterChallenge) Verify(counterParty ecdsa.PublicKey) error {
if err := echelper.VerifySignature(counterParty, o.Msg, o.Sig); err != nil {
if err := echelper.VerifySignature(&counterParty, o.Msg, o.Sig); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/challenge/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func verifyWithKeyBytes(keyBytes []byte, msg []byte, sig []byte) error {
return fmt.Errorf("parsing public key: %w", err)
}

return echelper.VerifySignature(*key, msg, sig)
return echelper.VerifySignature(key, msg, sig)
}

type InnerResponse struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/echelper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func Sign(signer crypto.Signer, data []byte) ([]byte, error) {
digest, err := hashForSignature(data)
digest, err := HashForSignature(data)
if err != nil {
return nil, fmt.Errorf("hashing data: %w", err)
}
Expand All @@ -31,13 +31,13 @@ func Sign(signer crypto.Signer, data []byte) ([]byte, error) {
return signature, nil
}

func VerifySignature(counterParty ecdsa.PublicKey, data []byte, signature []byte) error {
digest, err := hashForSignature(data)
func VerifySignature(counterParty *ecdsa.PublicKey, data []byte, signature []byte) error {
digest, err := HashForSignature(data)
if err != nil {
return fmt.Errorf("hashing inner box: %w", err)
}

if !ecdsa.VerifyASN1(&counterParty, digest, signature) {
if !ecdsa.VerifyASN1(counterParty, digest, signature) {
return fmt.Errorf("invalid signature")
}

Expand Down Expand Up @@ -135,7 +135,7 @@ func SignWithTimeout(signer crypto.Signer, data []byte, duration, interval time.
}
}

func hashForSignature(data []byte) ([]byte, error) {
func HashForSignature(data []byte) ([]byte, error) {
hash := sha256.New()
_, err := hash.Write(data)
if err != nil {
Expand Down
23 changes: 14 additions & 9 deletions pkg/secureenclave/secureenclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ type SecureEnclaveSigner struct {

// New verifies that the provided public key already exists in the secure enclave.
// Then returns a new Secure Enclave Keyer using the provided public key.
func New(publicKeySha1 []byte) (*SecureEnclaveSigner, error) {
pubKey, err := findKey(publicKeySha1)
func New(pubKey *ecdsa.PublicKey) (*SecureEnclaveSigner, error) {
if pubKey == nil {
return nil, errors.New("nil public key")
}

lookUp, err := publicKeyLookUpHash(pubKey)
if err != nil {
return nil, err
}

pubKey, err = findKey(lookUp)
if err != nil {
return nil, fmt.Errorf("finding existing public key: %w", err)
}
Expand Down Expand Up @@ -72,19 +81,15 @@ func (s *SecureEnclaveSigner) Sign(rand io.Reader, digest []byte, opts crypto.Si
return result, nil
}

// CreateKey creates a new secure enclave key and returns the hash used to access it.
func CreateKey() ([]byte, error) {
// CreateKey creates a new secure enclave key and returns the public key.
func CreateKey() (*ecdsa.PublicKey, error) {
wrapper := C.wrapCreateKey()
result, err := unwrap(wrapper)
if err != nil {
return nil, err
}

sha1 := sha1.New()
if _, err := sha1.Write(result); err != nil {
return nil, fmt.Errorf("hashing secure enclave create key result to sha1: %w", err)
}
return sha1.Sum(nil), nil
return rawToEcdsa(result), nil
}

// unwrap a Wrapper struct to a Go byte slice
Expand Down
2 changes: 1 addition & 1 deletion pkg/secureenclave/secureenclave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestSecureEnclaveSigning(t *testing.T) {

publicKey := seSigner.Public().(*ecdsa.PublicKey)

require.NoError(t, echelper.VerifySignature(*publicKey, dataToSign, signature))
require.NoError(t, echelper.VerifySignature(publicKey, dataToSign, signature))
}

func TestSecureEnclaveErrors(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tpm/tpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestTpmSigning(t *testing.T) {

publicKey := tpmSigner.Public().(*ecdsa.PublicKey)

require.NoError(t, echelper.VerifySignature(*publicKey, dataToSign, signature))
require.NoError(t, echelper.VerifySignature(publicKey, dataToSign, signature))
}

func TestTpmErrors(t *testing.T) {
Expand Down

0 comments on commit db516b7

Please sign in to comment.