/
ed25519.go
263 lines (212 loc) · 5.83 KB
/
ed25519.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package ed25519
import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"github.com/casper-ecosystem/casper-golang-sdk/keypair"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
)
const ED25519_PEM_SECRET_KEY_TAG = "PRIVATE KEY"
const ED25519_PEM_PUBLIC_KEY_TAG = "PUBLIC KEY"
// Ed25519Random creates a random Ed25519 keypair
func Ed25519Random() (keypair.KeyPair, error) {
var rawSeed [32]byte
_, err := io.ReadFull(rand.Reader, rawSeed[:])
if err != nil {
return nil, err
}
return Ed25519FromSeed(rawSeed[:]), nil
}
// Ed25519FromSeed creates a new keypair from the provided Ed25519 seed
func Ed25519FromSeed(seed []byte) keypair.KeyPair {
newKeyPair := ed25519KeyPair{
seed: seed,
}
publKey, privKey := newKeyPair.keys()
newKeyPair.publicKey = publKey
newKeyPair.privateKey = privKey
return &newKeyPair
}
// in private key stored only part of the private key
// ed25519 private key for signature is 64 bytes, first 32 bytes is a private key like here and other 32 bytes are public key
type ed25519KeyPair struct {
seed []byte
publicKey []byte
privateKey []byte
}
func (key *ed25519KeyPair) RawSeed() []byte {
return key.seed
}
func (key *ed25519KeyPair) KeyTag() keypair.KeyTag {
return keypair.KeyTagEd25519
}
// PublicKey returns Ed25519 public key
func (key *ed25519KeyPair) PublicKey() keypair.PublicKey {
return keypair.PublicKey{
Tag: key.KeyTag(),
PubKeyData: key.publicKey,
}
}
// Sign signs the message by using the keyPair
func (key *ed25519KeyPair) Sign(mes []byte) keypair.Signature {
sign := ed25519.Sign(append(key.privateKey, key.publicKey...), mes)
return keypair.Signature{
Tag: key.KeyTag(),
SignatureData: sign,
}
}
// Verify verifies the signature along with the raw message
func (key *ed25519KeyPair) Verify(sign []byte, mes []byte) bool {
return ed25519.Verify(key.publicKey, mes, sign)
}
// AccountHash generates the accountHash for the Ed25519 public key
func (key *ed25519KeyPair) AccountHash() string {
buffer := append([]byte(keypair.StrKeyTagEd25519), keypair.Separator)
buffer = append(buffer, key.publicKey...)
hash := blake2b.Sum256(buffer)
return fmt.Sprintf("account-hash-%s", hex.EncodeToString(hash[:]))
}
// keys generates a public and private key
func (key *ed25519KeyPair) keys() (ed25519.PublicKey, ed25519.PrivateKey) {
reader := bytes.NewReader(key.seed)
pub, priv, err := ed25519.GenerateKey(reader)
if err != nil {
panic(err)
}
return pub, append(make([]byte, 0), priv[:32]...)
}
// ParseKeyPair constructs keyPair from a public key and private key
func ParseKeyPair(publicKey []byte, privateKey []byte) keypair.KeyPair {
pub, _ := ParsePublicKey(publicKey)
priv, _ := ParsePrivateKey(privateKey)
keyPair := ed25519KeyPair{
publicKey: pub,
privateKey: priv,
}
return &keyPair
}
func ParseKey(bytes []byte, from int, to int) ([]byte, error) {
length := len(bytes)
var key []byte
if length == 32 {
key = bytes
}
if length == 64 {
key = bytes[from:to]
}
if length > 32 && length < 64 {
key = bytes[length%32:]
}
if key == nil || len(key) != 32 {
return nil, errors.New("Unexpected key length")
}
return key, nil
}
func ParsePublicKey(bytes []byte) ([]byte, error) {
return ParseKey(bytes, 12, 32)
}
func ParsePrivateKey(bytes []byte) ([]byte, error) {
return ParseKey(bytes, 16, 32)
}
func ParsePublicKeyFile(path string) ([]byte, error) {
key, err := keypair.ReadBase64File(path)
if err != nil {
return nil, err
}
return ParsePublicKey(key)
}
func ParsePrivateKeyFile(path string) ([]byte, error) {
key, err := keypair.ReadBase64File(path)
if err != nil {
return nil, err
}
return ParsePrivateKey(key)
}
// AccountHex generates the accountHex for the Ed25519 public key
func AccountHex(publicKey []byte) string {
dst := make([]byte, hex.EncodedLen(len(publicKey)))
hex.Encode(dst, publicKey)
return "01" + string(dst)
}
// AccountHex generates the accountHex for the Ed25519 public key
func AccountHash(pubKey []byte) string {
buffer := append([]byte(keypair.StrKeyTagEd25519), keypair.Separator)
buffer = append(buffer, pubKey...)
hash := blake2b.Sum256(buffer)
return hex.EncodeToString(hash[:])
}
// ParseKeyFiles parses the key pair from publicKey file and privateKey file
func ParseKeyFiles(pubKeyPath, privKeyPath string) (keypair.KeyPair, error) {
pub, err := ParsePublicKeyFile(pubKeyPath)
if err != nil {
return nil, err
}
priv, err := ParsePrivateKeyFile(privKeyPath)
if err != nil {
return nil, err
}
keyPair := ed25519KeyPair{
publicKey: pub,
privateKey: priv,
}
return &keyPair, nil
}
// ExportPrivateKeyInPem expects the private key encoded in pem
func ExportPrivateKeyInPem(privateKey string) []byte {
derPrefix := make([]byte, 16)
derPrefix[0] = 48
derPrefix[1] = 46
derPrefix[2] = 2
derPrefix[3] = 1
derPrefix[4] = 0
derPrefix[5] = 48
derPrefix[6] = 5
derPrefix[7] = 6
derPrefix[8] = 3
derPrefix[9] = 43
derPrefix[10] = 101
derPrefix[11] = 112
derPrefix[12] = 4
derPrefix[13] = 34
derPrefix[14] = 4
derPrefix[15] = 32
privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return nil
}
block := &pem.Block{
Type: ED25519_PEM_SECRET_KEY_TAG,
Bytes: append(derPrefix, privateKeyBytes...),
}
return pem.EncodeToMemory(block)
}
// ExportPublicKeyInPem exports the public key encoded in pem
func ExportPublicKeyInPem(publicKey string) []byte {
derPrefix := make([]byte, 12)
derPrefix[0] = 48
derPrefix[1] = 42
derPrefix[2] = 48
derPrefix[3] = 5
derPrefix[4] = 6
derPrefix[5] = 3
derPrefix[6] = 43
derPrefix[7] = 101
derPrefix[8] = 112
derPrefix[9] = 3
derPrefix[10] = 33
derPrefix[11] = 0
publicKeyBytes, err := hex.DecodeString(publicKey)
if err != nil {
return nil
}
block := &pem.Block{
Type: ED25519_PEM_PUBLIC_KEY_TAG,
Bytes: append(derPrefix, publicKeyBytes...),
}
return pem.EncodeToMemory(block)
}