Require an algorithm identifier in crypto_pwhash()

This commit is contained in:
Frank Denis 2016-04-01 20:29:28 +02:00
parent 5d8c878ffb
commit 384e08b7f4
3 changed files with 41 additions and 14 deletions

View File

@ -1,6 +1,20 @@
#include <errno.h>
#include "crypto_pwhash.h" #include "crypto_pwhash.h"
int
crypto_pwhash_alg_argon2i13(void)
{
return crypto_pwhash_ALG_ARGON2I13;
}
int
crypto_pwhash_alg_default(void)
{
return crypto_pwhash_ALG_ARGON2I13;
}
size_t size_t
crypto_pwhash_saltbytes(void) crypto_pwhash_saltbytes(void)
{ {
@ -59,10 +73,12 @@ int
crypto_pwhash(unsigned char * const out, unsigned long long outlen, crypto_pwhash(unsigned char * const out, unsigned long long outlen,
const char * const passwd, unsigned long long passwdlen, const char * const passwd, unsigned long long passwdlen,
const unsigned char * const salt, const unsigned char * const salt,
unsigned long long opslimit, size_t memlimit, unsigned long long opslimit, size_t memlimit, int alg)
const struct crypto_pwhash_options *options)
{ {
(void) options; if (alg != crypto_pwhash_ALG_ARGON2I13) {
errno = EINVAL;
return -1;
}
return crypto_pwhash_argon2i(out, outlen, passwd, passwdlen, salt, return crypto_pwhash_argon2i(out, outlen, passwd, passwdlen, salt,
opslimit, memlimit); opslimit, memlimit);
} }

View File

@ -13,6 +13,14 @@
extern "C" { extern "C" {
#endif #endif
#define crypto_pwhash_ALG_ARGON2I13 1
SODIUM_EXPORT
int crypto_pwhash_alg_argon2i13(void);
#define crypto_pwhash_ALG_DEFAULT crypto_pwhash_ALG_ARGON2I13
SODIUM_EXPORT
int crypto_pwhash_alg_default(void);
#define crypto_pwhash_SALTBYTES crypto_pwhash_argon2i_SALTBYTES #define crypto_pwhash_SALTBYTES crypto_pwhash_argon2i_SALTBYTES
SODIUM_EXPORT SODIUM_EXPORT
size_t crypto_pwhash_saltbytes(void); size_t crypto_pwhash_saltbytes(void);
@ -49,14 +57,11 @@ size_t crypto_pwhash_opslimit_sensitive(void);
SODIUM_EXPORT SODIUM_EXPORT
size_t crypto_pwhash_memlimit_sensitive(void); size_t crypto_pwhash_memlimit_sensitive(void);
typedef struct crypto_pwhash_options crypto_pwhash_options;
SODIUM_EXPORT SODIUM_EXPORT
int crypto_pwhash(unsigned char * const out, unsigned long long outlen, int crypto_pwhash(unsigned char * const out, unsigned long long outlen,
const char * const passwd, unsigned long long passwdlen, const char * const passwd, unsigned long long passwdlen,
const unsigned char * const salt, const unsigned char * const salt,
unsigned long long opslimit, size_t memlimit, unsigned long long opslimit, size_t memlimit, int alg)
const crypto_pwhash_options *options)
__attribute__ ((warn_unused_result)); __attribute__ ((warn_unused_result));
SODIUM_EXPORT SODIUM_EXPORT

View File

@ -92,7 +92,7 @@ static void tv(void)
if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, if (crypto_pwhash(out, (unsigned long long) tests[i].outlen,
passwd, tests[i].passwdlen, passwd, tests[i].passwdlen,
(const unsigned char *) salt, tests[i].opslimit, (const unsigned char *) salt, tests[i].opslimit,
tests[i].memlimit, NULL) != 0) { tests[i].memlimit, crypto_pwhash_alg_default()) != 0) {
printf("[tv] pwhash failure (maybe intentional): [%u]\n", (unsigned int) i); printf("[tv] pwhash failure (maybe intentional): [%u]\n", (unsigned int) i);
continue; continue;
} }
@ -142,7 +142,7 @@ static void tv2(void)
if (crypto_pwhash(out, (unsigned long long) tests[i].outlen, if (crypto_pwhash(out, (unsigned long long) tests[i].outlen,
passwd, tests[i].passwdlen, passwd, tests[i].passwdlen,
(const unsigned char *) salt, tests[i].opslimit, (const unsigned char *) salt, tests[i].opslimit,
tests[i].memlimit, NULL) != 0) { tests[i].memlimit, crypto_pwhash_alg_default()) != 0) {
printf("[tv2] pwhash failure: [%u]\n", (unsigned int) i); printf("[tv2] pwhash failure: [%u]\n", (unsigned int) i);
continue; continue;
} }
@ -151,23 +151,27 @@ static void tv2(void)
} while (++i < (sizeof tests) / (sizeof tests[0])); } while (++i < (sizeof tests) / (sizeof tests[0]));
if (crypto_pwhash(out, sizeof out, "password", strlen("password"), if (crypto_pwhash(out, sizeof out, "password", strlen("password"),
salt, 3, 1, NULL) != -1) { 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_default()) != -1) {
printf("[tv2] pwhash should have failed (1)\n"); printf("[tv2] pwhash should have failed (1)\n");
} }
if (crypto_pwhash(out, sizeof out, "password", strlen("password"), if (crypto_pwhash(out, sizeof out, "password", strlen("password"),
salt, 3, 1ULL << 12, NULL) != -1) { salt, 3, 1ULL << 12, crypto_pwhash_alg_default()) != -1) {
printf("[tv2] pwhash should have failed (2)\n"); printf("[tv2] pwhash should have failed (2)\n");
} }
if (crypto_pwhash(out, sizeof out, "password", strlen("password"), if (crypto_pwhash(out, sizeof out, "password", strlen("password"),
salt, 2, 1ULL << 12, NULL) != -1) { salt, 2, 1ULL << 12, crypto_pwhash_alg_default()) != -1) {
printf("[tv2] pwhash should have failed (3)\n"); printf("[tv2] pwhash should have failed (3)\n");
} }
if (crypto_pwhash(out, 0x100000000ULL, "password", strlen("password"), if (crypto_pwhash(out, 0x100000000ULL, "password", strlen("password"),
salt, 3, 1ULL << 12, NULL) != -1) { salt, 3, 1ULL << 12, crypto_pwhash_alg_default()) != -1) {
printf("[tv2] pwhash with a long output length should have failed\n"); printf("[tv2] pwhash with a long output length should have failed\n");
} }
if (crypto_pwhash(out, sizeof out, "password", 0x100000000ULL, if (crypto_pwhash(out, sizeof out, "password", 0x100000000ULL,
salt, 3, 1ULL << 12, NULL) != -1) { salt, 3, 1ULL << 12, crypto_pwhash_alg_default()) != -1) {
printf("[tv2] pwhash with a long password length should have failed\n"); printf("[tv2] pwhash with a long password length should have failed\n");
} }
} }
@ -349,6 +353,8 @@ int main(void)
crypto_pwhash_memlimit_moderate()); crypto_pwhash_memlimit_moderate());
assert(crypto_pwhash_argon2i_memlimit_sensitive() == assert(crypto_pwhash_argon2i_memlimit_sensitive() ==
crypto_pwhash_memlimit_sensitive()); crypto_pwhash_memlimit_sensitive());
assert(crypto_pwhash_alg_argon2i13() == crypto_pwhash_ALG_ARGON2I13);
assert(crypto_pwhash_alg_argon2i13() == crypto_pwhash_alg_default());
sodium_free(salt); sodium_free(salt);
sodium_free(str_out); sodium_free(str_out);