Skip to content

Commit a7de861

Browse files
authored
feat: add support for pbkdf in keystore. (#1078)
1 parent 9c44baa commit a7de861

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

lib/keystore.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ defmodule Keystore do
6262
Scrypt.hash(password, salt, log_n, r, p, @derived_key_size)
6363
end
6464

65-
# TODO: support pbkdf2
66-
defp derive_key!(%{"function" => "pbkdf2"} = drf, _password) do
67-
%{"dklen" => _dklen, "salt" => _salt, "c" => _c, "prf" => "hmac-sha256"} = drf
65+
defp derive_key!(%{"function" => "pbkdf2", "params" => params}, password) do
66+
%{"dklen" => dklen, "salt" => hex_salt, "c" => c, "prf" => "hmac-sha256"} = params
67+
salt = parse_binary!(hex_salt)
68+
69+
if byte_size(salt) != @salt_bytes do
70+
raise "Invalid salt size: #{byte_size(salt)}"
71+
end
72+
73+
:crypto.pbkdf2_hmac(:sha256, password, salt, c, dklen)
6874
end
6975

7076
defp decrypt_secret(derived_key, iv, cipher_message) do

test/unit/keystore_test.exs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ defmodule Unit.KeystoreTest do
33

44
@eip_password "testpassword"
55
@eip_secret Base.decode16!("000000000019D6689C085AE165831E934FF763AE46A2A6C172B3F1B60A8CE26F")
6+
@pubkey Base.decode16!(
7+
"9612D7A727C9D0A22E185A1C768478DFE919CADA9266988CB32359C11F2B7B27F4AE4040902382AE2910C15E2B420D07"
8+
)
69

710
# Taken from lighthouse
811
@scrypt_json ~s({
@@ -37,16 +40,53 @@ defmodule Unit.KeystoreTest do
3740
"version": 4
3841
})
3942

43+
# Taken from lighthouse, minus "path": "m/12381/60/0/0",
44+
@pbkdf2_json ~s({
45+
"crypto": {
46+
"kdf": {
47+
"function": "pbkdf2",
48+
"params": {
49+
"dklen": 32,
50+
"c": 262144,
51+
"prf": "hmac-sha256",
52+
"salt": "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
53+
},
54+
"message": ""
55+
},
56+
"checksum": {
57+
"function": "sha256",
58+
"params": {},
59+
"message": "18b148af8e52920318084560fd766f9d09587b4915258dec0676cba5b0da09d8"
60+
},
61+
"cipher": {
62+
"function": "aes-128-ctr",
63+
"params": {
64+
"iv": "264daa3f303d7259501c93d997d84fe6"
65+
},
66+
"message": "a9249e0ca7315836356e4c7440361ff22b9fe71e2e2ed34fc1eb03976924ed48"
67+
}
68+
},
69+
"pubkey": "9612d7a727c9d0a22e185a1c768478dfe919cada9266988cb32359c11f2b7b27f4ae4040902382ae2910c15e2b420d07",
70+
"uuid": "64625def-3331-4eea-ab6f-782f3ed16a83",
71+
"version": 4
72+
})
73+
4074
test "eip scrypt test vector" do
4175
{pubkey, privkey} = Keystore.decode_str!(@scrypt_json, @eip_password)
4276

43-
expected_pubkey =
44-
Base.decode16!(
45-
"9612D7A727C9D0A22E185A1C768478DFE919CADA9266988CB32359C11F2B7B27F4AE4040902382AE2910C15E2B420D07"
46-
)
77+
assert privkey == @eip_secret
78+
assert pubkey == @pubkey
79+
80+
digest = :crypto.hash(:sha256, "test message")
81+
{:ok, signature} = Bls.sign(privkey, digest)
82+
assert Bls.valid?(pubkey, digest, signature)
83+
end
84+
85+
test "eip pbkdf2 test vector" do
86+
{pubkey, privkey} = Keystore.decode_str!(@pbkdf2_json, @eip_password)
4787

4888
assert privkey == @eip_secret
49-
assert pubkey == expected_pubkey
89+
assert pubkey == @pubkey
5090

5191
digest = :crypto.hash(:sha256, "test message")
5292
{:ok, signature} = Bls.sign(privkey, digest)

0 commit comments

Comments
 (0)