2015-07-12 17:46:28 +02:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto"
|
2022-08-15 22:35:00 +02:00
|
|
|
"crypto/ed25519"
|
2015-07-12 17:46:28 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
2022-08-15 22:35:00 +02:00
|
|
|
"fmt"
|
2015-07-12 17:46:28 +02:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PemLabelEd25519 = "ED25519 PRIVATE KEY" // TODO find correct label
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Ed25519PrivateKey struct {
|
2022-08-15 22:35:00 +02:00
|
|
|
private_key ed25519.PrivateKey
|
2015-07-12 17:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ed25519PublicKey struct {
|
2022-08-15 22:35:00 +02:00
|
|
|
public_key ed25519.PublicKey
|
2015-07-12 17:46:28 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a new private key of type ed25519.
|
|
|
|
func NewPrivateKeyEd25519() (*Ed25519PrivateKey, error) {
|
|
|
|
_, pr_raw, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-15 22:35:00 +02:00
|
|
|
return &Ed25519PrivateKey{pr_raw}, nil
|
2015-07-12 17:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-08-15 22:35:00 +02:00
|
|
|
pr_loaded := make([]byte, ed25519.PrivateKeySize)
|
|
|
|
length := copy(pr_loaded, raw)
|
2015-07-12 17:46:28 +02:00
|
|
|
if length != ed25519.PrivateKeySize {
|
2022-08-15 22:35:00 +02:00
|
|
|
return nil, fmt.Errorf("private key length incorrect - got: %d - expected: %d", length, ed25519.PrivateKeySize)
|
2015-07-12 17:46:28 +02:00
|
|
|
}
|
|
|
|
return &Ed25519PrivateKey{pr_loaded}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO implement the raw API for the private key
|
|
|
|
func (pr *Ed25519PrivateKey) PrivateKey() crypto.PrivateKey {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the public key for this private key.
|
|
|
|
func (pr *Ed25519PrivateKey) Public() PublicKey {
|
|
|
|
buf := bytes.NewBufferString(string(pr.private_key[:])) // create a bytes buffer to read the private key
|
|
|
|
pu_raw, _, err := ed25519.GenerateKey(buf) // use the already built private key again
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-15 22:35:00 +02:00
|
|
|
return &Ed25519PublicKey{pu_raw}
|
2015-07-12 17:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2022-08-15 22:35:00 +02:00
|
|
|
result := ed25519.Sign(pr.private_key, hashed_message.Sum(nil))[:]
|
2015-07-12 17:46:28 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the private key into the Pem format.
|
|
|
|
func (pr Ed25519PrivateKey) MarshalPem() (io.WriterTo, error) {
|
2017-05-12 15:27:44 +02:00
|
|
|
pem_block, err := pr.ToPem()
|
|
|
|
if err != nil { // it does not currently return an error, but maybe that will change
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-12 17:46:28 +02:00
|
|
|
return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:27:44 +02:00
|
|
|
func (pr Ed25519PrivateKey) ToPem() (pem.Block, error) {
|
|
|
|
return pem.Block{Type: PemLabelEd25519, Bytes: pr.private_key[:]}, nil
|
|
|
|
}
|
|
|
|
|
2015-07-12 17:46:28 +02:00
|
|
|
// 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) {
|
2022-08-15 22:35:00 +02:00
|
|
|
pu_loaded := make([]byte, ed25519.PublicKeySize)
|
|
|
|
length := copy(pu_loaded, raw)
|
2015-07-12 17:46:28 +02:00
|
|
|
if length != ed25519.PublicKeySize {
|
|
|
|
return nil, errors.New("public key length incorrect")
|
|
|
|
}
|
|
|
|
return &Ed25519PublicKey{pu_loaded}, nil
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:19:25 +02:00
|
|
|
// ToPem returns the pem encoded public key.
|
|
|
|
func (pu Ed25519PublicKey) ToPem() (pem.Block, error) {
|
|
|
|
return pem.Block{Type: PemLabelPublic, Bytes: pu.public_key[:]}, nil
|
|
|
|
}
|
|
|
|
|
2015-07-12 17:46:28 +02:00
|
|
|
// Export the public key into the pem format.
|
|
|
|
func (pu Ed25519PublicKey) MarshalPem() (io.WriterTo, error) {
|
2017-05-12 22:19:25 +02:00
|
|
|
pem_block, err := pu.ToPem()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-12 17:46:28 +02:00
|
|
|
return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-08-15 22:35:00 +02:00
|
|
|
sig := make([]byte, ed25519.SignatureSize)
|
2015-07-12 17:46:28 +02:00
|
|
|
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)
|
2022-08-15 22:35:00 +02:00
|
|
|
return ed25519.Verify(pu.public_key, hashed_message.Sum(nil), sig), nil
|
2015-07-12 17:46:28 +02:00
|
|
|
}
|