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
34 changes: 25 additions & 9 deletions ipcl/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@

namespace ipcl {

BigNumber getRandomBN(int bit_len) {
IppStatus ippGenRandom(Ipp32u* rand, int bits, void* ctx) {
#ifdef IPCL_RNG_INSTR_RDSEED
return ippsTRNGenRDSEED(rand, bits, ctx);
#elif defined(IPCL_RNG_INSTR_RDRAND)
return ippsPRNGenRDRAND(rand, bits, ctx);
#else
return ippsPRNGen(rand, bits, ctx);
#endif
}

IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx) {
#ifdef IPCL_RNG_INSTR_RDSEED
return ippsTRNGenRDSEED_BN(rand, bits, ctx);
#elif defined(IPCL_RNG_INSTR_RDRAND)
return ippsPRNGenRDRAND_BN(rand, bits, ctx);
#else
return ippsPRNGen_BN(rand, bits, ctx);
#endif
}

BigNumber getRandomBN(int bits) {
IppStatus stat;
int bn_buf_size;

int bn_len = BITSIZE_WORD(bit_len);
int bn_len = BITSIZE_WORD(bits);
stat = ippsBigNumGetSize(bn_len, &bn_buf_size);
ERROR_CHECK(stat == ippStsNoErr,
"getRandomBN: get IppsBigNumState context error.");
Expand All @@ -26,13 +46,9 @@ BigNumber getRandomBN(int bit_len) {
ERROR_CHECK(stat == ippStsNoErr,
"getRandomBN: init big number context error.");

#ifdef IPCL_RNG_INSTR_RDSEED
ippsTRNGenRDSEED_BN(pBN, bit_len, NULL);
#elif defined(IPCL_RNG_INSTR_RDRAND)
ippsPRNGenRDRAND_BN(pBN, bit_len, NULL);
#else
ippsPRNGen_BN(pBN, bit_len, NULL);
#endif
stat = ippGenRandomBN(pBN, bits, NULL);
ERROR_CHECK(stat == ippStsNoErr,
"getRandomBN: generate random big number error.");

return BigNumber{pBN};
}
Expand Down
26 changes: 23 additions & 3 deletions ipcl/include/ipcl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,32 @@ namespace ipcl {

constexpr int IPCL_CRYPTO_MB_SIZE = 8;

/**
* Random generator wrapper.Generates a random unsigned Big Number of the
* specified bit length
* @param[in] rand Pointer to the output unsigned integer big number
* @param[in] bits The number of generated bits
* @param[in] ctx Pointer to the IppsPRNGState context.
* @return Error code
*/
IppStatus ippGenRandom(Ipp32u* rand, int bits, void* ctx);

/**
* Random generator wrapper.Generates a random positive Big Number of the
* specified bit length
* @param[in] rand Pointer to the output Big Number
* @param[in] bits The number of generated bits
* @param[in] ctx Pointer to the IppsPRNGState context.
* @return Error code
*/
IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx);

/**
* Get random value
* @param[in] length bit length
* @return the random value of type BigNumber
* @param[in] bits The number of Big Number bits
* @return The random value of type Big Number
*/
BigNumber getRandomBN(int length);
BigNumber getRandomBN(int bits);

} // namespace ipcl
#endif // IPCL_INCLUDE_IPCL_COMMON_HPP_
42 changes: 20 additions & 22 deletions ipcl/keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,29 @@ namespace ipcl {
constexpr int N_BIT_SIZE_MAX = 2048;
constexpr int N_BIT_SIZE_MIN = 200;

BigNumber getPrimeBN(int maxBitSize) {
int PrimeSize;
ippsPrimeGetSize(maxBitSize, &PrimeSize);
auto primeGen = std::vector<Ipp8u>(PrimeSize);
ippsPrimeInit(maxBitSize, reinterpret_cast<IppsPrimeState*>(primeGen.data()));

// default seed bit size
constexpr int seedBitSize = 160;
BigNumber seed = getRandomBN(seedBitSize);

auto rand = std::vector<Ipp8u>(PrimeSize);
ippsPRNGInit(seedBitSize, reinterpret_cast<IppsPRNGState*>(rand.data()));

ippsPRNGSetSeed(seed, reinterpret_cast<IppsPRNGState*>(rand.data()));

// generate maxBit prime
BigNumber pBN(0, maxBitSize / 8);
BigNumber getPrimeBN(int max_bits) {
int prime_size;
ippsPrimeGetSize(max_bits, &prime_size);
auto prime_ctx = std::vector<Ipp8u>(prime_size);
ippsPrimeInit(max_bits, reinterpret_cast<IppsPrimeState*>(prime_ctx.data()));

#if defined(IPCL_RNG_INSTR_RDSEED) || defined(IPCL_RNG_INSTR_RDRAND)
bool rand_param = NULL;
#else
auto buff = std::vector<Ipp8u>(prime_size);
auto rand_param = buff.data();
ippsPRNGInit(160, reinterpret_cast<IppsPRNGState*>(rand_param));
#endif

BigNumber prime_bn(0, max_bits / 8);
while (ippStsNoErr !=
ippsPrimeGen_BN(pBN, maxBitSize, 10,
reinterpret_cast<IppsPrimeState*>(primeGen.data()),
ippsPRNGen,
reinterpret_cast<IppsPRNGState*>(rand.data()))) {
ippsPrimeGen_BN(prime_bn, max_bits, 10,
reinterpret_cast<IppsPrimeState*>(prime_ctx.data()),
ippGenRandom,
reinterpret_cast<IppsPRNGState*>(rand_param))) {
}

return pBN;
return prime_bn;
}

static BigNumber getPrimeDistance(int64_t key_size) {
Expand Down