-
Notifications
You must be signed in to change notification settings - Fork 2
/
key.go
executable file
·149 lines (115 loc) · 2.85 KB
/
key.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package crypto
import (
"bytes"
"crypto"
"crypto/ed25519"
"crypto/rand"
"errors"
"io"
"golang.org/x/crypto/curve25519"
)
var ErrWrongFormat = errors.New("wrong format")
type PublicKey struct {
verifiedKey ed25519.PublicKey
key []byte
}
func (p *PublicKey) GobEncode() ([]byte, error) {
return p.Encode()
}
func (p *PublicKey) GobDecode(b []byte) error {
return p.Decode(b)
}
func (p *PublicKey) Verify(data []byte, signature []byte) bool {
return ed25519.Verify(p.verifiedKey, data, signature)
}
func (p *PublicKey) Encode() ([]byte, error) {
var buffer bytes.Buffer
buffer.Write(p.key)
buffer.Write(p.verifiedKey)
return buffer.Bytes(), nil
}
func (p *PublicKey) Decode(b []byte) error {
if len(b) != 64 {
return ErrWrongFormat
}
p.key = make([]byte, 32)
copy(p.key, b[:32])
p.verifiedKey = make([]byte, 32)
copy(p.verifiedKey, b[32:])
return nil
}
type PrivateKey struct {
signedKey ed25519.PrivateKey
key []byte
}
func (p *PrivateKey) SharedKey(public *PublicKey) ([]byte, error) {
return curve25519.X25519(p.key, public.key)
}
func (p *PrivateKey) Sign(data []byte) ([]byte, error) {
signature, err := p.signedKey.Sign(rand.Reader, data, crypto.Hash(0))
if err != nil {
return nil, err
}
return signature, nil
}
func (p *PrivateKey) Encode() ([]byte, error) {
var buffer bytes.Buffer
buffer.Write(p.key)
buffer.Write(p.signedKey)
return buffer.Bytes(), nil
}
func (p *PrivateKey) Decode(b []byte) error {
if len(b) != 96 {
return ErrWrongFormat
}
p.key = make([]byte, 32)
copy(p.key, b[:32])
p.signedKey = make([]byte, 64)
copy(p.signedKey, b[32:])
return nil
}
func NewKeyPair() (*PublicKey, *PrivateKey, error) {
publicKey := &PublicKey{}
privateKey := &PrivateKey{}
{
public, private, err := newX25519Keys()
if err != nil {
return nil, nil, err
}
publicKey.key = public
privateKey.key = private
}
{
public, private, err := newEd25519Keys()
if err != nil {
return nil, nil, err
}
publicKey.verifiedKey = public
privateKey.signedKey = private
}
return publicKey, privateKey, nil
}
func newX25519Keys() ([]byte, []byte, error) {
var publicKey, privateKey [32]byte
if _, err := io.ReadFull(rand.Reader, privateKey[:]); err != nil {
return nil, nil, err
}
curve25519.ScalarBaseMult(&publicKey, &privateKey)
return publicKey[:], privateKey[:], nil
}
func newEd25519Keys() (ed25519.PublicKey, ed25519.PrivateKey, error) {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}
return publicKey, privateKey, nil
}
// IsKeysMatched checks whether both public and private key are belong to each other
func IsKeysMatched(public *PublicKey, private *PrivateKey) bool {
sampleContent := []byte("sample")
signature, err := private.Sign(sampleContent)
if err != nil {
return false
}
return public.Verify(sampleContent, signature)
}