diff --git a/ed25519.go b/ed25519.go index cb56a70..0104beb 100644 --- a/ed25519.go +++ b/ed25519.go @@ -3,12 +3,12 @@ package pki import ( "bytes" "crypto" + "crypto/ed25519" "crypto/rand" "encoding/pem" "errors" + "fmt" "io" - - "github.com/agl/ed25519" ) const ( @@ -17,11 +17,11 @@ const ( type ( Ed25519PrivateKey struct { - private_key [ed25519.PrivateKeySize]byte + private_key ed25519.PrivateKey } Ed25519PublicKey struct { - public_key [ed25519.PublicKeySize]byte + public_key ed25519.PublicKey } ) @@ -31,16 +31,16 @@ func NewPrivateKeyEd25519() (*Ed25519PrivateKey, error) { if err != nil { return nil, err } - return &Ed25519PrivateKey{*pr_raw}, nil + return &Ed25519PrivateKey{pr_raw}, nil } // Restore an ed25519 private key from a raw byte stream. // TODO does this have to be asn1? all other functions expect asn1 func LoadPrivateKeyEd25519(raw []byte) (*Ed25519PrivateKey, error) { - var pr_loaded [ed25519.PrivateKeySize]byte - length := copy(pr_loaded[:], raw) + pr_loaded := make([]byte, ed25519.PrivateKeySize) + length := copy(pr_loaded, raw) if length != ed25519.PrivateKeySize { - return nil, errors.New("private key length incorrect") + return nil, fmt.Errorf("private key length incorrect - got: %d - expected: %d", length, ed25519.PrivateKeySize) } return &Ed25519PrivateKey{pr_loaded}, nil } @@ -57,14 +57,14 @@ func (pr *Ed25519PrivateKey) Public() PublicKey { if err != nil { return nil } - return &Ed25519PublicKey{*pu_raw} + return &Ed25519PublicKey{pu_raw} } // Hash the message given the hash algorythm and sign the hash using the private key. func (pr *Ed25519PrivateKey) Sign(message []byte, hash crypto.Hash) ([]byte, error) { hashed_message := hash.New() hashed_message.Write(message) - result := ed25519.Sign(&pr.private_key, hashed_message.Sum(nil))[:] + result := ed25519.Sign(pr.private_key, hashed_message.Sum(nil))[:] return result, nil } @@ -84,8 +84,8 @@ func (pr Ed25519PrivateKey) ToPem() (pem.Block, error) { // Load the public key from a raw byte stream. // TODO should this be read from ASN.1? All other functions do that. func LoadPublicKeyEd25519(raw []byte) (*Ed25519PublicKey, error) { - var pu_loaded [ed25519.PublicKeySize]byte - length := copy(pu_loaded[:], raw) + pu_loaded := make([]byte, ed25519.PublicKeySize) + length := copy(pu_loaded, raw) if length != ed25519.PublicKeySize { return nil, errors.New("public key length incorrect") } @@ -108,12 +108,12 @@ func (pu Ed25519PublicKey) MarshalPem() (io.WriterTo, error) { // Hash the message with the hash algorythm and check the signature against the result. func (pu Ed25519PublicKey) Verify(message []byte, signature []byte, hash crypto.Hash) (bool, error) { - var sig [ed25519.SignatureSize]byte + sig := make([]byte, ed25519.SignatureSize) length := copy(sig[:], signature) if length != ed25519.SignatureSize { return false, errors.New("signature does not fit length") } hashed_message := hash.New() hashed_message.Write(message) - return ed25519.Verify(&pu.public_key, hashed_message.Sum(nil), &sig), nil + return ed25519.Verify(pu.public_key, hashed_message.Sum(nil), sig), nil }