Example #1
0
int
crypto_pwhash(unsigned char * const out, unsigned long long outlen,
              const char * const passwd, unsigned long long passwdlen,
              const unsigned char * const salt,
              unsigned long long opslimit, size_t memlimit, int alg)
{
    switch (alg) {
    case crypto_pwhash_ALG_ARGON2ID13:
    case crypto_pwhash_ALG_ARGON2I13:
        return crypto_pwhash_argon2i(out, outlen, passwd, passwdlen, salt,
                                     opslimit, memlimit, alg);
    default:
        errno = EINVAL;
        return -1;
    }
}
static void
tv2(void)
{
    static struct {
        const char *       passwd_hex;
        size_t             passwd_len;
        const char *       salt_hex;
        size_t             outlen;
        unsigned long long opslimit;
        size_t             memlimit;
        unsigned int       lanes;
    } tests[] = {
        { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
          "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
          "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5"
          "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6",
          127,
          "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2",
          155, 4, 1397645, 1 },
        { "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
          "65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
          "a232d76d52dfbca38ca8dcbd665b17d1665f7cf5fe59772ec909733b24de97d6f5"
          "8d220b20c60d7c07ec1fd93c52c31020300c6c1facd77937a597c7a6",
          127,
          "5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2",
          155, 3, 1397645, 1 },
    };
    char          passwd[256];
    unsigned char salt[crypto_pwhash_SALTBYTES];
    unsigned char out[256];
    char          out_hex[256 * 2 + 1];
    size_t        i = 0U;

    do {
        sodium_hex2bin((unsigned char *) passwd, sizeof passwd,
                       tests[i].passwd_hex, strlen(tests[i].passwd_hex), NULL,
                       NULL, NULL);
        sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex,
                       strlen(tests[i].salt_hex), NULL, NULL, NULL);
        if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, passwd,
                          tests[i].passwd_len, (const unsigned char *) salt,
                          tests[i].opslimit, tests[i].memlimit,
                          crypto_pwhash_alg_argon2i13()) != 0) {
            printf("[tv2] pwhash failure: [%u]\n", (unsigned int) i);
            continue;
        }
        sodium_bin2hex(out_hex, sizeof out_hex, out, tests[i].outlen);
        printf("%s\n", out_hex);
    } while (++i < (sizeof tests) / (sizeof tests[0]));

    if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 3,
                      1ULL << 12, 0) != -1) {
        printf("[tv2] pwhash should have failed (0)\n");
    }
    if (crypto_pwhash_argon2i(out, sizeof out, "password", strlen("password"), salt, 3,
                              1ULL << 12, 0) != -1) {
        printf("[tv2] pwhash should have failed (0')\n");
    }
    if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 3,
                      1, crypto_pwhash_alg_argon2i13()) != -1) {
        printf("[tv2] pwhash should have failed (1)\n");
    }
    if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 3,
                      1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) {
        printf("[tv2] pwhash should have failed (2)\n");
    }
    if (crypto_pwhash(out, sizeof out, "password", strlen("password"), salt, 2,
                      1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) {
        printf("[tv2] pwhash should have failed (3)\n");
    }
    if (crypto_pwhash(out, 15, "password", strlen("password"), salt, 3,
                      1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) {
        printf("[tv2] pwhash with a short output length should have failed\n");
    }
    if (crypto_pwhash(out, sizeof out, "password", 0x100000000ULL, salt, 3,
                      1ULL << 12, crypto_pwhash_alg_argon2i13()) != -1) {
        printf("[tv2] pwhash with a long password length should have failed\n");
    }
}
Example #3
0
int
main(void)
{
    tv();
    tv2();
    tv3();
    str_tests();

    assert(crypto_pwhash_bytes_min() > 0U);
    assert(crypto_pwhash_bytes_max() > crypto_pwhash_bytes_min());
    assert(crypto_pwhash_passwd_max() > crypto_pwhash_passwd_min());
    assert(crypto_pwhash_saltbytes() > 0U);
    assert(crypto_pwhash_strbytes() > 1U);
    assert(crypto_pwhash_strbytes() > strlen(crypto_pwhash_strprefix()));

    assert(crypto_pwhash_opslimit_min() > 0U);
    assert(crypto_pwhash_opslimit_max() > 0U);
    assert(crypto_pwhash_memlimit_min() > 0U);
    assert(crypto_pwhash_memlimit_max() > 0U);
    assert(crypto_pwhash_opslimit_interactive() > 0U);
    assert(crypto_pwhash_memlimit_interactive() > 0U);
    assert(crypto_pwhash_opslimit_moderate() > 0U);
    assert(crypto_pwhash_memlimit_moderate() > 0U);
    assert(crypto_pwhash_opslimit_sensitive() > 0U);
    assert(crypto_pwhash_memlimit_sensitive() > 0U);
    assert(strcmp(crypto_pwhash_primitive(), "argon2i") == 0);

    assert(crypto_pwhash_bytes_min() == crypto_pwhash_BYTES_MIN);
    assert(crypto_pwhash_bytes_max() == crypto_pwhash_BYTES_MAX);
    assert(crypto_pwhash_passwd_min() == crypto_pwhash_PASSWD_MIN);
    assert(crypto_pwhash_passwd_max() == crypto_pwhash_PASSWD_MAX);
    assert(crypto_pwhash_saltbytes() == crypto_pwhash_SALTBYTES);
    assert(crypto_pwhash_strbytes() == crypto_pwhash_STRBYTES);

    assert(crypto_pwhash_opslimit_min() == crypto_pwhash_OPSLIMIT_MIN);
    assert(crypto_pwhash_opslimit_max() == crypto_pwhash_OPSLIMIT_MAX);
    assert(crypto_pwhash_memlimit_min() == crypto_pwhash_MEMLIMIT_MIN);
    assert(crypto_pwhash_memlimit_max() == crypto_pwhash_MEMLIMIT_MAX);
    assert(crypto_pwhash_opslimit_interactive() ==
           crypto_pwhash_OPSLIMIT_INTERACTIVE);
    assert(crypto_pwhash_memlimit_interactive() ==
           crypto_pwhash_MEMLIMIT_INTERACTIVE);
    assert(crypto_pwhash_opslimit_moderate() ==
           crypto_pwhash_OPSLIMIT_MODERATE);
    assert(crypto_pwhash_memlimit_moderate() ==
           crypto_pwhash_MEMLIMIT_MODERATE);
    assert(crypto_pwhash_opslimit_sensitive() ==
           crypto_pwhash_OPSLIMIT_SENSITIVE);
    assert(crypto_pwhash_memlimit_sensitive() ==
           crypto_pwhash_MEMLIMIT_SENSITIVE);

    assert(crypto_pwhash_argon2id_bytes_min() == crypto_pwhash_bytes_min());
    assert(crypto_pwhash_argon2id_bytes_max() == crypto_pwhash_bytes_max());
    assert(crypto_pwhash_argon2id_passwd_min() == crypto_pwhash_passwd_min());
    assert(crypto_pwhash_argon2id_passwd_max() == crypto_pwhash_passwd_max());
    assert(crypto_pwhash_argon2id_saltbytes() == crypto_pwhash_saltbytes());
    assert(crypto_pwhash_argon2id_strbytes() == crypto_pwhash_strbytes());
    assert(strcmp(crypto_pwhash_argon2id_strprefix(),
                  crypto_pwhash_strprefix()) == 0);
    assert(crypto_pwhash_argon2id_opslimit_min() ==
           crypto_pwhash_opslimit_min());
    assert(crypto_pwhash_argon2id_opslimit_max() ==
           crypto_pwhash_opslimit_max());
    assert(crypto_pwhash_argon2id_memlimit_min() ==
           crypto_pwhash_memlimit_min());
    assert(crypto_pwhash_argon2id_memlimit_max() ==
           crypto_pwhash_memlimit_max());
    assert(crypto_pwhash_argon2id_opslimit_interactive() ==
           crypto_pwhash_opslimit_interactive());
    assert(crypto_pwhash_argon2id_opslimit_moderate() ==
           crypto_pwhash_opslimit_moderate());
    assert(crypto_pwhash_argon2id_opslimit_sensitive() ==
           crypto_pwhash_opslimit_sensitive());
    assert(crypto_pwhash_argon2id_memlimit_interactive() ==
           crypto_pwhash_memlimit_interactive());
    assert(crypto_pwhash_argon2id_memlimit_moderate() ==
           crypto_pwhash_memlimit_moderate());
    assert(crypto_pwhash_argon2id_memlimit_sensitive() ==
           crypto_pwhash_memlimit_sensitive());
    assert(crypto_pwhash_alg_argon2id13() ==
           crypto_pwhash_argon2id_alg_argon2id13());
    assert(crypto_pwhash_alg_argon2i13() == crypto_pwhash_ALG_ARGON2I13);
    assert(crypto_pwhash_alg_argon2i13() != crypto_pwhash_alg_default());
    assert(crypto_pwhash_alg_argon2id13() == crypto_pwhash_ALG_ARGON2ID13);
    assert(crypto_pwhash_alg_argon2id13() != crypto_pwhash_alg_argon2i13());
    assert(crypto_pwhash_alg_argon2id13() == crypto_pwhash_alg_default());

    assert(crypto_pwhash_argon2id(guard_page, 0, (const char *) guard_page, 0, guard_page,
                                  crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
                                  crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
                                  0) == -1);
    assert(crypto_pwhash_argon2id(guard_page, 0, (const char *) guard_page, 0, guard_page,
                                 crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
                                 crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
                                 crypto_pwhash_ALG_ARGON2I13) == -1);
    assert(crypto_pwhash_argon2i(guard_page, 0, (const char *) guard_page, 0, guard_page,
                                 crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
                                 crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
                                 0) == -1);
    assert(crypto_pwhash_argon2i(guard_page, 0, (const char *) guard_page, 0, guard_page,
                                 crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE,
                                 crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE,
                                 crypto_pwhash_ALG_ARGON2ID13) == -1);

    printf("OK\n");

    return 0;
}