Hello all,

I need to be able to initialise an ECC private key with entropy coming
from a source which doesn't support the
CryptoPP::RandomNumberGenerator interface. All the ECC examples I have
found use:

#define ECC_CURVE CryptoPP::ASN1::secp256k1()
        CryptoPP::ECIES< ECC_ALGORITHM >::PrivateKey    PrivateKey;
        CryptoPP::AutoSeededRandomPool rng;
        PrivateKey.Initialize( rng, ECC_CURVE );

or something similar to create the private key. I've traced through
the code this calls and tried to generate a way of doing this without
passing an rng, as shown below. The code seems to work but I'm a
little nervous and was hoping for a free code review from those more
familiar with the library. Apologies for the cheek but if someone can
confirm the following is not fatally flawed I would be very grateful?

Thanks for looking,
Patrick

PS : thank you, Jeffrey Walton, for your codeProject examples (if you
happen to read this)

//following code butchered from CryptoPP functions:
//  DL_PrivateKeyImpl -> void GenerateRandom(RandomNumberGenerator
&rng, const NameValuePairs &params)
//  void Integer::Randomize(RandomNumberGenerator &rng, const Integer
&min, const Integer &max)
//  void Integer::Randomize(RandomNumberGenerator &rng, size_t nbits)

#define ECC_ALGORITHM CryptoPP::ECP
#define ECC_CURVE CryptoPP::ASN1::secp256k1()

        //get the range of the ECC exponent
        CryptoPP::Integer themin = CryptoPP::Integer::One();
        CryptoPP::Integer themax =
CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>(ECC_CURVE).GetMaxExponent();
        CryptoPP::Integer Range = themax - themin;
        //calc the number of bits of randomness required
        const unsigned int nbits = Range.BitCount();
        const size_t nbytes = nbits/8 + 1;
        //generate random value within the range and assign to x
        CryptoPP::Integer x;
        do
        {
            //will use other source of randomness here
            CryptoPP::AutoSeededRandomPool rng;
            CryptoPP::SecByteBlock buf(nbytes);
            rng.GenerateBlock( buf, buf.SizeInBytes() );

            if (nbytes)
                buf[0] = (byte)CryptoPP::Crop(buf[0], nbits % 8);
                x = CryptoPP::Integer(buf, nbytes,
CryptoPP::Integer::UNSIGNED);
        }while (x > Range);
        x += themin;
        //set up the private key
        CryptoPP::ECIES< ECC_ALGORITHM >::PrivateKey    PrivateKey;
        PrivateKey.Initialize( ECC_CURVE, x );

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [EMAIL PROTECTED]
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---

Reply via email to