1 1.1 riastrad 2 1.1 riastrad #define TEST_NAME "pwhash_argon2i" 3 1.1 riastrad #include "cmptest.h" 4 1.1 riastrad 5 1.1 riastrad #define OUT_LEN 128 6 1.1 riastrad #define OPSLIMIT 3 7 1.1 riastrad #define MEMLIMIT 5000000 8 1.1 riastrad 9 1.1 riastrad static void 10 1.1 riastrad tv(void) 11 1.1 riastrad { 12 1.1 riastrad static struct { 13 1.1 riastrad const char * passwd_hex; 14 1.1 riastrad size_t passwd_len; 15 1.1 riastrad const char * salt_hex; 16 1.1 riastrad size_t outlen; 17 1.1 riastrad unsigned long long opslimit; 18 1.1 riastrad size_t memlimit; 19 1.1 riastrad unsigned int lanes; 20 1.1 riastrad } tests[] = { 21 1.1 riastrad { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0" 22 1.1 riastrad "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d" 23 1.1 riastrad "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5" 24 1.1 riastrad "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6", 25 1.1 riastrad 127, 26 1.1 riastrad "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2", 27 1.1 riastrad 155, 5, 7256678, 1 }, 28 1.1 riastrad { "e125cee61c8cb7778d9e5ad0a6f5d978ce9f84de213a8556d9ffe202020ab4a6ed" 29 1.1 riastrad "9074a4eb3416f9b168f137510f3a30b70b96cbfa219ff99f6c6eaffb15c06b60e0" 30 1.1 riastrad "0cc2890277f0fd3c622115772f7048adaebed86e", 31 1.1 riastrad 86, 32 1.1 riastrad "f1192dd5dc2368b9cd421338b22433455ee0a3699f9379a08b9650ea2c126f0d", 33 1.1 riastrad 250, 4, 7849083, 1 }, 34 1.1 riastrad { "92263cbf6ac376499f68a4289d3bb59e5a22335eba63a32e6410249155b956b6a3" 35 1.1 riastrad "b48d4a44906b18b897127300b375b8f834f1ceffc70880a885f47c33876717e392" 36 1.1 riastrad "be57f7da3ae58da4fd1f43daa7e44bb82d3717af4319349c24cd31e46d295856b0" 37 1.1 riastrad "441b6b289992a11ced1cc3bf3011604590244a3eb737ff221129215e4e4347f491" 38 1.1 riastrad "5d41292b5173d196eb9add693be5319fdadc242906178bb6c0286c9b6ca6012746" 39 1.1 riastrad "711f58c8c392016b2fdfc09c64f0f6b6ab7b", 40 1.1 riastrad 183, 41 1.1 riastrad "3b840e20e9555e9fb031c4ba1f1747ce25cc1d0ff664be676b9b4a90641ff194", 42 1.1 riastrad 249, 3, 7994791, 1 }, 43 1.1 riastrad { "027b6d8e8c8c474e9b69c7d9ed4f9971e8e1ce2f6ba95048414c3970f0f09b70e3" 44 1.1 riastrad "b6c5ae05872b3d8678705b7d381829c351a5a9c88c233569b35d6b0b809df44b64" 45 1.1 riastrad "51a9c273f1150e2ef8a0b5437eb701e373474cd44b97ef0248ebce2ca0400e1b53" 46 1.1 riastrad "f3d86221eca3f18eb45b702b9172440f774a82cbf1f6f525df30a6e293c873cce6" 47 1.1 riastrad "9bb078ed1f0d31e7f9b8062409f37f19f8550aae", 48 1.1 riastrad 152, 49 1.1 riastrad "eb2a3056a09ad2d7d7f975bcd707598f24cd32518cde3069f2e403b34bfee8a5", 5, 50 1.1 riastrad 4, 1397645, 1 }, 51 1.1 riastrad { "4a857e2ee8aa9b6056f2424e84d24a72473378906ee04a46cb05311502d5250b82" 52 1.1 riastrad "ad86b83c8f20a23dbb74f6da60b0b6ecffd67134d45946ac8ebfb3064294bc097d" 53 1.1 riastrad "43ced68642bfb8bbbdd0f50b30118f5e", 54 1.1 riastrad 82, 55 1.1 riastrad "39d82eef32010b8b79cc5ba88ed539fbaba741100f2edbeca7cc171ffeabf258", 56 1.1 riastrad 190, 3, 1432947, 1 }, 57 1.1 riastrad { "c7b09aec680e7b42fedd7fc792e78b2f6c1bea8f4a884320b648f81e8cf515e8ba" 58 1.1 riastrad "9dcfb11d43c4aae114c1734aa69ca82d44998365db9c93744fa28b63fd16000e82" 59 1.1 riastrad "61cbbe083e7e2da1e5f696bde0834fe53146d7e0e35e7de9920d041f5a5621aabe" 60 1.1 riastrad "02da3e2b09b405b77937efef3197bd5772e41fdb73fb5294478e45208063b5f58e" 61 1.1 riastrad "089dbeb6d6342a909c1307b3fff5fe2cf4da56bdae50848f", 62 1.1 riastrad 156, 63 1.1 riastrad "039c056d933b475032777edbaffac50f143f64c123329ed9cf59e3b65d3f43b6", 64 1.1 riastrad 178, 3, 4886999, 1 }, 65 1.1 riastrad { "b540beb016a5366524d4605156493f9874514a5aa58818cd0c6dfffaa9e90205f1" 66 1.1 riastrad "7b", 67 1.1 riastrad 34, 68 1.1 riastrad "44071f6d181561670bda728d43fb79b443bb805afdebaf98622b5165e01b15fb", 69 1.1 riastrad 231, 1, 1631659, 1 }, 70 1.1 riastrad { "a14975c26c088755a8b715ff2528d647cd343987fcf4aa25e7194a8417fb2b4b3f" 71 1.1 riastrad "7268da9f3182b4cfb22d138b2749d673a47ecc7525dd15a0a3c66046971784bb63" 72 1.1 riastrad "d7eae24cc84f2631712075a10e10a96b0e0ee67c43e01c423cb9c44e5371017e9c" 73 1.1 riastrad "496956b632158da3fe12addecb88912e6759bc37f9af2f45af72c5cae3b179ffb6" 74 1.1 riastrad "76a697de6ebe45cd4c16d4a9d642d29ddc0186a0a48cb6cd62bfc3dd229d313b30" 75 1.1 riastrad "1560971e740e2cf1f99a9a090a5b283f35475057e96d7064e2e0fc81984591068d" 76 1.1 riastrad "55a3b4169f22cccb0745a2689407ea1901a0a766eb99", 77 1.1 riastrad 220, 78 1.1 riastrad "3d968b2752b8838431165059319f3ff8910b7b8ecb54ea01d3f54769e9d98daf", 79 1.1 riastrad 167, 3, 1784128, 1 }, 80 1.1 riastrad }; 81 1.1 riastrad char passwd[256]; 82 1.1 riastrad unsigned char salt[crypto_pwhash_SALTBYTES]; 83 1.1 riastrad unsigned char out[256]; 84 1.1 riastrad char out_hex[256 * 2 + 1]; 85 1.1 riastrad size_t i = 0U; 86 1.1 riastrad 87 1.1 riastrad do { 88 1.1 riastrad sodium_hex2bin((unsigned char *) passwd, sizeof passwd, 89 1.1 riastrad tests[i].passwd_hex, strlen(tests[i].passwd_hex), NULL, 90 1.1 riastrad NULL, NULL); 91 1.1 riastrad sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex, 92 1.1 riastrad strlen(tests[i].salt_hex), NULL, NULL, NULL); 93 1.1 riastrad if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, passwd, 94 1.1 riastrad tests[i].passwd_len, (const unsigned char *) salt, 95 1.1 riastrad tests[i].opslimit, tests[i].memlimit, 96 1.1 riastrad crypto_pwhash_alg_argon2i13()) != 0) { 97 1.1 riastrad printf("[tv] pwhash failure (maybe intentional): [%u]\n", 98 1.1 riastrad (unsigned int) i); 99 1.1 riastrad continue; 100 1.1 riastrad } 101 1.1 riastrad sodium_bin2hex(out_hex, sizeof out_hex, out, tests[i].outlen); 102 1.1 riastrad printf("%s\n", out_hex); 103 1.1 riastrad } while (++i < (sizeof tests) / (sizeof tests[0])); 104 1.1 riastrad } 105 1.1 riastrad 106 1.1 riastrad static void 107 1.1 riastrad tv2(void) 108 1.1 riastrad { 109 1.1 riastrad static struct { 110 1.1 riastrad const char * passwd_hex; 111 1.1 riastrad size_t passwd_len; 112 1.1 riastrad const char * salt_hex; 113 1.1 riastrad size_t outlen; 114 1.1 riastrad unsigned long long opslimit; 115 1.1 riastrad size_t memlimit; 116 1.1 riastrad unsigned int lanes; 117 1.1 riastrad } tests[] = { 118 1.1 riastrad { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0" 119 1.1 riastrad "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d" 120 1.1 riastrad "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5" 121 1.1 riastrad "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6", 122 1.1 riastrad 127, 123 1.1 riastrad "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2", 124 1.1 riastrad 155, 4, 397645, 1 }, 125 1.1 riastrad { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0" 126 1.1 riastrad "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d" 127 1.1 riastrad "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5" 128 1.1 riastrad "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6", 129 1.1 riastrad 127, 130 1.1 riastrad "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2", 131 1.1 riastrad 155, 3, 397645, 1 }, 132 1.1 riastrad }; 133 1.1 riastrad char passwd[256]; 134 1.1 riastrad unsigned char salt[crypto_pwhash_SALTBYTES]; 135 1.1 riastrad unsigned char out[256]; 136 1.1 riastrad char out_hex[256 * 2 + 1]; 137 1.1 riastrad size_t i = 0U; 138 1.1 riastrad 139 1.1 riastrad do { 140 1.1 riastrad sodium_hex2bin((unsigned char *) passwd, sizeof passwd, 141 1.1 riastrad tests[i].passwd_hex, strlen(tests[i].passwd_hex), NULL, 142 1.1 riastrad NULL, NULL); 143 1.1 riastrad sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex, 144 1.1 riastrad strlen(tests[i].salt_hex), NULL, NULL, NULL); 145 1.1 riastrad if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, passwd, 146 1.1 riastrad tests[i].passwd_len, (const unsigned char *) salt, 147 1.1 riastrad tests[i].opslimit, tests[i].memlimit, 148 1.1 riastrad crypto_pwhash_alg_argon2i13()) != 0) { 149 1.1 riastrad printf("[tv2] pwhash failure: [%u]\n", (unsigned int) i); 150 1.1 riastrad continue; 151 1.1 riastrad } 152 1.1 riastrad sodium_bin2hex(out_hex, sizeof out_hex, out, tests[i].outlen); 153 1.1 riastrad printf("%s\n", out_hex); 154 1.1 riastrad } while (++i < (sizeof tests) / (sizeof tests[0])); 155 1.1 riastrad 156 1.1 riastrad if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 3, 157 1.1 riastrad 1ULL << 12, 0) != -1) { 158 1.1 riastrad printf("[tv2] pwhash should have failed (0)\n"); 159 1.1 riastrad } 160 1.1 riastrad if (crypto_pwhash_argon2i(out, sizeof out, "password", strlen("password"), salt, 3, 161 1.1 riastrad 1ULL << 12, 0) != -1) { 162 1.1 riastrad printf("[tv2] pwhash should have failed (0')\n"); 163 1.1 riastrad } 164 1.1 riastrad if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 3, 165 1.1 riastrad 1, crypto_pwhash_alg_argon2i13()) != -1) { 166 1.1 riastrad printf("[tv2] pwhash should have failed (1)\n"); 167 1.1 riastrad } 168 1.1 riastrad if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 3, 169 1.1 riastrad 1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) { 170 1.1 riastrad printf("[tv2] pwhash should have failed (2)\n"); 171 1.1 riastrad } 172 1.1 riastrad if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 2, 173 1.1 riastrad 1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) { 174 1.1 riastrad printf("[tv2] pwhash should have failed (3)\n"); 175 1.1 riastrad } 176 1.1 riastrad if (crypto_pwhash(out, 15, "password", strlen("password"), salt, 3, 177 1.1 riastrad 1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) { 178 1.1 riastrad printf("[tv2] pwhash with a short output length should have failed\n"); 179 1.1 riastrad } 180 1.1 riastrad if (crypto_pwhash(out, sizeof out, "password", 0x100000000ULL, salt, 3, 181 1.1 riastrad 1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) { 182 1.1 riastrad printf("[tv2] pwhash with a long password length should have failed\n"); 183 1.1 riastrad } 184 1.1 riastrad assert(crypto_pwhash_argon2i(out, sizeof out, "password", strlen("password"), salt, 185 1.1 riastrad OPSLIMIT, MEMLIMIT, crypto_pwhash_alg_argon2id13()) == -1); 186 1.1 riastrad } 187 1.1 riastrad 188 1.1 riastrad static void 189 1.1 riastrad tv3(void) 190 1.1 riastrad { 191 1.1 riastrad static struct { 192 1.1 riastrad const char *passwd; 193 1.1 riastrad const char *out; 194 1.1 riastrad } tests[] = { 195 1.1 riastrad { "", 196 1.1 riastrad "$argon2i$v=19$m=4096,t=1,p=1$X1NhbHQAAAAAAAAAAAAAAA$bWh++" 197 1.1 riastrad "MKN1OiFHKgIWTLvIi1iHicmHH7+Fv3K88ifFfI" }, 198 1.1 riastrad { "", 199 1.1 riastrad "$argon2i$v=19$m=2048,t=4,p=1$SWkxaUhpY21ISDcrRnYzSw$Mbg/" 200 1.1 riastrad "Eck1kpZir5T9io7C64cpffdTBaORgyriLQFgQj8" }, 201 1.1 riastrad { "^T5H$JYt39n%K*j:W]!1s?vg!:jGi]Ax?..l7[p0v:1jHTpla9;]bUN;?bWyCbtqg ", 202 1.1 riastrad "$argon2i$v=19$m=4096,t=3,p=2$X1NhbHQAAAAAAAAAAAAAAA$z/QMiU4lQxGsYNc/" 203 1.1 riastrad "+K/bizwsA1P11UG2dj/7+aILJ4I" }, 204 1.1 riastrad { "K3S=KyH#)36_?]LxeR8QNKw6X=gFbxai$C%29V*", 205 1.1 riastrad "$argon2i$v=19$m=4096,t=3,p=1$X1NhbHQAAAAAAAAAAAAAAA$fu2Wsecyt+" 206 1.1 riastrad "yPnBvSvYN16oP5ozRmkp0ixJ1YL19V3Uo" } 207 1.1 riastrad }; 208 1.1 riastrad char *out; 209 1.1 riastrad char *passwd; 210 1.1 riastrad size_t i = 0U; 211 1.1 riastrad 212 1.1 riastrad do { 213 1.1 riastrad out = (char *) sodium_malloc(strlen(tests[i].out) + 1U); 214 1.1 riastrad assert(out != NULL); 215 1.1 riastrad memcpy(out, tests[i].out, strlen(tests[i].out) + 1U); 216 1.1 riastrad passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U); 217 1.1 riastrad assert(passwd != NULL); 218 1.1 riastrad memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U); 219 1.1 riastrad if (crypto_pwhash_str_verify(out, passwd, strlen(passwd)) != 0) { 220 1.1 riastrad printf("[tv3] pwhash_str failure (maybe intentional): [%u]\n", 221 1.1 riastrad (unsigned int) i); 222 1.1 riastrad continue; 223 1.1 riastrad } 224 1.1 riastrad sodium_free(out); 225 1.1 riastrad sodium_free(passwd); 226 1.1 riastrad } while (++i < (sizeof tests) / (sizeof tests[0])); 227 1.1 riastrad } 228 1.1 riastrad 229 1.1 riastrad static void 230 1.1 riastrad str_tests(void) 231 1.1 riastrad { 232 1.1 riastrad char *str_out; 233 1.1 riastrad char *str_out2; 234 1.1 riastrad char *salt; 235 1.1 riastrad const char *passwd = "Correct Horse Battery Staple"; 236 1.1 riastrad 237 1.1 riastrad salt = (char *) sodium_malloc(crypto_pwhash_SALTBYTES); 238 1.1 riastrad str_out = (char *) sodium_malloc(crypto_pwhash_STRBYTES); 239 1.1 riastrad str_out2 = (char *) sodium_malloc(crypto_pwhash_STRBYTES); 240 1.1 riastrad memcpy(salt, ">A 16-bytes salt", crypto_pwhash_SALTBYTES); 241 1.1 riastrad if (crypto_pwhash_argon2i_str(str_out, passwd, strlen(passwd), OPSLIMIT, 242 1.1 riastrad MEMLIMIT) != 0) { 243 1.1 riastrad printf("pwhash_argon2i_str failure\n"); 244 1.1 riastrad } 245 1.1 riastrad if (crypto_pwhash_argon2i_str(str_out2, passwd, strlen(passwd), OPSLIMIT, 246 1.1 riastrad MEMLIMIT) != 0) { 247 1.1 riastrad printf("pwhash_argon2i_str(2) failure\n"); 248 1.1 riastrad } 249 1.1 riastrad if (strcmp(str_out, str_out2) == 0) { 250 1.1 riastrad printf("pwhash_argon2i_str() doesn't generate different salts\n"); 251 1.1 riastrad } 252 1.1 riastrad if (crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT) != 0) { 253 1.1 riastrad printf("needs_rehash() false positive\n"); 254 1.1 riastrad } 255 1.1 riastrad if (crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT / 2) != 1 || 256 1.1 riastrad crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT / 2, MEMLIMIT) != 1 || 257 1.1 riastrad crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT * 2) != 1 || 258 1.1 riastrad crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT * 2, MEMLIMIT) != 1) { 259 1.1 riastrad printf("needs_rehash() false negative\n"); 260 1.1 riastrad } 261 1.1 riastrad if (crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT / 2) != 1) { 262 1.1 riastrad printf("pwhash_str_needs_rehash() didn't handle argon2i\n"); 263 1.1 riastrad } 264 1.1 riastrad if (crypto_pwhash_str_needs_rehash(str_out + 1, OPSLIMIT, MEMLIMIT) != -1 || 265 1.1 riastrad crypto_pwhash_argon2i_str_needs_rehash(str_out + 1, OPSLIMIT, MEMLIMIT) != -1) { 266 1.1 riastrad printf("needs_rehash() didn't fail with an invalid hash string\n"); 267 1.1 riastrad } 268 1.1 riastrad if (sodium_is_zero((const unsigned char *) str_out + strlen(str_out), 269 1.1 riastrad crypto_pwhash_STRBYTES - strlen(str_out)) != 1 || 270 1.1 riastrad sodium_is_zero((const unsigned char *) str_out2 + strlen(str_out2), 271 1.1 riastrad crypto_pwhash_STRBYTES - strlen(str_out2)) != 1) { 272 1.1 riastrad printf("pwhash_str() doesn't properly pad with zeros\n"); 273 1.1 riastrad } 274 1.1 riastrad if (crypto_pwhash_argon2i_str_verify(str_out, passwd, strlen(passwd)) != 0) { 275 1.1 riastrad printf("pwhash_str_verify(1) failure\n"); 276 1.1 riastrad } 277 1.1 riastrad str_out[14]++; 278 1.1 riastrad if (crypto_pwhash_argon2i_str_verify(str_out, passwd, strlen(passwd)) != -1) { 279 1.1 riastrad printf("pwhash_str_verify(2) failure\n"); 280 1.1 riastrad } 281 1.1 riastrad str_out[14]--; 282 1.1 riastrad assert(str_out[crypto_pwhash_STRBYTES - 1U] == 0); 283 1.1 riastrad 284 1.1 riastrad if (crypto_pwhash_argon2i_str(str_out2, passwd, 0x100000000ULL, OPSLIMIT, 285 1.1 riastrad MEMLIMIT) != -1) { 286 1.1 riastrad printf("pwhash_str() with a large password should have failed\n"); 287 1.1 riastrad } 288 1.1 riastrad if (crypto_pwhash_argon2i_str(str_out2, passwd, strlen(passwd), 1, MEMLIMIT) != 289 1.1 riastrad -1) { 290 1.1 riastrad printf("pwhash_str() with a small opslimit should have failed\n"); 291 1.1 riastrad } 292 1.1 riastrad if (crypto_pwhash_argon2i_str_verify("$argon2i$m=65536,t=2,p=1c29tZXNhbHQ" 293 1.1 riastrad "$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ", 294 1.1 riastrad "password", 0x100000000ULL) != -1) { 295 1.1 riastrad printf("pwhash_str_verify(invalid(0)) failure\n"); 296 1.1 riastrad } 297 1.1 riastrad if (crypto_pwhash_argon2i_str_verify("$argon2i$m=65536,t=2,p=1c29tZXNhbHQ" 298 1.1 riastrad "$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ", 299 1.1 riastrad "password", strlen("password")) != -1) { 300 1.1 riastrad printf("pwhash_str_verify(invalid(1)) failure %d\n", errno); 301 1.1 riastrad } 302 1.1 riastrad if (crypto_pwhash_argon2i_str_verify("$argon2i$m=65536,t=2,p=1$c29tZXNhbHQ" 303 1.1 riastrad "9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ", 304 1.1 riastrad "password", strlen("password")) != -1) { 305 1.1 riastrad printf("pwhash_str_verify(invalid(2)) failure\n"); 306 1.1 riastrad } 307 1.1 riastrad if (crypto_pwhash_str_verify("$argon2i$m=65536,t=2,p=1$c29tZXNhbHQ" 308 1.1 riastrad "$b2G3seW+uPzerwQQC+/E1K50CLLO7YXy0JRcaTuswRo", 309 1.1 riastrad "password", strlen("password")) != -1) { 310 1.1 riastrad printf("pwhash_str_verify(invalid(3)) failure\n"); 311 1.1 riastrad } 312 1.1 riastrad if (crypto_pwhash_str_verify("$argon2i$v=19$m=65536,t=2,p=1c29tZXNhbHQ" 313 1.1 riastrad "$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA", 314 1.1 riastrad "password", strlen("password")) != -1) { 315 1.1 riastrad printf("pwhash_str_verify(invalid(4)) failure\n"); 316 1.1 riastrad } 317 1.1 riastrad if (crypto_pwhash_str_verify("$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ" 318 1.1 riastrad "wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA", 319 1.1 riastrad "password", strlen("password")) != -1) { 320 1.1 riastrad printf("pwhash_str_verify(invalid(5)) failure\n"); 321 1.1 riastrad } 322 1.1 riastrad if (crypto_pwhash_str_verify("$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ" 323 1.1 riastrad "$8iIuixkI73Js3G1uMbezQXD0b8LG4SXGsOwoQkdAQIM", 324 1.1 riastrad "password", strlen("password")) != -1) { 325 1.1 riastrad printf("pwhash_str_verify(invalid(6)) failure\n"); 326 1.1 riastrad } 327 1.1 riastrad if (crypto_pwhash_str_verify( 328 1.1 riastrad "$argon2i$v=19$m=4096,t=3,p=2$b2RpZHVlamRpc29kaXNrdw" 329 1.1 riastrad "$TNnWIwlu1061JHrnCqIAmjs3huSxYIU+0jWipu7Kc9M", 330 1.1 riastrad "password", strlen("password")) != 0) { 331 1.1 riastrad printf("pwhash_str_verify(valid(7)) failure\n"); 332 1.1 riastrad } 333 1.1 riastrad if (crypto_pwhash_str_verify( 334 1.1 riastrad "$argon2i$v=19$m=4096,t=3,p=2$b2RpZHVlamRpc29kaXNrdw" 335 1.1 riastrad "$TNnWIwlu1061JHrnCqIAmjs3huSxYIU+0jWipu7Kc9M", 336 1.1 riastrad "passwore", strlen("passwore")) != -1 || errno != EINVAL) { 337 1.1 riastrad printf("pwhash_str_verify(invalid(7)) failure\n"); 338 1.1 riastrad } 339 1.1 riastrad if (crypto_pwhash_str_verify( 340 1.1 riastrad "$Argon2i$v=19$m=4096,t=3,p=2$b2RpZHVlamRpc29kaXNrdw" 341 1.1 riastrad "$TNnWIwlu1061JHrnCqIAmjs3huSxYIU+0jWipu7Kc9M", 342 1.1 riastrad "password", strlen("password")) != -1 || errno != EINVAL) { 343 1.1 riastrad printf("pwhash_str_verify(invalid(8)) failure\n"); 344 1.1 riastrad } 345 1.1 riastrad if (crypto_pwhash_str_verify( 346 1.1 riastrad "$argon2i$v=1$m=4096,t=3,p=2$b2RpZHVlamRpc29kaXNrdw" 347 1.1 riastrad "$TNnWIwlu1061JHrnCqIAmjs3huSxYIU+0jWipu7Kc9M", 348 1.1 riastrad "password", strlen("password")) != -1 || errno != EINVAL) { 349 1.1 riastrad printf("pwhash_str_verify(invalid(9)) failure\n"); 350 1.1 riastrad } 351 1.1 riastrad if (crypto_pwhash_str_verify( 352 1.1 riastrad "$argon2i$v=1$m=4096,t=3,p=2$b2RpZHVla~=mRpc29kaXNrdw" 353 1.1 riastrad "$TNnWIwlu1061JHrnCqIAmjs3huSxYIU+0jWipu7Kc9M", 354 1.1 riastrad "password", strlen("password")) != -1 || errno != EINVAL) { 355 1.1 riastrad printf("pwhash_str_verify(invalid(10)) failure\n"); 356 1.1 riastrad } 357 1.1 riastrad if (crypto_pwhash_str_verify( 358 1.1 riastrad "$argon2i$v=1$m=4096,t=3,p=2$b2RpZHVlamRpc29kaXNrdw" 359 1.1 riastrad "$TNnWIwlu1061JHrnCqIAmjs3huSxYI~=U+0jWipu7Kc9M", 360 1.1 riastrad "password", strlen("password")) != -1 || errno != EINVAL) { 361 1.1 riastrad printf("pwhash_str_verify(invalid(11)) failure\n"); 362 1.1 riastrad } 363 1.1 riastrad assert(crypto_pwhash_str_alg(str_out, "test", 4, OPSLIMIT, MEMLIMIT, 364 1.1 riastrad crypto_pwhash_ALG_ARGON2I13) == 0); 365 1.1 riastrad assert(crypto_pwhash_argon2i_str_verify(str_out, "test", 4) == 0); 366 1.1 riastrad assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 367 1.1 riastrad OPSLIMIT, MEMLIMIT) == 0); 368 1.1 riastrad assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 369 1.1 riastrad OPSLIMIT / 2, MEMLIMIT) == 1); 370 1.1 riastrad assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 371 1.1 riastrad OPSLIMIT, MEMLIMIT / 2) == 1); 372 1.1 riastrad assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 0, 0) == 1); 373 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 0, 0) == -1); 374 1.1 riastrad assert(crypto_pwhash_argon2i_str_needs_rehash(str_out + 1, 375 1.1 riastrad OPSLIMIT, MEMLIMIT) == -1); 376 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 0, 0) == -1); 377 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash("", OPSLIMIT, MEMLIMIT) == -1); 378 1.1 riastrad assert(crypto_pwhash_str_alg(str_out, "test", 4, OPSLIMIT, MEMLIMIT, 379 1.1 riastrad crypto_pwhash_ALG_ARGON2ID13) == 0); 380 1.1 riastrad assert(crypto_pwhash_argon2id_str_verify(str_out, "test", 4) == 0); 381 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 382 1.1 riastrad OPSLIMIT, MEMLIMIT) == 0); 383 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 384 1.1 riastrad OPSLIMIT / 2, MEMLIMIT) == 1); 385 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 386 1.1 riastrad OPSLIMIT, MEMLIMIT / 2) == 1); 387 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 0, 0) == 1); 388 1.1 riastrad assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 0, 0) == -1); 389 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash("", OPSLIMIT, MEMLIMIT) == -1); 390 1.1 riastrad assert(crypto_pwhash_argon2id_str_needs_rehash(str_out + 1, 391 1.1 riastrad OPSLIMIT, MEMLIMIT) == -1); 392 1.1 riastrad sodium_free(salt); 393 1.1 riastrad sodium_free(str_out); 394 1.1 riastrad sodium_free(str_out2); 395 1.1 riastrad } 396 1.1 riastrad 397 1.1 riastrad int 398 1.1 riastrad main(void) 399 1.1 riastrad { 400 1.1 riastrad tv(); 401 1.1 riastrad tv2(); 402 1.1 riastrad tv3(); 403 1.1 riastrad str_tests(); 404 1.1 riastrad 405 1.1 riastrad assert(crypto_pwhash_argon2i_bytes_min() > 0U); 406 1.1 riastrad assert(crypto_pwhash_argon2i_bytes_max() > crypto_pwhash_argon2i_bytes_min()); 407 1.1 riastrad assert(crypto_pwhash_argon2i_passwd_max() > crypto_pwhash_argon2i_passwd_min()); 408 1.1 riastrad assert(crypto_pwhash_argon2i_saltbytes() > 0U); 409 1.1 riastrad assert(crypto_pwhash_argon2i_strbytes() > 1U); 410 1.1 riastrad assert(crypto_pwhash_argon2i_strbytes() > strlen(crypto_pwhash_argon2i_strprefix())); 411 1.1 riastrad 412 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_min() > 0U); 413 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_max() > 0U); 414 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_min() > 0U); 415 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_max() > 0U); 416 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_interactive() > 0U); 417 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_interactive() > 0U); 418 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_moderate() > 0U); 419 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_moderate() > 0U); 420 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_sensitive() > 0U); 421 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_sensitive() > 0U); 422 1.1 riastrad 423 1.1 riastrad assert(crypto_pwhash_argon2i_bytes_min() == crypto_pwhash_argon2i_BYTES_MIN); 424 1.1 riastrad assert(crypto_pwhash_argon2i_bytes_max() == crypto_pwhash_argon2i_BYTES_MAX); 425 1.1 riastrad assert(crypto_pwhash_argon2i_passwd_min() == crypto_pwhash_argon2i_PASSWD_MIN); 426 1.1 riastrad assert(crypto_pwhash_argon2i_passwd_max() == crypto_pwhash_argon2i_PASSWD_MAX); 427 1.1 riastrad assert(crypto_pwhash_argon2i_saltbytes() == crypto_pwhash_argon2i_SALTBYTES); 428 1.1 riastrad assert(crypto_pwhash_argon2i_strbytes() == crypto_pwhash_argon2i_STRBYTES); 429 1.1 riastrad 430 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_min() == crypto_pwhash_argon2i_OPSLIMIT_MIN); 431 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_max() == crypto_pwhash_argon2i_OPSLIMIT_MAX); 432 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_min() == crypto_pwhash_argon2i_MEMLIMIT_MIN); 433 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_max() == crypto_pwhash_argon2i_MEMLIMIT_MAX); 434 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_interactive() == 435 1.1 riastrad crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE); 436 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_interactive() == 437 1.1 riastrad crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE); 438 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_moderate() == 439 1.1 riastrad crypto_pwhash_argon2i_OPSLIMIT_MODERATE); 440 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_moderate() == 441 1.1 riastrad crypto_pwhash_argon2i_MEMLIMIT_MODERATE); 442 1.1 riastrad assert(crypto_pwhash_argon2i_opslimit_sensitive() == 443 1.1 riastrad crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE); 444 1.1 riastrad assert(crypto_pwhash_argon2i_memlimit_sensitive() == 445 1.1 riastrad crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE); 446 1.1 riastrad 447 1.1 riastrad assert(crypto_pwhash_argon2i_alg_argon2i13() == crypto_pwhash_argon2i_ALG_ARGON2I13); 448 1.1 riastrad 449 1.1 riastrad printf("OK\n"); 450 1.1 riastrad 451 1.1 riastrad return 0; 452 1.1 riastrad } 453