-
Notifications
You must be signed in to change notification settings - Fork 8
/
crypto.go
589 lines (513 loc) · 17.8 KB
/
crypto.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
package crypto
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"os"
"strings"
"unicode"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/drand/kyber/share"
drand_dkg "github.com/drand/kyber/share/dkg"
"github.com/ethereum/go-ethereum/common"
eth_crypto "github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
types "github.com/wealdtech/go-eth2-types/v2"
util "github.com/wealdtech/go-eth2-util"
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
e2m_core "github.com/bloxapp/eth2-key-manager/core"
e2m_deposit "github.com/bloxapp/eth2-key-manager/eth1_deposit"
)
const (
// b64 encrypted key length is 256
EncryptedKeyLength = 256
// BLSWithdrawalPrefixByte is the BLS withdrawal prefix
BLSWithdrawalPrefixByte = byte(0)
ETH1WithdrawalPrefixByte = byte(1)
)
func init() {
_ = bls.Init(bls.BLS12_381)
_ = bls.SetETHmode(bls.EthModeDraft07)
}
// GenerateKeys creates a random RSA key pair
func GenerateKeys() (*rsa.PrivateKey, *rsa.PublicKey, error) {
pv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
return pv, &pv.PublicKey, nil
}
// SignRSA create a RSA signature for incoming bytes
func SignRSA(sk *rsa.PrivateKey, byts []byte) ([]byte, error) {
r := sha256.Sum256(byts)
return sk.Sign(rand.Reader, r[:], &rsa.PSSOptions{
SaltLength: rsa.PSSSaltLengthAuto,
Hash: crypto.SHA256,
})
}
// Encrypt with secret key (base64) the bytes, return the encrypted key string
func Encrypt(pk *rsa.PublicKey, plainText []byte) ([]byte, error) {
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, pk, plainText)
if err != nil {
return nil, err
}
return encrypted, nil
}
// VerifyRSA verifies RSA signature for incoming message
func VerifyRSA(pk *rsa.PublicKey, msg, signature []byte) error {
r := sha256.Sum256(msg)
return rsa.VerifyPSS(pk, crypto.SHA256, r[:], signature, nil)
}
// ResultToShareSecretKey converts a private share at kyber DKG result to github.com/herumi/bls-eth-go-binary/bls private key
func ResultToShareSecretKey(result *drand_dkg.Result) (*bls.SecretKey, error) {
privShare := result.Key.PriShare()
bytsSk, err := privShare.V.MarshalBinary()
if err != nil {
return nil, err
}
sk := &bls.SecretKey{}
if err := sk.Deserialize(bytsSk); err != nil {
return nil, err
}
return sk, nil
}
// KyberShareToBLSKey converts a kyber private share to github.com/herumi/bls-eth-go-binary/bls private key
func KyberShareToBLSKey(privShare *share.PriShare) (*bls.SecretKey, error) {
bytsSk, err := privShare.V.MarshalBinary()
if err != nil {
return nil, err
}
sk := &bls.SecretKey{}
if err := sk.Deserialize(bytsSk); err != nil {
return nil, err
}
return sk, nil
}
// ResultsToValidatorPK converts a public polynomial at kyber DKG result to github.com/herumi/bls-eth-go-binary/bls public key
func ResultToValidatorPK(result *drand_dkg.Result, suite drand_dkg.Suite) (*bls.PublicKey, error) {
exp := share.NewPubPoly(suite, suite.Point().Base(), result.Key.Commitments())
bytsPK, err := exp.Commit().MarshalBinary()
if err != nil {
return nil, errors.Wrap(err, "could not marshal share")
}
pk := &bls.PublicKey{}
if err := pk.Deserialize(bytsPK); err != nil {
return nil, err
}
return pk, nil
}
// ParseRSAPubkey parses encoded to base64 x509 RSA public key
func ParseRSAPubkey(pk []byte) (*rsa.PublicKey, error) {
operatorKeyByte, err := base64.StdEncoding.DecodeString(string(pk))
if err != nil {
return nil, err
}
pemblock, _ := pem.Decode(operatorKeyByte)
if pemblock == nil {
return nil, errors.New("decode PEM block")
}
pbkey, err := x509.ParsePKIXPublicKey(pemblock.Bytes)
if err != nil {
return nil, err
}
return pbkey.(*rsa.PublicKey), nil
}
func EncodePublicKey(pk *rsa.PublicKey) ([]byte, error) {
pkBytes, err := x509.MarshalPKIXPublicKey(pk)
if err != nil {
return nil, err
}
pemByte := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pkBytes,
},
)
if pemByte == nil {
return nil, fmt.Errorf("failed to encode pub key to pem")
}
return []byte(base64.StdEncoding.EncodeToString(pemByte)), nil
}
// VerifyOwnerNonceSignature check that owner + nonce correctly signed
func VerifyOwnerNonceSignature(sig []byte, owner common.Address, pubKey []byte, nonce uint16) error {
data := fmt.Sprintf("%s:%d", owner.String(), nonce)
hash := eth_crypto.Keccak256([]byte(data))
sign := &bls.Sign{}
if err := sign.Deserialize(sig); err != nil {
return fmt.Errorf("failed to deserialize signature: %w", err)
}
pk := &bls.PublicKey{}
if err := pk.Deserialize(pubKey); err != nil {
return fmt.Errorf("failed to deserialize public key: %w", err)
}
if res := sign.VerifyByte(pk, hash); !res {
return errors.New("failed to verify signature")
}
return nil
}
// ReadEncryptedPrivateKey return rsa private key from secret key
func ReadEncryptedPrivateKey(keyData []byte, password string) (*rsa.PrivateKey, error) {
if strings.TrimSpace(password) == "" {
return nil, errors.New("Password required for encrypted PEM block")
}
// Unmarshal the JSON-encoded data
var data map[string]interface{}
if err := json.Unmarshal(keyData, &data); err != nil {
return nil, fmt.Errorf("parse JSON data: %w", err)
}
// Decrypt the private key using keystorev4
decryptedBytes, err := keystorev4.New().Decrypt(data, password)
if err != nil {
return nil, fmt.Errorf("decrypt private key: %w", err)
}
// Parse the decrypted PEM data
block, _ := pem.Decode(decryptedBytes)
if block == nil {
return nil, errors.New("parse PEM block")
}
// Parse the RSA private key
rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("parse RSA private key: %w", err)
}
return rsaKey, nil
}
// ConvertPemToPrivateKey return rsa private key from secret key
func ConvertPemToPrivateKey(skPem string) (*rsa.PrivateKey, error) {
block, _ := pem.Decode([]byte(skPem))
if block == nil {
return nil, errors.New("decode PEM block")
}
b := block.Bytes
return parsePrivateKey(b)
}
// parsePrivateKey parses an encoded x509 RSA private key
func parsePrivateKey(derBytes []byte) (*rsa.PrivateKey, error) {
parsedSk, err := x509.ParsePKCS1PrivateKey(derBytes)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse private key")
}
return parsedSk, nil
}
// RecoverValidatorPublicKey recovers a BLS master public key (validator pub key) from provided partial pub keys
func RecoverValidatorPublicKey(IDs []uint64, sharePks []*bls.PublicKey) (*bls.PublicKey, error) {
if len(IDs) != len(sharePks) {
return nil, fmt.Errorf("inconsistent IDs len")
}
validatorRecoveredPK := bls.PublicKey{}
idVec := make([]bls.ID, 0)
pkVec := make([]bls.PublicKey, 0)
for i, index := range IDs {
blsID := bls.ID{}
if err := blsID.SetDecString(fmt.Sprintf("%d", index)); err != nil {
return nil, err
}
idVec = append(idVec, blsID)
pkVec = append(pkVec, *sharePks[i])
}
if err := validatorRecoveredPK.Recover(pkVec, idVec); err != nil {
return nil, fmt.Errorf("error recovering validator pub key from shares")
}
return &validatorRecoveredPK, nil
}
// RecoverMasterSig recovers a BLS master signature from T-threshold partial signatures
func RecoverMasterSig(IDs []uint64, sigDepositShares []*bls.Sign) (*bls.Sign, error) {
if len(IDs) != len(sigDepositShares) {
return nil, fmt.Errorf("inconsistent IDs len")
}
reconstructedDepositMasterSig := bls.Sign{}
idVec := make([]bls.ID, 0)
sigVec := make([]bls.Sign, 0)
for i, index := range IDs {
blsID := bls.ID{}
if err := blsID.SetDecString(fmt.Sprintf("%d", index)); err != nil {
return nil, err
}
idVec = append(idVec, blsID)
sigVec = append(sigVec, *sigDepositShares[i])
}
if err := reconstructedDepositMasterSig.Recover(sigVec, idVec); err != nil {
return nil, fmt.Errorf("deposit root signature recovered from shares is invalid")
}
return &reconstructedDepositMasterSig, nil
}
// DepositData crates and signs a ETH2 deposit message
func DepositData(masterSig, withdrawalPubKey, publicKey []byte, network e2m_core.Network, amount phase0.Gwei) (*phase0.DepositData, [32]byte, error) {
if !e2m_deposit.IsSupportedDepositNetwork(network) {
return nil, [32]byte{}, fmt.Errorf("network %s is not supported", network)
}
depositMessage := &phase0.DepositMessage{
WithdrawalCredentials: ETH1WithdrawalCredentialsHash(withdrawalPubKey),
Amount: amount,
}
copy(depositMessage.PublicKey[:], publicKey)
objRoot, err := depositMessage.HashTreeRoot()
if err != nil {
return nil, [32]byte{}, fmt.Errorf("failed to determine the root hash of deposit data: %s", err)
}
// Compute domain
genesisForkVersion := network.GenesisForkVersion()
domain, err := types.ComputeDomain(types.DomainDeposit, genesisForkVersion[:], types.ZeroGenesisValidatorsRoot)
if err != nil {
return nil, [32]byte{}, fmt.Errorf("failed to calculate domain: %s", err)
}
signingData := phase0.SigningData{
ObjectRoot: objRoot,
}
copy(signingData.Domain[:], domain)
signedDepositData := &phase0.DepositData{
Amount: amount,
WithdrawalCredentials: depositMessage.WithdrawalCredentials,
}
copy(signedDepositData.PublicKey[:], publicKey)
copy(signedDepositData.Signature[:], masterSig)
depositDataRoot, err := signedDepositData.HashTreeRoot()
if err != nil {
return nil, [32]byte{}, fmt.Errorf("failed to determine the root hash of deposit data: %s", err)
}
return signedDepositData, depositDataRoot, nil
}
// withdrawalCredentialsHash forms a 32 byte hash of the withdrawal public
// address.
//
// The specification is as follows:
//
// withdrawal_credentials[:1] == BLS_WITHDRAWAL_PREFIX_BYTE
// withdrawal_credentials[1:] == hash(withdrawal_pubkey)[1:]
//
// where withdrawal_credentials is of type bytes32.
func BLSWithdrawalCredentialsHash(withdrawalPubKey []byte) []byte {
h := util.SHA256(withdrawalPubKey)
return append([]byte{BLSWithdrawalPrefixByte}, h[1:]...)[:32]
}
func ETH1WithdrawalCredentialsHash(withdrawalAddr []byte) []byte {
withdrawalCredentials := make([]byte, 32)
copy(withdrawalCredentials[:1], []byte{ETH1WithdrawalPrefixByte})
// withdrawalCredentials[1:12] == b'\x00' * 11 // this is not needed since cells are zeroed anyway
copy(withdrawalCredentials[12:], withdrawalAddr)
return withdrawalCredentials
}
// DepositDataRoot computes a deposit root used for ETH2 deposit message
func DepositDataRoot(withdrawalPubKey []byte, publicKey *bls.PublicKey, network e2m_core.Network, amount phase0.Gwei) ([]byte, error) {
if !e2m_deposit.IsSupportedDepositNetwork(network) {
return nil, fmt.Errorf("network %s is not supported", network)
}
depositMessage := &phase0.DepositMessage{
WithdrawalCredentials: ETH1WithdrawalCredentialsHash(withdrawalPubKey),
Amount: amount,
}
copy(depositMessage.PublicKey[:], publicKey.Serialize())
objRoot, err := depositMessage.HashTreeRoot()
if err != nil {
return nil, fmt.Errorf("failed to determine the root hash of deposit data: %s", err)
}
// Compute domain
genesisForkVersion := network.GenesisForkVersion()
domain, err := types.ComputeDomain(types.DomainDeposit, genesisForkVersion[:], types.ZeroGenesisValidatorsRoot)
if err != nil {
return nil, fmt.Errorf("failed to calculate domain: %s", err)
}
signingData := phase0.SigningData{
ObjectRoot: objRoot,
}
copy(signingData.Domain[:], domain)
root, err := signingData.HashTreeRoot()
if err != nil {
return nil, fmt.Errorf("failed to determine the root hash of signing container: %s", err)
}
return root[:], nil
}
// VerifyDepositData reconstructs and checks BLS signatures for ETH2 deposit message
func VerifyDepositData(depositData *phase0.DepositData, network e2m_core.Network) (bool, error) {
depositMessage := &phase0.DepositMessage{
WithdrawalCredentials: depositData.WithdrawalCredentials,
Amount: depositData.Amount,
}
copy(depositMessage.PublicKey[:], depositData.PublicKey[:])
depositMsgRoot, err := depositMessage.HashTreeRoot()
if err != nil {
return false, err
}
sigBytes := make([]byte, len(depositData.Signature))
copy(sigBytes, depositData.Signature[:])
sig, err := types.BLSSignatureFromBytes(sigBytes)
if err != nil {
return false, err
}
container := &phase0.SigningData{
ObjectRoot: depositMsgRoot,
}
genesisForkVersion := network.GenesisForkVersion()
domain, err := types.ComputeDomain(types.DomainDeposit, genesisForkVersion[:], types.ZeroGenesisValidatorsRoot)
if err != nil {
return false, err
}
copy(container.Domain[:], domain)
signingRoot, err := container.HashTreeRoot()
if err != nil {
return false, err
}
var pubkeyBytes [48]byte
copy(pubkeyBytes[:], depositData.PublicKey[:])
pubkey, err := types.BLSPublicKeyFromBytes(pubkeyBytes[:])
if err != nil {
return false, err
}
return sig.Verify(signingRoot[:], pubkey), nil
}
// SignDepositData creates a BLS signature for ETH2 deposit message
func SignDepositData(validationKey *bls.SecretKey, withdrawalPubKey []byte, validatorPublicKey *bls.PublicKey, network e2m_core.Network, amount phase0.Gwei) (*bls.Sign, []byte, error) {
if !e2m_deposit.IsSupportedDepositNetwork(network) {
return nil, nil, errors.Errorf("Network %s is not supported", network)
}
depositMessage := &phase0.DepositMessage{
WithdrawalCredentials: ETH1WithdrawalCredentialsHash(withdrawalPubKey),
Amount: amount,
}
copy(depositMessage.PublicKey[:], validatorPublicKey.Serialize())
objRoot, err := depositMessage.HashTreeRoot()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to determine the root hash of deposit data")
}
// Compute domain
genesisForkVersion := network.GenesisForkVersion()
domain, err := types.ComputeDomain(types.DomainDeposit, genesisForkVersion[:], types.ZeroGenesisValidatorsRoot)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to calculate domain")
}
signingData := phase0.SigningData{
ObjectRoot: objRoot,
}
copy(signingData.Domain[:], domain)
root, err := signingData.HashTreeRoot()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to determine the root hash of signing container")
}
// Sign
sig := validationKey.SignByte(root[:])
if err != nil {
return nil, nil, errors.Wrap(err, "failed to sign the root")
}
return sig, root[:], nil
}
// VerifyPartialSigs verifies provided partial BLS signatures
func VerifyPartialSigs(sigShares []*bls.Sign, sharePks []*bls.PublicKey, data []byte) error {
if len(sigShares) != len(sharePks) {
return fmt.Errorf("inconsistent slice lengths")
}
res := make([]bool, len(sigShares))
for i := 0; i < len(sigShares); i++ {
if sigShares[i].VerifyByte(sharePks[i], data) {
res[i] = true
}
}
var errStr = ""
for index, r := range res {
if !r {
errStr += fmt.Sprintf(" id %d sig %x root %x, errStr", index, sigShares[index].Serialize(), data)
continue
}
}
if errStr != "" {
return fmt.Errorf("error verifying partial deposit signature: %v", errStr)
}
return nil
}
// EncryptedPrivateKey reads an encoded RSA priv key from path encrypted with password
func EncryptedPrivateKey(path, pass string) (*rsa.PrivateKey, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
privateKey, err := ReadEncryptedPrivateKey(data, pass)
if err != nil {
return nil, err
}
return privateKey, nil
}
// NewID generates a random ID from 2 random concat UUIDs
func NewID() [24]byte {
var id [24]byte
b := uuid.New()
copy(id[:12], b[:])
b = uuid.New()
copy(id[12:], b[:])
return id
}
// GenerateSecurePassword randomly generates a password consisting of digits + english letters
func GenerateSecurePassword() (string, error) {
const alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var pass []rune
p := make([]byte, 64)
if _, err := rand.Reader.Read(p); err != nil {
return "", err
}
hash := sha512.Sum512(p)
for _, r := range string(hash[:]) {
if unicode.IsDigit(r) || strings.Contains(alpha, strings.ToLower(string(r))) {
pass = append(pass, r)
}
}
return string(pass), nil
}
// ReconstructSignatures receives a map of user indexes and serialized bls.Sign.
// It then reconstructs the original threshold signature using lagrange interpolation
func ReconstructSignatures(IDs []uint64, signatures [][]byte) (*bls.Sign, error) {
if len(IDs) != len(signatures) {
return nil, fmt.Errorf("inconsistent IDs len")
}
reconstructedSig := bls.Sign{}
idVec := make([]bls.ID, 0)
sigVec := make([]bls.Sign, 0)
for i, index := range IDs {
blsID := bls.ID{}
err := blsID.SetDecString(fmt.Sprintf("%d", index))
if err != nil {
return nil, err
}
idVec = append(idVec, blsID)
blsSig := bls.Sign{}
err = blsSig.Deserialize(signatures[i])
if err != nil {
return nil, err
}
sigVec = append(sigVec, blsSig)
}
err := reconstructedSig.Recover(sigVec, idVec)
return &reconstructedSig, err
}
// VerifyReconstructedSignature checks a reconstructed msg master signature against validator public key
func VerifyReconstructedSignature(sig *bls.Sign, validatorPubKey, msg []byte) error {
pk := &bls.PublicKey{}
if err := pk.Deserialize(validatorPubKey); err != nil {
return errors.Wrap(err, "could not deserialize validator pk")
}
// verify reconstructed sig
if res := sig.VerifyByte(pk, msg); !res {
return errors.New("could not reconstruct a valid signature")
}
return nil
}
func ReadEncryptedRSAKey(privKeyPath, privKeyPassPath string) (*rsa.PrivateKey, error) {
keyStorePassword, err := os.ReadFile(privKeyPassPath)
if err != nil {
return nil, fmt.Errorf("😥 Cant read operator`s key file: %s", err)
}
return EncryptedPrivateKey(privKeyPath, string(keyStorePassword))
}
func EncryptPrivateKey(priv []byte, keyStorePassword string) ([]byte, error) {
encryptedData, err := keystorev4.New().Encrypt(priv, keyStorePassword)
if err != nil {
return nil, fmt.Errorf("😥 Failed to encrypt private key: %s", err)
}
return json.Marshal(encryptedData)
}