mirror of
https://github.com/jedisct1/libsodium.git
synced 2024-12-24 04:25:10 -07:00
Add crypto_pwhash_scryptxsalsa208sha256() + output/salt len macros/functions.
This commit is contained in:
parent
d33d0f08e0
commit
7775a34c97
@ -64,6 +64,7 @@ libsodium_la_SOURCES = \
|
||||
crypto_pwhash/scryptxsalsa208sha256/scrypt_platform.c \
|
||||
crypto_pwhash/scryptxsalsa208sha256/pbkdf2-sha256.c \
|
||||
crypto_pwhash/scryptxsalsa208sha256/pbkdf2-sha256.h \
|
||||
crypto_pwhash/scryptxsalsa208sha256/pwhash_scryptxsalsa208sha256.c \
|
||||
crypto_pwhash/scryptxsalsa208sha256/sysendian.h \
|
||||
crypto_pwhash/scryptxsalsa208sha256/nosse/pwhash_scryptxsalsa208sha256.c \
|
||||
crypto_pwhash/scryptxsalsa208sha256/sse/pwhash_scryptxsalsa208sha256.c \
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "crypto_pwhash_scryptxsalsa208sha256.h"
|
||||
#include "crypto_scrypt.h"
|
||||
#include "runtime.h"
|
||||
|
||||
@ -215,7 +216,7 @@ escrypt_gensalt_r(uint32_t N_log2, uint32_t r, uint32_t p,
|
||||
}
|
||||
|
||||
int
|
||||
crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
||||
crypto_scrypt_compat(const uint8_t * passwd, size_t passwdlen,
|
||||
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
|
||||
uint8_t * buf, size_t buflen)
|
||||
{
|
||||
|
@ -33,7 +33,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
||||
* crypto_scrypt_compat(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
||||
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
|
||||
* p, buflen) and write the result into buf. The parameters r, p, and buflen
|
||||
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
|
||||
@ -41,7 +41,7 @@
|
||||
*
|
||||
* Return 0 on success; or -1 on error.
|
||||
*/
|
||||
extern int crypto_scrypt(const uint8_t * __passwd, size_t __passwdlen,
|
||||
extern int crypto_scrypt_compat(const uint8_t * __passwd, size_t __passwdlen,
|
||||
const uint8_t * __salt, size_t __saltlen,
|
||||
uint64_t __N, uint32_t __r, uint32_t __p,
|
||||
uint8_t * __buf, size_t __buflen);
|
||||
|
@ -0,0 +1,82 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "crypto_pwhash_scryptxsalsa208sha256.h"
|
||||
#include "crypto_scrypt.h"
|
||||
|
||||
static int
|
||||
pickparams(const size_t memlimit, unsigned long long opslimit,
|
||||
uint32_t * const N_log2, uint32_t * const p, uint32_t * const r)
|
||||
{
|
||||
unsigned long long maxN;
|
||||
unsigned long long maxrp;
|
||||
|
||||
if (opslimit < 32768) {
|
||||
opslimit = 32768;
|
||||
}
|
||||
*r = 8;
|
||||
if (opslimit < memlimit / 32) {
|
||||
*p = 1;
|
||||
maxN = opslimit / (*r * 4);
|
||||
for (*N_log2 = 1; *N_log2 < 63; *N_log2 += 1) {
|
||||
if ((uint64_t)(1) << *N_log2 > maxN / 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
maxN = memlimit / (*r * 128);
|
||||
for (*N_log2 = 1; *N_log2 < 63; *N_log2 += 1) {
|
||||
if ((uint64_t) (1) << *N_log2 > maxN / 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
maxrp = (opslimit / 4) / ((uint64_t) (1) << *N_log2);
|
||||
if (maxrp > 0x3fffffff) {
|
||||
maxrp = 0x3fffffff;
|
||||
}
|
||||
*p = (uint32_t) (maxrp) / *r;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t
|
||||
crypto_pwhash_scryptxsalsa208sha256_bytes(void)
|
||||
{
|
||||
return crypto_pwhash_scryptxsalsa208sha256_BYTES;
|
||||
}
|
||||
|
||||
size_t
|
||||
crypto_pwhash_scryptxsalsa208sha256_saltbytes(void)
|
||||
{
|
||||
return crypto_pwhash_scryptxsalsa208sha256_SALTBYTES;
|
||||
}
|
||||
|
||||
int
|
||||
crypto_pwhash_scryptxsalsa208sha256(unsigned char * const out,
|
||||
const char * const passwd,
|
||||
unsigned long long passwdlen,
|
||||
const unsigned char * const salt,
|
||||
size_t memlimit,
|
||||
unsigned long long opslimit)
|
||||
{
|
||||
uint32_t N_log2;
|
||||
uint32_t p;
|
||||
uint32_t r;
|
||||
|
||||
if (passwdlen > SIZE_MAX) {
|
||||
errno = EFBIG;
|
||||
return -1;
|
||||
}
|
||||
if (pickparams(memlimit, opslimit, &N_log2, &p, &r) != 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return crypto_scrypt_compat((const uint8_t *) passwd, (size_t) passwdlen,
|
||||
(const uint8_t *) salt,
|
||||
crypto_pwhash_scryptxsalsa208sha256_SALTBYTES,
|
||||
(uint64_t) (1) << N_log2, r, p,
|
||||
out, crypto_pwhash_scryptxsalsa208sha256_BYTES);
|
||||
}
|
@ -20,6 +20,7 @@ SODIUM_EXPORT = \
|
||||
sodium/crypto_onetimeauth.h \
|
||||
sodium/crypto_onetimeauth_poly1305.h \
|
||||
sodium/crypto_onetimeauth_poly1305_donna.h \
|
||||
sodium/crypto_pwhash_scryptxsalsa208sha256.h \
|
||||
sodium/crypto_scalarmult.h \
|
||||
sodium/crypto_scalarmult_curve25519.h \
|
||||
sodium/crypto_secretbox.h \
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <sodium/crypto_hash_sha512.h>
|
||||
#include <sodium/crypto_onetimeauth.h>
|
||||
#include <sodium/crypto_onetimeauth_poly1305.h>
|
||||
#include <sodium/crypto_pwhash_scryptxsalsa208sha256.h>
|
||||
#include <sodium/crypto_scalarmult.h>
|
||||
#include <sodium/crypto_scalarmult_curve25519.h>
|
||||
#include <sodium/crypto_secretbox.h>
|
||||
|
@ -0,0 +1,37 @@
|
||||
#ifndef crypto_pwhash_scryptxsalsa208sha256_H
|
||||
#define crypto_pwhash_scryptxsalsa208sha256_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "export.h"
|
||||
|
||||
#define crypto_pwhash_scryptxsalsa208sha256_BYTES 64
|
||||
#define crypto_pwhash_scryptxsalsa208sha256_SALTBYTES 32
|
||||
|
||||
#ifdef __cplusplus
|
||||
# if __GNUC__
|
||||
# pragma GCC diagnostic ignored "-Wlong-long"
|
||||
# endif
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
SODIUM_EXPORT
|
||||
size_t crypto_pwhash_scryptxsalsa208sha256_bytes(void);
|
||||
|
||||
SODIUM_EXPORT
|
||||
size_t crypto_pwhash_scryptxsalsa208sha256_saltbytes(void);
|
||||
|
||||
SODIUM_EXPORT
|
||||
int crypto_pwhash_scryptxsalsa208sha256(unsigned char * const out,
|
||||
const char * const passwd,
|
||||
unsigned long long passwdlen,
|
||||
const unsigned char * const salt,
|
||||
size_t memlimit,
|
||||
unsigned long long opslimit);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user