Skip to content

Commit

Permalink
alloc-less key validity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veorq committed Aug 1, 2019
1 parent bba529e commit e715def
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions types.go
Expand Up @@ -3,6 +3,7 @@ package e4common
import (
"bytes"
"encoding/hex"
"errors"
fmt "fmt"
utf8 "unicode/utf8"

Expand Down Expand Up @@ -50,6 +51,13 @@ const (
PubKey
)

var (
blankEd25519pk [ed25519.PublicKeySize]byte
zeroEd25519pk = blankEd25519pk[:]
blankEd25519sk [ed25519.PrivateKeySize]byte
zeroEd25519sk = blankEd25519sk[:]
)

// ToByte converts a command into its byte representation
func (c *Command) ToByte() byte {
switch *c {
Expand Down Expand Up @@ -130,32 +138,24 @@ func IsValidSymKey(key []byte) error {
// IsValidPrivKey checks that a key is of the expected length and not all zero.
func IsValidPrivKey(key []byte) error {

if len(key) != ed25519.PrivateKeySize {
return fmt.Errorf("Invalid private key length, expected %d, got %d", ed25519.PrivateKeySize, len(key))
if g, w := len(key), ed25519.PrivateKeySize; g != w {
return fmt.Errorf("Invalid private key length, expected %d, got %d", g, w)
}

zeros := make([]byte, ed25519.PublicKeySize)

if bytes.Equal(zeros, key) {
return fmt.Errorf("Invalid public key, all zeros")
if bytes.Equal(zeroEd25519sk, key) {
return errors.New("Invalid private key, all zeros")
}

return nil
}

// IsValidPubKey checks that a key is of the expected length and not all zero.
func IsValidPubKey(key []byte) error {

if len(key) != ed25519.PublicKeySize {
return fmt.Errorf("Invalid public key length, expected %d, got %d", ed25519.PublicKeySize, len(key))
if g, w := len(key), ed25519.PublicKeySize; g != w {
return fmt.Errorf("Invalid public key length, expected %d, got %d", g, w)
}

zeros := make([]byte, ed25519.PublicKeySize)

if bytes.Equal(zeros, key) {
return fmt.Errorf("Invalid public key, all zeros")
if bytes.Equal(zeroEd25519pk, key) {
return errors.New("Invalid public key, all zeros")
}

return nil
}

Expand Down

0 comments on commit e715def

Please sign in to comment.