-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprivate_key.go
660 lines (584 loc) · 19.5 KB
/
private_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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package easyecc
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math/big"
"os"
"strings"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tyler-smith/go-bip39"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
)
const (
PBKDF2_ITER = 16384
PBKDF2_SIZE = 32
)
var ErrUnsupportedCurve = fmt.Errorf("the operation is not supported on this curve")
var ErrDifferentCurves = fmt.Errorf("the keys must use the same curve")
var ErrUnsupportedKeyType = fmt.Errorf("unsupported key type")
type EllipticCurve int
const (
INVALID_CURVE EllipticCurve = -1
SECP256K1 EllipticCurve = 1
P256 EllipticCurve = 2
P384 EllipticCurve = 3
P521 EllipticCurve = 4
)
func (ec EllipticCurve) String() string {
switch ec {
case SECP256K1:
return "secp256k1"
case P256:
return "P-256"
case P384:
return "P-384"
case P521:
return "P-521"
}
return "Invalid"
}
func StringToEllipticCurve(s string) EllipticCurve {
switch strings.ToUpper(s) {
case "SECP256K1":
return SECP256K1
case "P-256":
return P256
case "P-384":
return P384
case "P-521":
return P521
}
return INVALID_CURVE
}
func getCurve(curve EllipticCurve) elliptic.Curve {
switch curve {
case SECP256K1:
return crypto.S256()
case P256:
return elliptic.P256()
case P384:
return elliptic.P384()
case P521:
return elliptic.P521()
}
return nil
}
func getKeyLength(curve EllipticCurve) int {
switch curve {
case SECP256K1:
return 32
case P256:
return 32
case P384:
return 48
case P521:
return 66
}
return -1
}
// PrivateKey represents elliptic cryptography private key.
type PrivateKey struct {
privateKey *ecdsa.PrivateKey
}
type privateKeyJSON struct {
Kty string `json:"kty"`
Crv string `json:"crv"`
X string `json:"x"`
Y string `json:"y"`
D string `json:"d"`
}
// GeneratePrivateKey creates a new random private key,
// given a curve.
func GeneratePrivateKey(curve EllipticCurve) (*PrivateKey, error) {
privateKey, err := ecdsa.GenerateKey(getCurve(curve), rand.Reader)
if err != nil {
return nil, fmt.Errorf("failed to generate private key, %v", err)
}
return &PrivateKey{privateKey: privateKey}, nil
}
// CreatePrivateKeyFromETHKey creates private key from eth generated key
func CreatePrivateKeyFromETHKey(privateKey *ecdsa.PrivateKey) *PrivateKey {
return &PrivateKey{privateKey: privateKey}
}
// CreatePrivateKey creates a private key on the given curve from secret.
func CreatePrivateKey(curve EllipticCurve, secret *big.Int) *PrivateKey {
privateKey := &ecdsa.PrivateKey{
D: secret}
privateKey.PublicKey.Curve = getCurve(curve)
privateKey.PublicKey.X, privateKey.PublicKey.Y = privateKey.PublicKey.Curve.ScalarBaseMult(secret.Bytes())
return &PrivateKey{privateKey: privateKey}
}
// CreatePrivateKeyFromPassword creates a private key on the given curve from password using PBKDF2 algorithm.
func CreatePrivateKeyFromPassword(curve EllipticCurve, password, salt []byte) *PrivateKey {
secret := pbkdf2.Key(password, salt, PBKDF2_ITER, PBKDF2_SIZE, sha256.New)
return CreatePrivateKey(curve, new(big.Int).SetBytes(secret))
}
// CreatePrivateKeyFromEncrypted creates a private key from from encrypted private
// key using the passphrase.
// Encryption is done using AES-256 with CGM cipher, with a key derived from the passphrase.
func CreatePrivateKeyFromEncrypted(curve EllipticCurve, data []byte, passphrase string) (*PrivateKey,
error) {
// Data length must be the key length plus at least one more byte.
// TODO: This doesn't look like a useful check.
if len(data) < getKeyLength(curve)+1 {
return nil, fmt.Errorf("invalid data")
}
salt, data := data[len(data)-32:], data[:len(data)-32]
key, _, err := deriveKey([]byte(passphrase), salt)
if err != nil {
return nil, err
}
keyBytes, err := decrypt(key, data)
if err != nil {
return nil, err
}
secret := new(big.Int).SetBytes(keyBytes)
return CreatePrivateKey(curve, secret), nil
}
// NewPrivateKeyFromFile loads private key using given curve
// from file and decrypts it using the given passphrase.
// If the passphrase is an empty string, no decryption is done (the file content is assumed
// to be not encrypted).
func CreatePrivateKeyFromFile(curve EllipticCurve, fileName string, passphrase string) (*PrivateKey, error) {
// TODO: Perhaps rename this function to something like LoadPrivateKey in the next major version?
b, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to load private key: %v", err)
}
if passphrase != "" {
return CreatePrivateKeyFromEncrypted(curve, b, passphrase)
}
secret := new(big.Int)
secret.SetBytes(b)
return CreatePrivateKey(curve, secret), nil
}
// CreatePrivateKeyFromJWKFile loads private key from file in JWK format, optionally
// decrypting it.
func CreatePrivateKeyFromJWKFile(fileName string, passphrase string) (*PrivateKey, error) {
// TODO: Rename this to LoadPrivateKeyAsJWK in the next major version.
data, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to load private key: %v", err)
}
var jsonBytes []byte
if passphrase != "" {
salt, data := data[len(data)-32:], data[:len(data)-32]
key, _, err := deriveKey([]byte(passphrase), salt)
if err != nil {
return nil, err
}
jsonBytes, err = decrypt(key, data)
if err != nil {
return nil, err
}
} else {
jsonBytes = data
}
return CreatePrivateKeyFromJWK(jsonBytes)
}
// NewPrivateKeyFromMnemonic creates private key on given curve from a mnemonic phrase.
func CreatePrivateKeyFromMnemonic(curve EllipticCurve, mnemonic string) (*PrivateKey, error) {
if curve != SECP256K1 && curve != P256 {
return nil, ErrUnsupportedCurve
}
b, err := bip39.EntropyFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
secret := new(big.Int).SetBytes(b)
return CreatePrivateKey(curve, secret), nil
}
// Secret returns the private key's secret.
func (pk *PrivateKey) Secret() *big.Int {
return pk.privateKey.D
}
// CreatePrivateKeyFromJWK creates private key from JWK-encoded
// representation.
// See https://www.rfc-editor.org/rfc/rfc7517.
func CreatePrivateKeyFromJWK(data []byte) (*PrivateKey, error) {
var pkJSON privateKeyJSON
err := json.Unmarshal(data, &pkJSON)
if err != nil {
return nil, err
}
if pkJSON.Kty != "EC" {
return nil, ErrUnsupportedKeyType
}
curve := StringToEllipticCurve(pkJSON.Crv)
if curve == INVALID_CURVE {
return nil, ErrUnsupportedCurve
}
// JWK uses Base64url encoding, which is Base64 encoding without padding.
dBytes, err := base64.
StdEncoding.WithPadding(base64.NoPadding).
DecodeString(pkJSON.D)
if err != nil {
return nil, err
}
d := new(big.Int)
d.SetBytes(dBytes)
return CreatePrivateKey(curve, d), nil
}
// Save saves the private key to the specified file. If the passphrase is given, the key will
// be encrypted with this passphrase. If the passphrase is an empty string, the key is not
// encrypted.
func (pk *PrivateKey) Save(fileName string, passphrase string) error {
if passphrase != "" {
data, err := pk.EncryptKeyWithPassphrase(passphrase)
if err != nil {
return err
}
return os.WriteFile(fileName, data, 0600)
}
// Pad with zero bytes if necessary.
b := padWithZeros(pk.privateKey.D.Bytes(), getKeyLength(pk.Curve()))
return os.WriteFile(fileName, b, 0600)
}
// SaveAsJWK writes the key to a file in JWK format, optionally encrypting it
// with a passphrase.
func (pk *PrivateKey) SaveAsJWK(fileName string, passphrase string) error {
jsonBytes, err := pk.MarshalToJWK()
if err != nil {
return err
}
if passphrase != "" {
data, err := encryptWithPassphrase(passphrase, jsonBytes)
if err != nil {
return err
}
return os.WriteFile(fileName, data, 0600)
}
return os.WriteFile(fileName, jsonBytes, 0600)
}
// PublicKey returns the public key derived from this private key.
func (pk *PrivateKey) PublicKey() *PublicKey {
return &PublicKey{publicKey: &pk.privateKey.PublicKey}
}
// Curve returns the elliptic curve for this public key.
func (pk *PrivateKey) Curve() EllipticCurve {
if pk.privateKey.Curve == crypto.S256() {
return SECP256K1
}
if pk.privateKey.Curve == elliptic.P256() {
return P256
}
if pk.privateKey.Curve == elliptic.P384() {
return P384
}
if pk.privateKey.Curve == elliptic.P521() {
return P521
}
return INVALID_CURVE
}
// Sign signs (ECDSA) the hash using the private key and returns signature.
// See https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm.
func (pk *PrivateKey) Sign(hash []byte) (*Signature, error) {
r, s, err := ecdsa.Sign(rand.Reader, pk.privateKey, hash)
if err != nil {
return nil, err
}
return &Signature{R: r, S: s}, nil
}
// getSharedEncryptionKeySecp256k1_Legacy computes a shared encryption key for SECP256K1 curve
// in a way that is consistent with how it's done in crypto/ecdh.
// This is the old way of doing this, which is somewhat different from what crypto/ecdh does.
// The latter returns X coordinate bytes while we join X and Y and hash the result.
func (pk *PrivateKey) getSharedEncryptionKeySecp256k1_Legacy(counterParty *PublicKey) []byte {
x, y := crypto.S256().ScalarMult(counterParty.X(), counterParty.Y(),
pk.privateKey.D.Bytes())
b := bytes.Join([][]byte{x.Bytes(), y.Bytes()}, nil)
hash := sha256.Sum256(b)
return hash[:]
}
// getSharedEncryptionKeySecp256k1 computes a shared encryption key for SECP256K1 curve
// in a way that is consistent with how it's done in crypto/ecdh.
func (pk *PrivateKey) getSharedEncryptionKeySecp256k1(counterParty *PublicKey) []byte {
x, _ := crypto.S256().ScalarMult(counterParty.X(), counterParty.Y(),
pk.privateKey.D.Bytes())
return x.Bytes()
}
func encrypt(key []byte, content []byte) ([]byte, error) {
c, err := aes.NewCipher(key[:32]) // The key must be 32 bytes long.
if err != nil {
return nil, fmt.Errorf("failed to create cipher: %v", err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, fmt.Errorf("failed to create GCM: %v", err)
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, fmt.Errorf("failed to populate nonce: %v", err)
}
return gcm.Seal(nonce, nonce, content, nil), nil
}
// EncryptSymmetric encrypts content using this private key. The same private key
// must be used for decryption.
// Encryption is done using AES-256 with CGM cipher.
func (pk *PrivateKey) EncryptSymmetric(content []byte) ([]byte, error) {
key := sha256.Sum256(pk.privateKey.D.Bytes())
return encrypt(key[:], content)
}
func decrypt(key []byte, content []byte) ([]byte, error) {
c, err := aes.NewCipher(key[:32]) // The key must be 32 bytes long.
if err != nil {
return nil, fmt.Errorf("failed to create cipher: %v", err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, fmt.Errorf("failed to create GCM: %v", err)
}
nonceSize := gcm.NonceSize()
if len(content) < nonceSize {
return nil, fmt.Errorf("invalid content")
}
nonce, ciphertext := content[:nonceSize], content[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("failed to decrypt: %v", err)
}
return plaintext, nil
}
// DecryptSymmetric decrypts the content that was previously encrypted using this private key.
// Decryption is done using AES-256 with CGM cipher.
func (pk *PrivateKey) DecryptSymmetric(content []byte) ([]byte, error) {
key := sha256.Sum256(pk.privateKey.D.Bytes())
return decrypt(key[:], content)
}
func encryptWithPassphrase(passphrase string, content []byte) ([]byte, error) {
key, salt, err := deriveKey([]byte(passphrase), nil)
if err != nil {
return nil, err
}
ciphertext, err := encrypt(key, content)
if err != nil {
return nil, err
}
ciphertext = append(ciphertext, salt...)
return ciphertext, nil
}
// EncryptKeyWithPassphrase encrypts this private key using a passphrase.
// Encryption is done using AES-256 with CGM cipher, with a key derived from the passphrase.
func (pk *PrivateKey) EncryptKeyWithPassphrase(passphrase string) ([]byte, error) {
return encryptWithPassphrase(passphrase, pk.privateKey.D.Bytes())
}
// Mnemonic returns a mnemonic phrase which can be used to recover this private key.
func (pk *PrivateKey) Mnemonic() (string, error) {
if pk.Curve() != SECP256K1 && pk.Curve() != P256 {
return "", ErrUnsupportedCurve
}
return bip39.NewMnemonic(padWithZeros(pk.privateKey.D.Bytes(), 32))
}
// Equal returns true if this key is equal to the other key.
func (pk *PrivateKey) Equal(other *PrivateKey) bool {
return pk.privateKey.D.Cmp(other.privateKey.D) == 0
}
// ToECDSA returns this key as crypto/ecdsa private key.
func (pk *PrivateKey) ToECDSA() *ecdsa.PrivateKey {
return pk.privateKey
}
// GetECDHEncryptionKey returns a shared key that can be used to encrypt data
// exchanged by two parties, using Elliptic Curve Diffie-Hellman algorithm (ECDH).
// For Alice and Bob, the key is guaranteed to be the
// same when it's derived from Alice's private key and Bob's public key or
// Alice's public key and Bob's private key.
//
// See https://en.wikipedia.org/wiki/Elliptic-curve_Diffie%E2%80%93Hellman.
func (pk *PrivateKey) GetECDHEncryptionKey(publicKey *PublicKey) ([]byte, error) {
if pk.Curve() != publicKey.Curve() {
return nil, ErrDifferentCurves
}
var privateKey *ecdh.PrivateKey
var pubKey *ecdh.PublicKey
var err error
switch pk.Curve() {
case SECP256K1:
// This curve is not supported by crypto/ecdh, so we have to handle
// it as a special case.
encryptionKey := pk.getSharedEncryptionKeySecp256k1(publicKey)
return padWithZeros(encryptionKey, 32), nil
case P256:
key := padWithZeros(pk.Secret().Bytes(), getKeyLength(pk.Curve()))
privateKey, err = ecdh.P256().NewPrivateKey(key)
if err != nil {
return nil, err
}
pubKey, err = ecdh.P256().NewPublicKey(publicKey.Serialize())
if err != nil {
return nil, err
}
case P384:
key := padWithZeros(pk.Secret().Bytes(), getKeyLength(pk.Curve()))
privateKey, err = ecdh.P384().NewPrivateKey(key)
if err != nil {
return nil, err
}
pubKey, err = ecdh.P384().NewPublicKey(publicKey.Serialize())
if err != nil {
return nil, err
}
case P521:
key := padWithZeros(pk.Secret().Bytes(), getKeyLength(pk.Curve()))
privateKey, err = ecdh.P521().NewPrivateKey(key)
if err != nil {
return nil, err
}
pubKey, err = ecdh.P521().NewPublicKey(publicKey.Serialize())
if err != nil {
return nil, err
}
}
encryptionKey, err := privateKey.ECDH(pubKey)
if err != nil {
return nil, err
}
return encryptionKey, nil
}
func (pk *PrivateKey) EncryptECDH(content []byte, publicKey *PublicKey) ([]byte, error) {
encryptionKey, err := pk.GetECDHEncryptionKey(publicKey)
if err != nil {
return nil, err
}
return encrypt(encryptionKey, content)
}
func (pk *PrivateKey) DecryptECDH(content []byte, publicKey *PublicKey) ([]byte, error) {
encryptionKey, err := pk.GetECDHEncryptionKey(publicKey)
if err != nil {
return nil, err
}
return decrypt(encryptionKey, content)
}
// MarshalToJWK returns the key JWK representation,
// see https://www.rfc-editor.org/rfc/rfc7517.
func (pk *PrivateKey) MarshalToJWK() ([]byte, error) {
xEncoded := base64.StdEncoding.
WithPadding(base64.NoPadding).
EncodeToString(pk.PublicKey().X().Bytes())
yEncoded := base64.StdEncoding.
WithPadding(base64.NoPadding).
EncodeToString(pk.PublicKey().Y().Bytes())
dEncoded := base64.StdEncoding.
WithPadding(base64.NoPadding).
EncodeToString(pk.Secret().Bytes())
return json.MarshalIndent(privateKeyJSON{
Kty: "EC",
Crv: pk.Curve().String(),
X: xEncoded,
Y: yEncoded,
D: dEncoded,
}, "", " ")
}
// deriveKey creates symmetric encryption key and salt (both are 32 bytes long)
// from password. If salt is not given (nil), new random one is created.
func deriveKey(password, salt []byte) ([]byte, []byte, error) {
if salt == nil {
salt = make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
return nil, nil, err
}
}
key, err := scrypt.Key(password, salt, 16384, 8, 1, 32)
if err != nil {
return nil, nil, err
}
return key, salt, nil
}
func padWithZeros(b []byte, l int) []byte {
for len(b) < l {
b = append([]byte{0}, b...)
}
return b
}
// Everything below is deprecated.
// NewRandomPrivateKey creates a new random private key using SECP256K1 curve.
//
// Deprecated: Use GeneratePrivateKey instead.
func NewRandomPrivateKey() (*PrivateKey, error) {
return GeneratePrivateKey(SECP256K1)
}
// NewPrivateKey returns new private key created from the secret using SECP256K1 curve.
//
// Deprecated: Use CreatePrivateKey instead.
func NewPrivateKey(secret *big.Int) *PrivateKey {
return CreatePrivateKey(SECP256K1, secret)
}
// NewPrivateKeyFromPassword creates a new private key from password and salt using SECP256K1 curve.
//
// Deprecated: Use CreatePrivateKeyFromPassword.
func NewPrivateKeyFromPassword(password, salt []byte) *PrivateKey {
return CreatePrivateKeyFromPassword(SECP256K1, password, salt)
}
// NewPrivateKeyFromEncryptedWithPassphrase creates a new private key using SECP256K1 curve
// from encrypted private key using the passphrase.
//
// Deprecated: Use CreatePrivateKeyFromEncrypted instead.
func NewPrivateKeyFromEncryptedWithPassphrase(data []byte, passphrase string) (*PrivateKey, error) {
return CreatePrivateKeyFromEncrypted(SECP256K1, data, passphrase)
}
// NewPrivateKeyFromFile loads private key using SECP256K1 curve
// from file and decrypts it using the given passphrase.
// If the passphrase is an empty string, no decryption is done (the file content is assumed
// to be not encrypted).
//
// Deprecated: Use CreatePrivateKeyFromFile instead.
func NewPrivateKeyFromFile(fileName string, passphrase string) (*PrivateKey, error) {
b, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to load private key: %w", err)
}
if len(b) < 32 {
return nil, fmt.Errorf("invalid private key length")
}
if passphrase != "" {
return NewPrivateKeyFromEncryptedWithPassphrase(b, passphrase)
}
if len(b) != 32 {
return nil, fmt.Errorf("invalid private key length")
}
secret := new(big.Int)
secret.SetBytes(b)
return NewPrivateKey(secret), nil
}
// NewPrivateKeyFromMnemonic creates private key on SECP256K1 curve from a mnemonic phrase.
//
// Deprecated: Use CreatePrivateKeyFromMnemonic instead.
func NewPrivateKeyFromMnemonic(mnemonic string) (*PrivateKey, error) {
return CreatePrivateKeyFromMnemonic(SECP256K1, mnemonic)
}
// Encrypt encrypts content with a shared key derived from this private key and the
// counter party public key. Works only on secp256k1 curve.
//
// Deprecated: Use EncryptECDH instead, which works on all supported curves.
// Notice that Encrypt/Decrypt and EncryptECDH/DecryptECDH are not compatible on
// secp256k1 curve, since they are using different ways of generating shared encryption key.
func (pk *PrivateKey) Encrypt(content []byte, publicKey *PublicKey) ([]byte, error) {
if pk.Curve() != SECP256K1 {
return nil, ErrUnsupportedCurve
}
encryptionKey := pk.getSharedEncryptionKeySecp256k1_Legacy(publicKey)
return encrypt(encryptionKey, content)
}
// Decrypt decrypts content with a shared key derived from this private key and the
// counter party public key.
//
// Deprecated: Use DecryptECDH instead, which works on all supported curves.
// Notice that Encrypt/Decrypt and EncryptECDH/DecryptECDH are not compatible on
// secp256k1 curve, since they are using different ways of generating shared encryption key.
func (pk *PrivateKey) Decrypt(content []byte, publicKey *PublicKey) ([]byte, error) {
if pk.Curve() != SECP256K1 {
return nil, ErrUnsupportedCurve
}
encryptionKey := pk.getSharedEncryptionKeySecp256k1_Legacy(publicKey)
return decrypt(encryptionKey, content)
}