Skip to content

Commit 805dc10

Browse files
author
HristianHristov
committed
[GH-#84] Introducing key lenght check verification
1 parent 847dd3d commit 805dc10

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

apps/aecore/lib/aecore/keys/worker.ex

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Aecore.Keys.Worker do
99

1010
@filename_pub "key.pub"
1111
@filename_priv "key"
12+
@pub_key_length 65
1213

1314
def start_link() do
1415
GenServer.start_link(
@@ -123,19 +124,23 @@ defmodule Aecore.Keys.Worker do
123124
0
124125
}
125126
end
126-
127127
def handle_call(
128128
{:verify, {term, signature, pub_key}},
129129
_from,
130130
%{algo: algo, digest: digest, curve: curve} = state
131131
) do
132-
result =
133-
:crypto.verify(algo, digest, :erlang.term_to_binary(term), signature, [
134-
pub_key,
135-
:crypto.ec_curve(curve)
136-
])
132+
case is_valid_pub_key(pub_key) do
133+
true ->
134+
result =
135+
:crypto.verify(algo, digest, :erlang.term_to_binary(term), signature, [
136+
pub_key,
137+
:crypto.ec_curve(curve)
138+
])
137139

138-
{:reply, result, state}
140+
{:reply, result, state}
141+
false ->
142+
{:reply, {:error, "Key length is not valid!"}, state}
143+
end
139144
end
140145

141146
def handle_call(
@@ -294,6 +299,12 @@ defmodule Aecore.Keys.Worker do
294299
pub
295300
end
296301

302+
defp is_valid_pub_key(pub_key_str) do
303+
pub_key_str
304+
|> Base.decode16!()
305+
|> byte_size() == @pub_key_length
306+
end
307+
297308
defp padding128(bin) do
298309
pad0 = 128 - :erlang.size(bin)
299310
pad1 = pad0 * 8

apps/aecore/test/aecore_keys_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,17 @@ defmodule AecoreKeysTest do
2222
{:ok, to_account} = Keys.pubkey()
2323
assert {:ok, _} = Keys.sign_tx(to_account, 5, Map.get(Chain.chain_state, to_account, %{nonce: 0}).nonce + 1)
2424
end
25+
26+
test "check pubkey length" do
27+
pub_key_str = "041A470AE9831B61D9951A10D49663419CE087DF1BD7DB06578971767F032D389CB283AD4DD4E3532F3A5F3C89B006092CB6CECE39CAC3B06C2CB6DF8B51C73675"
28+
29+
assert false == Keys.verify("", "", pub_key_str)
30+
end
31+
32+
test "wrong key verification" do
33+
pub_key_str = "041A470AE9831B61D9951A10D49663419CE087DF1BD7DB06578971767F032D389CB283AD4DD4E3"
34+
35+
assert {:error, _} = Keys.verify("", "", pub_key_str)
36+
end
37+
2538
end

0 commit comments

Comments
 (0)