aboutsummaryrefslogtreecommitdiff
path: root/vendor/git.zero-knowledge.org/gibheer/pki/ed25519.go
blob: 0104beb62f96ba7daa308a0b26792029f3aadb61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package pki

import (
	"bytes"
	"crypto"
	"crypto/ed25519"
	"crypto/rand"
	"encoding/pem"
	"errors"
	"fmt"
	"io"
)

const (
	PemLabelEd25519 = "ED25519 PRIVATE KEY" // TODO find correct label
)

type (
	Ed25519PrivateKey struct {
		private_key ed25519.PrivateKey
	}

	Ed25519PublicKey struct {
		public_key ed25519.PublicKey
	}
)

// 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
	}
	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) {
	pr_loaded := make([]byte, ed25519.PrivateKeySize)
	length := copy(pr_loaded, raw)
	if length != ed25519.PrivateKeySize {
		return nil, fmt.Errorf("private key length incorrect - got: %d - expected: %d", length, ed25519.PrivateKeySize)
	}
	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
	}
	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))[:]
	return result, nil
}

// Export the private key into the Pem format.
func (pr Ed25519PrivateKey) MarshalPem() (io.WriterTo, error) {
	pem_block, err := pr.ToPem()
	if err != nil { // it does not currently return an error, but maybe that will change
		return nil, err
	}
	return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
}

func (pr Ed25519PrivateKey) ToPem() (pem.Block, error) {
	return pem.Block{Type: PemLabelEd25519, Bytes: pr.private_key[:]}, nil
}

// 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) {
	pu_loaded := make([]byte, ed25519.PublicKeySize)
	length := copy(pu_loaded, raw)
	if length != ed25519.PublicKeySize {
		return nil, errors.New("public key length incorrect")
	}
	return &Ed25519PublicKey{pu_loaded}, nil
}

// ToPem returns the pem encoded public key.
func (pu Ed25519PublicKey) ToPem() (pem.Block, error) {
	return pem.Block{Type: PemLabelPublic, Bytes: pu.public_key[:]}, nil
}

// Export the public key into the pem format.
func (pu Ed25519PublicKey) MarshalPem() (io.WriterTo, error) {
	pem_block, err := pu.ToPem()
	if err != nil {
		return nil, err
	}
	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) {
	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
}