2018-04-01 00:09:38 +00:00
|
|
|
// scrypt.cpp - written and placed in public domain by Jeffrey Walton.
|
2018-04-02 02:30:03 +00:00
|
|
|
// Based on reference source code by Colin Percival for
|
|
|
|
// Scrypt and Daniel Bernstein for Salsa20 core.
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "scrypt.h"
|
2018-04-01 01:26:38 +00:00
|
|
|
#include "algparam.h"
|
2018-04-01 00:09:38 +00:00
|
|
|
#include "argnames.h"
|
|
|
|
#include "pwdbased.h"
|
|
|
|
#include "stdcpp.h"
|
|
|
|
#include "salsa.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "sha.h"
|
|
|
|
|
2018-04-01 01:26:38 +00:00
|
|
|
#include <sstream>
|
2019-01-21 03:45:53 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2018-04-01 00:09:38 +00:00
|
|
|
#ifdef _OPENMP
|
|
|
|
# include <omp.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-16 19:43:58 +00:00
|
|
|
// https://github.com/weidai11/cryptopp/issues/777
|
2019-01-10 18:11:43 +00:00
|
|
|
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
|
2019-01-16 19:43:58 +00:00
|
|
|
# if defined(__clang__)
|
|
|
|
# pragma GCC diagnostic ignored "-Wtautological-compare"
|
2019-01-22 07:37:38 +00:00
|
|
|
# elif defined(__GNUC__)
|
|
|
|
# pragma GCC diagnostic ignored "-Wtype-limits"
|
2019-01-16 19:43:58 +00:00
|
|
|
# endif
|
2019-01-10 18:11:43 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-01 00:09:38 +00:00
|
|
|
ANONYMOUS_NAMESPACE_BEGIN
|
|
|
|
|
2018-04-01 00:25:58 +00:00
|
|
|
using CryptoPP::byte;
|
|
|
|
using CryptoPP::word32;
|
|
|
|
using CryptoPP::word64;
|
2018-04-07 14:39:01 +00:00
|
|
|
using CryptoPP::GetWord;
|
|
|
|
using CryptoPP::PutWord;
|
2018-04-02 07:51:51 +00:00
|
|
|
using CryptoPP::Salsa20_Core;
|
2018-04-01 00:09:38 +00:00
|
|
|
using CryptoPP::rotlConstant;
|
|
|
|
using CryptoPP::LITTLE_ENDIAN_ORDER;
|
2018-04-07 14:39:01 +00:00
|
|
|
using CryptoPP::AlignedSecByteBlock;
|
2018-04-01 00:09:38 +00:00
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void LE32ENC(byte* out, word32 in)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-07 14:39:01 +00:00
|
|
|
PutWord(false, LITTLE_ENDIAN_ORDER, out, in);
|
2018-04-01 00:09:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline word32 LE32DEC(const byte* in)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-07 14:39:01 +00:00
|
|
|
return GetWord<word32>(false, LITTLE_ENDIAN_ORDER, in);
|
2018-04-01 00:09:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline word64 LE64DEC(const byte* in)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-07 14:39:01 +00:00
|
|
|
return GetWord<word64>(false, LITTLE_ENDIAN_ORDER, in);
|
2018-04-01 00:09:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void BlockCopy(byte* dest, byte* src, size_t len)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2019-01-23 11:15:51 +00:00
|
|
|
// OpenMP 4.0 released July 2013.
|
|
|
|
#if _OPENMP >= 201307
|
|
|
|
#pragma omp simd
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < len; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
dest[i] = src[i];
|
2019-01-23 11:15:51 +00:00
|
|
|
#else
|
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
dest[i] = src[i];
|
|
|
|
#endif
|
2018-04-01 00:09:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void BlockXOR(byte* dest, byte* src, size_t len)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2019-01-22 09:58:54 +00:00
|
|
|
// OpenMP 4.0 released July 2013.
|
|
|
|
#if _OPENMP >= 201307
|
2019-01-22 08:42:53 +00:00
|
|
|
#pragma omp simd
|
2019-01-21 03:10:36 +00:00
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
dest[i] ^= src[i];
|
|
|
|
#else
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < len; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
dest[i] ^= src[i];
|
2019-01-21 03:10:36 +00:00
|
|
|
#endif
|
2018-04-01 00:09:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void PBKDF2_SHA256(byte* buf, size_t dkLen,
|
2018-04-01 10:58:00 +00:00
|
|
|
const byte* passwd, size_t passwdlen,
|
|
|
|
const byte* salt, size_t saltlen, byte count)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
|
|
|
using CryptoPP::SHA256;
|
|
|
|
using CryptoPP::PKCS5_PBKDF2_HMAC;
|
|
|
|
|
|
|
|
PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
|
|
|
|
pbkdf.DeriveKey(buf, dkLen, 0, passwd, passwdlen, salt, saltlen, count, 0.0f);
|
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void Salsa20_8(byte B[64])
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-02 07:51:51 +00:00
|
|
|
word32 B32[16];
|
2018-04-01 00:09:38 +00:00
|
|
|
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < 16; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
B32[i] = LE32DEC(&B[i * 4]);
|
|
|
|
|
2018-04-02 07:51:51 +00:00
|
|
|
Salsa20_Core(B32, 8);
|
2018-04-01 00:09:38 +00:00
|
|
|
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < 16; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
LE32ENC(&B[4 * i], B32[i]);
|
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void BlockMix(byte* B, byte* Y, size_t r)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-01 00:19:07 +00:00
|
|
|
byte X[64];
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
// 1: X <-- B_{2r - 1}
|
|
|
|
BlockCopy(X, &B[(2 * r - 1) * 64], 64);
|
|
|
|
|
|
|
|
// 2: for i = 0 to 2r - 1 do
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < 2 * r; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
|
|
|
// 3: X <-- H(X \xor B_i)
|
|
|
|
BlockXOR(X, &B[i * 64], 64);
|
|
|
|
Salsa20_8(X);
|
|
|
|
|
|
|
|
// 4: Y_i <-- X
|
|
|
|
BlockCopy(&Y[i * 64], X, 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1})
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < r; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
BlockCopy(&B[i * 64], &Y[(i * 2) * 64], 64);
|
2018-04-01 10:58:00 +00:00
|
|
|
|
2018-04-02 02:30:03 +00:00
|
|
|
for (size_t i = 0; i < r; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
BlockCopy(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
|
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline word64 Integerify(byte* B, size_t r)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-01 10:58:00 +00:00
|
|
|
byte* X = &B[(2 * r - 1) * 64];
|
2018-04-01 00:09:38 +00:00
|
|
|
return LE64DEC(X);
|
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:51 +00:00
|
|
|
inline void Smix(byte* B, size_t r, word64 N, byte* V, byte* XY)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-01 10:58:00 +00:00
|
|
|
byte* X = XY;
|
|
|
|
byte* Y = XY+128*r;
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
// 1: X <-- B
|
|
|
|
BlockCopy(X, B, 128 * r);
|
|
|
|
|
|
|
|
// 2: for i = 0 to N - 1 do
|
2018-04-02 02:30:03 +00:00
|
|
|
for (word64 i = 0; i < N; ++i)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
|
|
|
// 3: V_i <-- X
|
|
|
|
BlockCopy(&V[i * (128 * r)], X, 128 * r);
|
|
|
|
|
|
|
|
// 4: X <-- H(X)
|
|
|
|
BlockMix(X, Y, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 6: for i = 0 to N - 1 do
|
2018-04-02 02:30:03 +00:00
|
|
|
for (word64 i = 0; i < N; ++i)
|
2018-04-01 10:58:00 +00:00
|
|
|
{
|
2018-04-01 00:09:38 +00:00
|
|
|
// 7: j <-- Integerify(X) mod N
|
2018-04-01 10:58:00 +00:00
|
|
|
word64 j = Integerify(X, r) & (N - 1);
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
// 8: X <-- H(X \xor V_j)
|
|
|
|
BlockXOR(X, &V[j * (128 * r)], 128 * r);
|
|
|
|
BlockMix(X, Y, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10: B' <-- X
|
|
|
|
BlockCopy(B, X, 128 * r);
|
|
|
|
}
|
2018-04-01 01:26:38 +00:00
|
|
|
|
2018-04-01 00:09:38 +00:00
|
|
|
ANONYMOUS_NAMESPACE_END
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
size_t Scrypt::GetValidDerivedLength(size_t keylength) const
|
|
|
|
{
|
2019-08-16 11:12:14 +00:00
|
|
|
if (keylength > MaxDerivedKeyLength())
|
|
|
|
return MaxDerivedKeyLength();
|
2018-04-01 00:09:38 +00:00
|
|
|
return keylength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scrypt::ValidateParameters(size_t derivedLen, word64 cost, word64 blockSize, word64 parallelization) const
|
|
|
|
{
|
2019-05-19 22:52:37 +00:00
|
|
|
// https://github.com/weidai11/cryptopp/issues/842
|
|
|
|
CRYPTOPP_ASSERT(derivedLen != 0);
|
|
|
|
CRYPTOPP_ASSERT(cost != 0);
|
|
|
|
CRYPTOPP_ASSERT(blockSize != 0);
|
|
|
|
CRYPTOPP_ASSERT(parallelization != 0);
|
|
|
|
|
2019-05-20 01:24:32 +00:00
|
|
|
if (cost == 0)
|
|
|
|
throw InvalidArgument("Scrypt: cost cannot be 0");
|
|
|
|
|
|
|
|
if (blockSize == 0)
|
|
|
|
throw InvalidArgument("Scrypt: block size cannot be 0");
|
|
|
|
|
|
|
|
if (parallelization == 0)
|
|
|
|
throw InvalidArgument("Scrypt: parallelization cannot be 0");
|
|
|
|
|
2018-04-02 02:30:03 +00:00
|
|
|
// Optimizer should remove this on 32-bit platforms
|
2023-06-23 19:07:13 +00:00
|
|
|
if ((std::numeric_limits<size_t>::max)() > (std::numeric_limits<word32>::max)())
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-01 00:19:07 +00:00
|
|
|
const word64 maxLen = ((static_cast<word64>(1) << 32) - 1) * 32;
|
2018-04-01 00:09:38 +00:00
|
|
|
if (derivedLen > maxLen) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "derivedLen " << derivedLen << " is larger than " << maxLen;
|
|
|
|
throw InvalidArgument("Scrypt: " + oss.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 03:43:00 +00:00
|
|
|
// https://github.com/weidai11/cryptopp/issues/787
|
2023-06-23 19:07:13 +00:00
|
|
|
CRYPTOPP_ASSERT(parallelization <= static_cast<word64>((std::numeric_limits<int>::max)()));
|
|
|
|
if (parallelization > static_cast<word64>((std::numeric_limits<int>::max)()))
|
2019-01-21 03:43:00 +00:00
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << " parallelization " << parallelization << " is larger than ";
|
2023-06-23 19:07:13 +00:00
|
|
|
oss << (std::numeric_limits<int>::max)();
|
2019-01-21 03:43:00 +00:00
|
|
|
throw InvalidArgument("Scrypt: " + oss.str());
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:49:00 +00:00
|
|
|
CRYPTOPP_ASSERT(IsPowerOf2(cost));
|
2018-04-01 00:09:38 +00:00
|
|
|
if (IsPowerOf2(cost) == false)
|
|
|
|
throw InvalidArgument("Scrypt: cost must be a power of 2");
|
|
|
|
|
2018-04-01 00:19:07 +00:00
|
|
|
const word64 prod = static_cast<word64>(blockSize) * parallelization;
|
2018-04-01 19:49:00 +00:00
|
|
|
CRYPTOPP_ASSERT(prod < (1U << 30));
|
|
|
|
|
2018-04-01 00:09:38 +00:00
|
|
|
if (prod >= (1U << 30)) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "r*p " << prod << " is larger than " << (1U << 30);
|
|
|
|
throw InvalidArgument("Scrypt: " + oss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scrypt has several tests that effectively verify allocations like
|
|
|
|
// '128 * r * N' and '128 * r * p' do not overflow. They are the tests
|
|
|
|
// that set errno to ENOMEM. We can make the logic a little more clear
|
|
|
|
// using word128. At first blush the word128 may seem like overkill.
|
2022-01-04 10:06:35 +00:00
|
|
|
// However, this algorithm is dominated by slow moving parts, so a
|
2018-04-01 00:09:38 +00:00
|
|
|
// one-time check is insignificant in the bigger picture.
|
|
|
|
#if defined(CRYPTOPP_WORD128_AVAILABLE)
|
|
|
|
const word128 maxElems = static_cast<word128>(SIZE_MAX);
|
|
|
|
bool bLimit = (maxElems >= static_cast<word128>(cost) * blockSize * 128U);
|
|
|
|
bool xyLimit = (maxElems >= static_cast<word128>(parallelization) * blockSize * 128U);
|
|
|
|
bool vLimit = (maxElems >= static_cast<word128>(blockSize) * 256U + 64U);
|
|
|
|
#else
|
|
|
|
const word64 maxElems = static_cast<word64>(SIZE_MAX);
|
|
|
|
bool bLimit = (blockSize < maxElems / 128U / cost);
|
|
|
|
bool xyLimit = (blockSize < maxElems / 128U / parallelization);
|
|
|
|
bool vLimit = (blockSize < (maxElems - 64U) / 256U);
|
2018-04-01 01:26:38 +00:00
|
|
|
#endif
|
2018-04-01 00:09:38 +00:00
|
|
|
|
2018-04-01 01:26:38 +00:00
|
|
|
CRYPTOPP_ASSERT(bLimit); CRYPTOPP_ASSERT(xyLimit); CRYPTOPP_ASSERT(vLimit);
|
2018-04-01 00:09:38 +00:00
|
|
|
if (!bLimit || !xyLimit || !vLimit)
|
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
2018-04-01 10:58:00 +00:00
|
|
|
size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen,
|
|
|
|
const byte*secret, size_t secretLen, const NameValuePairs& params) const
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
|
|
|
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
|
|
|
|
CRYPTOPP_ASSERT(derived && derivedLen);
|
2019-08-16 11:12:14 +00:00
|
|
|
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
word64 cost=0, blockSize=0, parallelization=0;
|
|
|
|
if(params.GetValue("Cost", cost) == false)
|
|
|
|
cost = defaultCost;
|
|
|
|
|
|
|
|
if(params.GetValue("BlockSize", blockSize) == false)
|
|
|
|
blockSize = defaultBlockSize;
|
|
|
|
|
|
|
|
if(params.GetValue("Parallelization", parallelization) == false)
|
|
|
|
parallelization = defaultParallelization;
|
|
|
|
|
|
|
|
ConstByteArrayParameter salt;
|
|
|
|
(void)params.GetValue("Salt", salt);
|
|
|
|
|
|
|
|
return DeriveKey(derived, derivedLen, secret, secretLen, salt.begin(), salt.size(), cost, blockSize, parallelization);
|
|
|
|
}
|
|
|
|
|
2018-04-01 10:58:00 +00:00
|
|
|
size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen, const byte*secret, size_t secretLen,
|
|
|
|
const byte*salt, size_t saltLen, word64 cost, word64 blockSize, word64 parallel) const
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
|
|
|
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
|
|
|
|
CRYPTOPP_ASSERT(derived && derivedLen);
|
2019-08-16 11:12:14 +00:00
|
|
|
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
|
2018-04-01 00:09:38 +00:00
|
|
|
|
2019-08-16 11:12:14 +00:00
|
|
|
ThrowIfInvalidDerivedKeyLength(derivedLen);
|
2018-04-01 00:09:38 +00:00
|
|
|
ValidateParameters(derivedLen, cost, blockSize, parallel);
|
|
|
|
|
2019-05-19 22:52:37 +00:00
|
|
|
AlignedSecByteBlock B(static_cast<size_t>(blockSize * parallel * 128U));
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
// 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen)
|
|
|
|
PBKDF2_SHA256(B, B.size(), secret, secretLen, salt, saltLen, 1);
|
|
|
|
|
2019-01-21 03:43:00 +00:00
|
|
|
// Visual Studio and OpenMP 2.0 fixup. We must use int, not size_t.
|
|
|
|
int maxParallel=0;
|
|
|
|
if (!SafeConvert(parallel, maxParallel))
|
2023-06-23 19:07:13 +00:00
|
|
|
maxParallel = (std::numeric_limits<int>::max)();
|
2019-01-21 03:10:36 +00:00
|
|
|
|
2018-05-20 12:33:21 +00:00
|
|
|
#ifdef _OPENMP
|
2019-01-21 03:10:36 +00:00
|
|
|
int threads = STDMIN(omp_get_max_threads(), maxParallel);
|
2018-05-20 12:33:21 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-02 17:40:33 +00:00
|
|
|
// http://stackoverflow.com/q/49604260/608639
|
2018-05-20 12:33:21 +00:00
|
|
|
#pragma omp parallel num_threads(threads)
|
2018-04-01 00:09:38 +00:00
|
|
|
{
|
2018-04-02 18:41:37 +00:00
|
|
|
// Each thread gets its own copy
|
2018-04-01 10:58:00 +00:00
|
|
|
AlignedSecByteBlock XY(static_cast<size_t>(blockSize * 256U));
|
|
|
|
AlignedSecByteBlock V(static_cast<size_t>(blockSize * cost * 128U));
|
|
|
|
|
|
|
|
// 2: for i = 0 to p - 1 do
|
2018-04-02 17:40:33 +00:00
|
|
|
#pragma omp for
|
2019-01-21 03:10:36 +00:00
|
|
|
for (int i = 0; i < maxParallel; ++i)
|
2018-04-01 10:58:00 +00:00
|
|
|
{
|
|
|
|
// 3: B_i <-- MF(B_i, N)
|
|
|
|
const ptrdiff_t offset = static_cast<ptrdiff_t>(blockSize*i*128);
|
|
|
|
Smix(B+offset, static_cast<size_t>(blockSize), cost, V, XY);
|
|
|
|
}
|
2018-04-01 00:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 5: DK <-- PBKDF2(P, B, 1, dkLen)
|
2018-04-01 10:58:00 +00:00
|
|
|
PBKDF2_SHA256(derived, derivedLen, secret, secretLen, B, B.size(), 1);
|
2018-04-01 00:09:38 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|