summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qcryptographichash.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Doc: Fix documentation issues for Qt CoreTopi Reinio2023-12-191-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Fix template arguments in \fn signatures for Qt::compareThreeWay() functions. * Fix template arguments in \fn signatures for QDebug::operator<<() functions. * Fix \sa links to specific overloads of QSpan functions. * Fix \sa links to specific overloads of QFileInfo::fileTime(). * Remove references to 'Custom Type Example' (example has been removed). * Fix linking to 'JSON Save Game' example. * Fix references to 'Queued Custom Type' example. * Fix linking to QCryptographicHash::Algorithm. * Fix linking to Qt Qml module. * Fix undocumented parameters in qHypot(). Pick-to: 6.7 Change-Id: If9eb9978a14e147f003672a682972b319454c311 Reviewed-by: Luca Di Sera <luca.disera@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* Doc: Settle on 'function' instead of 'method' in QMAC documentationKai Köhne2023-12-051-3/+3
| | | | | | | | | | Function is the more popular term in the documentation. Method is also a tad confusing, because QCryptographicHash calls the algorithm 'method'. Pick-to: 6.6 Change-Id: I9922f837b04311a4ef45e1c6de6b8e12a32e4f1e Reviewed-by: Venugopal Shivashankar <Venugopal.Shivashankar@qt.io>
* Doc: Improve documentation for QMessageAuthenticationCodeKai Köhne2023-12-041-8/+19
| | | | | | | | | | | | | | | * Mention 'HMAC' as something people will search for * Change description to actually reflect code snippet (where the key is passed to the constructor) * At least mention that the security of the HMAC depends also on the length of the key, as the code snippet uses an artificial/short key. Not sure whether we should further expand on this, or link to some other source? Pick-to: 6.5 6.6 Fixes: QTBUG-119499 Change-Id: I2768d9a9d553957e1a778c798d82a73468bee16f Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: fallback to non-OpenSSL implementation for KeccakJan Grulich2023-11-081-21/+44
| | | | | | | | | | | Current versions of OpenSSL 3 don't support Keccak hashes as these are going to be introduced with OpenSSL 3.2 so we should rather fallback to the non-OpenSSL implementation instead of using SHA3. Fixes: QTBUG-118814 Pick-to: 6.5 6.6 Change-Id: Iedeb81cd76d43d920fc10e1efdac261bc12a394c Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: Do not rely on auto-loading of the default providerJan Grulich2023-11-061-2/+5
| | | | | | | | | | | | | | | When using OpenSSL implementation, we were assuming the default provider will be automatically loaded, but this is not going to happen after it gets unloaded. Even the documentation says that automatic loading of the default provider occurs max once and if it's explicitly unloaded, it will not be automatically loaded again. In our case we are explicitly loading and unloading the provider after MD4 hash is used so using it afterwards we will always fail. Fixes: QTBUG-118227 Pick-to: 6.5 6.6 Change-Id: I8107b9ab02321b57978c3d25a061672fd2a7aee8 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: don't forget to unload OpenSSL providersJan Grulich2023-10-021-5/+15
| | | | | | | | | | | | | Automatically unload loaded crypto providers on cleanup. In most cases we don't load them, but when we do (e.g. when MD4 is used), we would be leaking memory. Fixes: QTBUG-115233 Pick-to: 6.5 6.6 Change-Id: I91318d391ab35d00647d1e9e2408fc987811a2d3 Reviewed-by: Ahmad Samir <a.samirh78@gmail.com> Reviewed-by: Axel Spoerl <axel.spoerl@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: fix incorrect GCC checkMarc Mutz2023-08-181-1/+1
| | | | | | | | | | | It's Q_CC_GNU, not Q_CC_GCC. Amends 0411d98192ede84b115618ece876579fd79252f8. Pick-to: 6.6 6.5 Change-Id: I56c4393e5955ac1bbcc8a198b11487cff775b16d Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
* Deprecate Q_ASSUME()Thiago Macieira2023-08-141-3/+3
| | | | | | | | | | | | | | | | | | | | We've known for a long time that this is producing worse code with GCC because of how we implemented in Q_ASSUME_IMPL(). So bite the bullet and actually deprecate the macro, replacing all extant Q_ASSUME() with Q_ASSERT(). The replacement is in C++23. Backporting the support onto Q_ASSUME_IMPL was previously rejected by reviewers. [ChangeLog][Deprecation Notice] The Q_ASSUME() macro is deprecated. This macro has different side-effects depending on the compiler used (GCC compared to Clang and MSVC), and there are certain conditions under which GCC is known to produce worse code than if the macro was absent. To give a hint to the compiler for optimizations, use the C++23 [[assume]] attribute. Change-Id: I80612a7d275c41f1baf0fffd177a3a4ad819fb2d Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* Introduce openssl-hash featureAlexey Edelev2023-06-291-2/+2
| | | | | | | | | | | | | The feature allows to not use OpenSSL in QCryptographicHash implementation and removes OpenSSL from dependencies of QtCore if disabled. Fixes: QTBUG-114783 Pick-to: 6.6 Change-Id: I2a49fa9ddfa5acedcfc95a3330fd7863a8052a5c Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: constexpr ALL of QSmallByteArrayMarc Mutz2023-03-211-19/+19
| | | | | | | | | | ... because we can. Pick-to: 6.5 Change-Id: I03872a69ac4625ca73b0a7f0310a2a951615b000 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: have setKey() call initMessageHash()Marc Mutz2023-03-211-3/+2
| | | | | | | | | | | | All callers of Private::setKey() follow the call with one to initMessageHash(), so move the call into setKey(). We can't remove initMessageHash(), because reset() still needs to call it without a setKey(). Pick-to: 6.5 Change-Id: I1fc8cd5cda90f1595cedcc323a4cee8baa7ce6a5 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticatonCode: re-use QCryptographicHash's result bufferMarc Mutz2023-03-201-8/+3
| | | | | | | | | | | | QMessageAuthenticationCodePrivate::result always mirrored QCryptographicHash's, so we only need one, greatly reducing sizeof(QMessageAuthentcationCodePrivate) and avoiding unnecessary copies. Pick-to: 6.5 Change-Id: I56365b29bd9e096a4582b1764b6fef4c5243735b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* QMessageAuthenticationCode: remove Private::methodMarc Mutz2023-03-201-3/+2
| | | | | | | | | ...it's the same as messageHash.method, so we don't need to store it twice. Pick-to: 6.5 Change-Id: I926dbfa177c25e9bc3ebed9149da5ce8bad8cf2f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: reuse messageHash mutexMarc Mutz2023-03-201-3/+2
| | | | | | | | | | | | | | QMessageAuthenticationCode operates the embedded QCryptographicHash in an unshared way, in which QCryptographicHashPrivate::finalizeMutex is never used. So kick QMessageAuthenticationCodePrivate::finalizeMutex out and use QCryptographicHashPrivate::finalizeMutex instead. The only effect it the reduction in QMessageAuthenticationCodePrivate size by sizeof(QBasicMutex). Pick-to: 6.5 Change-Id: I02208eb42f8247939619f74ad3e040cb0d34b5ba Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QCryptographicHash: move EVP struct into the contexts unionMarc Mutz2023-03-171-40/+80
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ... making it just another context. Well, except the EVP struct has non-trivial SMFs, so adding it to the union deletes all the union's SMFs, too. To restore them, we need to give the union a class name (State). Since we anyway need to provide ctors, inline all init() functions into them. State having a ctor means we need to call it, with the method argument, in the Private ctor-init-list. That requires the State member to have a name. Since the union thus no longer defines variant members within Private, move most of the functions that operated on the variant members into State or EVP to avoid having to sprinkle state./evp.-prefixes all over the code. This gives the opportunity to re-share some code that was duplicated when we dragged #ifdef USING_OPENSSL3 out of the functions into namespace scope. The move of the functions into State robs them of their access to other Private members, to wit, 'method' and 'result', so we have to pass them as arguments now. I tried to split this up into smaller patches, but it kinda has to happen all at once, sorry. Pick-to: 6.5 Change-Id: If864d4d46075d00420f6e849cd68c4b824c1a50a Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: move EVP members into a structMarc Mutz2023-03-171-15/+17
| | | | | | | | | | | | ... and move the evp_{init,reset,finalizeUnchecked}() functions into it as new members without the evp_ prefix. Need to pass QCHPrivate members as function arguments now. This is in preparation of moving these members into the union. Pick-to: 6.5 Change-Id: I64805d8aac91f777fbc5c13b1a08b37e5227169a Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: separate EVP and non-EVP OpenSSL3 operationsMarc Mutz2023-03-171-5/+22
| | | | | | | | | | These, too, will end up on different classes, eventually. The addData() function is the odd one out here, as it's EVP part it trivial. Pick-to: 6.5 Change-Id: I24c7334edd8ba218e4cfcb64d0ed67bc281c4525 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: separate OpenSSL3 and traditional modes moreMarc Mutz2023-03-171-21/+51
| | | | | | | | | | | | | | | | ... by defining separate init(), reset(), addData() and finalizeUnchecked() functions instead of doing #ifdef'ery inside the function bodies. The purpose of the exercise is that these functions will later end up in different classes. For now, we can already drop the UINT_MAX loop for the OpenSSL30 case, as both EVP_DigestUpdate() and the Blake functions use size_t lengths in the API. Pick-to: 6.5 Change-Id: I9ad618c90bb54b429db3225061cd5cfd25243fca Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* QCryptographicHash: split initialization from reset()ingMarc Mutz2023-03-171-19/+86
| | | | | | | | | | | | | | | | | | | | Before the OpenSSL3 mode was added, these operations were the same. The placement-new of the active union member was arguably not even necessary, as just naming it should start their lifetime; certainly all initialization was vacuous, anyway. So it didn't really matter that we re-did it on each reset(). But OpenSSL3 mode is different: initialization allocates (and can fail) whereas reset() does not¹, so it makes sense to separate them, so as to re-establish the lost proper noexcept'ness of reset(). ¹ or so we assume This will also help when moving the EVP members into the union later. Pick-to: 6.5 Change-Id: Id1c9a00cddb95fb2235162409c2301dd4c35228a Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: fix missing EVP_DigestInit_ex() after EVP_MD_CTX_reset()Marc Mutz2023-03-171-0/+1
| | | | | | | | | | | | EVP_MD_CTX_reset() alone is not sufficient. A following EVP_DigestUpdate() crashes, so re-init the context, too. Amends 189444d8c4a1384479962e98e699bf1fd2908de8. Pick-to: 6.5 Change-Id: I5dede0adddd7f6b0c62540f6d45917b3236e3d02 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* QMessageAuthenticationCode: make reset() noexceptMarc Mutz2023-03-171-1/+1
| | | | | | | | | | | | | | | | This is the last method that can and should be noexcept: - the ctor allocates the Private, so can't be - static hash() allocates QByteArray::data(), so also cannot be - ditto result() All other functions are already noexcept. Fixes: QTBUG-111688 Change-Id: I237c3d1292452da5ca92712531c3527c1fb2f59b Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: port to QByteArrayView [3/3]: static hash()Marc Mutz2023-03-171-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | The class is not used frequently enough to warrant the overhead of a Q_WEAK_ QByteArray overload to fix the SiC Type A for users that pass objects that implicitly convert to QByteArray, esp. since we'd need _four_ overloads to handle the two byte array arguments here, and still cause ambiguities, because there's only one level of "weakness" in Q_WEAK_OVERLOAD, but we'd need two. QCryptographicHash::hash() also doesn't have it. [ChangeLog][QtCore][QMessageAuthenticationCode] The constructor, setKey(), addData() methods as well as the static hash() function take arguments by QByteArrayView instead of QByteArray now. [ChangeLog][Potentially Source-Incompatible Changes] More APIs now take QByteArrayView instead of QByteArray. You will now get a compile error when your code passes to such functions objects that implicitly convert to QByteArray, but not QByteArrayView. Wrapping such arguments in QByteArray{~~~} to make the cast explicit is a backwards-compatible way to avoid this problem. Fixes: QTBUG-111676 Change-Id: Iaff832df3b315ca2eee7bff93720b6681182036f Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: port to QByteArrayView [2/3]: addData()Marc Mutz2023-03-171-2/+7
| | | | | | | | | | | | | | | | | | | | | The class is not used frequently enough to warrant the overhead of a Q_WEAK_ QByteArray overload to fix the SiC Type A for users that pass objects that implicitly convert to QByteArray. QCryptographicHash also doesn't have it. Also mark addData() noexcept. Now that it takes a view instead of an owning container, it only calls other noexcept functions. Make the new overload carry actual documentation instead of just \overload. ChangeLog will be on patch [3/3]. Task-number: QTBUG-111676 Task-number: QTBUG-111688 Change-Id: Ie6447bf54d7d442b1a76761bc0f28f868c08ef09 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: port to QByteArrayView [1/3]: ctor/setKey()Marc Mutz2023-03-171-2/+13
| | | | | | | | | | | | | | | | | The class is not used frequently enough to warrant the overhead of a Q_WEAK_ QByteArray overload to fix the SiC Type A for users that pass objects that implicitly convert to QByteArray. QCryptographicHash also doesn't have it. Also mark setKey() noexcept. Now that it takes a view instead of an owning container, it only calls other noexcept functions. ChangeLog will be on patch [3/3]. Task-number: QTBUG-111676 Task-number: QTBUG-111688 Change-Id: Ic2321d0d41ce8eb4d0390889f28b3fd4bd9af103 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: add move SMFs and swap()Marc Mutz2023-03-161-0/+33
| | | | | | | | | | | | QCryptographicHash is move-only these days, so QMessageAuthenticationCode should not be left behind. [ChangeLog][QtCore][QMessageAuthenticationCode] Added move constructor, move assignment operator and swap() function. Fixes: QTBUG-111677 Change-Id: I420f24c04828e8ad7043a9e8c9e7e2d47dd183e0 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* [docs] Fix typo in QMessageAuthenticationCode::reset() documentationMarc Mutz2023-03-021-1/+1
| | | | | | | | Pefer → Prefer Pick-to: 6.5 Change-Id: Ie9fd09452f5d99d799a045ee59fc11a2b9c5a599 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Mark QMessageAuthenticationCodePrivate::initMessageHash() as noexceptMarc Mutz2023-03-021-2/+2
| | | | | | | | | | | It only calls other noexcept functions: - QCryptographicHash::addData(QByteArrayView) - xored() Pick-to: 6.5 Task-number: QTBUG-111688 Change-Id: Idd8c485a48b8243b359638086c373288c1c6f96d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: port Private::setKey() to QByteArrayViewMarc Mutz2023-03-021-2/+2
| | | | | | | | | | | | The only change is the signature of the function. Also mark it as noexcept. It calls all functions in-contract and doesn't allocate memory. Task-number: QTBUG-111676 Pick-to: 6.5 Change-Id: I505c1f51da704fd46e538a68d6d8703f7cdefbc8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* De-pessimize QCryptographicHash::reset() in OpenSSL3 modeMarc Mutz2023-03-021-0/+6
| | | | | | | | | | | | | | | | | | Instead of going through the whole dance with provider loading, context creation etc, just call EVP_MD_CTX_reset() to reuse the already-setup context, if any. This makes QCryptographicHash::reset() adhere to its noexcept specification again (assuming EVP_MD_CTX_reset() doesn't allocate state, and, despite its non-void return type, cannot fail on a valid context), and should greatly improve the speed of reset(), addData(), resultView() cycles. Pick-to: 6.5 Change-Id: I7c35b61cbeab1ffd6dd258e8389ea614d49e2e1e Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Jan Grulich <jgrulich@redhat.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* Long live QMessageAuthenticationCode::resultView()!Marc Mutz2023-03-011-3/+18
| | | | | | | | | | | Use it in a few places. [ChangeLog][QtCore][QMessageAuthenticationCode] Added QCryptographicHash-style resultView(). Change-Id: I745d71f86f9c19c9a9aabb2021c6617775dab1cf Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* QMessageAutenticationCode: add some Q_ASSUME()s to xored()Marc Mutz2023-03-011-0/+26
| | | | | | | | | ... telling the compiler all there is to know about block.size(), so it hopefully vectorizes the function well. Pick-to: 6.5 Change-Id: Icb3d5c983402e52a65c72af15f806f3a3fe6411f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: statically assert HMAC works for all algorithmsMarc Mutz2023-03-011-1/+8
| | | | | | | | | | | ... by checking that for each algorithm, that the algorithm's result size is not larger than the algorithm's block size. Pick-to: 6.5 Change-Id: I4daf7900e72766d180954b15edb06687a57f939f Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: use QSmallByteArray for the keyMarc Mutz2023-03-011-23/+42
| | | | | | | | | | | | | | | | | | | | | Add more API to QSmallByteArray: - const op[] overload - copy construction and -assignment from QSmallByteArrays with smaller N (for implicitly converting a HashResult into a HashBlock, incl. a static assertion that it always fits - STL-style assign() This allows us to use HashBlock for the key, instead of a QByteArray. To make-it-cool®, add a method that xor's all bytes in a QSmallByteArray with a given value and returns the result. We use that algorithm twice in the implementation, and the code simplification at the call sites is amazing. Pick-to: 6.5 Change-Id: I7fbc11538544b4a8d1771dc8ad025be154d446df Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* Fix QCH:supportsAlgorithm() result for unsupported hashes in OpenSSLJan Grulich2023-02-281-1/+21
| | | | | | | | | | | OpenSSL doesn't support some Blake2s and Blake2b hashes and querying these would automatically report that they are unsupported, while we are actually using non-OpenSSL implementataion for these and therefore they are always supported. Pick-to: 6.5 Change-Id: I300694459891c3103502705d6c8271caa47d8d01 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QCryptographicHash: don't include openssl/sha.hMarc Mutz2023-02-271-8/+0
| | | | | | | | | | | | | | | | | | | | | | ... it conflicts with rfc6234/sha.h's SHA{1,224,256,384,512} symbols. We can't drop the rfc6234/sha.h header, as openssl/sha.h doesn't give us the algorithm's block sizes, necessary for HMAC (QMessageAuthenticationCode). But we can drop openssl/sha.h. The only reason we included the header was to get access to SHA<N>_DIGEST_LENGTH, but this is a well-known value and easily obtained from rfc6234/sha.h as SHA<N>HashSize, so use that. Even reduces #ifdef'ery. Amends d9f9d03fd34d951eb587fe082a0dbda33b8df248. Fixes: QTBUG-111467 Pick-to: 6.5 Change-Id: Ice19ad8c788fb2828666647cc40abb894cd7af2b Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
* QMessageAuthenticationCode: Extract Method setKey() from initMessageHash()Marc Mutz2023-02-261-15/+32
| | | | | | | | | | | | 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>
* QSmallByteArray: add sufficient API to make conversion to QByteArrayView ↵Marc Mutz2023-02-261-3/+18
| | | | | | | | | | | | implicit Iterators were missing. Provide a const data() overload to implement the iterator functions in their natural form. Pick-to: 6.5 Change-Id: I906013e55fce2effbedba0d283fb4cea3b512fdd Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QCryptographicHash: implement non-OpenSSL3 part of supportsAlgorithm()Marc Mutz2023-02-261-2/+30
| | | | | | | | | | | | | Unconditionally returning true is incorrect, not only just since NumAlgorithms was added in 0411d98192ede84b115618ece876579fd79252f8. The values may have gaps, we might be compiling with SHA1_ONLY, and, on a language-lawyer level, enums can legally contain values other than those explicitly enumerated, so give the right answer in all of these cases. Pick-to: 6.5 Change-Id: I705d4f759b2572b8b3d1cee18b7939939eedbbac Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: make finalizeUnchecked() properly noexceptMarc Mutz2023-02-251-4/+21
| | | | | | | | | | | | | | | | | Replace the QVarLengthArray with HashBlock, an instantiation of QSmallByteArray. Unlike QVLA, which may allocate memory when capacity() exceeds Prealloc (not the case here, but could become an issue with future hash algorithms, we're at 144 now, up from the traditional 64), QSmallByteArray never throws, and the few Q_ASSERT()s it contains don't matter, because we use the class' functions in-contract here. Requires to add an indexing operator to QSmallByteArray. Pick-to: 6.5 Change-Id: Ica3656adc190e0141e065287fadc38e0cebce0f4 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: replace result QByteArray with QSmallByteArrayMarc Mutz2023-02-251-4/+4
| | | | | | | | | | | | | | | This minimizes the impedance mismatch between QCryptographicViewPrivate and QMessageAuthenticationCodePrivate and is the penultimate step to finally marking QMAC's finalizeUnchecked() noexcept, too (QCH's has been for a few Qt versions now). It also enables adding a QMAC::resultView() a la QCH::resultView(), but that's another commit, as 6.5 is closed for new API. Pick-to: 6.5 Change-Id: I3fe228585c560d8d32e99e12bba2be21fddaf642 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: reuse messageHash in finalizeUnchecked()Marc Mutz2023-02-251-5/+5
| | | | | | | | | | | | | | | | | | | 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>
* QCryptographicHash: give the result container a catchy nameMarc Mutz2023-02-251-1/+3
| | | | | | | | | | Repeating QSmallByteArray<maxHashLength()> when reusing the type for QMessageAuthenticationCode isn't DRY. Pick-to: 6.5 Change-Id: I6d5ca2c14c5ea696a4d28eeb1fb384ff5671d161 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: make qt_hash_block_size() constexprMarc Mutz2023-02-251-1/+1
| | | | | | | | | | Because we can, and as a preparation for using it to determine the size of a fixed-size buffer for the HMAC's key material. Pick-to: 6.5 Change-Id: I4add1115ef6d649baab25a842e1238db8a98bb7d Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QCryptographicHash: auto-calculate MaxHashLengthMarc Mutz2023-02-251-10/+26
| | | | | | | | | | | | | | | | | | | | | Add Algorithm::NumAlgorithms and use it to iterate over all statically-available algorithms, querying their hashLengthInternal(). This avoids having to statically_assert(<= MaxHashLength) everywhere, and auto-adjusts the buffer size in SHA1_ONLY builds. Yes, the extra case labels for NumAlgorithms are a nuisance, but at least the compiler will remind us when we forget, unlike a missing static_cast(<= MaxHashLength) that might easily be forgotten. Adjust the test (which iterates over the QMetaEnum for QCryptographicHash::Algorithm, so finds NumAlgorithms and tries to pass it to the hash() function which responds with a Q_UNREACHABLE(). Only test hashLength() == 0 for that enum value. Pick-to: 6.5 Change-Id: I70155d2460464f0b2094e136eb6bea185effc9d5 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: make messageHash a QCryptographicHashPrivateMarc Mutz2023-02-241-2/+4
| | | | | | | | | | | | | | | | | | | | | ... avoiding one more memory allocation, and giving us access to QCryptographicHashPrivate::result, for use in subsequent commits. The only real adjustment to users of QMACPrivate::messageHash is that instead of messageHash.result(); they now need to use messageHash.finalizeUnchecked(); messageHash.resultView() // .toByteArray() I.e. explicitly finalize. Pick-to: 6.5 Change-Id: I80b1158b062554bbf8afa7241674a892de27f204 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QCryptographicHash: move addData(QIODevice*) to PrivateMarc Mutz2023-02-241-1/+7
| | | | | | | | To be reused in QMessageAuthenticationCode. Pick-to: 6.5 Change-Id: Ie4f003ad38ce9072cf6ee52ef2d7a63438e4d7ae Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: re-use messageHash for hashing the keyMarc Mutz2023-02-241-1/+11
| | | | | | | | | | | | With the OpenSSL3 code allocating state on the heap instead of in QCH::Private's inline union, reset() should be faster than even a static hash() call. Even in the non-OpenSSL3 case, using less QCH::Private objects to do the same thing means we increase effective data cache size. Pick-to: 6.5 Change-Id: I0b1347864081169a24c5d349702931afdab6c5bf Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QMessageAuthenticationCode: remove lazy initialization of messageHashMarc Mutz2023-02-231-12/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | This just complicates the code, for the small benefit of avoiding a messageHash seeding from an empty key that then has to be reset. This lazy initialization is in the way of using QCH's SmallByteArray for the key, which this author thinks is the more important optimization, because it will allow passing keys by QByteArrayView, removing the impedance mismatch between QMAC and QCH. Since the QMAC API doesn't distinguish between the absence of a key, and the presence of a null (ie. empty) key, we can't not call initMessageHash() when the key is empty, so we should suggest to pass the actual key to the constructor as often as possible, and use setKey() only to change the key afterwards. [ChangeLog][QtCore][QMessageAuthenticationCode] No longer delays processing of the key to the first setData() or result() call. While passing a default-constructed key to the constructor and then calling setKey() continues to work, for optimal performance, we suggest to pass the actual key as a constructor argument and call setKey() only to change the key. Pick-to: 6.5 Change-Id: If0a078f37a16f8306f77d2b2bd5dacf23ce5c3e2 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: make Private::method member constMarc Mutz2023-02-231-1/+1
| | | | | | | | | Like in QCryptographicHash, it's never re-set, so, like there, make it immutable. Pick-to: 6.5 Change-Id: I88f3dc15febffa8950256aedc5e8d1385fc86ddd Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* QMessageAuthenticationCode: use QCryptographicHashPrivate in finalizeUnchecked()Marc Mutz2023-02-231-2/+3
| | | | | | | | | | | | | ... basically inlining static QCH::hash(), which, however, accepts only one piece of data, while we have two. Avoids the memory allocation of the QCH d-pointer. The toByteArray() is now the only memory allocation left in finalizeUnchecked(), and will be removed in a subsequent commit. Pick-to: 6.5 Change-Id: I7549d6e1c116a4cdc29dac74b867dfa6647022a0 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>