Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ipcl/include/ipcl/utils/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ constexpr float IPCL_HYBRID_MODEXP_RATIO_ENCRYPT = 0.25;
constexpr float IPCL_HYBRID_MODEXP_RATIO_DECRYPT = 0.12;
constexpr float IPCL_HYBRID_MODEXP_RATIO_MULTIPLY = 0.18;

constexpr int IPCL_RDRAND_RETRIES = 3;

/**
* Generate random number with std mt19937
* @param[in,out] addr Location used to store the generated random number
Expand Down
81 changes: 54 additions & 27 deletions ipcl/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,64 @@ void rand32u(std::vector<Ipp32u>& addr) {
}

IppStatus ippGenRandom(Ipp32u* rand, int bits, void* ctx) {
if (kRNGenType == RNGenType::RDSEED)
return ippsTRNGenRDSEED(rand, bits, ctx);
else if (kRNGenType == RNGenType::RDRAND)
return ippsPRNGenRDRAND(rand, bits, ctx);
else if (kRNGenType == RNGenType::PSEUDO)
return ippsPRNGen(rand, bits, ctx);
else
ERROR_CHECK(false, "ippGenRandom: RNGenType does not exist.");
IppStatus stat;
switch (kRNGenType) {
case RNGenType::RDSEED:
stat = ippsTRNGenRDSEED(rand, bits, ctx);
if (stat == ippStsNoErr) break;
case RNGenType::RDRAND: {
int count = 0;
do {
stat = ippsPRNGenRDRAND(rand, bits, ctx);
count++;
} while ((stat != ippStsNoErr) && (count < IPCL_RDRAND_RETRIES));
break;
}
case RNGenType::PSEUDO:
stat = ippsPRNGen(rand, bits, ctx);
break;
default:
ERROR_CHECK(false, "ippGenRandom: RNGenType does not exist.");
}

return stat;
}

IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx) {
if (kRNGenType == RNGenType::RDSEED) {
return ippsTRNGenRDSEED_BN(rand, bits, ctx);
} else if (kRNGenType == RNGenType::RDRAND) {
return ippsPRNGenRDRAND_BN(rand, bits, ctx);
} else if (kRNGenType == RNGenType::PSEUDO) {
int seed_size = 160;
int size;
ippsPRNGGetSize(&size);
auto prng = std::vector<Ipp8u>(size);
ippsPRNGInit(seed_size, reinterpret_cast<IppsPRNGState*>(prng.data()));

auto seed = std::vector<Ipp32u>(seed_size);
rand32u(seed);
BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS);
ippsPRNGSetSeed(BN(seed_bn), reinterpret_cast<IppsPRNGState*>(prng.data()));
return ippsPRNGen_BN(rand, bits,
reinterpret_cast<IppsPRNGState*>(prng.data()));
} else {
ERROR_CHECK(false, "ippGenRandomBN: RNGenType does not exist.");
IppStatus stat;
switch (kRNGenType) {
case RNGenType::RDSEED:
stat = ippsTRNGenRDSEED_BN(rand, bits, ctx);
if (stat == ippStsNoErr) break;
case RNGenType::RDRAND: {
int count = 0;
do {
stat = ippsPRNGenRDRAND_BN(rand, bits, ctx);
count++;
} while ((stat != ippStsNoErr) && (count < IPCL_RDRAND_RETRIES));
break;
}
case RNGenType::PSEUDO: {
int seed_size = 160;
int size;
ippsPRNGGetSize(&size);
auto prng = std::vector<Ipp8u>(size);
ippsPRNGInit(seed_size, reinterpret_cast<IppsPRNGState*>(prng.data()));

auto seed = std::vector<Ipp32u>(seed_size);
rand32u(seed);
BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS);
ippsPRNGSetSeed(BN(seed_bn),
reinterpret_cast<IppsPRNGState*>(prng.data()));
stat = ippsPRNGen_BN(rand, bits,
reinterpret_cast<IppsPRNGState*>(prng.data()));
break;
}
default:
ERROR_CHECK(false, "ippGenRandomBN: RNGenType does not exist.");
}

return stat;
}

BigNumber getRandomBN(int bits) {
Expand Down