diff --git a/ipcl/keygen.cpp b/ipcl/keygen.cpp index fe064bb..45e24d0 100644 --- a/ipcl/keygen.cpp +++ b/ipcl/keygen.cpp @@ -1,6 +1,9 @@ // Copyright (C) 2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include #include #include "ipcl/ipcl.hpp" @@ -10,6 +13,13 @@ namespace ipcl { constexpr int N_BIT_SIZE_MAX = 2048; constexpr int N_BIT_SIZE_MIN = 200; +static void rand32u(std::vector& addr) { + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist(0, UINT_MAX); + for (auto& x : addr) x = (dist(rng) << 16) + dist(rng); +} + BigNumber getPrimeBN(int max_bits) { int prime_size; ippsPrimeGetSize(max_bits, &prime_size); @@ -19,9 +29,15 @@ BigNumber getPrimeBN(int max_bits) { #if defined(IPCL_RNG_INSTR_RDSEED) || defined(IPCL_RNG_INSTR_RDRAND) Ipp8u* rand_param = NULL; #else + constexpr int seed_size = 160; auto buff = std::vector(prime_size); auto rand_param = buff.data(); - ippsPRNGInit(160, reinterpret_cast(rand_param)); + ippsPRNGInit(seed_size, reinterpret_cast(rand_param)); + + auto seed = std::vector(seed_size); + rand32u(seed); + BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS); + ippsPRNGSetSeed(BN(seed_bn), reinterpret_cast(rand_param)); #endif BigNumber prime_bn(0, max_bits / 8); @@ -68,6 +84,7 @@ static void getNormalBN(int64_t n_length, BigNumber& p, BigNumber& q, static void getDJNBN(int64_t n_length, BigNumber& p, BigNumber& q, BigNumber& n, BigNumber& ref_dist) { BigNumber gcd; + do { do { p = getPrimeBN(n_length / 2); diff --git a/ipcl/utils/common.cpp b/ipcl/utils/common.cpp index e0130aa..780304e 100644 --- a/ipcl/utils/common.cpp +++ b/ipcl/utils/common.cpp @@ -41,7 +41,12 @@ IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx) { #elif defined(IPCL_RNG_INSTR_RDRAND) return ippsPRNGenRDRAND_BN(rand, bits, ctx); #else - return ippsPRNGen_BN(rand, bits, ctx); + int size; + ippsPRNGGetSize(&size); + auto prng = std::vector(size); + ippsPRNGInit(160, reinterpret_cast(prng.data())); + return ippsPRNGen_BN(rand, bits, + reinterpret_cast(prng.data())); #endif #endif // IPCL_RUNTIME_IPP_RNG }