From 7775a34c9772f228e02ea430554c59e2e67d2957 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 6 May 2014 23:50:46 -0700 Subject: [PATCH] Add crypto_pwhash_scryptxsalsa208sha256() + output/salt len macros/functions. --- src/libsodium/Makefile.am | 1 + .../crypto_scrypt-common.c | 3 +- .../scryptxsalsa208sha256/crypto_scrypt.h | 4 +- .../pwhash_scryptxsalsa208sha256.c | 82 +++++++++++++++++++ src/libsodium/include/Makefile.am | 1 + src/libsodium/include/sodium.h | 1 + .../crypto_pwhash_scryptxsalsa208sha256.h | 37 +++++++++ 7 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/libsodium/crypto_pwhash/scryptxsalsa208sha256/pwhash_scryptxsalsa208sha256.c create mode 100644 src/libsodium/include/sodium/crypto_pwhash_scryptxsalsa208sha256.h diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index 6f7f6842..06aedc9c 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -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 \ diff --git a/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt-common.c b/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt-common.c index 9d5de17a..252c2326 100644 --- a/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt-common.c +++ b/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt-common.c @@ -21,6 +21,7 @@ #include #include +#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) { diff --git a/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt.h b/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt.h index 1ffc7830..073a52bc 100644 --- a/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt.h +++ b/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt.h @@ -33,7 +33,7 @@ #include /** - * 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); diff --git a/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/pwhash_scryptxsalsa208sha256.c b/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/pwhash_scryptxsalsa208sha256.c new file mode 100644 index 00000000..84fda614 --- /dev/null +++ b/src/libsodium/crypto_pwhash/scryptxsalsa208sha256/pwhash_scryptxsalsa208sha256.c @@ -0,0 +1,82 @@ + +#include +#include +#include +#include + +#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); +} diff --git a/src/libsodium/include/Makefile.am b/src/libsodium/include/Makefile.am index 0630d02c..4cc68f56 100644 --- a/src/libsodium/include/Makefile.am +++ b/src/libsodium/include/Makefile.am @@ -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 \ diff --git a/src/libsodium/include/sodium.h b/src/libsodium/include/sodium.h index 3032638e..66103e05 100644 --- a/src/libsodium/include/sodium.h +++ b/src/libsodium/include/sodium.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include diff --git a/src/libsodium/include/sodium/crypto_pwhash_scryptxsalsa208sha256.h b/src/libsodium/include/sodium/crypto_pwhash_scryptxsalsa208sha256.h new file mode 100644 index 00000000..ee4c1fe0 --- /dev/null +++ b/src/libsodium/include/sodium/crypto_pwhash_scryptxsalsa208sha256.h @@ -0,0 +1,37 @@ +#ifndef crypto_pwhash_scryptxsalsa208sha256_H +#define crypto_pwhash_scryptxsalsa208sha256_H + +#include +#include + +#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