QMessageAuthenticationCode: reuse messageHash in finalizeUnchecked()

The only reason why we couldn't before was because we had a view on
messageHash's result, which would have been clobbered when resetting
messageHash for reuse.

Since QMessageAuthenticationCodePrivate now contains
QCryptographicHashPrivate, we gain access to the latter's
QSmallByteArray result, and can just take a copy before reset()ing.

Re-using a QCryptographicHash is faster than creating a new one,
esp. in OpenSSL3 mode, where construction allocates a context while
reset() no longer does.

Pick-to: 6.5
Change-Id: I3bc0454918840a104fd53909e79b6fe21326bfbf
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2023-02-23 16:01:20 +01:00
parent 2a726582b7
commit 289a54a809

View File

@ -1206,7 +1206,7 @@ void QMessageAuthenticationCodePrivate::finalizeUnchecked()
const int blockSize = qt_hash_block_size(method); const int blockSize = qt_hash_block_size(method);
messageHash.finalizeUnchecked(); messageHash.finalizeUnchecked();
QByteArrayView hashedMessage = messageHash.resultView(); const HashResult hashedMessage = messageHash.result;
QVarLengthArray<char> oKeyPad(blockSize); QVarLengthArray<char> oKeyPad(blockSize);
const char * const keyData = key.constData(); const char * const keyData = key.constData();
@ -1214,10 +1214,10 @@ void QMessageAuthenticationCodePrivate::finalizeUnchecked()
for (int i = 0; i < blockSize; ++i) for (int i = 0; i < blockSize; ++i)
oKeyPad[i] = keyData[i] ^ 0x5c; oKeyPad[i] = keyData[i] ^ 0x5c;
QCryptographicHashPrivate hash(method); messageHash.reset();
hash.addData(oKeyPad); messageHash.addData(oKeyPad);
hash.addData(hashedMessage); messageHash.addData(hashedMessage.toByteArrayView());
hash.finalizeUnchecked(); messageHash.finalizeUnchecked();
result = hash.resultView().toByteArray(); result = hash.resultView().toByteArray();
} }