Address comments

This commit is contained in:
Zed 2020-05-20 07:40:51 +02:00 committed by zah
parent 8496e20a78
commit 14ad100b45
2 changed files with 25 additions and 21 deletions

View File

@ -6,13 +6,12 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
json, math, json, math, strutils,
stew/results, eth/keyfile/uuid,
nimcrypto/[sha2, rijndael, pbkdf2, bcmode, hash, sysrand, utils], stew/[results, byteutils],
nimcrypto/[sha2, rijndael, pbkdf2, bcmode, hash, sysrand],
./crypto ./crypto
import strutils except fromHex
export results export results
type type
@ -109,11 +108,11 @@ proc decryptKeystore*(data, passphrase: string): KsResult[seq[byte]] =
crypto = ks{"crypto"}.to(Crypto[KdfPbkdf2]) crypto = ks{"crypto"}.to(Crypto[KdfPbkdf2])
kdfParams = crypto.kdf.params kdfParams = crypto.kdf.params
salt = fromHex(kdfParams.salt) salt = hexToSeqByte(kdfParams.salt)
decKey = sha256.pbkdf2(passphrase, salt, kdfParams.c, kdfParams.dklen) decKey = sha256.pbkdf2(passphrase, salt, kdfParams.c, kdfParams.dklen)
iv = fromHex(crypto.cipher.params.iv) iv = hexToSeqByte(crypto.cipher.params.iv)
cipherMsg = fromHex(crypto.cipher.message) cipherMsg = hexToSeqByte(crypto.cipher.message)
checksumMsg = fromHex(crypto.checksum.message) checksumMsg = hexToSeqByte(crypto.checksum.message)
else: else:
return err "ks: unknown cipher" return err "ks: unknown cipher"
@ -166,7 +165,7 @@ proc encryptKeystore*[T: KdfParams](secret: openarray[byte];
pbkdf2Params.dklen) pbkdf2Params.dklen)
var kdf = Kdf[KdfPbkdf2](function: "pbkdf2", params: pbkdf2Params, message: "") var kdf = Kdf[KdfPbkdf2](function: "pbkdf2", params: pbkdf2Params, message: "")
kdf.params.salt = kdfSalt.toHex(lowercase=true) kdf.params.salt = kdfSalt.toHex()
else: else:
return return
@ -185,17 +184,17 @@ proc encryptKeystore*[T: KdfParams](secret: openarray[byte];
kdf: kdf, kdf: kdf,
checksum: Checksum( checksum: Checksum(
function: "sha256", function: "sha256",
message: sum.toHex(lowercase=true) message: sum.toHex()
), ),
cipher: Cipher( cipher: Cipher(
function: "aes-128-ctr", function: "aes-128-ctr",
params: CipherParams(iv: aesIv.toHex(lowercase=true)), params: CipherParams(iv: aesIv.toHex()),
message: cipherMsg.toHex(lowercase=true) message: cipherMsg.toHex()
) )
), ),
pubkey: pubkey.toHex(), pubkey: pubkey.toHex(),
path: path, path: path,
uuid: "", # TODO: uuid library? uuid: $(uuidGenerate().tryGet()), # error handling?
version: 4 version: 4
) )

View File

@ -9,7 +9,7 @@
import import
unittest, ./testutil, json, unittest, ./testutil, json,
nimcrypto/utils, stew/byteutils,
../beacon_chain/spec/keystore ../beacon_chain/spec/keystore
from strutils import replace from strutils import replace
@ -80,10 +80,9 @@ const
const const
password = "testpassword" password = "testpassword"
secret = fromHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") secret = hexToSeqByte("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
salt = fromHex("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") salt = hexToSeqByte("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
iv = fromHex("264daa3f303d7259501c93d997d84fe6") iv = hexToSeqByte("264daa3f303d7259501c93d997d84fe6")
uuid = "64625def-3331-4eea-ab6f-782f3ed16a83"
suiteReport "Keystore": suiteReport "Keystore":
timedTest "Pbkdf2 decryption": timedTest "Pbkdf2 decryption":
@ -94,9 +93,15 @@ suiteReport "Keystore":
timedTest "Pbkdf2 encryption": timedTest "Pbkdf2 encryption":
let encrypt = encryptKeystore[KdfPbkdf2](secret, password, salt=salt, iv=iv, let encrypt = encryptKeystore[KdfPbkdf2](secret, password, salt=salt, iv=iv,
path="m/12381/60/0/0", ugly=false) path="m/12381/60/0/0", ugly=false)
check encrypt.isOk check encrypt.isOk
check encrypt.get() == pbkdf2Vector.replace(uuid, "")
var
encryptJson = parseJson(encrypt.get())
pbkdf2Json = parseJson(pbkdf2Vector)
encryptJson{"uuid"} = %""
pbkdf2Json{"uuid"} = %""
check encryptJson == pbkdf2Json
timedTest "Pbkdf2 error": timedTest "Pbkdf2 error":
check encryptKeystore[KdfPbkdf2](secret, "", salt = [byte 1]).isErr check encryptKeystore[KdfPbkdf2](secret, "", salt = [byte 1]).isErr