Skip to content

Commit

Permalink
fix(eddsa): Fix eddsa private key parsing bug
Browse files Browse the repository at this point in the history
The eddsa private key is encoded as PEM(PKCS#8) format.
While decoding the private key a bug was introduced to generate
a new key from the encoded bytes.
Fixed the issue by decoding the private key with same format.
  • Loading branch information
hanish520 committed Mar 10, 2024
1 parent 301532c commit f8f5c48
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crypto/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,17 @@ func ParsePrivateKey(buf []byte) (key hotstuff.PrivateKey, err error) {
case ecdsacrypto.PrivateKeyFileType:
key, err = x509.ParseECPrivateKey(b.Bytes)
case eddsa.PrivateKeyFileType:
key = ed25519.NewKeyFromSeed(b.Bytes[:32])
//key = ed25519.NewKeyFromSeed(b.Bytes[:32])
var genericKey any
var ok bool
genericKey, err = x509.ParsePKCS8PrivateKey(b.Bytes)
if err != nil {
return nil, err
}

Check warning on line 242 in crypto/keygen/keygen.go

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L241-L242

Added lines #L241 - L242 were not covered by tests
key, ok = genericKey.(ed25519.PrivateKey)
if !ok {
return nil, fmt.Errorf("failed to parse key")
}

Check warning on line 246 in crypto/keygen/keygen.go

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L245-L246

Added lines #L245 - L246 were not covered by tests
case bls12.PrivateKeyFileType:
k := &bls12.PrivateKey{}
k.FromBytes(b.Bytes)
Expand Down

0 comments on commit f8f5c48

Please sign in to comment.