1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Add tests for scrypt rehash

This commit is contained in:
Frank Denis 2017-09-13 18:47:55 +02:00
parent 2a2b85eeea
commit 390f865e35

View File

@ -294,17 +294,14 @@ tv3(void)
} while (++i < (sizeof tests) / (sizeof tests[0])); } while (++i < (sizeof tests) / (sizeof tests[0]));
} }
int static void
main(void) str_tests(void)
{ {
char *str_out; char *str_out;
char *str_out2; char *str_out2;
char *salt; char *salt;
const char *passwd = "Correct Horse Battery Staple"; const char *passwd = "Correct Horse Battery Staple";
tv();
tv2();
tv3();
salt = (char *) sodium_malloc(crypto_pwhash_scryptsalsa208sha256_SALTBYTES); salt = (char *) sodium_malloc(crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
str_out = str_out =
(char *) sodium_malloc(crypto_pwhash_scryptsalsa208sha256_STRBYTES); (char *) sodium_malloc(crypto_pwhash_scryptsalsa208sha256_STRBYTES);
@ -323,6 +320,24 @@ main(void)
if (strcmp(str_out, str_out2) == 0) { if (strcmp(str_out, str_out2) == 0) {
printf("pwhash_str doesn't generate different salts\n"); printf("pwhash_str doesn't generate different salts\n");
} }
if (crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out, OPSLIMIT, MEMLIMIT) != 0) {
printf("needs_rehash() false positive\n");
}
if (crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out, OPSLIMIT, MEMLIMIT / 2) != 1 ||
crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out, OPSLIMIT / 2, MEMLIMIT) != 1 ||
crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out, OPSLIMIT, MEMLIMIT * 2) != 1 ||
crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out, OPSLIMIT * 2, MEMLIMIT) != 1) {
printf("needs_rehash() false negative\n");
}
if (crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out + 1, OPSLIMIT, MEMLIMIT) != -1) {
printf("needs_rehash() didn't fail with an invalid hash string\n");
}
if (crypto_pwhash_scryptsalsa208sha256_str_verify(str_out, passwd, if (crypto_pwhash_scryptsalsa208sha256_str_verify(str_out, passwd,
strlen(passwd)) != 0) { strlen(passwd)) != 0) {
printf("pwhash_str_verify failure\n"); printf("pwhash_str_verify failure\n");
@ -339,6 +354,27 @@ main(void)
str_out[14]--; str_out[14]--;
assert(str_out[crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1U] == 0); assert(str_out[crypto_pwhash_scryptsalsa208sha256_STRBYTES - 1U] == 0);
assert(crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
(str_out, 0, 0) == 1);
assert(crypto_pwhash_str_needs_rehash(str_out, 0, 0) == -1);
assert(crypto_pwhash_str_needs_rehash(str_out, OPSLIMIT, MEMLIMIT) == -1);
assert(crypto_pwhash_scryptsalsa208sha256_str_needs_rehash
("", OPSLIMIT, MEMLIMIT) == -1);
sodium_free(salt);
sodium_free(str_out);
sodium_free(str_out2);
}
int
main(void)
{
tv();
tv2();
tv3();
str_tests();
assert(crypto_pwhash_scryptsalsa208sha256_bytes_min() > 0U); assert(crypto_pwhash_scryptsalsa208sha256_bytes_min() > 0U);
assert(crypto_pwhash_scryptsalsa208sha256_bytes_max() > assert(crypto_pwhash_scryptsalsa208sha256_bytes_max() >
crypto_pwhash_scryptsalsa208sha256_bytes_min()); crypto_pwhash_scryptsalsa208sha256_bytes_min());
@ -358,10 +394,6 @@ main(void)
assert(crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive() > 0U); assert(crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive() > 0U);
assert(crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive() > 0U); assert(crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive() > 0U);
sodium_free(salt);
sodium_free(str_out);
sodium_free(str_out2);
printf("OK\n"); printf("OK\n");
return 0; return 0;