summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-09-14 22:46:46 +0200
committerLinus Nordberg <linus@nordberg.se>2014-09-14 22:46:46 +0200
commit748d7fc86d20f9fc8f7ba947c2c1df6c41e0a28e (patch)
treee663ef4e77462833668d13113ea4d9d3f80bee37
parentf6a02d69404fcb6486a4dfd04bbd4fecd7a0e2e8 (diff)
Adjust plop for changes in ht.
-rw-r--r--ebin/plop.app2
-rw-r--r--src/plop.erl68
-rw-r--r--src/plop_sup.erl5
3 files changed, 31 insertions, 44 deletions
diff --git a/ebin/plop.app b/ebin/plop.app
index c4da4f1..82ec9c4 100644
--- a/ebin/plop.app
+++ b/ebin/plop.app
@@ -7,6 +7,6 @@
{vsn, "0.0.1"},
{modules, [plop_app, plop_sup, plop, db, ht, hex]},
{applications, [kernel, stdlib, mnesia]}, % crypto, public_key
- {registered, [plop, db]},
+ {registered, [plop, ht, db]},
{mod, {plop_app, ["test/eckey.pem", "test/eckey-public.pem"]}}
]}.
diff --git a/src/plop.erl b/src/plop.erl
index a4bbc2a..0fbd632 100644
--- a/src/plop.erl
+++ b/src/plop.erl
@@ -44,8 +44,7 @@
-record(state, {pubkey :: public_key:rsa_public_key(),
privkey :: public_key:rsa_private_key(),
- logid :: binary(),
- hashtree :: ht:history_tree()}).
+ logid :: binary()}).
%% @doc The parts of an STH which is to be signed. Used as the
%% interface to plop:sth/1, for testing.
@@ -86,13 +85,10 @@ init([PrivKeyfile, PubKeyfile]) ->
LogID = crypto:hash(sha256, public_key:der_encode(
'ECPoint',
element(2, element(1, Public_key)))), % FIXME!
- %% WARNING FIXME: The building of the tree is immensely expensive
- %% and slow -- don't do this with more than a couple of hundred of
- %% entries!
+ _Tree = ht:reset_tree([db:size() - 1]),
{ok, #state{pubkey = Public_key,
privkey = Private_key,
- logid = LogID,
- hashtree = build_tree_from_db()}}.
+ logid = LogID}}.
handle_cast(_Request, State) ->
{noreply, State}.
@@ -133,18 +129,16 @@ handle_call({add, #timestamped_entry{
timestamp = Timestamp_in, entry = Entry}},
_From,
State = #state{privkey = Privkey,
- logid = LogID,
- hashtree = Tree}) ->
+ logid = LogID}) ->
TimestampedEntry = #timestamped_entry{
timestamp = timestamp(Timestamp_in),
entry = Entry},
- {NewTree, SPT} = do_add(TimestampedEntry, Privkey, LogID, Tree),
- {reply, SPT, State#state{hashtree = NewTree}};
+ {ok, SPT} = do_add(TimestampedEntry, Privkey, LogID),
+ {reply, SPT, State};
handle_call({sth, Data}, _From,
- Plop = #state{privkey = PrivKey,
- hashtree = Tree}) ->
- {reply, sth(PrivKey, Tree, Data), Plop};
+ Plop = #state{privkey = PrivKey}) ->
+ {reply, sth(PrivKey, Data), Plop};
handle_call({get, {Start, End}}, _From, Plop) ->
{reply, db:get_by_index(Start, End), Plop};
@@ -158,26 +152,17 @@ handle_call({test, pubkey}, _From,
{reply, PK, Plop}.
%%%%%%%%%%%%%%%%%%%%
--spec build_tree_from_db() -> ht:history_tree().
-build_tree_from_db() ->
- ht:new(db:size() - 1).
-
-%% -spec build_tree(ht:head(), list()) -> ht:head().
-%% build_tree(Tree, []) ->
-%% Tree;
-%% build_tree(Tree, [H|T]) ->
-%% Data = db_get_single_entry(H),
-%% build_tree(ht:append(Tree, Data), T).
-
%% db_get_single_entry(N) ->
%% [#mtl{entry = #timestamped_entry{entry = #plop_entry{data = Data}}}] =
%% db:get_by_index(N, N),
%% Data.
--spec do_add(timestamped_entry(), public_key:rsa_private_key(),
- binary(), any()) -> {any(), binary()}.
+-spec do_add(timestamped_entry(),
+ public_key:rsa_private_key(),
+ binary()) -> {ok|error, binary()}.
do_add(TimestampedEntry = #timestamped_entry{entry = PlopEntry},
- Privkey, LogID, Tree) ->
+ Privkey,
+ LogID) ->
DB_hash = crypto:hash(sha256, serialise(PlopEntry)),
Record = db:find(DB_hash),
case Record of
@@ -190,18 +175,17 @@ do_add(TimestampedEntry = #timestamped_entry{entry = PlopEntry},
#timestamped_entry{
timestamp = E#timestamped_entry.timestamp,
entry = PlopEntry}}},
- {Tree, SPT}; % State not changed, cached SPT.
+ {ok, SPT};
[] ->
NewSPT = spt(LogID, Privkey, TimestampedEntry),
MTL = #mtl{entry = TimestampedEntry},
- %%io:format("Creating new entry: index=~p~n", [ht:size(Tree)]),
- DB_data = #plop{index = ht:size(Tree),
+ %%io:format("Creating new entry: index=~p~n", [ht:size()]),
+ DB_data = #plop{index = ht:size(),
hash = DB_hash,
mtl = MTL,
spt = NewSPT},
{atomic, ok} = db:add(DB_data),
- {ht:add(Tree, serialise(MTL)), % New tree.
- NewSPT}; % New SPT.
+ {ht:add(serialise(MTL)), NewSPT};
Err -> {error, Err}
end.
@@ -229,13 +213,13 @@ spt(LogID, PrivKey, #timestamped_entry{
signature = Signature}.
%% @doc Signed Tree Head as specified in RFC6962 section 3.2.
--spec sth(#'ECPrivateKey'{}, ht:history_tree(), sth_signed() | list()) -> sth().
-sth(PrivKey, Tree, []) ->
- sth(PrivKey, Tree, #sth_signed{timestamp = now});
-sth(PrivKey, Tree, #sth_signed{version = Version, timestamp = Timestamp_in}) ->
+-spec sth(#'ECPrivateKey'{}, sth_signed() | list()) -> sth().
+sth(PrivKey, []) ->
+ sth(PrivKey, #sth_signed{timestamp = now});
+sth(PrivKey, #sth_signed{version = Version, timestamp = Timestamp_in}) ->
Timestamp = timestamp(Timestamp_in),
- Treesize = ht:size(Tree),
- Roothash = ht:tree_hash(Tree),
+ Treesize = ht:size(),
+ Roothash = ht:tree_hash(),
BinToSign = serialise(#sth_signed{
version = Version,
signature_type = tree_hash,
@@ -455,15 +439,13 @@ add_test() ->
timestamp = 4711,
entry = #plop_entry{type = test, data = Data1}},
S#state.privkey,
- S#state.logid,
- S#state.hashtree),
+ S#state.logid),
{_Tree1, SPT1} =
do_add(#timestamped_entry{
timestamp = 4712,
entry = #plop_entry{type = test, data = Data1}},
S#state.privkey,
- S#state.logid,
- S#state.hashtree),
+ S#state.logid),
?assertEqual(SPT, SPT1),
TE = #timestamped_entry{
diff --git a/src/plop_sup.erl b/src/plop_sup.erl
index 089a812..a5ce905 100644
--- a/src/plop_sup.erl
+++ b/src/plop_sup.erl
@@ -23,6 +23,11 @@ init(Args) ->
permanent,
10000,
worker, [db]},
+ {the_ht,
+ {ht, start_link, []},
+ permanent,
+ 10000,
+ worker, [ht]},
{the_plop,
{plop, start_link, Args}, % All arguments go to plop.
permanent,