pwhash_argon2i.c 21 KB

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