-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed25519_model.go
384 lines (328 loc) · 9.9 KB
/
ed25519_model.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2018 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"fmt"
"io"
)
// Ed25519SeedCryptoEngine wraps a cryptographic engine ed25519 and seed for this engine
type Ed25519SeedCryptoEngine struct {
seed io.Reader
}
// CreateDsaSigner implemented interface CryptoEngine method
func (ref *Ed25519SeedCryptoEngine) CreateDsaSigner(keyPair *KeyPair) DsaSigner {
return NewEd25519DsaSigner(keyPair)
}
// CreateKeyGenerator implemented interface CryptoEngine method
func (ref *Ed25519SeedCryptoEngine) CreateKeyGenerator() KeyGenerator {
return NewEd25519KeyGenerator(ref.seed)
}
// CreateBlockCipher implemented interface CryptoEngine method
func (ref *Ed25519SeedCryptoEngine) CreateBlockCipher(senderKeyPair *KeyPair, recipientKeyPair *KeyPair) BlockCipher {
return NewEd25519BlockCipher(senderKeyPair, recipientKeyPair, ref.seed)
}
// CreateKeyAnalyzer implemented interface CryptoEngine method
func (ref *Ed25519SeedCryptoEngine) CreateKeyAnalyzer() KeyAnalyzer {
return NewEd25519KeyAnalyzer()
}
// Ed25519BlockCipher Implementation of the block cipher for Ed25519.
type Ed25519BlockCipher struct {
senderKeyPair *KeyPair
recipientKeyPair *KeyPair
keyLength int
seed io.Reader
}
// NewEd25519BlockCipher return Ed25519BlockCipher
func NewEd25519BlockCipher(senderKeyPair *KeyPair, recipientKeyPair *KeyPair, seed io.Reader) *Ed25519BlockCipher {
if seed == nil {
seed = rand.Reader
}
ref := Ed25519BlockCipher{
senderKeyPair,
recipientKeyPair,
len(recipientKeyPair.PublicKey.Raw),
seed,
}
return &ref
}
func (ref *Ed25519BlockCipher) encode(message []byte, sharedKey []byte, ivData []byte) ([]byte, error) {
c, err := aes.NewCipher(sharedKey)
if err != nil {
return nil, err
}
messageSize := len(message)
blockSize := c.BlockSize()
paddingSize := blockSize - (messageSize % blockSize)
bufferSize := messageSize + paddingSize
buf := make([]byte, bufferSize)
copy(buf[:messageSize], message)
for i := 0; i < paddingSize; i++ {
buf[messageSize+i] = uint8(paddingSize)
}
enc := cipher.NewCBCEncrypter(c, ivData)
ciphertext := make([]byte, len(buf))
enc.CryptBlocks(ciphertext, buf)
return ciphertext, nil
}
func (ref *Ed25519BlockCipher) decode(ciphertext []byte, sharedKey []byte, ivData []byte) ([]byte, error) {
c, err := aes.NewCipher(sharedKey)
if err != nil {
return nil, err
}
dec := cipher.NewCBCDecrypter(c, ivData)
buf := make([]byte, len(ciphertext))
dec.CryptBlocks(buf, ciphertext)
bufferSize := len(buf)
paddingSize := int(buf[bufferSize-1] & 0xFF)
if paddingSize == 0 || paddingSize > c.BlockSize() {
return nil, errors.New("blocks are corrupted, paddingSize is wrong")
}
messageSize := bufferSize - paddingSize
for i := messageSize; i < bufferSize; i++ {
if int(buf[i]) != paddingSize {
return nil, errors.New("blocks are corrupted, fake byte is not equal to paddingSize")
}
}
return buf[:messageSize], nil
}
// GetSharedKey create shared bytes
func (ref *Ed25519BlockCipher) GetSharedKey(privateKey *PrivateKey, publicKey *PublicKey, salt []byte) ([]byte, error) {
grA, err := NewEd25519EncodedGroupElement(publicKey.Raw)
if err != nil {
return nil, err
}
senderA, err := grA.Decode()
if err != nil {
return nil, err
}
senderA.PrecomputeForScalarMultiplication()
el, err := senderA.scalarMultiply(PrepareForScalarMultiply(privateKey))
if err != nil {
return nil, err
}
sharedKey, err := el.Encode()
if err != nil {
return nil, err
}
for i := 0; i < ref.keyLength; i++ {
sharedKey.Raw[i] ^= salt[i]
}
return HashesSha3_256(sharedKey.Raw)
}
// Encrypt slice byte
func (ref *Ed25519BlockCipher) Encrypt(input []byte) ([]byte, error) {
// Setup salt.
salt := make([]byte, ref.keyLength)
_, err := io.ReadFull(ref.seed, salt)
if err != nil {
return nil, err
}
// Derive shared key.
sharedKey, err := ref.GetSharedKey(ref.senderKeyPair.PrivateKey, ref.recipientKeyPair.PublicKey, salt)
if err != nil {
return nil, err
}
// Setup IV.
ivData := make([]byte, 16)
_, err = io.ReadFull(ref.seed, ivData)
if err != nil {
return nil, err
}
// Encode.
buf, err := ref.encode(input, sharedKey, ivData)
if err != nil {
return nil, err
}
result := append(append(salt, ivData...), buf...)
return result, nil
}
// Decrypt slice byte
func (ref *Ed25519BlockCipher) Decrypt(input []byte) ([]byte, error) {
if len(input) < 64 {
return nil, errors.New("input is to short for decryption")
}
salt := input[:ref.keyLength]
ivData := input[ref.keyLength:48]
encData := input[48:]
// Derive shared key.
sharedKey, err := ref.GetSharedKey(ref.recipientKeyPair.PrivateKey, ref.senderKeyPair.PublicKey, salt)
if err != nil {
return nil, err
}
// Decode.
return ref.decode(encData, sharedKey, ivData)
}
// Ed25519DsaSigner implement DsaSigned interface with Ed25519 algo
type Ed25519DsaSigner struct {
KeyPair *KeyPair
}
// NewEd25519DsaSigner creates a Ed25519 DSA signer.
func NewEd25519DsaSigner(keyPair *KeyPair) *Ed25519DsaSigner {
return &Ed25519DsaSigner{keyPair}
}
// Sign message
func (ref *Ed25519DsaSigner) Sign(mess []byte) (*Signature, error) {
if !ref.KeyPair.HasPrivateKey() {
return nil, errors.New("cannot sign without private key")
}
// Hash the private key to improve randomness.
hash, err := HashesSha3_512(ref.KeyPair.PrivateKey.Raw)
if err != nil {
return nil, err
}
// r = H(hash_b,...,hash_2b-1, data) where b=256.
hashR, err := HashesSha3_512(
hash[32:], // only include the last 32 bytes of the private key hash
mess)
if err != nil {
return nil, err
}
r, err := NewEd25519EncodedFieldElement(hashR)
if err != nil {
return nil, err
}
// Reduce size of r since we are calculating mod group order anyway
rModQ := r.modQ()
// R = rModQ * base point.
R, err := Ed25519Group.BASE_POINT().scalarMultiply(rModQ)
if err != nil {
return nil, err
}
encodedR, err := R.Encode()
if err != nil {
return nil, err
}
// S = (r + H(encodedR, encodedA, data) * a) mod group order where
// encodedR and encodedA are the little endian encodings of the group element R and the public key A and
// a is the lower 32 bytes of hash after clamping.
hashH, err := HashesSha3_512(
encodedR.Raw,
ref.KeyPair.PublicKey.Raw,
mess)
if err != nil {
return nil, err
}
h, err := NewEd25519EncodedFieldElement(hashH)
if err != nil {
return nil, err
}
hModQ := h.modQ()
encodedS := hModQ.multiplyAndAddModQ(PrepareForScalarMultiply(ref.KeyPair.PrivateKey),
rModQ)
// Signature is (encodedR, encodedS)
signature, err := NewSignature(encodedR.Raw, encodedS.Raw)
if err != nil {
return nil, err
}
if !ref.IsCanonicalSignature(signature) {
return nil, errors.New("Generated signature is not canonical")
}
return signature, nil
}
// Verify reports whether sig is a valid signature of message 'data' by publicKey. It
// prevent panic inside ed25519.Verify
func (ref *Ed25519DsaSigner) Verify(mess []byte, signature *Signature) (res bool) {
if !ref.IsCanonicalSignature(signature) {
return false
}
if isEqualConstantTime(ref.KeyPair.PublicKey.Raw, make([]byte, 32)) {
return false
}
// h = H(encodedR, encodedA, data).
rawEncodedR := signature.R
rawEncodedA := ref.KeyPair.PublicKey.Raw
hashR, err := HashesSha3_512(
rawEncodedR,
rawEncodedA,
mess)
if err != nil {
fmt.Println(err)
return false
}
h, err := NewEd25519EncodedFieldElement(hashR)
if err != nil {
fmt.Println(err)
return false
}
// hReduced = h mod group order
hModQ := h.modQ()
// Must compute A.
A, err := (&Ed25519EncodedGroupElement{rawEncodedA}).Decode()
if err != nil {
fmt.Println(err)
return false
}
A.PrecomputeForDoubleScalarMultiplication()
// R = encodedS * B - H(encodedR, encodedA, data) * A
calculatedR, err := Ed25519Group.BASE_POINT().doubleScalarMultiplyVariableTime(
A,
hModQ,
&Ed25519EncodedFieldElement{Ed25519FieldZeroShort(), signature.S})
if err != nil {
fmt.Println(err)
return false
}
// Compare calculated R to given R.
encodedCalculatedR, err := calculatedR.Encode()
if err != nil {
fmt.Println(err)
return false
}
return isEqualConstantTime(encodedCalculatedR.Raw, rawEncodedR)
}
// IsCanonicalSignature check signature on canonical
func (ref *Ed25519DsaSigner) IsCanonicalSignature(signature *Signature) bool {
sgnS := signature.GetS().Uint64()
return sgnS != Ed25519Group.GROUP_ORDER.Uint64() && sgnS > 0
}
// MakeSignatureCanonical return canonical signature
func (ref *Ed25519DsaSigner) MakeSignatureCanonical(signature *Signature) (*Signature, error) {
sign := make([]byte, 64)
copy(sign, signature.S)
s, err := NewEd25519EncodedFieldElement(sign)
if err != nil {
return nil, err
}
sModQ := s.modQ()
return NewSignature(signature.R, sModQ.Raw)
}
// Ed25519KeyGenerator Implementation of the key generator for Ed25519.
type Ed25519KeyGenerator struct {
seed io.Reader
}
// NewEd25519KeyGenerator return new Ed25519KeyGenerator
func NewEd25519KeyGenerator(seed io.Reader) *Ed25519KeyGenerator {
if seed == nil {
seed = rand.Reader
}
ref := Ed25519KeyGenerator{seed}
return &ref
}
// GenerateKeyPair generate key pair use ed25519.GenerateKey
func (ref *Ed25519KeyGenerator) GenerateKeyPair() (*KeyPair, error) {
seed := make([]byte, 32)
_, err := io.ReadFull(ref.seed, seed[:])
if err != nil {
return nil, err
} // seed is the private key.
// seed is the private key.
privateKey := NewPrivateKey(seed)
publicKey := ref.DerivePublicKey(privateKey)
return NewKeyPair(privateKey, publicKey, CryptoEngines.Ed25519Engine)
}
// DerivePublicKey return public key based on Ed25519Group.BASE_POINT
func (ref *Ed25519KeyGenerator) DerivePublicKey(privateKey *PrivateKey) *PublicKey {
a := PrepareForScalarMultiply(privateKey)
// a * base point is the public key.
pubKey, err := Ed25519Group.BASE_POINT().scalarMultiply(a)
if err != nil {
panic(err)
}
el, _ := pubKey.Encode()
return NewPublicKey(el.Raw)
}