Skip to content

Commit

Permalink
Test blocks signed with small RSA keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Berman committed Jan 16, 2025
1 parent 8f0f729 commit 7014ef7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/arweave/src/ar_wallet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
load_key/1, load_keyfile/1, new_keyfile/0, new_keyfile/1,
new_keyfile/2, base64_address_with_optional_checksum_to_decoded_address/1,
base64_address_with_optional_checksum_to_decoded_address_safe/1, wallet_filepath/1,
wallet_filepath/3,
get_or_create_wallet/1, recover_key/3]).

-include("../include/ar.hrl").
Expand Down
20 changes: 20 additions & 0 deletions apps/arweave/test/ar_post_block_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,26 @@ rejects_blocks_with_invalid_double_signing_proof_test_() ->
test_with_mocked_functions([{ar_fork, height_2_9, fun() -> 0 end}],
fun test_reject_block_invalid_double_signing_proof/0).

rejects_blocks_with_small_rsa_keys_test_() ->
{timeout, 60, fun test_rejects_blocks_with_small_rsa_keys/0}.

test_rejects_blocks_with_small_rsa_keys() ->
[B0] = ar_weave:init(),
ar_test_node:start(B0),
ok = ar_events:subscribe(block),
ar_test_node:mine(main),
BI = ar_test_node:assert_wait_until_height(main, 1),
B1 = ar_storage:read_block(hd(BI)),
Key2 = ar_test_node:new_custom_size_rsa_wallet(512), % normal 512-byte key
B2 = sign_block(B1, B0, Key2),
post_block(B2, invalid_resigned_solution_hash), % because reward_addr changed
Key3 = ar_test_node:new_custom_size_rsa_wallet(66), % 66-byte key
B3 = sign_block(B1, B0, Key3),
post_block(B3, invalid_signature),
Key4 = ar_test_node:new_custom_size_rsa_wallet(511),
B4 = sign_block(B1, B0, Key4),
post_block(B4, invalid_signature).

test_reject_block_invalid_double_signing_proof() ->
[test_reject_block_invalid_double_signing_proof(KeyType)
|| KeyType <- [?RSA_KEY_TYPE, ?ECDSA_KEY_TYPE]].
Expand Down
48 changes: 43 additions & 5 deletions apps/arweave/test/ar_test_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
mock_to_force_invalid_h1/0, get_difficulty_for_invalid_hash/0, invalid_solution/0,
valid_solution/0, new_mock/2, mock_function/3, unmock_module/1, remote_call/4,
load_fixture/1,
get_default_storage_module_packing/2, generate_genesis_data/1, get_genesis_chunk/1]).
get_default_storage_module_packing/2, generate_genesis_data/1, get_genesis_chunk/1,
all_nodes/1, new_custom_size_rsa_wallet/1]).

%% The "legacy" interface.
-export([start/0, start/1, start/2, start/3, start/4,
Expand All @@ -35,9 +36,10 @@

mock_functions/1, test_with_mocked_functions/2, test_with_mocked_functions/3]).

-include_lib("arweave/include/ar.hrl").
-include_lib("arweave/include/ar_config.hrl").
-include_lib("arweave/include/ar_consensus.hrl").
-include("../include/ar.hrl").
-include("../include/ar_config.hrl").
-include("../include/ar_consensus.hrl").

-include_lib("eunit/include/eunit.hrl").

%% May occasionally take quite long on a slow CI server, expecially in tests
Expand Down Expand Up @@ -76,6 +78,42 @@ all_peers(e2e) ->
all_nodes(TestType) ->
[{TestType, main} | all_peers(TestType)].

new_custom_size_rsa_wallet(Size) ->
KeyType = ?RSA_KEY_TYPE,
KeyAlg = element(1, KeyType),
PublicExpnt = 65537,
{[Expnt, Pub], [Expnt, Pub, Priv, P1, P2, E1, E2, C]} =
crypto:generate_key(rsa, {Size * 8, PublicExpnt}),
Key =
ar_serialize:jsonify(
{
[
{kty, <<"RSA">>},
{ext, true},
{e, ar_util:encode(Expnt)},
{n, ar_util:encode(Pub)},
{d, ar_util:encode(Priv)},
{p, ar_util:encode(P1)},
{q, ar_util:encode(P2)},
{dp, ar_util:encode(E1)},
{dq, ar_util:encode(E2)},
{qi, ar_util:encode(C)}
]
}
),
Filename = ar_wallet:wallet_filepath(wallet_address, Pub, KeyType),
case filelib:ensure_dir(Filename) of
ok ->
case ar_storage:write_file_atomic(Filename, Key) of
ok ->
{{KeyType, Priv, Pub}, {KeyType, Pub}};
Error2 ->
Error2
end;
Error ->
Error
end.

boot_peers([]) ->
ok;
boot_peers([{TestType, Node} | Peers]) ->
Expand Down Expand Up @@ -693,7 +731,7 @@ insert_root(Params) ->
end.

sign_tx(Node, Wallet, Args, SignFun) ->
{_, {KeyType, Pub}} = Wallet,
{_, {_, Pub}} = Wallet,
Data = maps:get(data, Args, <<>>),
DataSize = maps:get(data_size, Args, byte_size(Data)),
Format = maps:get(format, Args, 1),
Expand Down

0 comments on commit 7014ef7

Please sign in to comment.