Skip to content

Commit 6b2a03f

Browse files
committed
Merge pull request #1085 from Gustav-Simonsson/key_store_v3
crypto: key store v3
2 parents 8774fdc + d23ec6c commit 6b2a03f

File tree

8 files changed

+381
-91
lines changed

8 files changed

+381
-91
lines changed

common/test_utils.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package common
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
)
8+
9+
// LoadJSON reads the given file and unmarshals its content.
10+
func LoadJSON(file string, val interface{}) error {
11+
content, err := ioutil.ReadFile(file)
12+
if err != nil {
13+
return err
14+
}
15+
if err := json.Unmarshal(content, val); err != nil {
16+
if syntaxerr, ok := err.(*json.SyntaxError); ok {
17+
line := findLine(content, syntaxerr.Offset)
18+
return fmt.Errorf("JSON syntax error at %v:%v: %v", file, line, err)
19+
}
20+
return fmt.Errorf("JSON unmarshal error in %v: %v", file, err)
21+
}
22+
return nil
23+
}
24+
25+
// findLine returns the line number for the given offset into data.
26+
func findLine(data []byte, offset int64) (line int) {
27+
line = 1
28+
for i, r := range string(data) {
29+
if int64(i) >= offset {
30+
return
31+
}
32+
if r == '\n' {
33+
line++
34+
}
35+
}
36+
return
37+
}

crypto/crypto.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,31 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error
258258
return key, err
259259
}
260260

261-
func aesCBCDecrypt(key []byte, cipherText []byte, iv []byte) (plainText []byte, err error) {
261+
// AES-128 is selected due to size of encryptKey
262+
func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
262263
aesBlock, err := aes.NewCipher(key)
263264
if err != nil {
264-
return plainText, err
265+
return nil, err
266+
}
267+
stream := cipher.NewCTR(aesBlock, iv)
268+
outText := make([]byte, len(inText))
269+
stream.XORKeyStream(outText, inText)
270+
return outText, err
271+
}
272+
273+
func aesCBCDecrypt(key, cipherText, iv []byte) ([]byte, error) {
274+
aesBlock, err := aes.NewCipher(key)
275+
if err != nil {
276+
return nil, err
265277
}
266278
decrypter := cipher.NewCBCDecrypter(aesBlock, iv)
267-
paddedPlainText := make([]byte, len(cipherText))
268-
decrypter.CryptBlocks(paddedPlainText, cipherText)
269-
plainText = PKCS7Unpad(paddedPlainText)
270-
if plainText == nil {
279+
paddedPlaintext := make([]byte, len(cipherText))
280+
decrypter.CryptBlocks(paddedPlaintext, cipherText)
281+
plaintext := PKCS7Unpad(paddedPlaintext)
282+
if plaintext == nil {
271283
err = errors.New("Decryption failed: PKCS7Unpad failed after AES decryption")
272284
}
273-
return plainText, err
285+
return plaintext, err
274286
}
275287

276288
// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes

crypto/key.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
const (
38-
version = "1"
38+
version = 3
3939
)
4040

4141
type Key struct {
@@ -51,24 +51,30 @@ type plainKeyJSON struct {
5151
Address string `json:"address"`
5252
PrivateKey string `json:"privatekey"`
5353
Id string `json:"id"`
54-
Version string `json:"version"`
54+
Version int `json:"version"`
5555
}
5656

57-
type encryptedKeyJSON struct {
57+
type encryptedKeyJSONV3 struct {
58+
Address string `json:"address"`
59+
Crypto cryptoJSON
60+
Id string `json:"id"`
61+
Version int `json:"version"`
62+
}
63+
64+
type encryptedKeyJSONV1 struct {
5865
Address string `json:"address"`
5966
Crypto cryptoJSON
6067
Id string `json:"id"`
6168
Version string `json:"version"`
6269
}
6370

6471
type cryptoJSON struct {
65-
Cipher string `json:"cipher"`
66-
CipherText string `json:"ciphertext"`
67-
CipherParams cipherparamsJSON `json:"cipherparams"`
68-
KDF string `json:"kdf"`
69-
KDFParams scryptParamsJSON `json:"kdfparams"`
70-
MAC string `json:"mac"`
71-
Version string `json:"version"`
72+
Cipher string `json:"cipher"`
73+
CipherText string `json:"ciphertext"`
74+
CipherParams cipherparamsJSON `json:"cipherparams"`
75+
KDF string `json:"kdf"`
76+
KDFParams map[string]interface{} `json:"kdfparams"`
77+
MAC string `json:"mac"`
7278
}
7379

7480
type cipherparamsJSON struct {

0 commit comments

Comments
 (0)