From c853ee34a2d2d047cc456a9fc78b2904b22dad9c Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Mon, 17 Aug 2015 16:02:42 +0200 Subject: Wrap entries in plop wrapper --- src/frontend.erl | 6 +++--- src/plop.erl | 46 ++++++++++++++++++++++++++++++++++++++++++---- src/tlv.erl | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/frontend.erl b/src/frontend.erl index 89df7b8..ccaf811 100644 --- a/src/frontend.erl +++ b/src/frontend.erl @@ -230,7 +230,7 @@ verify_entry(Entry) -> Module:Function(Entry). check_entry(LeafHash, Index) -> - case db:get_by_leaf_hash(LeafHash) of + case plop:get_by_leaf_hash(LeafHash) of notfound -> {notfound, Index}; {Index, LeafHash, Entry} -> @@ -252,8 +252,8 @@ check_entry(LeafHash, Index) -> end. check_entry_noreverse(LeafHash, Index) -> - case db:entry_for_leafhash(LeafHash) of - noentry -> + case plop:entry_for_leafhash(LeafHash) of + notfound -> {notfound, Index}; Entry -> case verify_entry(Entry) of diff --git a/src/plop.erl b/src/plop.erl index b6d7ff1..b812e4a 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -27,6 +27,7 @@ -export([get_logid/0, serialise/1]). -export([add/3, sth/0, get/1, get/2, spt/1, consistency/2, inclusion/2, inclusion_and_entry/2]). -export([generate_timestamp/0, save_sth/1, verify_sth/4]). +-export([get_by_leaf_hash/1, entry_for_leafhash/1]). %% API for tests. -export([testing_get_pubkey/0]). @@ -73,16 +74,32 @@ handle_http_reply(TreeLeafHash, RepliesUntilQuorum, end end. +wrap_entry(Entry) -> + EncodedEntry = tlv:encodelist(Entry), + tlv:encodelist([{<<"PLOP">>, EncodedEntry}, + {<<"S256">>, crypto:hash(sha256, EncodedEntry)}]). + +unwrap_entry(WrappedEntry) -> + [{<<"PLOP">>, Entry}, {<<"S256">>, Hash}] = tlv:decodelist(WrappedEntry), + ComputedHash = crypto:hash(sha256, Entry), + case ComputedHash of + Hash -> + tlv:decodelist(Entry); + _ -> + error + end. + %%%%%%%%%%%%%%%%%%%% -spec add(binary(), binary(), binary()) -> ok. add(LogEntry, TreeLeafHash, EntryHash) -> lager:debug("add leafhash ~s", [mochihex:to_hex(TreeLeafHash)]), + WrappedLogEntry = wrap_entry(LogEntry), case storage_nodes() of [] -> exit(internal_merge_not_supported); Nodes -> util:spawn_and_wait(fun () -> - store_at_all_nodes(Nodes, {LogEntry, TreeLeafHash, EntryHash}) + store_at_all_nodes(Nodes, {WrappedLogEntry, TreeLeafHash, EntryHash}) end) end. @@ -126,7 +143,12 @@ get(Start, End) -> -spec get(binary()) -> notfound | {notfetched, binary(), binary()}. get(Hash) -> - db:get_by_entry_hash(Hash). + case db:get_by_entry_hash(Hash) of + notfound -> + notfound; + {notfetched, LeafHash, Entry} -> + {notfetched, LeafHash, unwrap_entry(Entry)} + end. spt(Data) -> #signature{algorithm = #sig_and_hash_alg{ @@ -177,7 +199,7 @@ inclusion_and_entry(Index, TreeSize) -> {I, _MTLHash, noentry} -> {notfound, io:format("Unknown index ~p", [I])}; {I, _MTLHash, Entry} -> - {ok, Entry, ht:path(I, TreeSize - 1)} + {ok, unwrap_entry(Entry), ht:path(I, TreeSize - 1)} end end. @@ -288,8 +310,24 @@ store_at_all_nodes(Nodes, {LogEntry, TreeLeafHash, EntryHash}) -> Any end. +get_by_leaf_hash(LeafHash) -> + case db:get_by_leaf_hash(LeafHash) of + notfound -> + notfound; + {Index, LeafHash, Entry} -> + {Index, LeafHash, unwrap_entry(Entry)} + end. + +entry_for_leafhash(LeafHash) -> + case db:entry_for_leafhash(LeafHash) of + noentry -> + notfound; + Entry -> + unwrap_entry(Entry) + end. + fill_in_entry({_Index, LeafHash, notfetched}) -> - db:get_by_leaf_hash(LeafHash). + get_by_leaf_hash(LeafHash). %%%%%%%%%%%%%%%%%%%% diff --git a/src/tlv.erl b/src/tlv.erl index b1d3428..fec016b 100644 --- a/src/tlv.erl +++ b/src/tlv.erl @@ -3,22 +3,37 @@ -module(tlv). --export([encode/2, decode/1]). +-export([encode/2, decode/1, encodelist/1, decodelist/1]). -spec encode(binary(), binary()) -> binary(). -encode(Type, Value) -> - 4 = byte_size(Type), % FIXME: make Type a 4 octet binary +encode(Type, Value) when byte_size(Type) == 4 -> Len = byte_size(Value) + 8, - <>. + <>. -spec decode(binary()) -> {binary(), binary(), binary()}. decode(Data) -> <> = Data, Length = TotalLength - 8, - <> = Rest1, - <> = Rest2, + <> = Rest1, + <> = Rest2, {Type, Value, Rest3}. +%% Convenience functions + +-spec encodelist([{binary(), binary()}]) -> binary(). +encodelist(List) -> + list_to_binary([encode(Type, Value) || {Type, Value} <- List]). + +-spec decodelist(binary()) -> [{binary(), binary()}]. +decodelist(Data) -> + decodelist(Data, []). + +decodelist(<<>>, Acc) -> + lists:reverse(Acc); +decodelist(Data, Acc) -> + {Type, Value, Rest} = decode(Data), + decodelist(Rest, [{Type, Value} | Acc]). + %%%%%%%%%%%%%%%%%%%% -include_lib("eunit/include/eunit.hrl"). @@ -32,3 +47,13 @@ the_test_() -> fun(TestVectors) -> [?_assertEqual({T, V, <<>>}, decode(encode(T, V))) || {T, V} <- TestVectors] end}. + + +-define(LISTTESTDATA, [?TESTDATA1, ?TESTDATA2]). +list_test() -> + Encoded = encodelist(?LISTTESTDATA), + ?assertEqual(decodelist(Encoded), ?LISTTESTDATA), + {T1, V1, Rest} = decode(Encoded), + ?assertEqual({T1, V1}, ?TESTDATA1), + {T2, V2, <<>>} = decode(Rest), + ?assertEqual({T2, V2}, ?TESTDATA2). -- cgit v1.1