aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/keystore/passphrase.go
blob: a0b6cf5385d21f96835ba5d32be57003b65b2b8b (plain) (blame)
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
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// 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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

/*

This key store behaves as KeyStorePlain with the difference that
the private key is encrypted and on disk uses another JSON encoding.

The crypto is documented at https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition

*/

package keystore

import (
    "bytes"
    "crypto/aes"
    "crypto/rand"
    "crypto/sha256"
    "encoding/hex"
    "encoding/json"
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "path/filepath"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/math"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/pborman/uuid"
    "golang.org/x/crypto/pbkdf2"
    "golang.org/x/crypto/scrypt"
)

const (
    keyHeaderKDF = "scrypt"

    // StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
    // memory and taking approximately 1s CPU time on a modern processor.
    StandardScryptN = 1 << 18

    // StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
    // memory and taking approximately 1s CPU time on a modern processor.
    StandardScryptP = 1

    // LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
    // memory and taking approximately 100ms CPU time on a modern processor.
    LightScryptN = 1 << 12

    // LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
    // memory and taking approximately 100ms CPU time on a modern processor.
    LightScryptP = 6

    scryptR     = 8
    scryptDKLen = 32
)

type keyStorePassphrase struct {
    keysDirPath string
    scryptN     int
    scryptP     int
    // skipKeyFileVerification disables the security-feature which does
    // reads and decrypts any newly created keyfiles. This should be 'false' in all
    // cases except tests -- setting this to 'true' is not recommended.
    skipKeyFileVerification bool
}

func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
    // Load the key from the keystore and decrypt its contents
    keyjson, err := ioutil.ReadFile(filename)
    if err != nil {
        return nil, err
    }
    key, err := DecryptKey(keyjson, auth)
    if err != nil {
        return nil, err
    }
    // Make sure we're really operating on the requested key (no swap attacks)
    if key.Address != addr {
        return nil, fmt.Errorf("key content mismatch: have account %x, want %x", key.Address, addr)
    }
    return key, nil
}

// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
    _, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP, false}, rand.Reader, auth)
    return a.Address, err
}

func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {
    keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
    if err != nil {
        return err
    }
    // Write into temporary file
    tmpName, err := writeTemporaryKeyFile(filename, keyjson)
    if err != nil {
        return err
    }
    if !ks.skipKeyFileVerification {
        // Verify that we can decrypt the file with the given password.
        _, err = ks.GetKey(key.Address, tmpName, auth)
        if err != nil {
            msg := "An error was encountered when saving and verifying the keystore file. \n" +
                "This indicates that the keystore is corrupted. \n" +
                "The corrupted file is stored at \n%v\n" +
                "Please file a ticket at:\n\n" +
                "https://github.com/ethereum/go-ethereum/issues." +
                "The error was : %s"
            return fmt.Errorf(msg, tmpName, err)
        }
    }
    return os.Rename(tmpName, filename)
}

func (ks keyStorePassphrase) JoinPath(filename string) string {
    if filepath.IsAbs(filename) {
        return filename
    }
    return filepath.Join(ks.keysDirPath, filename)
}

// Encryptdata encrypts the data given as 'data' with the password 'auth'.
func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error) {

    salt := make([]byte, 32)
    if _, err := io.ReadFull(rand.Reader, salt); err != nil {
        panic("reading from crypto/rand failed: " + err.Error())
    }
    derivedKey, err := scrypt.Key(auth, salt, scryptN, scryptR, scryptP, scryptDKLen)
    if err != nil {
        return CryptoJSON{}, err
    }
    encryptKey := derivedKey[:16]

    iv := make([]byte, aes.BlockSize) // 16
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        panic("reading from crypto/rand failed: " + err.Error())
    }
    cipherText, err := aesCTRXOR(encryptKey, data, iv)
    if err != nil {
        return CryptoJSON{}, err
    }
    mac := crypto.Keccak256(derivedKey[16:32], cipherText)

    scryptParamsJSON := make(map[string]interface{}, 5)
    scryptParamsJSON["n"] = scryptN
    scryptParamsJSON["r"] = scryptR
    scryptParamsJSON["p"] = scryptP
    scryptParamsJSON["dklen"] = scryptDKLen
    scryptParamsJSON["salt"] = hex.EncodeToString(salt)
    cipherParamsJSON := cipherparamsJSON{
        IV: hex.EncodeToString(iv),
    }

    cryptoStruct := CryptoJSON{
        Cipher:       "aes-128-ctr",
        CipherText:   hex.EncodeToString(cipherText),
        CipherParams: cipherParamsJSON,
        KDF:          keyHeaderKDF,
        KDFParams:    scryptParamsJSON,
        MAC:          hex.EncodeToString(mac),
    }
    return cryptoStruct, nil
}

