QMessageAuthenticationCode: Extract Method setKey() from initMessageHash()

This makes it explicit when we process the key (setKey() called) and
when we don't. That the old initMessageHash() left the key alone if it
already had the correct size was properly hidden in plain sight. The
split makes clearer what's going on.

Pick-to: 6.5
Change-Id: Ib013dbf8b976aa9f564224866091256aa8434cbd
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-02-23 17:11:49 +01:00
parent bdb13a1b31
commit ce955cd3ca

View File

@ -1076,6 +1076,7 @@ public:
QCryptographicHashPrivate messageHash; QCryptographicHashPrivate messageHash;
const QCryptographicHash::Algorithm method; const QCryptographicHash::Algorithm method;
void setKey(const QByteArray &k);
void initMessageHash(); void initMessageHash();
void finalize(); void finalize();
@ -1088,7 +1089,34 @@ public:
/*! /*!
\internal \internal
Transforms key into a block-sized format and then seeds messageHash from it. Transforms key \a newKey into a block-sized format and stores it in member
\c key.
This function assumes it can use messageHash (i.e. it's in its initial
state (reset() has been called)).
*/
void QMessageAuthenticationCodePrivate::setKey(const QByteArray &newKey)
{
const int blockSize = qt_hash_block_size(method);
if (newKey.size() > blockSize) {
messageHash.addData(newKey);
messageHash.finalizeUnchecked();
static_assert(maxHashLength() <= maxHashBlockSize());
key = messageHash.resultView().toByteArray();
messageHash.reset();
} else {
key = newKey;
}
if (key.size() < blockSize)
key.resize(blockSize, '\0');
}
/*!
\internal
Seeds messageHash from \c key.
This function assumes that messageHash is in its initial state (reset() has This function assumes that messageHash is in its initial state (reset() has
been called). been called).
@ -1097,19 +1125,6 @@ void QMessageAuthenticationCodePrivate::initMessageHash()
{ {
const int blockSize = qt_hash_block_size(method); const int blockSize = qt_hash_block_size(method);
if (key.size() > blockSize) {
messageHash.addData(key);
messageHash.finalizeUnchecked();
key = messageHash.resultView().toByteArray();
messageHash.reset();
}
if (key.size() < blockSize) {
const int size = key.size();
key.resize(blockSize);
memset(key.data() + size, 0, blockSize - size);
}
QVarLengthArray<char> iKeyPad(blockSize); QVarLengthArray<char> iKeyPad(blockSize);
const char * const keyData = key.constData(); const char * const keyData = key.constData();
@ -1156,7 +1171,7 @@ QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algor
const QByteArray &key) const QByteArray &key)
: d(new QMessageAuthenticationCodePrivate(method)) : d(new QMessageAuthenticationCodePrivate(method))
{ {
d->key = key; d->setKey(key);
d->initMessageHash(); d->initMessageHash();
} }
@ -1210,8 +1225,10 @@ void QMessageAuthenticationCode::reset()
*/ */
void QMessageAuthenticationCode::setKey(const QByteArray &key) void QMessageAuthenticationCode::setKey(const QByteArray &key)
{ {
d->key = key; d->result.clear();
reset(); d->messageHash.reset();
d->setKey(key);
d->initMessageHash();
} }
/*! /*!
@ -1290,7 +1307,7 @@ QByteArray QMessageAuthenticationCode::hash(const QByteArray &message, const QBy
QCryptographicHash::Algorithm method) QCryptographicHash::Algorithm method)
{ {
QMessageAuthenticationCodePrivate mac(method); QMessageAuthenticationCodePrivate mac(method);
mac.key = key; mac.setKey(key);
mac.initMessageHash(); mac.initMessageHash();
mac.messageHash.addData(message); mac.messageHash.addData(message);
mac.finalizeUnchecked(); mac.finalizeUnchecked();