2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-31 08:39:33 -06:00
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
2015-04-21 10:00:30 -05:00
|
|
|
"encoding/hex"
|
2014-12-31 08:39:33 -06:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2015-10-06 11:39:42 -05:00
|
|
|
"strings"
|
2015-02-10 05:29:50 -06:00
|
|
|
|
2015-04-02 14:14:25 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-09-10 10:48:20 -05:00
|
|
|
"github.com/pborman/uuid"
|
2014-12-31 08:39:33 -06:00
|
|
|
)
|
|
|
|
|
2015-04-21 10:00:30 -05:00
|
|
|
const (
|
2015-05-23 20:42:10 -05:00
|
|
|
version = 3
|
2015-04-21 10:00:30 -05:00
|
|
|
)
|
|
|
|
|
2014-12-31 08:39:33 -06:00
|
|
|
type Key struct {
|
2015-01-24 19:07:20 -06:00
|
|
|
Id uuid.UUID // Version 4 "random" for unique id not derived from key data
|
|
|
|
// to simplify lookups we also store the address
|
2015-04-02 14:14:25 -05:00
|
|
|
Address common.Address
|
2014-12-31 08:39:33 -06:00
|
|
|
// we only store privkey as pubkey/address can be derived from it
|
|
|
|
// privkey in this struct is always in plaintext
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:45:45 -06:00
|
|
|
type plainKeyJSON struct {
|
2015-05-10 13:30:02 -05:00
|
|
|
Address string `json:"address"`
|
|
|
|
PrivateKey string `json:"privatekey"`
|
|
|
|
Id string `json:"id"`
|
2015-05-23 20:42:10 -05:00
|
|
|
Version int `json:"version"`
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:42:10 -05:00
|
|
|
type encryptedKeyJSONV3 struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Crypto cryptoJSON
|
|
|
|
Id string `json:"id"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type encryptedKeyJSONV1 struct {
|
2015-05-10 13:30:02 -05:00
|
|
|
Address string `json:"address"`
|
|
|
|
Crypto cryptoJSON
|
|
|
|
Id string `json:"id"`
|
|
|
|
Version string `json:"version"`
|
2015-04-15 06:24:12 -05:00
|
|
|
}
|
|
|
|
|
2015-05-10 13:30:02 -05:00
|
|
|
type cryptoJSON struct {
|
2015-05-23 20:42:10 -05:00
|
|
|
Cipher string `json:"cipher"`
|
|
|
|
CipherText string `json:"ciphertext"`
|
|
|
|
CipherParams cipherparamsJSON `json:"cipherparams"`
|
|
|
|
KDF string `json:"kdf"`
|
|
|
|
KDFParams map[string]interface{} `json:"kdfparams"`
|
|
|
|
MAC string `json:"mac"`
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
2015-05-10 13:30:02 -05:00
|
|
|
type cipherparamsJSON struct {
|
|
|
|
IV string `json:"iv"`
|
2015-04-15 06:24:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type scryptParamsJSON struct {
|
2015-05-10 13:30:02 -05:00
|
|
|
N int `json:"n"`
|
|
|
|
R int `json:"r"`
|
|
|
|
P int `json:"p"`
|
|
|
|
DkLen int `json:"dklen"`
|
|
|
|
Salt string `json:"salt"`
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Key) MarshalJSON() (j []byte, err error) {
|
2015-01-15 10:45:45 -06:00
|
|
|
jStruct := plainKeyJSON{
|
2015-04-21 10:00:30 -05:00
|
|
|
hex.EncodeToString(k.Address[:]),
|
|
|
|
hex.EncodeToString(FromECDSA(k.PrivateKey)),
|
2015-05-10 13:30:02 -05:00
|
|
|
k.Id.String(),
|
|
|
|
version,
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
2015-01-15 10:45:45 -06:00
|
|
|
j, err = json.Marshal(jStruct)
|
2015-01-07 09:06:26 -06:00
|
|
|
return j, err
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Key) UnmarshalJSON(j []byte) (err error) {
|
2015-01-15 10:45:45 -06:00
|
|
|
keyJSON := new(plainKeyJSON)
|
2014-12-31 08:39:33 -06:00
|
|
|
err = json.Unmarshal(j, &keyJSON)
|
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
return err
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
u := new(uuid.UUID)
|
2015-04-21 10:00:30 -05:00
|
|
|
*u = uuid.Parse(keyJSON.Id)
|
2015-01-24 19:07:20 -06:00
|
|
|
k.Id = *u
|
2015-04-21 10:00:30 -05:00
|
|
|
addr, err := hex.DecodeString(keyJSON.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privkey, err := hex.DecodeString(keyJSON.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k.Address = common.BytesToAddress(addr)
|
|
|
|
k.PrivateKey = ToECDSA(privkey)
|
2014-12-31 08:39:33 -06:00
|
|
|
|
2015-04-21 10:00:30 -05:00
|
|
|
return nil
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
2015-03-23 08:00:06 -05:00
|
|
|
func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
|
|
|
|
id := uuid.NewRandom()
|
|
|
|
key := &Key{
|
|
|
|
Id: id,
|
2015-06-04 09:52:23 -05:00
|
|
|
Address: PubkeyToAddress(privateKeyECDSA.PublicKey),
|
2015-03-23 08:00:06 -05:00
|
|
|
PrivateKey: privateKeyECDSA,
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:45:45 -06:00
|
|
|
func NewKey(rand io.Reader) *Key {
|
2015-03-10 19:10:25 -05:00
|
|
|
randBytes := make([]byte, 64)
|
2015-01-19 13:24:30 -06:00
|
|
|
_, err := rand.Read(randBytes)
|
2015-01-15 10:45:45 -06:00
|
|
|
if err != nil {
|
|
|
|
panic("key generation: could not read from random source: " + err.Error())
|
|
|
|
}
|
2014-12-31 08:39:33 -06:00
|
|
|
reader := bytes.NewReader(randBytes)
|
2015-03-10 19:10:25 -05:00
|
|
|
privateKeyECDSA, err := ecdsa.GenerateKey(S256(), reader)
|
2014-12-31 08:39:33 -06:00
|
|
|
if err != nil {
|
2015-03-10 19:10:25 -05:00
|
|
|
panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
2015-03-23 08:00:06 -05:00
|
|
|
return NewKeyFromECDSA(privateKeyECDSA)
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
2015-10-06 11:39:42 -05:00
|
|
|
|
|
|
|
// generate key whose address fits into < 155 bits so it can fit into
|
|
|
|
// the Direct ICAP spec. for simplicity and easier compatibility with
|
|
|
|
// other libs, we retry until the first byte is 0.
|
|
|
|
func NewKeyForDirectICAP(rand io.Reader) *Key {
|
|
|
|
randBytes := make([]byte, 64)
|
|
|
|
_, err := rand.Read(randBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic("key generation: could not read from random source: " + err.Error())
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(randBytes)
|
|
|
|
privateKeyECDSA, err := ecdsa.GenerateKey(S256(), reader)
|
|
|
|
if err != nil {
|
|
|
|
panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
|
|
|
|
}
|
|
|
|
key := NewKeyFromECDSA(privateKeyECDSA)
|
|
|
|
if !strings.HasPrefix(key.Address.Hex(), "0x00") {
|
|
|
|
return NewKeyForDirectICAP(rand)
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|