Fix BotanHKDFContext::clone

And excercise it in the unittest
This commit is contained in:
Albert Astals Cid 2020-01-21 17:41:24 +01:00
parent 4449dd088b
commit 80f58d05f0
2 changed files with 28 additions and 6 deletions

View File

@ -284,12 +284,21 @@ protected:
Botan::S2K* m_s2k; Botan::S2K* m_s2k;
}; };
static QString qcaHkdfToBotanHkdf(const QString &type)
{
if ( type == "hkdf(sha256)" )
return QString("SHA-256");
return {};
}
//----------------------------------------------------------- //-----------------------------------------------------------
class BotanHKDFContext: public QCA::HKDFContext class BotanHKDFContext: public QCA::HKDFContext
{ {
public: public:
BotanHKDFContext(const QString &hashName, QCA::Provider *p, const QString &type) : QCA::HKDFContext(p, type) BotanHKDFContext(QCA::Provider *p, const QString &type) : QCA::HKDFContext(p, type)
{ {
const QString hashName = qcaHkdfToBotanHkdf(type);
Botan::HMAC *hashObj; Botan::HMAC *hashObj;
hashObj = new Botan::HMAC(Botan::HashFunction::create_or_throw(hashName.toStdString()).release()); hashObj = new Botan::HMAC(Botan::HashFunction::create_or_throw(hashName.toStdString()).release());
m_hkdf = new Botan::HKDF(hashObj); m_hkdf = new Botan::HKDF(hashObj);
@ -302,7 +311,7 @@ public:
Context *clone() const override Context *clone() const override
{ {
return new BotanHKDFContext( *this ); return new BotanHKDFContext( provider(), type() );
} }
QCA::SymmetricKey makeKey(const QCA::SecureArray &secret, const QCA::InitializationVector &salt, QCA::SymmetricKey makeKey(const QCA::SecureArray &secret, const QCA::InitializationVector &salt,
@ -662,6 +671,15 @@ public:
return list; return list;
} }
QStringList hkdfTypes() const
{
static QStringList list;
if (list.isEmpty()) {
list += "hkdf(sha256)";
}
return list;
}
QStringList features() const override QStringList features() const override
{ {
static QStringList list; static QStringList list;
@ -669,7 +687,7 @@ public:
list += "random"; list += "random";
list += hmacTypes(); list += hmacTypes();
list += pbkdfTypes(); list += pbkdfTypes();
list += "hkdf(sha256)"; list += hkdfTypes();
list += cipherTypes(); list += cipherTypes();
list += hashTypes(); list += hashTypes();
} }
@ -686,8 +704,8 @@ public:
return new BotanHMACContext( this, type ); return new BotanHMACContext( this, type );
else if ( pbkdfTypes().contains(type) ) else if ( pbkdfTypes().contains(type) )
return new BotanPBKDFContext( this, type ); return new BotanPBKDFContext( this, type );
else if ( type == "hkdf(sha256)" ) else if ( hkdfTypes().contains(type) )
return new BotanHKDFContext( QString("SHA-256"), this, type ); return new BotanHKDFContext( this, type );
else if ( cipherTypes().contains( type ) ) else if ( cipherTypes().contains( type ) )
return new BotanCipherContext( this, type ); return new BotanCipherContext( this, type );
else else

View File

@ -482,7 +482,11 @@ void KDFUnitTest::hkdfTests()
QCA::SecureArray password = QCA::hexToArray( secret ); QCA::SecureArray password = QCA::hexToArray( secret );
QCA::InitializationVector saltv( QCA::hexToArray( salt ) ); QCA::InitializationVector saltv( QCA::hexToArray( salt ) );
QCA::InitializationVector infov( QCA::hexToArray( info ) ); QCA::InitializationVector infov( QCA::hexToArray( info ) );
QCA::SymmetricKey key = QCA::HKDF("sha256", provider).makeKey( password, QCA::HKDF hkdf = QCA::HKDF("sha256", provider);
QCA::HKDF copy = hkdf;
copy.context(); // detach
QCA::SymmetricKey key = hkdf.makeKey( password,
saltv, saltv,
infov, infov,
output.size() / 2 ); output.size() / 2 );