Skip to content

Commit

Permalink
sec: Make GenerateSig() return a Sig object
Browse files Browse the repository at this point in the history
Before this commit, this function returned a []byte slice containing the
raw signature.  Now the function returns a Sig object (which also
contains the sig type and key hash).
  • Loading branch information
ccollins476ad committed Oct 24, 2019
1 parent 267010c commit 6a0584f
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions image/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,43 @@ func GenerateSigEd25519(key sec.PrivSignKey, hash []byte) ([]byte, error) {
return sig, nil
}

func GenerateSig(key sec.PrivSignKey, hash []byte) ([]byte, error) {
key.AssertValid()
func GenerateSig(key sec.PrivSignKey, hash []byte) (sec.Sig, error) {
pub := key.PubKey()
typ, err := pub.SigType()
if err != nil {
return sec.Sig{}, err
}

if key.Rsa != nil {
return GenerateSigRsa(key, hash)
} else if key.Ec != nil {
return GenerateSigEc(key, hash)
} else {
return GenerateSigEd25519(key, hash)
var data []byte

switch typ {
case sec.SIG_TYPE_RSA2048, sec.SIG_TYPE_RSA3072:
data, err = GenerateSigRsa(key, hash)

case sec.SIG_TYPE_ECDSA224, sec.SIG_TYPE_ECDSA256:
data, err = GenerateSigEc(key, hash)

case sec.SIG_TYPE_ED25519:
data, err = GenerateSigEd25519(key, hash)

default:
err = errors.Errorf("unknown sig type: %v", typ)
}

if err != nil {
return sec.Sig{}, err
}

keyHash, err := pub.Hash()
if err != nil {
return sec.Sig{}, err
}

return sec.Sig{
Type: typ,
KeyHash: keyHash,
Data: data,
}, nil
}

func BuildKeyHashTlv(keyBytes []byte) ImageTlv {
Expand Down Expand Up @@ -243,9 +270,9 @@ func BuildSigTlvs(keys []sec.PrivSignKey, hash []byte) ([]ImageTlv, error) {
tlv = ImageTlv{
Header: ImageTlvHdr{
Type: sigTlvType(key),
Len: uint16(len(sig)),
Len: uint16(len(sig.Data)),
},
Data: sig,
Data: sig.Data,
}
tlvs = append(tlvs, tlv)
}
Expand Down

0 comments on commit 6a0584f

Please sign in to comment.