quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

pwhash_argon2id.c (26385B)


      1 
      2 #define TEST_NAME "pwhash_argon2id"
      3 #include "cmptest.h"
      4 
      5 #define OUT_LEN 128
      6 #define OPSLIMIT 3
      7 #define MEMLIMIT 5000000
      8 
      9 static void
     10 tv(void)
     11 {
     12     static struct {
     13         const char *       passwd_hex;
     14         size_t             passwd_len;
     15         const char *       salt_hex;
     16         size_t             outlen;
     17         unsigned long long opslimit;
     18         size_t             memlimit;
     19         unsigned int       lanes;
     20     } tests[] = {
     21         { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
     22           "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
     23           "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5"
     24           "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6",
     25           127,
     26           "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2",
     27           155, 5, 7256678, 1 },
     28         { "e125cee61c8cb7778d9e5ad0a6f5d978ce9f84de213a8556d9ffe202020ab4a6ed"
     29           "9074a4eb3416f9b168f137510f3a30b70b96cbfa219ff99f6c6eaffb15c06b60e0"
     30           "0cc2890277f0fd3c622115772f7048adaebed86e",
     31           86,
     32           "f1192dd5dc2368b9cd421338b22433455ee0a3699f9379a08b9650ea2c126f0d",
     33           250, 4, 7849083, 1 },
     34         { "92263cbf6ac376499f68a4289d3bb59e5a22335eba63a32e6410249155b956b6a3"
     35           "b48d4a44906b18b897127300b375b8f834f1ceffc70880a885f47c33876717e392"
     36           "be57f7da3ae58da4fd1f43daa7e44bb82d3717af4319349c24cd31e46d295856b0"
     37           "441b6b289992a11ced1cc3bf3011604590244a3eb737ff221129215e4e4347f491"
     38           "5d41292b5173d196eb9add693be5319fdadc242906178bb6c0286c9b6ca6012746"
     39           "711f58c8c392016b2fdfc09c64f0f6b6ab7b",
     40           183,
     41           "3b840e20e9555e9fb031c4ba1f1747ce25cc1d0ff664be676b9b4a90641ff194",
     42           249, 3, 7994791, 1 },
     43         { "027b6d8e8c8c474e9b69c7d9ed4f9971e8e1ce2f6ba95048414c3970f0f09b70e3"
     44           "b6c5ae05872b3d8678705b7d381829c351a5a9c88c233569b35d6b0b809df44b64"
     45           "51a9c273f1150e2ef8a0b5437eb701e373474cd44b97ef0248ebce2ca0400e1b53"
     46           "f3d86221eca3f18eb45b702b9172440f774a82cbf1f6f525df30a6e293c873cce6"
     47           "9bb078ed1f0d31e7f9b8062409f37f19f8550aae",
     48           152,
     49           "eb2a3056a09ad2d7d7f975bcd707598f24cd32518cde3069f2e403b34bfee8a5", 5,
     50           4, 1397645, 1 },
     51         { "4a857e2ee8aa9b6056f2424e84d24a72473378906ee04a46cb05311502d5250b82"
     52           "ad86b83c8f20a23dbb74f6da60b0b6ecffd67134d45946ac8ebfb3064294bc097d"
     53           "43ced68642bfb8bbbdd0f50b30118f5e",
     54           82,
     55           "39d82eef32010b8b79cc5ba88ed539fbaba741100f2edbeca7cc171ffeabf258",
     56           190, 3, 1432947, 1 },
     57         { "c7b09aec680e7b42fedd7fc792e78b2f6c1bea8f4a884320b648f81e8cf515e8ba"
     58           "9dcfb11d43c4aae114c1734aa69ca82d44998365db9c93744fa28b63fd16000e82"
     59           "61cbbe083e7e2da1e5f696bde0834fe53146d7e0e35e7de9920d041f5a5621aabe"
     60           "02da3e2b09b405b77937efef3197bd5772e41fdb73fb5294478e45208063b5f58e"
     61           "089dbeb6d6342a909c1307b3fff5fe2cf4da56bdae50848f",
     62           156,
     63           "039c056d933b475032777edbaffac50f143f64c123329ed9cf59e3b65d3f43b6",
     64           178, 3, 4886999, 1 },
     65         { "b540beb016a5366524d4605156493f9874514a5aa58818cd0c6dfffaa9e90205f1"
     66           "7b",
     67           34,
     68           "44071f6d181561670bda728d43fb79b443bb805afdebaf98622b5165e01b15fb",
     69           231, 1, 1631659, 1 },
     70         { "a14975c26c088755a8b715ff2528d647cd343987fcf4aa25e7194a8417fb2b4b3f"
     71           "7268da9f3182b4cfb22d138b2749d673a47ecc7525dd15a0a3c66046971784bb63"
     72           "d7eae24cc84f2631712075a10e10a96b0e0ee67c43e01c423cb9c44e5371017e9c"
     73           "496956b632158da3fe12addecb88912e6759bc37f9af2f45af72c5cae3b179ffb6"
     74           "76a697de6ebe45cd4c16d4a9d642d29ddc0186a0a48cb6cd62bfc3dd229d313b30"
     75           "1560971e740e2cf1f99a9a090a5b283f35475057e96d7064e2e0fc81984591068d"
     76           "55a3b4169f22cccb0745a2689407ea1901a0a766eb99",
     77           220,
     78           "3d968b2752b8838431165059319f3ff8910b7b8ecb54ea01d3f54769e9d98daf",
     79           167, 3, 1784128, 1 },
     80     };
     81     char          passwd[256];
     82     unsigned char salt[crypto_pwhash_SALTBYTES];
     83     unsigned char out[256];
     84     char          out_hex[256 * 2 + 1];
     85     size_t        i = 0U;
     86 
     87     do {
     88         sodium_hex2bin((unsigned char *) passwd, sizeof passwd,
     89                        tests[i].passwd_hex, strlen(tests[i].passwd_hex), NULL,
     90                        NULL, NULL);
     91         sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex,
     92                        strlen(tests[i].salt_hex), NULL, NULL, NULL);
     93         if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, passwd,
     94                           tests[i].passwd_len, (const unsigned char *) salt,
     95                           tests[i].opslimit, tests[i].memlimit,
     96                           crypto_pwhash_alg_default()) != 0) {
     97             printf("[tv] pwhash failure (maybe intentional): [%u]\n",
     98                    (unsigned int) i);
     99             continue;
    100         }
    101         sodium_bin2hex(out_hex, sizeof out_hex, out, tests[i].outlen);
    102         printf("%s\n", out_hex);
    103     } while (++i < (sizeof tests) / (sizeof tests[0]));
    104 }
    105 
    106 static void
    107 tv2(void)
    108 {
    109     static struct {
    110         const char *       passwd_hex;
    111         size_t             passwd_len;
    112         const char *       salt_hex;
    113         size_t             outlen;
    114         unsigned long long opslimit;
    115         size_t             memlimit;
    116         unsigned int       lanes;
    117     } tests[] = {
    118         { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
    119           "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
    120           "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5"
    121           "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6",
    122           127,
    123           "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2",
    124           155, 4, 397645, 1 },
    125         { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
    126           "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
    127           "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5"
    128           "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6",
    129           127,
    130           "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2",
    131           155, 3, 397645, 1 },
    132     };
    133     char          passwd[256];
    134     unsigned char salt[crypto_pwhash_SALTBYTES];
    135     unsigned char out[256];
    136     char          out_hex[256 * 2 + 1];
    137     size_t        i = 0U;
    138 
    139     do {
    140         sodium_hex2bin((unsigned char *) passwd, sizeof passwd,
    141                        tests[i].passwd_hex, strlen(tests[i].passwd_hex), NULL,
    142                        NULL, NULL);
    143         sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex,
    144                        strlen(tests[i].salt_hex), NULL, NULL, NULL);
    145         if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, passwd,
    146                           tests[i].passwd_len, (const unsigned char *) salt,
    147                           tests[i].opslimit, tests[i].memlimit,
    148                           crypto_pwhash_alg_default()) != 0) {
    149             printf("[tv2] pwhash failure: [%u]\n", (unsigned int) i);
    150             continue;
    151         }
    152         sodium_bin2hex(out_hex, sizeof out_hex, out, tests[i].outlen);
    153         printf("%s\n", out_hex);
    154     } while (++i < (sizeof tests) / (sizeof tests[0]));
    155 
    156     if (crypto_pwhash_argon2id(out, sizeof out, "password", strlen("password"), salt, 3,
    157                                1ULL << 12, 0) != -1) {
    158         printf("[tv2] pwhash should have failed (0)\n");
    159     }
    160     if (crypto_pwhash_argon2id(out, sizeof out, "password", strlen("password"), salt, 3,
    161                                1, crypto_pwhash_argon2id_alg_argon2id13()) != -1) {
    162         printf("[tv2] pwhash should have failed (1)\n");
    163     }
    164     if (crypto_pwhash_argon2id(out, sizeof out, "password", strlen("password"), salt, 3,
    165                                1ULL << 12, crypto_pwhash_argon2id_alg_argon2id13()) != -1) {
    166         printf("[tv2] pwhash should have failed (2)\n");
    167     }
    168     if (crypto_pwhash_argon2id(out, sizeof out, "password", strlen("password"), salt, 2,
    169                                1ULL << 12, crypto_pwhash_argon2id_alg_argon2id13()) != -1) {
    170         printf("[tv2] pwhash should have failed (3)\n");
    171     }
    172     if (crypto_pwhash_argon2id(out, 15, "password", strlen("password"), salt, 3,
    173                                1ULL << 12, crypto_pwhash_argon2id_alg_argon2id13()) != -1) {
    174         printf("[tv2] pwhash with a short output length should have failed\n");
    175     }
    176     if (crypto_pwhash_argon2id(out, sizeof out, "password", 0x100000000ULL, salt, 3,
    177                                1ULL << 12, crypto_pwhash_argon2id_alg_argon2id13()) != -1) {
    178         printf("[tv2] pwhash with a long password length should have failed\n");
    179     }
    180     assert(crypto_pwhash_argon2id(out, sizeof out, "password", strlen("password"), salt,
    181                                   OPSLIMIT, MEMLIMIT, crypto_pwhash_alg_argon2i13()) == -1);
    182 }
    183 
    184 static void
    185 tv3(void)
    186 {
    187     static struct {
    188         const char *passwd;
    189         const char *out;
    190     } tests[] = {
    191         { "",
    192           "$argon2id$v=19$m=4096,t=0,p=1$X1NhbHQAAAAAAAAAAAAAAA$bWh++MKN1OiFHKgIWTLvIi1iHicmHH7+Fv3K88ifFfI" },
    193         { "",
    194           "$argon2id$v=19$m=2048,t=4,p=1$SWkxaUhpY21ISDcrRnYzSw$Mbg/Eck1kpZir5T9io7C64cpffdTBaORgyriLQFgQj8" },
    195         { "",
    196           "$argon2id$v=19$m=4882,t=2,p=1$bA81arsiXysd3WbTRzmEOw$Nm8QBM+7RH1DXo9rvp5cwKEOOOfD2g6JuxlXihoNcpE" },
    197         { "^T5H$JYt39n%K*j:W]!1s?vg!:jGi]Ax?..l7[p0v:1jHTpla9;]bUN;?bWyCbtqg ",
    198           "$argon2id$v=19$m=4096,t=0,p=1$PkEgMTYtYnl0ZXMgc2FsdA$ltB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA" },
    199         { "^T5H$JYt39n%K*j:W]!1s?vg!:jGi]Ax?..l7[p0v:1jHTpla9;]bUN;?bWyCbtqg ",
    200           "$argon2id$v=19$m=4096,t=19,p=1$PkEgMTYtYnl0ZXMgc2FsdA$ltB/ue1kPtBMBGfsysMpPigE6hiNEKZ9vs8vLNVDQGA" },
    201         { "K3S=KyH#)36_?]LxeR8QNKw6X=gFbxai$C%29V*",
    202           "$argon2id$v=19$m=4096,t=1,p=3$PkEgcHJldHR5IGxvbmcgc2FsdA$HUqx5Z1b/ZypnUrvvJ5UC2Q+T6Q1WwASK/Kr9dRbGA0" }
    203     };
    204     char   *out;
    205     char   *passwd;
    206     size_t  i = 0U;
    207     int     ret;
    208 
    209     do {
    210         out = (char *) sodium_malloc(strlen(tests[i].out) + 1U);
    211         assert(out != NULL);
    212         memcpy(out, tests[i].out, strlen(tests[i].out) + 1U);
    213         passwd = (char *) sodium_malloc(strlen(tests[i].passwd) + 1U);
    214         assert(passwd != NULL);
    215         memcpy(passwd, tests[i].passwd, strlen(tests[i].passwd) + 1U);
    216         ret = crypto_pwhash_str_verify(out, passwd, strlen(passwd));
    217         sodium_free(out);
    218         sodium_free(passwd);
    219         if (ret != 0) {
    220             printf("[tv3] pwhash_argon2id_str failure (maybe intentional): [%u]\n",
    221                    (unsigned int) i);
    222         }
    223     } while (++i < (sizeof tests) / (sizeof tests[0]));
    224 }
    225 
    226 static void
    227 str_tests(void)
    228 {
    229     char       *str_out;
    230     char       *str_out2;
    231     char       *salt;
    232     const char *passwd = "Correct Horse Battery Staple";
    233 
    234     salt     = (char *) sodium_malloc(crypto_pwhash_argon2id_SALTBYTES);
    235     str_out  = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES);
    236     str_out2 = (char *) sodium_malloc(crypto_pwhash_argon2id_STRBYTES);
    237     memcpy(salt, ">A 16-bytes salt", crypto_pwhash_argon2id_SALTBYTES);
    238     if (crypto_pwhash_str(str_out, passwd, strlen(passwd), OPSLIMIT,
    239                           MEMLIMIT) != 0) {
    240         printf("pwhash_str failure\n");
    241     }
    242     if (crypto_pwhash_str(str_out2, passwd, strlen(passwd), OPSLIMIT,
    243                           MEMLIMIT) != 0) {
    244         printf("pwhash_str(2) failure\n");
    245     }
    246     if (strcmp(str_out, str_out2) == 0) {
    247         printf("pwhash_str() doesn't generate different salts\n");
    248     }
    249     if (crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT) != 0 ||
    250         crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT) != 0) {
    251         printf("needs_rehash() false positive\n");
    252     }
    253     if (crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT / 2) != 1 ||
    254         crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT - 1, MEMLIMIT) != 1 ||
    255         crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT * 2) != 1 ||
    256         crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT + 1, MEMLIMIT) != 1) {
    257         printf("needs_rehash() false negative (0)\n");
    258     }
    259     if (crypto_pwhash_argon2id_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT / 2) != 1 ||
    260         crypto_pwhash_argon2id_str_needs_rehash(str_out, OPSLIMIT - 1, MEMLIMIT) != 1 ||
    261         crypto_pwhash_argon2id_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT * 2) != 1 ||
    262         crypto_pwhash_argon2id_str_needs_rehash(str_out, OPSLIMIT + 1, MEMLIMIT) != 1) {
    263         printf("needs_rehash() false negative (1)\n");
    264     }
    265     if (crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT / 2) != -1 ||
    266         crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT - 1, MEMLIMIT) != -1 ||
    267         crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT * 2) != -1 ||
    268         crypto_pwhash_argon2i_str_needs_rehash(str_out, OPSLIMIT + 1, MEMLIMIT) != -1) {
    269         printf("needs_rehash() false negative (2)\n");
    270     }
    271     if (crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT / 2) != 1) {
    272         printf("pwhash_str_needs_rehash() didn't handle argon2id\n");
    273     }
    274     if (crypto_pwhash_str_needs_rehash(str_out + 1, OPSLIMIT, MEMLIMIT) != -1 ||
    275         crypto_pwhash_argon2id_str_needs_rehash(str_out + 1, OPSLIMIT, MEMLIMIT) != -1) {
    276         printf("needs_rehash() didn't fail with an invalid hash string\n");
    277     }
    278     if (sodium_is_zero((const unsigned char *) str_out + strlen(str_out),
    279                        crypto_pwhash_STRBYTES - strlen(str_out)) != 1 ||
    280         sodium_is_zero((const unsigned char *) str_out2 + strlen(str_out2),
    281                        crypto_pwhash_STRBYTES - strlen(str_out2)) != 1) {
    282         printf("pwhash_argon2id_str() doesn't properly pad with zeros\n");
    283     }
    284     if (crypto_pwhash_argon2id_str_verify(str_out, passwd, strlen(passwd)) != 0) {
    285         printf("pwhash_argon2id_str_verify(1) failure\n");
    286     }
    287     if (crypto_pwhash_str_verify(str_out, passwd, strlen(passwd)) != 0) {
    288         printf("pwhash_str_verify(1') failure\n");
    289     }
    290     str_out[14]++;
    291     if (crypto_pwhash_str_verify(str_out, passwd, strlen(passwd)) != -1) {
    292         printf("pwhash_argon2id_str_verify(2) failure\n");
    293     }
    294     str_out[14]--;
    295     assert(str_out[crypto_pwhash_argon2id_STRBYTES - 1U] == 0);
    296 
    297     if (crypto_pwhash_str(str_out2, passwd, 0x100000000ULL, OPSLIMIT,
    298                           MEMLIMIT) != -1) {
    299         printf("pwhash_str() with a large password should have failed\n");
    300     }
    301     if (crypto_pwhash_str(str_out2, passwd, strlen(passwd), 1, MEMLIMIT) != 0) {
    302         printf("pwhash_str() with a small opslimit should not have failed\n");
    303     }
    304     if (crypto_pwhash_str(str_out2, passwd, strlen(passwd), 0, MEMLIMIT) != -1) {
    305         printf("pwhash_argon2id_str() with a null opslimit should have failed\n");
    306     }
    307     {
    308         const char *str_in_ ="$argon2id$m=65536,t=2,p=1c29tZXNhbHQ"
    309             "$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ";
    310         char       *str_in = (char *) sodium_malloc(strlen(str_in_) + 1U);
    311 
    312         const char *password_in_ = "password";
    313         char       *password_in = (char *) sodium_malloc(strlen(password_in_) + 1U);
    314 
    315         memcpy(str_in, str_in_, strlen(str_in_) + 1U);
    316         memcpy(password_in, password_in_, strlen(password_in_) + 1U);
    317 
    318         if (crypto_pwhash_argon2i_str_verify(str_in, password_in,
    319                                              0x100000000ULL) != -1) {
    320             printf("pwhash_str_verify(invalid(0)) failure\n");
    321         }
    322 
    323         sodium_free(password_in);
    324         sodium_free(str_in);
    325     }
    326     if (crypto_pwhash_str_verify("$argon2id$m=65536,t=2,p=1c29tZXNhbHQ"
    327                                  "$9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ",
    328                                  "password", strlen("password")) != -1) {
    329         printf("pwhash_str_verify(invalid(1)) failure %d\n", errno);
    330     }
    331     if (crypto_pwhash_str_verify("$argon2id$m=65536,t=2,p=1$c29tZXNhbHQ"
    332                                  "9sTbSlTio3Biev89thdrlKKiCaYsjjYVJxGAL3swxpQ",
    333                                  "password", strlen("password")) != -1) {
    334         printf("pwhash_str_verify(invalid(2)) failure\n");
    335     }
    336     if (crypto_pwhash_str_verify("$argon2id$m=65536,t=2,p=1$c29tZXNhbHQ"
    337                                  "$b2G3seW+uPzerwQQC+/E1K50CLLO7YXy0JRcaTuswRo",
    338                                  "password", strlen("password")) != -1) {
    339         printf("pwhash_str_verify(invalid(3)) failure\n");
    340     }
    341     if (crypto_pwhash_str_verify("$argon2id$v=19$m=65536,t=2,p=1c29tZXNhbHQ"
    342                                  "$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA",
    343                                  "password", strlen("password")) != -1) {
    344         printf("pwhash_str_verify(invalid(4)) failure\n");
    345     }
    346     if (crypto_pwhash_str_verify("$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ"
    347                                  "wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA",
    348                                  "password", strlen("password")) != -1) {
    349         printf("pwhash_str_verify(invalid(5)) failure\n");
    350     }
    351     if (crypto_pwhash_str_verify("$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ"
    352                                  "$8iIuixkI73Js3G1uMbezQXD0b8LG4SXGsOwoQkdAQIM",
    353                                  "password", strlen("password")) != -1) {
    354         printf("pwhash_str_verify(invalid(6)) failure\n");
    355     }
    356     if (crypto_pwhash_str_verify("$argon2id$v=19$m=256,t=3,p=1$MDEyMzQ1Njc"
    357                                  "$G5ajKFCoUzaXRLdz7UJb5wGkb2Xt+X5/GQjUYtS2+TE",
    358                                  "password", strlen("password")) != 0) {
    359         printf("pwhash_str_verify(valid(7)) failure\n");
    360     }
    361     if (crypto_pwhash_str_verify("$argon2id$v=19$m=256,t=3,p=1$MDEyMzQ1Njc"
    362                                  "$G5ajKFCoUzaXRLdz7UJb5wGkb2Xt+X5/GQjUYtS2+TE",
    363                                  "passwore", strlen("passwore")) != -1 || errno != EINVAL) {
    364         printf("pwhash_str_verify(invalid(7)) failure\n");
    365     }
    366     if (crypto_pwhash_str_verify("$Argon2id$v=19$m=256,t=3,p=1$MDEyMzQ1Njc"
    367                                  "$G5ajKFCoUzaXRLdz7UJb5wGkb2Xt+X5/GQjUYtS2+TE",
    368                                  "password", strlen("password")) != -1 || errno != EINVAL) {
    369         printf("pwhash_str_verify(invalid(8)) failure\n");
    370     }
    371     if (crypto_pwhash_str_verify("$argon2id$v=19$m=256,t=3,p=2$MDEyMzQ1Njc"
    372                                  "$G5ajKFCoUzaXRLdz7UJb5wGkb2Xt+X5/GQjUYtS2+TE",
    373                                  "password", strlen("password")) != -1 || errno != EINVAL) {
    374         printf("pwhash_str_verify(invalid(9)) failure\n");
    375     }
    376     assert(crypto_pwhash_str_alg(str_out, "test", 4, OPSLIMIT, MEMLIMIT,
    377                                  crypto_pwhash_ALG_ARGON2ID13) == 0);
    378     assert(crypto_pwhash_argon2id_str_verify(str_out, "test", 4) == 0);
    379     assert(crypto_pwhash_argon2id_str_needs_rehash(str_out,
    380                                                    OPSLIMIT, MEMLIMIT) == 0);
    381     assert(crypto_pwhash_argon2id_str_needs_rehash(str_out,
    382                                                    OPSLIMIT / 2, MEMLIMIT) == 1);
    383     assert(crypto_pwhash_argon2id_str_needs_rehash(str_out,
    384                                                    OPSLIMIT, MEMLIMIT / 2) == 1);
    385     assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 0, 0) == 1);
    386     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 0, 0) == -1);
    387     assert(crypto_pwhash_argon2id_str_needs_rehash(str_out + 1,
    388                                                    OPSLIMIT, MEMLIMIT) == -1);
    389     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 0, 0) == -1);
    390     assert(crypto_pwhash_argon2i_str_needs_rehash("", OPSLIMIT, MEMLIMIT) == -1);
    391     assert(crypto_pwhash_str_alg(str_out, "test", 4, OPSLIMIT, MEMLIMIT,
    392                                  crypto_pwhash_ALG_ARGON2I13) == 0);
    393     assert(crypto_pwhash_argon2i_str_verify(str_out, "test", 4) == 0);
    394     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out,
    395                                                   OPSLIMIT, MEMLIMIT) == 0);
    396     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out,
    397                                                   OPSLIMIT / 2, MEMLIMIT) == 1);
    398     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out,
    399                                                   OPSLIMIT, MEMLIMIT / 2) == 1);
    400     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out, 0, 0) == 1);
    401     assert(crypto_pwhash_argon2id_str_needs_rehash(str_out, 0, 0) == -1);
    402     assert(crypto_pwhash_argon2i_str_needs_rehash("", OPSLIMIT, MEMLIMIT) == -1);
    403     assert(crypto_pwhash_argon2i_str_needs_rehash(str_out + 1,
    404                                                   OPSLIMIT, MEMLIMIT) == -1);
    405     sodium_free(salt);
    406     sodium_free(str_out);
    407     sodium_free(str_out2);
    408 }
    409 
    410 int
    411 main(void)
    412 {
    413     tv();
    414     tv2();
    415     tv3();
    416     str_tests();
    417 
    418     assert(crypto_pwhash_bytes_min() > 0U);
    419     assert(crypto_pwhash_bytes_max() > crypto_pwhash_bytes_min());
    420     assert(crypto_pwhash_passwd_max() > crypto_pwhash_passwd_min());
    421     assert(crypto_pwhash_saltbytes() > 0U);
    422     assert(crypto_pwhash_strbytes() > 1U);
    423     assert(crypto_pwhash_strbytes() > strlen(crypto_pwhash_strprefix()));
    424 
    425     assert(crypto_pwhash_opslimit_min() > 0U);
    426     assert(crypto_pwhash_opslimit_max() > 0U);
    427     assert(crypto_pwhash_memlimit_min() > 0U);
    428     assert(crypto_pwhash_memlimit_max() > 0U);
    429     assert(crypto_pwhash_opslimit_interactive() > 0U);
    430     assert(crypto_pwhash_memlimit_interactive() > 0U);
    431     assert(crypto_pwhash_opslimit_moderate() > 0U);
    432     assert(crypto_pwhash_memlimit_moderate() > 0U);
    433     assert(crypto_pwhash_opslimit_sensitive() > 0U);
    434     assert(crypto_pwhash_memlimit_sensitive() > 0U);
    435     assert(strcmp(crypto_pwhash_primitive(), "argon2id,argon2i") == 0);
    436 
    437     assert(crypto_pwhash_bytes_min() == crypto_pwhash_BYTES_MIN);
    438     assert(crypto_pwhash_bytes_max() == crypto_pwhash_BYTES_MAX);
    439     assert(crypto_pwhash_passwd_min() == crypto_pwhash_PASSWD_MIN);
    440     assert(crypto_pwhash_passwd_max() == crypto_pwhash_PASSWD_MAX);
    441     assert(crypto_pwhash_saltbytes() == crypto_pwhash_SALTBYTES);
    442     assert(crypto_pwhash_strbytes() == crypto_pwhash_STRBYTES);
    443 
    444     assert(crypto_pwhash_opslimit_min() == crypto_pwhash_OPSLIMIT_MIN);
    445     assert(crypto_pwhash_opslimit_max() == crypto_pwhash_OPSLIMIT_MAX);
    446     assert(crypto_pwhash_memlimit_min() == crypto_pwhash_MEMLIMIT_MIN);
    447     assert(crypto_pwhash_memlimit_max() == crypto_pwhash_MEMLIMIT_MAX);
    448     assert(crypto_pwhash_opslimit_interactive() ==
    449            crypto_pwhash_OPSLIMIT_INTERACTIVE);
    450     assert(crypto_pwhash_memlimit_interactive() ==
    451            crypto_pwhash_MEMLIMIT_INTERACTIVE);
    452     assert(crypto_pwhash_opslimit_moderate() ==
    453            crypto_pwhash_OPSLIMIT_MODERATE);
    454     assert(crypto_pwhash_memlimit_moderate() ==
    455            crypto_pwhash_MEMLIMIT_MODERATE);
    456     assert(crypto_pwhash_opslimit_sensitive() ==
    457            crypto_pwhash_OPSLIMIT_SENSITIVE);
    458     assert(crypto_pwhash_memlimit_sensitive() ==
    459            crypto_pwhash_MEMLIMIT_SENSITIVE);
    460 
    461     assert(crypto_pwhash_argon2id_bytes_min() == crypto_pwhash_bytes_min());
    462     assert(crypto_pwhash_argon2id_bytes_max() == crypto_pwhash_bytes_max());
    463     assert(crypto_pwhash_argon2id_passwd_min() == crypto_pwhash_passwd_min());
    464     assert(crypto_pwhash_argon2id_passwd_max() == crypto_pwhash_passwd_max());
    465     assert(crypto_pwhash_argon2id_saltbytes() == crypto_pwhash_saltbytes());
    466     assert(crypto_pwhash_argon2id_strbytes() == crypto_pwhash_strbytes());
    467     assert(strcmp(crypto_pwhash_argon2id_strprefix(),
    468                   crypto_pwhash_strprefix()) == 0);
    469     assert(crypto_pwhash_argon2id_opslimit_min() ==
    470            crypto_pwhash_opslimit_min());
    471     assert(crypto_pwhash_argon2id_opslimit_max() ==
    472            crypto_pwhash_opslimit_max());
    473     assert(crypto_pwhash_argon2id_memlimit_min() ==
    474            crypto_pwhash_memlimit_min());
    475     assert(crypto_pwhash_argon2id_memlimit_max() ==
    476            crypto_pwhash_memlimit_max());
    477     assert(crypto_pwhash_argon2id_opslimit_interactive() ==
    478            crypto_pwhash_opslimit_interactive());
    479     assert(crypto_pwhash_argon2id_opslimit_moderate() ==
    480            crypto_pwhash_opslimit_moderate());
    481     assert(crypto_pwhash_argon2id_opslimit_sensitive() ==
    482            crypto_pwhash_opslimit_sensitive());
    483     assert(crypto_pwhash_argon2id_memlimit_interactive() ==
    484            crypto_pwhash_memlimit_interactive());
    485     assert(crypto_pwhash_argon2id_memlimit_moderate() ==
    486            crypto_pwhash_memlimit_moderate());
    487     assert(crypto_pwhash_argon2id_memlimit_sensitive() ==
    488            crypto_pwhash_memlimit_sensitive());
    489     assert(crypto_pwhash_alg_argon2id13() ==
    490            crypto_pwhash_argon2id_alg_argon2id13());
    491     assert(crypto_pwhash_alg_argon2i13() == crypto_pwhash_ALG_ARGON2I13);
    492     assert(crypto_pwhash_alg_argon2i13() != crypto_pwhash_alg_default());
    493     assert(crypto_pwhash_alg_argon2id13() == crypto_pwhash_ALG_ARGON2ID13);
    494     assert(crypto_pwhash_alg_argon2id13() != crypto_pwhash_alg_argon2i13());
    495     assert(crypto_pwhash_alg_argon2id13() == crypto_pwhash_alg_default());
    496 
    497     assert(crypto_pwhash_argon2id(guard_page, 0, (const char *) guard_page, 0, guard_page,
    498                                   crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
    499                                   crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
    500                                   0) == -1);
    501     assert(crypto_pwhash_argon2id(guard_page, 0, (const char *) guard_page, 0, guard_page,
    502                                  crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
    503                                  crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
    504                                  crypto_pwhash_ALG_ARGON2I13) == -1);
    505     assert(crypto_pwhash_argon2i(guard_page, 0, (const char *) guard_page, 0, guard_page,
    506                                  crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
    507                                  crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
    508                                  0) == -1);
    509     assert(crypto_pwhash_argon2i(guard_page, 0, (const char *) guard_page, 0, guard_page,
    510                                  crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
    511                                  crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
    512                                  crypto_pwhash_ALG_ARGON2ID13) == -1);
    513 
    514     printf("OK\n");
    515 
    516     return 0;
    517 }