blob: f08ed1c112d2dde3c9a279d83f198f97eb3d6a21 [file] [log] [blame]
Nelson Videsf9ee3062021-02-10 20:13:20 +01001-module(pbkdf2_SUITE).
2
3%% API
4-export([all/0,
5 groups/0,
6 init_per_suite/1,
7 end_per_suite/1,
8 init_per_group/2,
9 end_per_group/2,
10 init_per_testcase/2,
11 end_per_testcase/2]).
12
13%% test cases
14-export([
15 erlang_and_nif_are_equivalent_sha1/1,
16 erlang_and_nif_are_equivalent_sha224/1,
17 erlang_and_nif_are_equivalent_sha256/1,
18 erlang_and_nif_are_equivalent_sha384/1,
Nelson Vides117590d2021-03-09 22:06:12 +010019 erlang_and_nif_are_equivalent_sha512/1
Nelson Videsf9ee3062021-02-10 20:13:20 +010020 ]).
21-export([
22 test_vector_sha1_1/1,
23 test_vector_sha1_2/1,
24 test_vector_sha1_3/1,
25 test_vector_sha1_4/1,
26 test_vector_sha1_5/1,
27 test_vector_sha256_1/1,
28 test_vector_sha256_2/1,
29 test_vector_sha256_3/1,
30 test_vector_sha256_4/1,
31 test_vector_sha256_5/1,
32 test_vector_sha256_6/1
33 ]).
34
35-include_lib("common_test/include/ct.hrl").
36-include_lib("proper/include/proper.hrl").
37-include_lib("eunit/include/eunit.hrl").
38
39all() ->
40 [
41 {group, equivalents},
Nelson Vides117590d2021-03-09 22:06:12 +010042 {group, test_vectors}
Nelson Videsf9ee3062021-02-10 20:13:20 +010043 ].
44
45groups() ->
46 [
47 {equivalents, [parallel],
48 [
49 erlang_and_nif_are_equivalent_sha1,
50 erlang_and_nif_are_equivalent_sha224,
51 erlang_and_nif_are_equivalent_sha256,
52 erlang_and_nif_are_equivalent_sha384,
53 erlang_and_nif_are_equivalent_sha512
54 ]},
55 {test_vectors, [parallel],
56 [
57 test_vector_sha1_1,
58 test_vector_sha1_2,
59 test_vector_sha1_3,
60 test_vector_sha1_4,
61 test_vector_sha1_5,
62 test_vector_sha256_1,
63 test_vector_sha256_2,
64 test_vector_sha256_3,
65 test_vector_sha256_4,
66 test_vector_sha256_5,
67 test_vector_sha256_6
68 ]}
69 ].
70
71%%%===================================================================
72%%% Overall setup/teardown
73%%%===================================================================
74init_per_suite(Config) ->
75 Config.
76
77end_per_suite(_Config) ->
78 ok.
79
80%%%===================================================================
81%%% Group specific setup/teardown
82%%%===================================================================
83init_per_group(_Groupname, Config) ->
84 Config.
85
86end_per_group(_Groupname, _Config) ->
87 ok.
88
89%%%===================================================================
90%%% Testcase specific setup/teardown
91%%%===================================================================
92init_per_testcase(_TestCase, Config) ->
93 Config.
94
95end_per_testcase(_TestCase, _Config) ->
96 ok.
97
98%%%===================================================================
99%%% Individual Test Cases (from groups() definition)
100%%%===================================================================
101
102erlang_and_nif_are_equivalent_sha1(_Config) ->
103 erlang_and_nif_are_equivalent_(sha).
104
105erlang_and_nif_are_equivalent_sha224(_Config) ->
106 erlang_and_nif_are_equivalent_(sha224).
107
108erlang_and_nif_are_equivalent_sha256(_Config) ->
109 erlang_and_nif_are_equivalent_(sha256).
110
111erlang_and_nif_are_equivalent_sha384(_Config) ->
112 erlang_and_nif_are_equivalent_(sha384).
113
114erlang_and_nif_are_equivalent_sha512(_Config) ->
115 erlang_and_nif_are_equivalent_(sha512).
116
117erlang_and_nif_are_equivalent_(Sha) ->
118 Prop = ?FORALL({Pass, Salt, Count},
119 {binary(), binary(), range(2,20000)},
120 fast_pbkdf2:pbkdf2(Sha, Pass, Salt, Count)
121 =:= erl_pbkdf2:pbkdf2_oneblock(Sha, Pass, Salt, Count)
122 ),
123 ?assert(proper:quickcheck(Prop, [verbose, long_result,
124 {numtests, 100},
125 {start_size, 2},
126 {max_size, 64}])).
127
128
129%% Taken from the official RFC https://www.ietf.org/rfc/rfc6070.txt
130
131test_vector_sha1_1(_Config) ->
132 {P,S,It,DkLen,Result} = {<<"password">>,<<"salt">>,1,20,
133 base16:decode(<<"0c60c80f961f0e71f3a9b524af6012062fe037a6">>)},
134 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha, P, S, It, DkLen)).
135
136test_vector_sha1_2(_Config) ->
137 {P,S,It,DkLen,Result} = {<<"password">>,<<"salt">>,2,20,
138 base16:decode(<<"ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957">>)},
139 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha, P, S, It, DkLen)).
140
141test_vector_sha1_3(_Config) ->
142 {P,S,It,DkLen,Result} = {<<"password">>,<<"salt">>,4096,20,
143 base16:decode(<<"4b007901b765489abead49d926f721d065a429c1">>)},
144 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha, P, S, It, DkLen)).
145
146test_vector_sha1_4(_Config) ->
147 {P,S,It,DkLen,Result} = {<<"password">>,<<"salt">>,16777216,20,
148 base16:decode(<<"eefe3d61cd4da4e4e9945b3d6ba2158c2634e984">>)},
149 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha, P, S, It, DkLen)).
150
151test_vector_sha1_5(_Config) ->
152 {P,S,It,DkLen,Result} = {<<"passwordPASSWORDpassword">>,<<"saltSALTsaltSALTsaltSALTsaltSALTsalt">>,4096,25,
153 base16:decode(<<"3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038">>)},
154 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha, P, S, It, DkLen)).
155
156
157%% Taken from https://stackoverflow.com/a/5136918/8853275
158test_vector_sha256_1(_Config) ->
159 {P,S,It,DkLen,Result} = {<<"password">>, <<"salt">>, 1, 32,
160 base16:decode(<<"120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b">>)},
161 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)).
162
163test_vector_sha256_2(_Config) ->
164 {P,S,It,DkLen,Result} = {<<"password">>, <<"salt">>, 2, 32,
165 base16:decode(<<"ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43">>)},
166 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)).
167
168test_vector_sha256_3(_Config) ->
169 {P,S,It,DkLen,Result} = {<<"password">>, <<"salt">>, 4096, 32,
170 base16:decode(<<"c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a">>)},
171 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)).
172
173test_vector_sha256_4(_Config) ->
174 {P,S,It,DkLen,Result} = {<<"password">>, <<"salt">>, 16777216, 32,
175 base16:decode(<<"cf81c66fe8cfc04d1f31ecb65dab4089f7f179e89b3b0bcb17ad10e3ac6eba46">>)},
176 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)).
177
178test_vector_sha256_5(_Config) ->
179 {P,S,It,DkLen,Result} = {<<"passwordPASSWORDpassword">>,<<"saltSALTsaltSALTsaltSALTsaltSALTsalt">>,4096,40,
Nelson Vides8d564652021-02-23 15:20:12 +0100180 base16:decode(<<"348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9">>)},
Nelson Videsf9ee3062021-02-10 20:13:20 +0100181 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)).
182
183test_vector_sha256_6(_Config) ->
184 {P,S,It,DkLen,Result} = {<<"pass\0word">>, <<"sa\0lt">>, 4096, 16,
185 base16:decode(<<"89b69d0516f829893c696226650a8687">>)},
186 ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)).