jedisct1 / libsodium

A modern, portable, easy to use crypto library.
https://libsodium.org
Other
12.22k stars 1.74k forks source link

crypto_pwhash() crashes when `opslimit` is 1 or 2 #1118

Closed CindyZhouYH closed 2 years ago

CindyZhouYH commented 2 years ago

According to the documentation:

opslimit must be between crypto_pwhash_OPSLIMIT_MIN and crypto_pwhash_OPSLIMIT_MAX

I set it to 1, which seems to meet the requirements, but the function returns a none zero value. The same happens when opslimit is 2. Normal results can be obtained when opslimit is greater than or equal to 3.

Are there any special restrictions on this function?

jedisct1 commented 2 years ago
#include <sodium.h>
#include <stdio.h>

int
main(void)
{
    char          hex[32 + 1];
    unsigned char out[16];
    unsigned char salt[crypto_pwhash_SALTBYTES] = { 0 };

    if (sodium_init() != 0) {
        return 1;
    }
    printf("opslimit_min=%llu\n", crypto_pwhash_opslimit_min());
    if (crypto_pwhash(out, sizeof out, "password", sizeof "password" - 1, salt,
                      crypto_pwhash_OPSLIMIT_MIN, crypto_pwhash_MEMLIMIT_MIN,
                      crypto_pwhash_ALG_DEFAULT) != 0) {
        return 1;
    }
    puts(sodium_bin2hex(hex, sizeof hex, out, sizeof out));
    return 0;
}
$ zig cc test.c -lsodium
$ ./a.out

opslimit_min=1
a833aa2e95ef6f83eded7a75761e0dff

crypto_pwhash_OPSLIMIT_* are for the default hash function. If you are using another one, other limits may apply.

The minimum output length is also crypto_pwhash_BYTES_MIN.