aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorFelix Lange <fjl@users.noreply.github.com>2017-05-25 04:30:47 +0800
committerGitHub <noreply@github.com>2017-05-25 04:30:47 +0800
commitef25b826e655f8e6a57fc7a05454bf356382bd5f (patch)
treec843d8b5ca0064804e55f5d66dd910c0c106fc05 /crypto
parent261b3e235160d30cc7176e02fd0a43f2b60409c6 (diff)
parent136f78ff0a324f7f79296143a6ab7c2dd8a2c37d (diff)
downloadgo-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.tar
go-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.tar.gz
go-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.tar.bz2
go-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.tar.lz
go-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.tar.xz
go-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.tar.zst
go-tangerine-ef25b826e655f8e6a57fc7a05454bf356382bd5f.zip
Merge pull request #14502 from karalabe/mobile-import-ecdsa
Enforce 256 bit keys on raw import, support raw mobile imports
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.go27
1 files changed, 12 insertions, 15 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 0b8c06008..d38ffd0d5 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -22,12 +22,14 @@ import (
"crypto/rand"
"encoding/hex"
"errors"
+ "fmt"
"io"
"io/ioutil"
"math/big"
"os"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
)
@@ -76,23 +78,22 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
}
// ToECDSA creates a private key with the given D value.
-func ToECDSA(prv []byte) *ecdsa.PrivateKey {
- if len(prv) == 0 {
- return nil
- }
-
+func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) {
priv := new(ecdsa.PrivateKey)
priv.PublicKey.Curve = S256()
- priv.D = new(big.Int).SetBytes(prv)
- priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(prv)
- return priv
+ if 8*len(d) != priv.Params().BitSize {
+ return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize)
+ }
+ priv.D = new(big.Int).SetBytes(d)
+ priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
+ return priv, nil
}
func FromECDSA(prv *ecdsa.PrivateKey) []byte {
if prv == nil {
return nil
}
- return prv.D.Bytes()
+ return math.PaddedBigBytes(prv.D, 32)
}
func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
@@ -116,10 +117,7 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
if err != nil {
return nil, errors.New("invalid hex string")
}
- if len(b) != 32 {
- return nil, errors.New("invalid length, need 256 bits")
- }
- return ToECDSA(b), nil
+ return ToECDSA(b)
}
// LoadECDSA loads a secp256k1 private key from the given file.
@@ -139,8 +137,7 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
if err != nil {
return nil, err
}
-
- return ToECDSA(key), nil
+ return ToECDSA(key)
}
// SaveECDSA saves a secp256k1 private key to the given file with