|
|
@ -183,20 +183,14 @@ type KeyPair struct { |
|
|
|
// Validate checks the PublicKey matches the PrivateKey,
|
|
|
|
// Validate checks the PublicKey matches the PrivateKey,
|
|
|
|
// and sets the PublicKey if missing
|
|
|
|
// and sets the PublicKey if missing
|
|
|
|
func (kp *KeyPair) Validate() error { |
|
|
|
func (kp *KeyPair) Validate() error { |
|
|
|
keyLen := len(kp.PrivateKey) |
|
|
|
|
|
|
|
pubLen := len(kp.PublicKey) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case keyLen != PrivateKeySize: |
|
|
|
case kp.PrivateKey.IsZero(): |
|
|
|
// bad private key
|
|
|
|
// no private key
|
|
|
|
return ErrInvalidPrivateKey |
|
|
|
return ErrInvalidPrivateKey |
|
|
|
case pubLen == 0: |
|
|
|
case kp.PublicKey.IsZero(): |
|
|
|
// no public key, set it
|
|
|
|
// no public key, set it
|
|
|
|
kp.PublicKey = kp.PrivateKey.Public() |
|
|
|
kp.PublicKey = kp.PrivateKey.Public() |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
case pubLen != PublicKeySize: |
|
|
|
|
|
|
|
// bad public key
|
|
|
|
|
|
|
|
return ErrInvalidPublicKey |
|
|
|
|
|
|
|
case !kp.PrivateKey.Public().Equal(kp.PublicKey): |
|
|
|
case !kp.PrivateKey.Public().Equal(kp.PublicKey): |
|
|
|
// wrong public key
|
|
|
|
// wrong public key
|
|
|
|
return ErrInvalidPublicKey |
|
|
|
return ErrInvalidPublicKey |
|
|
|