From 860e54a58043eb42c725643a607c59ac32b4cb40 Mon Sep 17 00:00:00 2001 From: Zhao Pengfei Date: Wed, 17 Aug 2022 16:54:27 +0800 Subject: [PATCH] Removing seed setup and replacing rng function for PrimeGen_BN --- ipcl/common.cpp | 34 +++++++++++++++++++++-------- ipcl/include/ipcl/common.hpp | 26 +++++++++++++++++++--- ipcl/keygen.cpp | 42 +++++++++++++++++------------------- 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/ipcl/common.cpp b/ipcl/common.cpp index b8e6274..90209eb 100644 --- a/ipcl/common.cpp +++ b/ipcl/common.cpp @@ -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."); @@ -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}; } diff --git a/ipcl/include/ipcl/common.hpp b/ipcl/include/ipcl/common.hpp index e83d476..e15d44e 100644 --- a/ipcl/include/ipcl/common.hpp +++ b/ipcl/include/ipcl/common.hpp @@ -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_ diff --git a/ipcl/keygen.cpp b/ipcl/keygen.cpp index 789bfea..290e502 100644 --- a/ipcl/keygen.cpp +++ b/ipcl/keygen.cpp @@ -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(PrimeSize); - ippsPrimeInit(maxBitSize, reinterpret_cast(primeGen.data())); - - // default seed bit size - constexpr int seedBitSize = 160; - BigNumber seed = getRandomBN(seedBitSize); - - auto rand = std::vector(PrimeSize); - ippsPRNGInit(seedBitSize, reinterpret_cast(rand.data())); - - ippsPRNGSetSeed(seed, reinterpret_cast(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(prime_size); + ippsPrimeInit(max_bits, reinterpret_cast(prime_ctx.data())); + +#if defined(IPCL_RNG_INSTR_RDSEED) || defined(IPCL_RNG_INSTR_RDRAND) + bool rand_param = NULL; +#else + auto buff = std::vector(prime_size); + auto rand_param = buff.data(); + ippsPRNGInit(160, reinterpret_cast(rand_param)); +#endif + + BigNumber prime_bn(0, max_bits / 8); while (ippStsNoErr != - ippsPrimeGen_BN(pBN, maxBitSize, 10, - reinterpret_cast(primeGen.data()), - ippsPRNGen, - reinterpret_cast(rand.data()))) { + ippsPrimeGen_BN(prime_bn, max_bits, 10, + reinterpret_cast(prime_ctx.data()), + ippGenRandom, + reinterpret_cast(rand_param))) { } - return pBN; + return prime_bn; } static BigNumber getPrimeDistance(int64_t key_size) {