Skip to content

Commit

Permalink
bootenv/keydata_test.go: add test for deterministic derivation of ell…
Browse files Browse the repository at this point in the history
…iptic key
  • Loading branch information
sespiros committed Feb 20, 2024
1 parent 2e4c29a commit f2ffc65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bootenv/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (d *KeyDataScope) TestMatch(KDFAlg crypto.Hash, keyIdentifier []byte) bool
return bytes.Equal(h.Sum(nil), keyIdentifier)
}

func (d *KeyDataScope) DeriveSigner(key secboot.PrimaryKey, role string) (crypto.Signer, error) {
return d.deriveSigner(key, role)
}

func NewHashAlg(alg crypto.Hash) hashAlg {
return hashAlg(alg)
}
Expand Down
38 changes: 38 additions & 0 deletions bootenv/keydata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package bootenv_test

import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"encoding/base64"
"encoding/json"
Expand Down Expand Up @@ -681,3 +682,40 @@ func (s *keyDataPlatformSuite) TestEcdsaPublicKeyUnmarshalJSONInvalid(c *C) {
err = unmarshalledPk.UnmarshalJSON(pkBytes)
c.Check(err, ErrorMatches, "invalid key type")
}

func (s *keyDataPlatformSuite) TestDeriveSigner(c *C) {
primaryKey := s.newPrimaryKey(c, 32)
role := "test"

params := &KeyDataScopeParams{
PrimaryKey: primaryKey,
Role: role,
KDFAlg: crypto.SHA256,
MDAlg: crypto.SHA256,
ModelAlg: crypto.SHA256,
}

kds, err := NewKeyDataScope(params)
c.Assert(err, IsNil)
c.Check(kds, NotNil)

err = kds.IsBootEnvironmentAuthorized()
c.Check(err, IsNil)

signer, err := kds.DeriveSigner(primaryKey, role)
c.Assert(err, IsNil)

prevKey, ok := signer.(*ecdsa.PrivateKey)
c.Assert(ok, Equals, true)

for i := 0; i < 10; i++ {
signer, err := kds.DeriveSigner(primaryKey, role)
c.Assert(err, IsNil)

key, ok := signer.(*ecdsa.PrivateKey)
c.Assert(ok, Equals, true)
c.Check(key.Equal(prevKey), Equals, true)
prevKey = key
}

}

0 comments on commit f2ffc65

Please sign in to comment.