当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 4

MySQL Tools/Crypto++库
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
TestData
TestPrograms
TestScripts
TestVectors
.appveyor.yml
.cirrus.yml
.gitattributes
.gitignore
.travis.yml
3way.cpp
3way.h
Doxyfile
Filelist.txt
GNUmakefile
GNUmakefile-cross
History.txt
Install.txt
License.txt
Readme.txt
Security.md
adhoc.cpp.proto
adler32.cpp
adler32.h
adv_simd.h
aes.h
aes_armv4.S
aes_armv4.h
algebra.cpp
algebra.h
algparam.cpp
algparam.h
allocate.cpp
allocate.h
arc4.cpp
arc4.h
argnames.h
aria.cpp
aria.h
aria_simd.cpp
ariatab.cpp
arm_simd.h
asn.cpp
asn.h
authenc.cpp
authenc.h
base32.cpp
base32.h
base64.cpp
base64.h
basecode.cpp
basecode.h
bds10.zip
bench.h
bench1.cpp
bench2.cpp
bench3.cpp
bfinit.cpp
blake2.cpp
blake2.h
blake2b_simd.cpp
blake2s_simd.cpp
blowfish.cpp
blowfish.h
blumshub.cpp
blumshub.h
camellia.cpp
camellia.h
cast.cpp
cast.h
casts.cpp
cbcmac.cpp
cbcmac.h
ccm.cpp
ccm.h
chacha.cpp
chacha.h
chacha_avx.cpp
chacha_simd.cpp
chachapoly.cpp
chachapoly.h
cham.cpp
cham.h
cham_simd.cpp
channels.cpp
channels.h
cmac.cpp
cmac.h
config.h
config_align.h
config_asm.h
config_cpu.h
config_cxx.h
config_dll.h
config_int.h
config_misc.h
config_ns.h
config_os.h
config_ver.h
cpu.cpp
cpu.h
crc.cpp
crc.h
crc_simd.cpp
cryptdll.vcxproj
cryptdll.vcxproj.filters
cryptest.nmake
cryptest.sln
cryptest.vcxproj
cryptest.vcxproj.filters
cryptest.vcxproj.user
cryptlib.cpp
cryptlib.h
cryptlib.vcxproj
cryptlib.vcxproj.filters
cryptopp.mapfile
cryptopp.rc
cryptopp.supp
darn.cpp
darn.h
datatest.cpp
default.cpp
default.h
des.cpp
des.h
dessp.cpp
dh.cpp
dh.h
dh2.cpp
dh2.h
dll.cpp
dll.h
dlltest.cpp
dlltest.vcxproj
dlltest.vcxproj.filters
dmac.h
donna.h
donna_32.cpp
donna_32.h
donna_64.cpp
donna_64.h
donna_sse.cpp
donna_sse.h
drbg.h
dsa.cpp
dsa.h
eax.cpp
eax.h
ec2n.cpp
ec2n.h
eccrypto.cpp
eccrypto.h
ecp.cpp
ecp.h
ecpoint.h
elgamal.cpp
elgamal.h
emsa2.cpp
emsa2.h
eprecomp.cpp
eprecomp.h
esign.cpp
esign.h
factory.h
fhmqv.h
files.cpp
files.h
filters.cpp
filters.h
fips140.cpp
fips140.h
fipsalgt.cpp
fipstest.cpp
fltrimpl.h
gcm.cpp
gcm.h
gcm_simd.cpp
gf256.cpp
gf256.h
gf2_32.cpp
gf2_32.h
gf2n.cpp
gf2n.h
gf2n_simd.cpp
gfpcrypt.cpp
gfpcrypt.h
gost.cpp
gost.h
gzip.cpp
gzip.h
hashfwd.h
hc128.cpp
hc128.h
hc256.cpp
hc256.h
hex.cpp
hex.h
hight.cpp
hight.h
hkdf.h
hmac.cpp
hmac.h
hmqv.h
hrtimer.cpp
hrtimer.h
ida.cpp
ida.h
idea.cpp
idea.h
integer.cpp
integer.h
iterhash.cpp
iterhash.h
kalyna.cpp
kalyna.h
kalynatab.cpp
keccak.cpp
keccak.h
keccak_core.cpp
keccak_simd.cpp
lea.cpp
lea.h
lea_simd.cpp
lubyrack.h
luc.cpp
luc.h
mars.cpp
mars.h
marss.cpp
md2.cpp
md2.h
md4.cpp
md4.h
md5.cpp
md5.h
mdc.h
mersenne.h
misc.cpp
misc.h
modarith.h
modes.cpp
modes.h
modexppc.h
mqueue.cpp
mqueue.h
mqv.cpp
mqv.h
naclite.h
nbtheory.cpp
nbtheory.h
neon_simd.cpp
nr.h
oaep.cpp
oaep.h
oids.h
osrng.cpp
osrng.h
ossig.h
padlkrng.cpp
padlkrng.h
panama.cpp
panama.h
pch.cpp
pch.h
pkcspad.cpp
pkcspad.h
poly1305.cpp
poly1305.h
polynomi.cpp
polynomi.h
ppc_power7.cpp
ppc_power8.cpp
ppc_power9.cpp
ppc_simd.cpp
ppc_simd.h
pssr.cpp
pssr.h
pubkey.cpp
pubkey.h
pwdbased.h
queue.cpp
queue.h
rabbit.cpp
rabbit.h
rabin.cpp
rabin.h
randpool.cpp
randpool.h
rc2.cpp
rc2.h
rc5.cpp
rc5.h
rc6.cpp
rc6.h
rdrand.asm
rdrand.cpp
rdrand.h
rdseed.asm
rdtables.cpp
regtest1.cpp
regtest2.cpp
regtest3.cpp
regtest4.cpp
resource.h
rijndael.cpp
rijndael.h
rijndael_simd.cpp
ripemd.cpp
ripemd.h
rng.cpp
rng.h
rsa.cpp
rsa.h
rw.cpp
rw.h
safer.cpp
safer.h
salsa.cpp
salsa.h
scrypt.cpp
scrypt.h
seal.cpp
seal.h
secblock.h
secblockfwd.h
seckey.h
seed.cpp
seed.h
serpent.cpp
serpent.h
serpentp.h
sha.cpp
sha.h
sha1_armv4.S
sha1_armv4.h
sha256_armv4.S
sha256_armv4.h
sha3.cpp
sha3.h
sha512_armv4.S
sha512_armv4.h
sha_simd.cpp
shacal2.cpp
shacal2.h
shacal2_simd.cpp
shake.cpp
shake.h
shark.cpp
shark.h
sharkbox.cpp
simeck.cpp
simeck.h
simon.cpp
simon.h
simon128_simd.cpp
simple.cpp
simple.h
siphash.h
skipjack.cpp
skipjack.h
sm3.cpp
sm3.h
sm4.cpp
sm4.h
sm4_simd.cpp
smartptr.h
sosemanuk.cpp
sosemanuk.h
speck.cpp
speck.h
speck128_simd.cpp
square.cpp
square.h
squaretb.cpp
sse_simd.cpp
stdcpp.h
strciphr.cpp
strciphr.h
tea.cpp
tea.h
test.cpp
tftables.cpp
threefish.cpp
threefish.h
tiger.cpp
tiger.h
tigertab.cpp
trap.h
trunhash.h
ttmac.cpp
ttmac.h
tweetnacl.cpp
tweetnacl.h
twofish.cpp
twofish.h
validat0.cpp
validat1.cpp
validat10.cpp
validat2.cpp
validat3.cpp
validat4.cpp
validat5.cpp
validat6.cpp
validat7.cpp
validat8.cpp
validat9.cpp
validate.h
vc60.zip
vmac.cpp
vmac.h
vs2005.zip
wake.cpp
wake.h
whrlpool.cpp
whrlpool.h
words.h
x64dll.asm
x64masm.asm
xed25519.cpp
xed25519.h
xtr.cpp
xtr.h
xtrcrypt.cpp
xtrcrypt.h
xts.cpp
xts.h
zdeflate.cpp
zdeflate.h
zinflate.cpp
zinflate.h
zlib.cpp
zlib.h
克隆/下载
scrypt.cpp 9.85 KB
一键复制 编辑 原始数据 按行查看 历史
// scrypt.cpp - written and placed in public domain by Jeffrey Walton.
// Based on reference source code by Colin Percival for
// Scrypt and Daniel Bernstein for Salsa20 core.
#include "pch.h"
#include "scrypt.h"
#include "algparam.h"
#include "argnames.h"
#include "pwdbased.h"
#include "stdcpp.h"
#include "salsa.h"
#include "misc.h"
#include "sha.h"
#include <sstream>
#include <limits>
#ifdef _OPENMP
# include <omp.h>
#endif
// https://github.com/weidai11/cryptopp/issues/777
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
# if defined(__clang__)
# pragma GCC diagnostic ignored "-Wtautological-compare"
# elif defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wtype-limits"
# endif
#endif
ANONYMOUS_NAMESPACE_BEGIN
using CryptoPP::byte;
using CryptoPP::word32;
using CryptoPP::word64;
using CryptoPP::GetWord;
using CryptoPP::PutWord;
using CryptoPP::Salsa20_Core;
using CryptoPP::rotlConstant;
using CryptoPP::LITTLE_ENDIAN_ORDER;
using CryptoPP::AlignedSecByteBlock;
inline void LE32ENC(byte* out, word32 in)
{
PutWord(false, LITTLE_ENDIAN_ORDER, out, in);
}
inline word32 LE32DEC(const byte* in)
{
return GetWord<word32>(false, LITTLE_ENDIAN_ORDER, in);
}
inline word64 LE64DEC(const byte* in)
{
return GetWord<word64>(false, LITTLE_ENDIAN_ORDER, in);
}
inline void BlockCopy(byte* dest, byte* src, size_t len)
{
// OpenMP 4.0 released July 2013.
#if _OPENMP >= 201307
#pragma omp simd
for (size_t i = 0; i < len; ++i)
dest[i] = src[i];
#else
for (size_t i = 0; i < len; ++i)
dest[i] = src[i];
#endif
}
inline void BlockXOR(byte* dest, byte* src, size_t len)
{
// OpenMP 4.0 released July 2013.
#if _OPENMP >= 201307
#pragma omp simd
for (size_t i = 0; i < len; ++i)
dest[i] ^= src[i];
#else
for (size_t i = 0; i < len; ++i)
dest[i] ^= src[i];
#endif
}
inline void PBKDF2_SHA256(byte* buf, size_t dkLen,
const byte* passwd, size_t passwdlen,
const byte* salt, size_t saltlen, byte count)
{
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);
}
inline void Salsa20_8(byte B[64])
{
word32 B32[16];
for (size_t i = 0; i < 16; ++i)
B32[i] = LE32DEC(&B[i * 4]);
Salsa20_Core(B32, 8);
for (size_t i = 0; i < 16; ++i)
LE32ENC(&B[4 * i], B32[i]);
}
inline void BlockMix(byte* B, byte* Y, size_t r)
{
byte X[64];
// 1: X <-- B_{2r - 1}
BlockCopy(X, &B[(2 * r - 1) * 64], 64);
// 2: for i = 0 to 2r - 1 do
for (size_t i = 0; i < 2 * r; ++i)
{
// 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})
for (size_t i = 0; i < r; ++i)
BlockCopy(&B[i * 64], &Y[(i * 2) * 64], 64);
for (size_t i = 0; i < r; ++i)
BlockCopy(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
}
inline word64 Integerify(byte* B, size_t r)
{
byte* X = &B[(2 * r - 1) * 64];
return LE64DEC(X);
}
inline void Smix(byte* B, size_t r, word64 N, byte* V, byte* XY)
{
byte* X = XY;
byte* Y = XY+128*r;
// 1: X <-- B
BlockCopy(X, B, 128 * r);
// 2: for i = 0 to N - 1 do
for (word64 i = 0; i < N; ++i)
{
// 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
for (word64 i = 0; i < N; ++i)
{
// 7: j <-- Integerify(X) mod N
word64 j = Integerify(X, r) & (N - 1);
// 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);
}
ANONYMOUS_NAMESPACE_END
NAMESPACE_BEGIN(CryptoPP)
size_t Scrypt::GetValidDerivedLength(size_t keylength) const
{
if (keylength > MaxDerivedKeyLength())
return MaxDerivedKeyLength();
return keylength;
}
void Scrypt::ValidateParameters(size_t derivedLen, word64 cost, word64 blockSize, word64 parallelization) const
{
// https://github.com/weidai11/cryptopp/issues/842
CRYPTOPP_ASSERT(derivedLen != 0);
CRYPTOPP_ASSERT(cost != 0);
CRYPTOPP_ASSERT(blockSize != 0);
CRYPTOPP_ASSERT(parallelization != 0);
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");
// Optimizer should remove this on 32-bit platforms
if (std::numeric_limits<size_t>::max() > std::numeric_limits<word32>::max())
{
const word64 maxLen = ((static_cast<word64>(1) << 32) - 1) * 32;
if (derivedLen > maxLen) {
std::ostringstream oss;
oss << "derivedLen " << derivedLen << " is larger than " << maxLen;
throw InvalidArgument("Scrypt: " + oss.str());
}
}
// https://github.com/weidai11/cryptopp/issues/787
CRYPTOPP_ASSERT(parallelization <= static_cast<word64>(std::numeric_limits<int>::max()));
if (parallelization > static_cast<word64>(std::numeric_limits<int>::max()))
{
std::ostringstream oss;
oss << " parallelization " << parallelization << " is larger than ";
oss << std::numeric_limits<int>::max();
throw InvalidArgument("Scrypt: " + oss.str());
}
CRYPTOPP_ASSERT(IsPowerOf2(cost));
if (IsPowerOf2(cost) == false)
throw InvalidArgument("Scrypt: cost must be a power of 2");
const word64 prod = static_cast<word64>(blockSize) * parallelization;
CRYPTOPP_ASSERT(prod < (1U << 30));
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.
// However, this alogirthm is dominated by slow moving parts, so a
// 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);
#endif
CRYPTOPP_ASSERT(bLimit); CRYPTOPP_ASSERT(xyLimit); CRYPTOPP_ASSERT(vLimit);
if (!bLimit || !xyLimit || !vLimit)
throw std::bad_alloc();
}
size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen,
const byte*secret, size_t secretLen, const NameValuePairs& params) const
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
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);
}
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
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
ThrowIfInvalidDerivedKeyLength(derivedLen);
ValidateParameters(derivedLen, cost, blockSize, parallel);
AlignedSecByteBlock B(static_cast<size_t>(blockSize * parallel * 128U));
// 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen)
PBKDF2_SHA256(B, B.size(), secret, secretLen, salt, saltLen, 1);
// Visual Studio and OpenMP 2.0 fixup. We must use int, not size_t.
int maxParallel=0;
if (!SafeConvert(parallel, maxParallel))
maxParallel = std::numeric_limits<int>::max();
#ifdef _OPENMP
int threads = STDMIN(omp_get_max_threads(), maxParallel);
#endif
// http://stackoverflow.com/q/49604260/608639
#pragma omp parallel num_threads(threads)
{
// Each thread gets its own copy
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
#pragma omp for
for (int i = 0; i < maxParallel; ++i)
{
// 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);
}
}
// 5: DK <-- PBKDF2(P, B, 1, dkLen)
PBKDF2_SHA256(derived, derivedLen, secret, secretLen, B, B.size(), 1);
return 1;
}
NAMESPACE_END
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mysql-tools/cryptopp.git
git@gitee.com:mysql-tools/cryptopp.git
mysql-tools
cryptopp
Crypto++库
master

搜索帮助