Remove the Quality enum from the Random capability, as

discussed with Justin.

It was only really supported by the Botan provider, and
Botan dropped the idea in 1.6.0. Also, it never ensured
that the random number was actually secure.

svn path=/trunk/kdesupport/qca/; revision=620048
This commit is contained in:
Brad Hards 2007-01-05 05:22:34 +00:00
parent fcffefcc6b
commit 492ec1c0a8
8 changed files with 27 additions and 97 deletions

View File

@ -7,10 +7,10 @@
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -30,7 +30,7 @@ int main(int argc, char **argv)
qDebug() << "This example generates random numbers";
// the Initializer object sets things up, and
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
QCA::Initializer init;
@ -39,12 +39,6 @@ int main(int argc, char **argv)
randInt = QCA::Random::randomInt();
qDebug() << "A random number: " << randInt;
// If this was going to be a really important random
// number, you migth want to ask for higher Quality:
randInt = QCA::Random::randomInt(QCA::Random::LongTermKey);
// or if this was just a junk value, you could use:
randInt = QCA::Random::randomInt(QCA::Random::Nonce);
// If you wanted a random character (octet), you could
// use something like:
unsigned char randChar;
@ -56,8 +50,6 @@ int main(int argc, char **argv)
// If you need more random values, you may want to
// get an array, as shown below.
tenBytes = QCA::Random::randomArray(10);
// You can still use Quality settings, as shown below
tenBytes = QCA::Random::randomArray(10, QCA::Random::Nonce);
// To make this viewable, we convert to hexadecimal.
std::cout << "A random 10 byte array (in hex): ";
@ -68,7 +60,7 @@ int main(int argc, char **argv)
// This isn't normally the easiest way, but it does work
QCA::Random myRandomObject;
randChar = myRandomObject.nextByte();
tenBytes = myRandomObject.nextBytes(10, QCA::Random::Nonce);
tenBytes = myRandomObject.nextBytes(10);
return 0;
}

View File

