Skip to content

Commit

Permalink
fix: use x509 marshaling and parsing also for ed25519 keys
Browse files Browse the repository at this point in the history
  • Loading branch information
meling committed Mar 10, 2024
1 parent 5b1fa0b commit 8928cbc
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions crypto/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ func PrivateKeyToPEM(key hotstuff.PrivateKey) ([]byte, error) {
return nil, err
}
keyType = ecdsacrypto.PrivateKeyFileType
case ed25519.PrivateKey:
marshaled, err = x509.MarshalPKCS8PrivateKey(k)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L118-L119

Added lines #L118 - L119 were not covered by tests
keyType = eddsa.PrivateKeyFileType
case *bls12.PrivateKey:
marshaled = k.ToBytes()
keyType = bls12.PrivateKeyFileType
case ed25519.PrivateKey:
marshaled = make([]byte, ed25519.PrivateKeySize)
copy(marshaled, k)
keyType = eddsa.PrivateKeyFileType
}

b := &pem.Block{
Type: keyType,
Bytes: marshaled,
Expand Down Expand Up @@ -162,20 +165,21 @@ func PublicKeyToPEM(key hotstuff.PublicKey) ([]byte, error) {
return nil, err
}
keyType = ecdsacrypto.PublicKeyFileType
case ed25519.PublicKey:
marshaled, err = x509.MarshalPKIXPublicKey(k)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L171-L172

Added lines #L171 - L172 were not covered by tests
keyType = eddsa.PublicKeyFileType
case *bls12.PublicKey:
marshaled = k.ToBytes()
keyType = bls12.PublicKeyFileType
case ed25519.PublicKey:
marshaled = make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(marshaled, k)
keyType = eddsa.PublicKeyFileType
}

b := &pem.Block{
Type: keyType,
Bytes: marshaled,
}

return pem.EncodeToMemory(b), nil
}

Expand Down Expand Up @@ -228,12 +232,12 @@ func ParsePrivateKey(buf []byte) (key hotstuff.PrivateKey, err error) {
switch b.Type {
case ecdsacrypto.PrivateKeyFileType:
key, err = x509.ParseECPrivateKey(b.Bytes)
case eddsa.PrivateKeyFileType:
key = ed25519.NewKeyFromSeed(b.Bytes[:32])
case bls12.PrivateKeyFileType:
k := &bls12.PrivateKey{}
k.FromBytes(b.Bytes)
key = k
case eddsa.PrivateKeyFileType:
key = ed25519.NewKeyFromSeed(b.Bytes[:32])
default:
return nil, fmt.Errorf("private key file type did not match any known types %v", b.Type)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L242

Added line #L242 was not covered by tests
}
Expand Down Expand Up @@ -261,17 +265,15 @@ func ParsePublicKey(buf []byte) (key hotstuff.PublicKey, err error) {
switch b.Type {
case ecdsacrypto.PublicKeyFileType:
key, err = x509.ParsePKIXPublicKey(b.Bytes)
case eddsa.PublicKeyFileType:
key, err = x509.ParsePKIXPublicKey(b.Bytes)
case bls12.PublicKeyFileType:
k := &bls12.PublicKey{}
err = k.FromBytes(b.Bytes)
if err != nil {
return nil, err
}
key = k
case eddsa.PublicKeyFileType:
k := make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(k, b.Bytes)
key = k
default:
return nil, fmt.Errorf("public key file type did not match any known types %v", b.Type)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L278

Added line #L278 was not covered by tests
}
Expand Down

0 comments on commit 8928cbc

Please sign in to comment.