Skip to content

Commit f272879

Browse files
authored
Merge pull request #14565 from karalabe/relax-privkey-checks
accounts/keystore, crypto: don't enforce key checks on existing keyfiles
2 parents 799a469 + 72dd51e commit f272879

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

accounts/keystore/keystore_passphrase.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,8 @@ func DecryptKey(keyjson []byte, auth string) (*Key, error) {
182182
if err != nil {
183183
return nil, err
184184
}
185-
key, err := crypto.ToECDSA(keyBytes)
186-
if err != nil {
187-
return nil, err
188-
}
185+
key := crypto.ToECDSAUnsafe(keyBytes)
186+
189187
return &Key{
190188
Id: uuid.UUID(keyId),
191189
Address: crypto.PubkeyToAddress(key.PublicKey),

accounts/keystore/presale.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error
7474
return nil, err
7575
}
7676
ethPriv := crypto.Keccak256(plainText)
77-
ecKey, err := crypto.ToECDSA(ethPriv)
78-
if err != nil {
79-
return nil, err
80-
}
77+
ecKey := crypto.ToECDSAUnsafe(ethPriv)
78+
8179
key = &Key{
8280
Id: nil,
8381
Address: crypto.PubkeyToAddress(ecKey.PublicKey),

crypto/crypto.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ func Keccak512(data ...[]byte) []byte {
6868
return d.Sum(nil)
6969
}
7070

71-
// Deprecated: For backward compatibility as other packages depend on these
72-
func Sha3Hash(data ...[]byte) common.Hash { return Keccak256Hash(data...) }
73-
7471
// Creates an ethereum address given the bytes and the nonce
7572
func CreateAddress(b common.Address, nonce uint64) common.Address {
7673
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
@@ -79,21 +76,37 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
7976

8077
// ToECDSA creates a private key with the given D value.
8178
func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) {
79+
return toECDSA(d, true)
80+
}
81+
82+
// ToECDSAUnsafe blidly converts a binary blob to a private key. It should almost
83+
// never be used unless you are sure the input is valid and want to avoid hitting
84+
// errors due to bad origin encoding (0 prefixes cut off).
85+
func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey {
86+
priv, _ := toECDSA(d, false)
87+
return priv
88+
}
89+
90+
// toECDSA creates a private key with the given D value. The strict parameter
91+
// controls whether the key's length should be enforced at the curve size or
92+
// it can also accept legacy encodings (0 prefixes).
93+
func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
8294
priv := new(ecdsa.PrivateKey)
8395
priv.PublicKey.Curve = S256()
84-
if 8*len(d) != priv.Params().BitSize {
96+
if strict && 8*len(d) != priv.Params().BitSize {
8597
return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize)
8698
}
8799
priv.D = new(big.Int).SetBytes(d)
88100
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
89101
return priv, nil
90102
}
91103

92-
func FromECDSA(prv *ecdsa.PrivateKey) []byte {
93-
if prv == nil {
104+
// FromECDSA exports a private key into a binary dump.
105+
func FromECDSA(priv *ecdsa.PrivateKey) []byte {
106+
if priv == nil {
94107
return nil
95108
}
96-
return math.PaddedBigBytes(prv.D, 32)
109+
return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8)
97110
}
98111

99112
func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
@@ -121,7 +134,6 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
121134
}
122135

123136
// LoadECDSA loads a secp256k1 private key from the given file.
124-
// The key data is expected to be hex-encoded.
125137
func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
126138
buf := make([]byte, 64)
127139
fd, err := os.Open(file)

crypto/crypto_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232
3636
// These tests are sanity checks.
3737
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
3838
// and that the sha3 library uses keccak-f permutation.
39-
func TestSha3Hash(t *testing.T) {
39+
func TestKeccak256Hash(t *testing.T) {
4040
msg := []byte("abc")
4141
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
4242
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)

0 commit comments

Comments
 (0)