@ -46,31 +46,12 @@ namespace QCA
an alternative random number source, by implementing
another provider.
You can select the "quality" of the random numbers. For
best results, you should use Nonce or PublicValue for values
that are likely to become public, and SessionKey or LongTermKey
for those values that are more critical. All that said, please
note that this is only a hint to the provider - it may make
no difference at all.
The normal use of this class is expected to be through the
static members - randomChar(), randomInt() and randomArray().
*/
class QCA_EXPORT Random : public Algorithm
{
public:
/**
* How much entropy to use for the random numbers that
* are required.
*/
enum Quality
{
Nonce, ///< Low quality, will become public
PublicValue,///< Will become public
SessionKey, ///< Intended to remain private
LongTermKey ///< Best available quality
};
/**
* Standard Constructor
*
@ -84,12 +65,10 @@ namespace QCA
*
* This method isn't normally required - you should use
* the static randomChar() method instead.
*
* \param q the quality of the random byte that is required
*
* \sa randomChar
*/
uchar nextByte(Quality q = SessionKey);
uchar nextByte();
/**
* Provide a specified number of random bytes
@ -98,11 +77,10 @@ namespace QCA
* the static randomArray() method instead.
*
* \param size the number of bytes to provide
* \param q the quality of the random bytes that are required
*
* \sa randomArray
*/
QSecureArray nextBytes(int size, Quality q = SessionKey);
QSecureArray nextBytes(int size);
/**
* Provide a random character (byte)
@ -112,12 +90,10 @@ namespace QCA
* \code
* myRandomChar = QCA::Random::randomChar();
* \endcode
*
* \param q the quality of the random character that is required
*
* If you need a number of bytes, perhaps randomArray() may be of use
*/
static uchar randomChar(Quality q = SessionKey);
static uchar randomChar();
/**
* Provide a random integer
@ -125,15 +101,10 @@ namespace QCA
* This is the normal way of obtaining a single random integer,
* as shown below:
* \code
* // default quality
* myRandomInt = QCA::Random::randomInt();
* // cheap integer
* myCheapInt = QCA::Random::randomInt( QCA::Random::Nonce );
* \endcode
*
* \param q the quality of the random integer that is required
*/
static uint randomInt(Quality q = SessionKey);
static uint randomInt();
/**
* Provide a specified number of random bytes
@ -141,14 +112,11 @@ namespace QCA
* \code
* // build a 30 byte secure array.
* QSecureArray arry = QCA::Random::randomArray(30);
* // take 20 bytes, as high a quality as we can get
* QSecureArray newKey = QCA::Random::randomArray(20, QCA::Random::LongTermKey);
* \endcode
*
* \param size the number of bytes to provide
* \param q the quality of the random bytes that are required
*/
static QSecureArray randomArray(int size, Quality q = SessionKey);
static QSecureArray randomArray(int size);
};
/**

View File

@ -49,7 +49,7 @@ class QCA_EXPORT RandomContext : public BasicContext
Q_OBJECT
public:
RandomContext(Provider *p) : BasicContext(p, "random") {}
virtual QSecureArray nextBytes(int size, Random::Quality q) = 0;
virtual QSecureArray nextBytes(int size) = 0;
};
class QCA_EXPORT HashContext : public BasicContext

View File

@ -42,35 +42,16 @@ public:
return new botanRandomContext( *this );
}
QSecureArray nextBytes(int size, QCA::Random::Quality quality)
QSecureArray nextBytes(int size)
{
QSecureArray buf(size);
#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,5,0)
Botan::Global_RNG::randomize( (Botan::byte*)buf.data(), buf.size(), lookup_quality(quality) );
Botan::Global_RNG::randomize( (Botan::byte*)buf.data(), buf.size(), Botan::SessionKey );
#else
Botan::Global_RNG::randomize( (Botan::byte*)buf.data(), buf.size() );
#endif
return buf;
}
private:
#if BOTAN_VERSION_CODE < BOTAN_VERSION_CODE_FOR(1,5,0)
Botan::RNG_Quality lookup_quality( QCA::Random::Quality quality )
{
if ( QCA::Random::Nonce == quality )
return Botan::Nonce;
else if ( QCA::Random::PublicValue == quality )
return Botan::PublicValue;
else if ( QCA::Random::SessionKey == quality )
return Botan::SessionKey;
else if ( QCA::Random::LongTermKey == quality )
return Botan::LongTermKey;
else
// this can't happen, but insurance against an accidental
// addition of a value to the enum
return Botan::SessionKey;
}
#endif
};

View File

@ -1,6 +1,6 @@
/*
* Copyright (C) 2003-2005 Justin Karneges <justin@affinix.com>
* Copyright (C) 2004,2005 Brad Hards <bradh@frogmouth.net>
* Copyright (C) 2004,2005,2007 Brad Hards <bradh@frogmouth.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -33,32 +33,32 @@ Random::Random(const QString &provider)
{
}
uchar Random::nextByte(Quality q)
uchar Random::nextByte()
{
return (uchar)(nextBytes(1, q)[0]);
return (uchar)(nextBytes(1)[0]);
}
QSecureArray Random::nextBytes(int size, Quality q)
QSecureArray Random::nextBytes(int size)
{
return static_cast<RandomContext *>(context())->nextBytes(size, q);
return static_cast<RandomContext *>(context())->nextBytes(size);
}
uchar Random::randomChar(Quality q)
uchar Random::randomChar()
{
return globalRNG().nextByte(q);
return globalRNG().nextByte();
}
uint Random::randomInt(Quality q)
uint Random::randomInt()
{
QSecureArray a = globalRNG().nextBytes(sizeof(int), q);
QSecureArray a = globalRNG().nextBytes(sizeof(int));
uint x;
memcpy(&x, a.data(), a.size());
return x;
}
QSecureArray Random::randomArray(int size, Quality q)
QSecureArray Random::randomArray(int size)
{
return globalRNG().nextBytes(size, q);
return globalRNG().nextBytes(size);
}
//----------------------------------------------------------------------------

View File

@ -1042,7 +1042,7 @@ SymmetricKey::SymmetricKey()
SymmetricKey::SymmetricKey(int size)
{
set(globalRNG().nextBytes(size, Random::SessionKey));
set(globalRNG().nextBytes(size));
}
SymmetricKey::SymmetricKey(const QSecureArray &a)
@ -1150,7 +1150,7 @@ InitializationVector::InitializationVector()
InitializationVector::InitializationVector(int size)
{
set(globalRNG().nextBytes(size, Random::Nonce));
set(globalRNG().nextBytes(size));
}
InitializationVector::InitializationVector(const QSecureArray &a)

View File

@ -44,7 +44,7 @@ public:
return new DefaultRandomContext(provider());
}
virtual QSecureArray nextBytes(int size, Random::Quality)
virtual QSecureArray nextBytes(int size)
{
QSecureArray buf(size);
for(int n = 0; n < (int)buf.size(); ++n)

View File

@ -86,19 +86,8 @@ void RandomUnitTest::testGetData()
QCOMPARE( QCA::Random().randomArray(3) == QCA::Random().randomArray(3), false );
QCOMPARE( QCA::Random::randomArray(3) == QCA::Random::randomArray(3), false );
QCOMPARE( randObject.nextByte(QCA::Random::Nonce) == randObject.nextByte(QCA::Random::SessionKey), false );
QCOMPARE( QCA::Random().nextByte(QCA::Random::PublicValue) == QCA::Random().nextByte(), false );
QCOMPARE( randObject.nextBytes(4) == randObject.nextBytes(4, QCA::Random::Nonce), false );
QCOMPARE( randObject.randomChar(QCA::Random::LongTermKey) == randObject.randomChar(), false );
QCOMPARE( QCA::Random().randomChar() == QCA::Random().randomChar(QCA::Random::PublicValue), false );
QCOMPARE( QCA::Random::randomChar(QCA::Random::PublicValue) == QCA::Random::randomChar(QCA::Random::Nonce), false );
QCOMPARE( QCA::Random().randomInt(QCA::Random::Nonce) == QCA::Random().randomInt(), false );
QCOMPARE( QCA::Random::randomInt(QCA::Random::Nonce) == QCA::Random::randomInt(QCA::Random::Nonce), false );
QCOMPARE( QCA::Random().randomArray(3, QCA::Random::Nonce) == QCA::Random().randomArray(3), false );
QCOMPARE( QCA::Random::randomArray(3, QCA::Random::SessionKey) == QCA::Random::randomArray(3, QCA::Random::PublicValue), false );
for (int len = 1; len <= 1024; len*=2 ) {
QCOMPARE( QCA::globalRNG().nextBytes(len, QCA::Random::SessionKey).size(), len );
QCOMPARE( QCA::globalRNG().nextBytes(len).size(), len );
}
}
}