-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecies.go
469 lines (387 loc) · 13.2 KB
/
ecies.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package ecies
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/hmac"
"crypto/subtle"
"fmt"
"hash"
"io"
"math"
"math/big"
)
var (
ErrImport = fmt.Errorf("ecies: failed to import key")
ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve")
ErrInvalidParams = fmt.Errorf("ecies: invalid ECIES parameters")
ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key")
ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity")
ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big")
)
// PublicKey is a representation of an elliptic curve public key.
type PublicKey struct {
X *big.Int
Y *big.Int
elliptic.Curve
Params *ECIESParams
}
// Export an ECIES public key as an ECDSA public key.
func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey {
return &ecdsa.PublicKey{Curve: pub.Curve, X: pub.X, Y: pub.Y}
}
// Import an ECDSA public key as an ECIES public key.
func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey {
return &PublicKey{
X: pub.X,
Y: pub.Y,
Curve: pub.Curve,
Params: ParamsFromCurve(pub.Curve),
}
}
// PrivateKey is a representation of an elliptic curve private key.
type PrivateKey struct {
PublicKey
D *big.Int
}
// Export an ECIES private key as an ECDSA private key.
func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey {
pub := &prv.PublicKey
pubECDSA := pub.ExportECDSA()
return &ecdsa.PrivateKey{PublicKey: *pubECDSA, D: prv.D}
}
// Import an ECDSA private key as an ECIES private key.
func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey {
pub := ImportECDSAPublic(&prv.PublicKey)
return &PrivateKey{*pub, prv.D}
}
// Generate an elliptic curve public / private keypair. If params is nil,
// the recommended default parameters for the key will be chosen.
func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error) {
pb, x, y, err := elliptic.GenerateKey(curve, rand)
if err != nil {
return
}
prv = new(PrivateKey)
prv.PublicKey.X = x
prv.PublicKey.Y = y
prv.PublicKey.Curve = curve
prv.D = new(big.Int).SetBytes(pb)
if params == nil {
params = ParamsFromCurve(curve)
}
prv.PublicKey.Params = params
return
}
// MaxSharedKeyLength returns the maximum length of the shared key the
// public key can produce.
func MaxSharedKeyLength(pub *PublicKey) int {
return (pub.Curve.Params().BitSize + 7) / 8
}
// ECDH key agreement method used to establish secret keys for encryption.
func (prv *PrivateKey) GenerateShared(pub *PublicKey, keyLength int) (sk []byte, err error) {
if prv.PublicKey.Curve != pub.Curve {
return nil, ErrInvalidCurve
}
if keyLength > MaxSharedKeyLength(pub) {
return nil, ErrSharedKeyTooBig
}
x, _ := pub.Curve.ScalarMult(pub.X, pub.Y, prv.D.Bytes())
if x == nil {
return nil, ErrSharedKeyIsPointAtInfinity
}
sk = make([]byte, keyLength)
skBytes := x.Bytes()
copy(sk[int(math.Max(0, float64(len(sk)-len(skBytes)))):], skBytes)
return sk, nil
}
var (
ErrKeyDataTooLong = fmt.Errorf("ecies: can't supply requested key data")
ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long")
ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
)
var (
big2To32 = new(big.Int).Exp(big.NewInt(2), big.NewInt(32), nil)
big2To32M1 = new(big.Int).Sub(big2To32, big.NewInt(1))
)
func incCounter(ctr []byte) {
if ctr[3]++; ctr[3] != 0 {
return
}
if ctr[2]++; ctr[2] != 0 {
return
}
if ctr[1]++; ctr[1] != 0 {
return
}
if ctr[0]++; ctr[0] != 0 {
return
}
}
// ConcatKDF derives a symmetric key from a shared secret using
// a concatenation key derivation function using the `hash` hashing
// algorithm as the base and continuing until at least `derivedKeyLength`
// bytes is available
// See NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).
func ConcatKDF(hash hash.Hash, sharedSecret, sharedInformation1 []byte, derivedKeyLength int) (derivedKey []byte, err error) {
if sharedInformation1 == nil {
sharedInformation1 = make([]byte, 0)
}
reps := ((derivedKeyLength + 7) * 8) / (hash.BlockSize() * 8)
if big.NewInt(int64(reps)).Cmp(big2To32M1) > 0 {
fmt.Println(big2To32M1)
return nil, ErrKeyDataTooLong
}
counter := []byte{0, 0, 0, 1}
derivedKey = make([]byte, 0)
for i := 0; i <= reps; i++ {
hash.Write(counter)
hash.Write(sharedSecret)
hash.Write(sharedInformation1)
derivedKey = append(derivedKey, hash.Sum(nil)...)
hash.Reset()
incCounter(counter)
}
// Trim the desired key length to derivedKeyLength
derivedKey = derivedKey[:derivedKeyLength]
return
}
// MessageTag computes the MAC of a message (called the tag) as per
// SEC 1, 3.5.
func MessageTag(hash func() hash.Hash, key, msg, shared []byte) []byte {
mac := hmac.New(hash, key)
mac.Write(msg)
mac.Write(shared)
tag := mac.Sum(nil)
return tag
}
// Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1.
//
// sharedInformation1 and sharedInformation2 contain shared information that is not part of the resulting
// ciphertext. sharedInformation1 is fed into key derivation, sharedInformation2 is fed into the MAC. If the
// shared information parameters aren't being used, they should be nil.
func Encrypt(rand io.Reader, pub *PublicKey, plaintext, sharedInformation1, sharedInformation2 []byte) (ciphertext []byte, err error) {
params := pub.Params
if params == nil {
if params = ParamsFromCurve(pub.Curve); params == nil {
err = ErrUnsupportedECIESParameters
return
}
}
// Generate an ephemeral key pair
privateKey, err := GenerateKey(rand, pub.Curve, params)
if err != nil {
return
}
// Serialize the properties of the used elliptic curve
curveParams := elliptic.Marshal(pub.Curve, privateKey.PublicKey.X, privateKey.PublicKey.Y)
symmetricKeyLength := params.KeyLen
macKeyLength := params.KeyLen
// Derive a shared secret from the generated private key and the peer public key
sharedSecret, err := privateKey.GenerateShared(pub, symmetricKeyLength+macKeyLength)
if err != nil {
return
}
// Extend the shared secret through Concatenation KDF to the desired length
// The extended key will then be split and used as an encryption key and digest key
hash := params.Hash()
derivedKey, err := ConcatKDF(hash, sharedSecret, sharedInformation1, symmetricKeyLength+macKeyLength)
if err != nil {
return
}
// The derived key is split into encryption key and digest key
// which is then hashed to produce the final HMAC key
encryptionKey := derivedKey[:symmetricKeyLength]
digestKey := derivedKey[symmetricKeyLength:]
hash.Write(digestKey)
digestKey = hash.Sum(nil)
hash.Reset()
// Encrypt the message using the first half of the derived symmetric key
encryptedMessage, err := SymmetricEncrypt(rand, params, encryptionKey, plaintext, nil)
if err != nil || len(encryptedMessage) <= params.BlockSize {
return
}
// Calculate message digest of the ciphertext using the specified hashing function
// and the second half of the derived symmetric key
digest := MessageTag(params.Hash, digestKey, encryptedMessage, sharedInformation2)
// Prepare ciphertext byte sink
ciphertext = make([]byte, len(curveParams)+len(encryptedMessage)+len(digest))
// Fill the ciphertext byte sink with:
// 1. marshalled curve properties
// 2. encrypted message
// 3. message digest for integrity checking
copy(ciphertext, curveParams)
copy(ciphertext[len(curveParams):], encryptedMessage)
copy(ciphertext[len(curveParams)+len(encryptedMessage):], digest)
return
}
// Decrypt decrypts an ECIES ciphertext.
func (privateKey *PrivateKey) Decrypt(rand io.Reader, ciphertext, sharedInformation1, sharedInformation2 []byte) (plaintext []byte, err error) {
if len(ciphertext) == 0 {
return nil, ErrInvalidMessage
}
params := privateKey.PublicKey.Params
if params == nil {
if params = ParamsFromCurve(privateKey.PublicKey.Curve); params == nil {
err = ErrUnsupportedECIESParameters
return
}
}
hash := params.Hash()
var (
curveParamsLength int
hashLength int = hash.Size()
messageStart int
messageEnd int
)
switch ciphertext[0] {
case 2, 3, 4:
curveParamsLength = ((privateKey.PublicKey.Curve.Params().BitSize + 7) / 4)
if len(ciphertext) < (curveParamsLength + hashLength + 1) {
err = ErrInvalidMessage
return
}
default:
err = ErrInvalidPublicKey
return
}
messageStart = curveParamsLength
messageEnd = len(ciphertext) - hashLength
publicKey := new(PublicKey)
publicKey.Curve = privateKey.PublicKey.Curve
publicKey.X, publicKey.Y = elliptic.Unmarshal(publicKey.Curve, ciphertext[:curveParamsLength])
if publicKey.X == nil {
err = ErrInvalidPublicKey
return
}
if !publicKey.Curve.IsOnCurve(publicKey.X, publicKey.Y) {
err = ErrInvalidCurve
return
}
sharedSecret, err := privateKey.GenerateShared(publicKey, params.KeyLen+params.KeyLen)
if err != nil {
return
}
derivedKey, err := ConcatKDF(hash, sharedSecret, sharedInformation1, params.KeyLen+params.KeyLen)
if err != nil {
return
}
encryptionKey := derivedKey[:params.KeyLen]
digestKey := derivedKey[params.KeyLen:]
hash.Write(digestKey)
digestKey = hash.Sum(nil)
hash.Reset()
digest := MessageTag(params.Hash, digestKey, ciphertext[messageStart:messageEnd], sharedInformation2)
if subtle.ConstantTimeCompare(ciphertext[messageEnd:], digest) != 1 {
err = ErrInvalidMessage
return
}
plaintext, err = SymmetricDecrypt(rand, params, encryptionKey, ciphertext[messageStart:messageEnd], nil)
return
}
// Galois/Counter Mode is an AEAD (authenticated cipher) - i.e. it is already
// authenticated and does not require additional tagging to protect the message
// integrity
func EncryptAEAD(rand io.Reader, pub *PublicKey, plaintext []byte) (ciphertext []byte, err error) {
params := pub.Params
if params == nil {
if params = ParamsFromCurve(pub.Curve); params == nil {
err = ErrUnsupportedECIESParameters
return
}
}
// Generate an ephemeral key pair
privateKey, err := GenerateKey(rand, pub.Curve, params)
if err != nil {
return
}
// Serialize the properties of the used elliptic curve
curveParams := elliptic.Marshal(pub.Curve, privateKey.PublicKey.X, privateKey.PublicKey.Y)
symmetricKeyLength := 128 / 8
if params.KeyLen > 256/8 {
symmetricKeyLength = 256 / 8
}
// Derive a shared secret from the generated private key and the peer public key
sharedSecret, err := privateKey.GenerateShared(pub, params.KeyLen)
if err != nil {
return
}
// Extend the shared secret through Concatenation KDF to the desired length
// The extended key will then be split and used as an encryption key and digest key
// The properties of the ephemeral public key are used as sharedInfo for the KDF
sharedInfoKDF := elliptic.Marshal(privateKey.PublicKey.Curve, privateKey.PublicKey.X, privateKey.PublicKey.Y)
encryptionKey, err := ConcatKDF(params.Hash(), sharedSecret, sharedInfoKDF, symmetricKeyLength)
if err != nil {
return
}
// Encrypt the message using the first half of the derived symmetric key
// The properties of the static public key are used as authentication data for
// the encryption
authenticationData := elliptic.Marshal(pub.Curve, pub.X, pub.Y)
encryptedMessage, err := SymmetricEncrypt(rand, params, encryptionKey, plaintext, authenticationData)
if err != nil || len(encryptedMessage) <= params.BlockSize {
return
}
// Prepare ciphertext byte sink
ciphertext = make([]byte, len(curveParams)+len(encryptedMessage))
// Fill the ciphertext byte sink with:
// 1. marshalled curve properties
// 2. encrypted message
copy(ciphertext, curveParams)
copy(ciphertext[len(curveParams):], encryptedMessage)
return
}
// Decryption in AEAD mode (w/o message tag)
func (privateKey *PrivateKey) DecryptAEAD(rand io.Reader, ciphertext []byte) (plaintext []byte, err error) {
if len(ciphertext) == 0 {
return nil, ErrInvalidMessage
}
params := privateKey.PublicKey.Params
if params == nil {
if params = ParamsFromCurve(privateKey.PublicKey.Curve); params == nil {
err = ErrUnsupportedECIESParameters
return
}
}
hash := params.Hash()
var curveParamsLength int
switch ciphertext[0] {
case 2, 3, 4:
curveParamsLength = ((privateKey.PublicKey.Curve.Params().BitSize + 7) / 4)
if len(ciphertext) < (curveParamsLength + 1) {
err = ErrInvalidMessage
return
}
default:
err = ErrInvalidPublicKey
return
}
publicKey := new(PublicKey)
publicKey.Curve = privateKey.PublicKey.Curve
publicKey.X, publicKey.Y = elliptic.Unmarshal(publicKey.Curve, ciphertext[:curveParamsLength])
if publicKey.X == nil {
err = ErrInvalidPublicKey
return
}
if !publicKey.Curve.IsOnCurve(publicKey.X, publicKey.Y) {
err = ErrInvalidCurve
return
}
// Serialize the properties of the used elliptic curve for use
// as authentication data
curveParams := elliptic.Marshal(publicKey.Curve, publicKey.X, publicKey.Y)
symmetricKeyLength := 128 / 8
if params.KeyLen > 256/8 {
symmetricKeyLength = 256 / 8
}
sharedSecret, err := privateKey.GenerateShared(publicKey, params.KeyLen)
if err != nil {
return
}
encryptionKey, err := ConcatKDF(hash, sharedSecret, curveParams, symmetricKeyLength)
if err != nil {
return
}
authenticationData := elliptic.Marshal(privateKey.PublicKey.Curve, privateKey.PublicKey.X, privateKey.PublicKey.Y)
plaintext, err = SymmetricDecrypt(rand, params, encryptionKey, ciphertext[curveParamsLength:], authenticationData)
return
}