Nelson Vides | f9ee306 | 2021-02-10 20:13:20 +0100 | [diff] [blame] | 1 | -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 Vides | 117590d | 2021-03-09 22:06:12 +0100 | [diff] [blame] | 19 | erlang_and_nif_are_equivalent_sha512/1 |
Nelson Vides | f9ee306 | 2021-02-10 20:13:20 +0100 | [diff] [blame] | 20 | ]). |
| 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 | |
| 39 | all() -> |
| 40 | [ |
| 41 | {group, equivalents}, |
Nelson Vides | 117590d | 2021-03-09 22:06:12 +0100 | [diff] [blame] | 42 | {group, test_vectors} |
Nelson Vides | f9ee306 | 2021-02-10 20:13:20 +0100 | [diff] [blame] | 43 | ]. |
| 44 | |
| 45 | groups() -> |
| 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 | %%%=================================================================== |
| 74 | init_per_suite(Config) -> |
| 75 | Config. |
| 76 | |
| 77 | end_per_suite(_Config) -> |
| 78 | ok. |
| 79 | |
| 80 | %%%=================================================================== |
| 81 | %%% Group specific setup/teardown |
| 82 | %%%=================================================================== |
| 83 | init_per_group(_Groupname, Config) -> |
| 84 | Config. |
| 85 | |
| 86 | end_per_group(_Groupname, _Config) -> |
| 87 | ok. |
| 88 | |
| 89 | %%%=================================================================== |
| 90 | %%% Testcase specific setup/teardown |
| 91 | %%%=================================================================== |
| 92 | init_per_testcase(_TestCase, Config) -> |
| 93 | Config. |
| 94 | |
| 95 | end_per_testcase(_TestCase, _Config) -> |
| 96 | ok. |
| 97 | |
| 98 | %%%=================================================================== |
| 99 | %%% Individual Test Cases (from groups() definition) |
| 100 | %%%=================================================================== |
| 101 | |
| 102 | erlang_and_nif_are_equivalent_sha1(_Config) -> |
| 103 | erlang_and_nif_are_equivalent_(sha). |
| 104 | |
| 105 | erlang_and_nif_are_equivalent_sha224(_Config) -> |
| 106 | erlang_and_nif_are_equivalent_(sha224). |
| 107 | |
| 108 | erlang_and_nif_are_equivalent_sha256(_Config) -> |
| 109 | erlang_and_nif_are_equivalent_(sha256). |
| 110 | |
| 111 | erlang_and_nif_are_equivalent_sha384(_Config) -> |
| 112 | erlang_and_nif_are_equivalent_(sha384). |
| 113 | |
| 114 | erlang_and_nif_are_equivalent_sha512(_Config) -> |
| 115 | erlang_and_nif_are_equivalent_(sha512). |
| 116 | |
| 117 | erlang_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 | |
| 131 | test_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 | |
| 136 | test_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 | |
| 141 | test_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 | |
| 146 | test_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 | |
| 151 | test_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 |
| 158 | test_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 | |
| 163 | test_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 | |
| 168 | test_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 | |
| 173 | test_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 | |
| 178 | test_vector_sha256_5(_Config) -> |
| 179 | {P,S,It,DkLen,Result} = {<<"passwordPASSWORDpassword">>,<<"saltSALTsaltSALTsaltSALTsaltSALTsalt">>,4096,40, |
Nelson Vides | 8d56465 | 2021-02-23 15:20:12 +0100 | [diff] [blame] | 180 | base16:decode(<<"348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9">>)}, |
Nelson Vides | f9ee306 | 2021-02-10 20:13:20 +0100 | [diff] [blame] | 181 | ?assertEqual(Result, fast_pbkdf2:pbkdf2(sha256, P, S, It, DkLen)). |
| 182 | |
| 183 | test_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)). |