1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 12:36:01 -07:00

Add crypto_pwhash_scryptxsalsa208sha256_str_verify()

This commit is contained in:
Frank Denis 2014-05-07 19:35:05 -07:00
parent fa05d907bf
commit e7488dec44
2 changed files with 39 additions and 2 deletions

View File

@ -3,10 +3,12 @@
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "crypto_pwhash_scryptxsalsa208sha256.h"
#include "crypto_scrypt.h"
#include "randombytes.h"
#include "utils.h"
#define SETTING_SIZE(saltbytes) \
(sizeof "$7$" - 1U) + \
@ -88,7 +90,7 @@ crypto_pwhash_scryptxsalsa208sha256(unsigned char * const out,
}
int
crypto_pwhash_scryptxsalsa208sha256_str(char * const out,
crypto_pwhash_scryptxsalsa208sha256_str(char out[crypto_pwhash_scryptxsalsa208sha256_STRBYTES],
const char * const passwd,
unsigned long long passwdlen,
size_t memlimit,
@ -121,6 +123,7 @@ crypto_pwhash_scryptxsalsa208sha256_str(char * const out,
if (escrypt_r(&escrypt_local, (const uint8_t *) passwd, (size_t) passwdlen,
(const uint8_t *) setting, (uint8_t *) out,
crypto_pwhash_scryptxsalsa208sha256_STRBYTES) == NULL) {
escrypt_free_local(&escrypt_local);
errno = EINVAL;
return -1;
}
@ -136,3 +139,32 @@ crypto_pwhash_scryptxsalsa208sha256_str(char * const out,
return 0;
}
int
crypto_pwhash_scryptxsalsa208sha256_str_verify(const char str[crypto_pwhash_scryptxsalsa208sha256_STRBYTES],
const char * const passwd,
unsigned long long passwdlen)
{
char wanted[crypto_pwhash_scryptxsalsa208sha256_STRBYTES];
escrypt_local_t escrypt_local;
int ret = -1;
if (memchr(str, 0, crypto_pwhash_scryptxsalsa208sha256_STRBYTES) !=
&str[crypto_pwhash_scryptxsalsa208sha256_STRBYTES - 1U]) {
return -1;
}
if (escrypt_init_local(&escrypt_local) != 0) {
return -1;
}
if (escrypt_r(&escrypt_local, (const uint8_t *) passwd, (size_t) passwdlen,
(const uint8_t *) str, (uint8_t *) wanted,
sizeof wanted) == NULL) {
escrypt_free_local(&escrypt_local);
return -1;
}
escrypt_free_local(&escrypt_local);
ret = sodium_memcmp(wanted, str, sizeof wanted);
sodium_memzero(wanted, sizeof wanted);
return ret;
}

View File

@ -33,12 +33,17 @@ int crypto_pwhash_scryptxsalsa208sha256(unsigned char * const out,
SODIUM_EXPORT
int
crypto_pwhash_scryptxsalsa208sha256_str(char * const out,
crypto_pwhash_scryptxsalsa208sha256_str(char out[crypto_pwhash_scryptxsalsa208sha256_STRBYTES],
const char * const passwd,
unsigned long long passwdlen,
size_t memlimit,
unsigned long long opslimit);
SODIUM_EXPORT
int crypto_pwhash_scryptxsalsa208sha256_str_verify(const char str[crypto_pwhash_scryptxsalsa208sha256_STRBYTES],
const char * const passwd,
unsigned long long passwdlen);
#ifdef __cplusplus
}
#endif