// EncryptKey encrypts a key using the specified scrypt parameters into a json
// blob that can be decrypted later on.
func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error) {
    keyBytes := math.PaddedBigBytes(key.PrivateKey.D, 32)
    cryptoStruct, err := EncryptDataV3(keyBytes, []byte(auth), scryptN, scryptP)
    if err != nil {
        return nil, err
    }
    encryptedKeyJSONV3 := encryptedKeyJSONV3{
        hex.EncodeToString(key.Address[:]),
        cryptoStruct,
        key.Id.String(),
        version,
    }
    return json.Marshal(encryptedKeyJSONV3)
}

// DecryptKey decrypts a key from a json blob, returning the private key itself.
func DecryptKey(keyjson []byte, auth string) (*Key, error) {
    // Parse the json into a simple map to fetch the key version
    m := make(map[string]interface{})
    if err := json.Unmarshal(keyjson, &m); err != nil {
        return nil, err
    }
    // Depending on the version try to parse one way or another
    var (
        keyBytes, keyId []byte
        err             error
    )
    if version, ok := m["version"].(string); ok && version == "1" {
        k := new(encryptedKeyJSONV1)
        if err := json.Unmarshal(keyjson, k); err != nil {
            return nil, err
        }
        keyBytes, keyId, err = decryptKeyV1(k, auth)
    } else {
        k := new(encryptedKeyJSONV3)
        if err := json.Unmarshal(keyjson, k); err != nil {
            return nil, err
        }
        keyBytes, keyId, err = decryptKeyV3(k, auth)
    }
    // Handle any decryption errors and return the key
    if err != nil {
        return nil, err
    }
    key := crypto.ToECDSAUnsafe(keyBytes)

    return &Key{
        Id:         uuid.UUID(keyId),
        Address:    crypto.PubkeyToAddress(key.PublicKey),
        PrivateKey: key,
    }, nil
}

func DecryptDataV3(cryptoJson CryptoJSON, auth string) ([]byte, error) {
    if cryptoJson.Cipher != "aes-128-ctr" {
        return nil, fmt.Errorf("Cipher not supported: %v", cryptoJson.Cipher)
    }
    mac, err := hex.DecodeString(cryptoJson.MAC)
    if err != nil {
        return nil, err
    }

    iv, err := hex.DecodeString(cryptoJson.CipherParams.IV)
    if err != nil {
        return nil, err
    }

    cipherText, err := hex.DecodeString(cryptoJson.CipherText)
    if err != nil {
        return nil, err
    }

    derivedKey, err := getKDFKey(cryptoJson, auth)
    if err != nil {
        return nil, err
    }

    calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
    if !bytes.Equal(calculatedMAC, mac) {
        return nil, ErrDecrypt
    }

    plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
    if err != nil {
        return nil, err
    }
    return plainText, err
}

func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) {
    if keyProtected.Version != version {
        return nil, nil, fmt.Errorf("Version not supported: %v", keyProtected.Version)
    }
    keyId = uuid.Parse(keyProtected.Id)
    plainText, err := DecryptDataV3(keyProtected.Crypto, auth)
    if err != nil {
        return nil, nil, err
    }
    return plainText, keyId, err
}

func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byte, keyId []byte, err error) {
    keyId = uuid.Parse(keyProtected.Id)
    mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
    if err != nil {
        return nil, nil, err
    }

    iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
    if err != nil {
        return nil, nil, err
    }

    cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
    if err != nil {
        return nil, nil, err
    }

    derivedKey, err := getKDFKey(keyProtected.Crypto, auth)
    if err != nil {
        return nil, nil, err
    }

    calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
    if !bytes.Equal(calculatedMAC, mac) {
        return nil, nil, ErrDecrypt
    }

    plainText, err := aesCBCDecrypt(crypto.Keccak256(derivedKey[:16])[:16], cipherText, iv)
    if err != nil {
        return nil, nil, err
    }
    return plainText, keyId, err
}

func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
    authArray := []byte(auth)
    salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
    if err != nil {
        return nil, err
    }
    dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])

    if cryptoJSON.KDF == keyHeaderKDF {
        n := ensureInt(cryptoJSON.KDFParams["n"])
        r := ensureInt(cryptoJSON.KDFParams["r"])
        p := ensureInt(cryptoJSON.KDFParams["p"])
        return scrypt.Key(authArray, salt, n, r, p, dkLen)

    } else if cryptoJSON.KDF == "pbkdf2" {
        c := ensureInt(cryptoJSON.KDFParams["c"])
        prf := cryptoJSON.KDFParams["prf"].(string)
        if prf != "hmac-sha256" {
            return nil, fmt.Errorf("Unsupported PBKDF2 PRF: %s", prf)
        }
        key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New)
        return key, nil
    }

    return nil, fmt.Errorf("Unsupported KDF: %s", cryptoJSON.KDF)
}

// TODO: can we do without this when unmarshalling dynamic JSON?
// why do integers in KDF params end up as float64 and not int after
// unmarshal?
func ensureInt(x interface{}) int {
    res, ok := x.(int)
    if !ok {
        res = int(x.(float64))
    }
    return res
}