summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2015-08-17 16:02:42 +0200
committerLinus Nordberg <linus@nordu.net>2015-08-19 16:28:35 +0200
commitc853ee34a2d2d047cc456a9fc78b2904b22dad9c (patch)
tree03e892ffd4ad25ebe23e69275dd725421654b26a /src
parent85411c74332775b6b2dbc06f40074a26e7f6ba3e (diff)
Wrap entries in plop wrapper
Diffstat (limited to 'src')
-rw-r--r--src/frontend.erl6
-rw-r--r--src/plop.erl46
-rw-r--r--src/tlv.erl37
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,
- <<Len:32, Type/binary, Value/binary>>.
+ <<Len:32, Type:4/binary, Value/binary>>.
-spec decode(binary()) -> {binary(), binary(), binary()}.
decode(Data) ->
<<TotalLength:32, Rest1/binary>> = Data,
Length = TotalLength - 8,
- <<Type:4/binary-unit:8, Rest2/binary>> = Rest1,
- <<Value:Length/binary-unit:8, Rest3/binary>> = Rest2,
+ <<Type:4/binary, Rest2/binary>> = Rest1,
+ <<Value:Length/binary, Rest3/binary>> = 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).