Skip to content

Commit

Permalink
fix: type casting to ed25519.PublicKey now works as expected
Browse files Browse the repository at this point in the history
We now use ed25519.PublicKey directly instead of []byte.
The ed25519.PublicKey is a type alias for []byte, but it is best
to use the correct type.
  • Loading branch information
meling committed Mar 10, 2024
1 parent a1b98e6 commit 5b1fa0b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
6 changes: 1 addition & 5 deletions crypto/eddsa/eddsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ func (ed *eddsaBase) verifySingle(sig *Signature, message []byte) bool {
ed.logger.Warnf("eddsaBase: got signature from replica whose ID (%d) was not in the config.", sig.Signer())
return false
}

Check warning on line 175 in crypto/eddsa/eddsa.go

View check run for this annotation

Codecov / codecov/patch

crypto/eddsa/eddsa.go#L173-L175

Added lines #L173 - L175 were not covered by tests
pk, ok := replica.PublicKey().([]byte)
if !ok {
ed.logger.Infof("eddsaBase: got public key from replica that was not of type []byte.")
pk = replica.PublicKey().(ed25519.PublicKey)
}
pk := replica.PublicKey().(ed25519.PublicKey)
return ed25519.Verify(pk, message, sig.sign)
}
19 changes: 9 additions & 10 deletions crypto/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GenerateECDSAPrivateKey() (pk *ecdsa.PrivateKey, err error) {
return pk, nil
}

// GenerateED25519Key generates 25519 key
// GenerateED25519Key generates ed25519 key.
func GenerateED25519Key() (pub ed25519.PublicKey, pk ed25519.PrivateKey, err error) {
pub, pk, err = ed25519.GenerateKey(rand.Reader)
if err != nil {
Expand Down Expand Up @@ -129,7 +129,7 @@ func PrivateKeyToPEM(key hotstuff.PrivateKey) ([]byte, error) {

// WritePrivateKeyFile writes a private key to the specified file.
func WritePrivateKeyFile(key hotstuff.PrivateKey, filePath string) (err error) {
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L132

Added line #L132 was not covered by tests
if err != nil {
return
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func PublicKeyToPEM(key hotstuff.PublicKey) ([]byte, error) {
marshaled = k.ToBytes()
keyType = bls12.PublicKeyFileType
case ed25519.PublicKey:
marshaled = make([]byte, ed25519.PublicKeySize)
marshaled = make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(marshaled, k)
keyType = eddsa.PublicKeyFileType
}
Expand All @@ -181,7 +181,7 @@ func PublicKeyToPEM(key hotstuff.PublicKey) ([]byte, error) {

// WritePublicKeyFile writes a public key to the specified file.
func WritePublicKeyFile(key hotstuff.PublicKey, filePath string) (err error) {
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L184

Added line #L184 was not covered by tests
if err != nil {
return
}
Expand All @@ -203,7 +203,7 @@ func WritePublicKeyFile(key hotstuff.PublicKey, filePath string) (err error) {

// WriteCertFile writes an x509 certificate to a file.
func WriteCertFile(cert *x509.Certificate, file string) (err error) {
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L206

Added line #L206 was not covered by tests
if err != nil {
return
}
Expand Down Expand Up @@ -233,10 +233,9 @@ func ParsePrivateKey(buf []byte) (key hotstuff.PrivateKey, err error) {
k.FromBytes(b.Bytes)
key = k
case eddsa.PrivateKeyFileType:
k := ed25519.NewKeyFromSeed(b.Bytes[:32])
key = k
key = ed25519.NewKeyFromSeed(b.Bytes[:32])
default:
return nil, fmt.Errorf("file type did not match any known types %v", b.Type)
return nil, fmt.Errorf("private key file type did not match any known types %v", b.Type)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L238

Added line #L238 was not covered by tests
}
if err != nil {
return nil, fmt.Errorf("failed to parse key: %w", err)
Expand Down Expand Up @@ -270,11 +269,11 @@ func ParsePublicKey(buf []byte) (key hotstuff.PublicKey, err error) {
}
key = k
case eddsa.PublicKeyFileType:
k := make([]byte, ed25519.PublicKeySize)
k := make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(k, b.Bytes)
key = k
default:
return nil, fmt.Errorf("file type did not match any known types %v", b.Type)
return nil, fmt.Errorf("public key file type did not match any known types %v", b.Type)

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

View check run for this annotation

Codecov / codecov/patch

crypto/keygen/keygen.go#L276

Added line #L276 was not covered by tests
}
if err != nil {
return nil, fmt.Errorf("failed to parse key: %w", err)
Expand Down

0 comments on commit 5b1fa0b

Please sign in to comment.