You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2023/08/03 13:31:26 UTC
[couchdb] branch fast_pbkdf2 created (now efafcb3d3)
This is an automated email from the ASF dual-hosted git repository.
rnewson pushed a change to branch fast_pbkdf2
in repository https://gitbox.apache.org/repos/asf/couchdb.git
at efafcb3d3 switch to fast_pbkdf2 for hashing
This branch includes the following new commits:
new efafcb3d3 switch to fast_pbkdf2 for hashing
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[couchdb] 01/01: switch to fast_pbkdf2 for hashing
Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rnewson pushed a commit to branch fast_pbkdf2
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit efafcb3d32a83abadb4926d43de590d3c47442b3
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu Aug 3 13:44:19 2023 +0100
switch to fast_pbkdf2 for hashing
---
.gitignore | 1 +
rebar.config.script | 3 +-
rel/reltool.config | 6 ++--
src/couch/src/couch.app.src | 3 +-
src/couch/src/couch_passwords.erl | 69 +++------------------------------------
5 files changed, 13 insertions(+), 69 deletions(-)
diff --git a/.gitignore b/.gitignore
index 30aed7787..fe9341bbf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -69,6 +69,7 @@ src/parse_trans/
src/proper/
src/rebar/
src/recon/
+src/fast_pbkdf2/
src/snappy/
src/ssl_verify_fun/
src/triq/
diff --git a/rebar.config.script b/rebar.config.script
index 9098e6db7..ad8787bc2 100644
--- a/rebar.config.script
+++ b/rebar.config.script
@@ -157,7 +157,8 @@ DepDescs = [
{jiffy, "jiffy", {tag, "CouchDB-1.0.9-2"}},
{mochiweb, "mochiweb", {tag, "v3.1.1"}},
{meck, "meck", {tag, "0.9.2"}},
-{recon, "recon", {tag, "2.5.3"}}
+{recon, "recon", {tag, "2.5.3"}},
+{fast_pbkdf2, {url, "https://github.com/esl/fast_pbkdf2"}, {tag, "1.0.2"}}
].
WithProper = lists:keyfind(with_proper, 1, CouchConfig) == {with_proper, true}.
diff --git a/rel/reltool.config b/rel/reltool.config
index 1dca6ff85..392d5c62c 100644
--- a/rel/reltool.config
+++ b/rel/reltool.config
@@ -66,7 +66,8 @@
%% extra
nouveau,
- recon
+ recon,
+ fast_pbkdf2
]},
{rel, "start_clean", "", [kernel, stdlib]},
{boot_rel, "couchdb"},
@@ -129,7 +130,8 @@
%% extra
{app, nouveau, [{incl_cond, include}]},
- {app, recon, [{incl_cond, include}]}
+ {app, recon, [{incl_cond, include}]},
+ {app, fast_pbkdf2, [{incl_cond, include}]}
]}.
{overlay_vars, "couchdb.config"}.
diff --git a/src/couch/src/couch.app.src b/src/couch/src/couch.app.src
index 6fc293ac1..bf58a7cae 100644
--- a/src/couch/src/couch.app.src
+++ b/src/couch/src/couch.app.src
@@ -49,7 +49,8 @@
ioq,
couch_stats,
hyper,
- couch_dist
+ couch_dist,
+ fast_pbkdf2
]},
{env, [
{httpd_global_handlers, [
diff --git a/src/couch/src/couch_passwords.erl b/src/couch/src/couch_passwords.erl
index b89104603..0f3083ce3 100644
--- a/src/couch/src/couch_passwords.erl
+++ b/src/couch/src/couch_passwords.erl
@@ -83,8 +83,8 @@ pbkdf2(Password, Salt, Iterations) when
is_integer(Iterations),
Iterations > 0
->
- {ok, Result} = pbkdf2(Password, Salt, Iterations, ?SHA1_OUTPUT_LENGTH),
- Result;
+ DerivedKey = fast_pbkdf2:pbkdf2(sha, Password, Salt, Iterations),
+ couch_util:to_hex_bin(DerivedKey);
pbkdf2(Password, Salt, Iterations) when
is_binary(Salt),
is_integer(Iterations),
@@ -113,60 +113,8 @@ pbkdf2(Password, Salt, Iterations, DerivedLength) when
Iterations > 0,
is_integer(DerivedLength)
->
- L = ceiling(DerivedLength / ?SHA1_OUTPUT_LENGTH),
- <<Bin:DerivedLength/binary, _/binary>> =
- iolist_to_binary(pbkdf2(Password, Salt, Iterations, L, 1, [])),
- {ok, couch_util:to_hex_bin(Bin)}.
-
--spec pbkdf2(binary(), binary(), integer(), integer(), integer(), iolist()) ->
- iolist().
-pbkdf2(_Password, _Salt, _Iterations, BlockCount, BlockIndex, Acc) when
- BlockIndex > BlockCount
-->
- lists:reverse(Acc);
-pbkdf2(Password, Salt, Iterations, BlockCount, BlockIndex, Acc) ->
- Block = pbkdf2(Password, Salt, Iterations, BlockIndex, 1, <<>>, <<>>),
- pbkdf2(Password, Salt, Iterations, BlockCount, BlockIndex + 1, [Block | Acc]).
-
--spec pbkdf2(
- binary(),
- binary(),
- integer(),
- integer(),
- integer(),
- binary(),
- binary()
-) -> binary().
-pbkdf2(_Password, _Salt, Iterations, _BlockIndex, Iteration, _Prev, Acc) when
- Iteration > Iterations
-->
- Acc;
-pbkdf2(Password, Salt, Iterations, BlockIndex, 1, _Prev, _Acc) ->
- InitialBlock = couch_util:hmac(
- sha,
- Password,
- <<Salt/binary, BlockIndex:32/integer>>
- ),
- pbkdf2(
- Password,
- Salt,
- Iterations,
- BlockIndex,
- 2,
- InitialBlock,
- InitialBlock
- );
-pbkdf2(Password, Salt, Iterations, BlockIndex, Iteration, Prev, Acc) ->
- Next = couch_util:hmac(sha, Password, Prev),
- pbkdf2(
- Password,
- Salt,
- Iterations,
- BlockIndex,
- Iteration + 1,
- Next,
- crypto:exor(Next, Acc)
- ).
+ DerivedKey = fast_pbkdf2:pbkdf2(sha, Password, Salt, Iterations, DerivedLength),
+ {ok, couch_util:to_hex_bin(DerivedKey)}.
%% verify two lists for equality without short-circuits to avoid timing attacks.
-spec verify(string(), string(), integer()) -> boolean().
@@ -189,12 +137,3 @@ verify(X, Y) when is_list(X) and is_list(Y) ->
end;
verify(_X, _Y) ->
false.
-
--spec ceiling(number()) -> integer().
-ceiling(X) ->
- T = erlang:trunc(X),
- case (X - T) of
- Neg when Neg < 0 -> T;
- Pos when Pos > 0 -> T + 1;
- _ -> T
- end.