12#include <private/qlocking_p.h>
18#include "../../3rdparty/sha1/sha1.cpp"
20#if defined(QT_BOOTSTRAPPED) && !defined(QT_CRYPTOGRAPHICHASH_ONLY_SHA1)
21# error "Are you sure you need the other hashing algorithms besides SHA-1?"
25#include "../../3rdparty/rfc6234/sha.h"
27#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
28#if !QT_CONFIG(openssl_hash)
30#include "../../3rdparty/md5/md5.h"
31#include "../../3rdparty/md5/md5.cpp"
32#include "../../3rdparty/md4/md4.h"
33#include "../../3rdparty/md4/md4.cpp"
43#include "../../3rdparty/sha3/KeccakSponge.c"
46#include "../../3rdparty/sha3/KeccakNISTInterface.c"
56#if Q_PROCESSOR_WORDSIZE == 8
58#include "../../3rdparty/sha3/KeccakF-1600-opt64.c"
66#include "../../3rdparty/sha3/KeccakF-1600-opt32.c"
90#include "../../3rdparty/rfc6234/sha224-256.c"
93#include "../../3rdparty/rfc6234/sha384-512.c"
107#include "qtcore-config_p.h"
109#if QT_CONFIG(system_libb2)
112#include "../../3rdparty/blake2/src/blake2b-ref.c"
113#include "../../3rdparty/blake2/src/blake2s-ref.c"
117#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(openssl_hash)
118#define USING_OPENSSL30
119#include <openssl/evp.h>
120#include <openssl/provider.h>
128 std::array<quint8, N> m_data;
129 static_assert(N <= std::numeric_limits<std::uint8_t>::max());
134 template <std::
size_t M, std::enable_if_t<M < N,
bool> = true>
135 constexpr QSmallByteArray(const QSmallByteArray<M> &other) noexcept
139 template <std::
size_t M, std::enable_if_t<M < N,
bool> = true>
140 constexpr QSmallByteArray &operator=(const QSmallByteArray<M> &other) noexcept
146 template <
typename Container>
147 constexpr void assign(
const Container &
c)
149 const size_t otherSize = size_t(std::size(
c));
151 memcpy(
data(), std::data(
c), otherSize);
152 m_size =
quint8(otherSize);
168 constexpr bool isEmpty() const noexcept {
return size() == 0; }
169 constexpr void clear() noexcept { m_size = 0; }
170 constexpr void resizeForOverwrite(
qsizetype s)
174 m_size = std::uint8_t(
s);
178 const auto oldSize =
size();
179 resizeForOverwrite(
s);
181 memset(
data() + oldSize,
v,
size() - oldSize);
186 constexpr auto begin() noexcept {
return data(); }
187 constexpr auto begin() const noexcept {
return data(); }
188 constexpr auto cbegin() const noexcept {
return begin(); }
189 constexpr auto end() noexcept {
return data() +
size(); }
190 constexpr auto end() const noexcept {
return data() +
size(); }
191 constexpr auto cend() const noexcept {
return end(); }
197#define CASE(Enum, Size) \
198 case QCryptographicHash:: Enum : \
202#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
205 CASE(Sha224, SHA224HashSize);
206 CASE(Sha256, SHA256HashSize);
207 CASE(Sha384, SHA384HashSize);
208 CASE(Sha512, SHA512HashSize);
209 CASE(Blake2s_128, 128 / 8);
243 for (
int i = 0;
i < A::NumAlgorithms; ++
i)
250#ifdef USING_OPENSSL30
254#define CASE(Enum, Name) \
255 case QCryptographicHash:: Enum : \
261 CASE(Sha224,
"SHA224");
262 CASE(Sha256,
"SHA256");
263 CASE(Sha384,
"SHA384");
264 CASE(Sha512,
"SHA512");
265 CASE(RealSha3_224,
"SHA3-224");
266 CASE(RealSha3_256,
"SHA3-256");
267 CASE(RealSha3_384,
"SHA3-384");
268 CASE(RealSha3_512,
"SHA3-512");
269 CASE(Keccak_224,
"SHA3-224");
270 CASE(Keccak_256,
"SHA3-256");
271 CASE(Keccak_384,
"SHA3-384");
272 CASE(Keccak_512,
"SHA3-512");
273 CASE(Blake2b_512,
"BLAKE2B512");
274 CASE(Blake2s_256,
"BLAKE2S256");
276 default:
return nullptr;
307 void reset() noexcept;
310 void finalize() noexcept;
313 void finalizeUnchecked() noexcept;
318#ifdef USING_OPENSSL30
319 struct EVP_MD_CTX_deleter {
320 void operator()(EVP_MD_CTX *
ctx)
const noexcept {
321 EVP_MD_CTX_free(
ctx);
324 struct EVP_MD_deleter {
325 void operator()(EVP_MD *md)
const noexcept {
329 using EVP_MD_CTX_ptr = std::unique_ptr<EVP_MD_CTX, EVP_MD_CTX_deleter>;
330 using EVP_MD_ptr = std::unique_ptr<EVP_MD, EVP_MD_deleter>;
332 EVP_MD_ptr algorithm;
334 bool initializationFailed;
337 void reset() noexcept;
345#ifdef USING_OPENSSL30
354#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
355#ifdef USING_OPENSSL30
380#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
381#ifndef USING_OPENSSL30
405 static const unsigned char sha3FinalSuffix = 0x80;
407 result.resizeForOverwrite(bitCount / 8);
411 switch (sha3Variant) {
547#ifdef USING_OPENSSL30
579 : initializationFailed{true}
586 if (!OSSL_PROVIDER_load(
nullptr,
"legacy"))
588 if (!OSSL_PROVIDER_load(
nullptr,
"default"))
592 context = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
603 algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(
method),
"-fips"));
608 initializationFailed = !EVP_DigestInit_ex(
context.get(),
algorithm.get(),
nullptr);
619#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
621 Q_ASSERT_X(
false,
"QCryptographicHash",
"Method not compiled in");
674 static_assert(std::is_trivially_destructible_v<State>);
684#ifdef USING_OPENSSL30
701void QCryptographicHashPrivate::EVP::reset() noexcept
703 if (!initializationFailed) {
707 EVP_MD_CTX_reset(
context.get());
708 initializationFailed = !EVP_DigestInit_ex(
context.get(), algorithm.get(),
nullptr);
719 sha1InitState(&sha1Context);
721#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
723 Q_ASSERT_X(
false,
"QCryptographicHash",
"Method not compiled in");
728 md4_init(&md4Context);
731 MD5Init(&md5Context);
734 SHA224Reset(&sha224Context);
737 SHA256Reset(&sha256Context);
740 SHA384Reset(&sha384Context);
743 SHA512Reset(&sha512Context);
775#if QT_DEPRECATED_SINCE(6, 4)
807#ifdef USING_OPENSSL30
812 const char *
data = bytes.data();
813 auto length = bytes.size();
819 blake2b_update(&blake2bContext,
reinterpret_cast<const uint8_t *
>(
data),
length);
823 blake2s_update(&blake2sContext,
reinterpret_cast<const uint8_t *
>(
data),
length);
824 }
else if (!evp.initializationFailed) {
825 EVP_DigestUpdate(evp.context.get(), (
const unsigned char *)
data,
length);
835 const char *
data = bytes.data();
836 auto length = bytes.size();
838#if QT_POINTER_SIZE == 8
849 sha1Update(&sha1Context, (
const unsigned char *)
data,
length);
851#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
853 Q_ASSERT_X(
false,
"QCryptographicHash",
"Method not compiled in");
858 md4_update(&md4Context, (
const unsigned char *)
data,
length);
861 MD5Update(&md5Context, (
const unsigned char *)
data,
length);
864 SHA224Input(&sha224Context,
reinterpret_cast<const unsigned char *
>(
data),
length);
867 SHA256Input(&sha256Context,
reinterpret_cast<const unsigned char *
>(
data),
length);
870 SHA384Input(&sha384Context,
reinterpret_cast<const unsigned char *
>(
data),
length);
873 SHA512Input(&sha512Context,
reinterpret_cast<const unsigned char *
>(
data),
length);
889 blake2b_update(&blake2bContext,
reinterpret_cast<const uint8_t *
>(
data),
length);
895 blake2s_update(&blake2sContext,
reinterpret_cast<const uint8_t *
>(
data),
length);
917 if (!
device->isReadable())
958 return d->resultView();
986#ifdef USING_OPENSSL30
994 blake2b_state
copy = blake2bContext;
1001 blake2s_state
copy = blake2sContext;
1005 evp.finalizeUnchecked(
result);
1009void QCryptographicHashPrivate::EVP::finalizeUnchecked(
HashResult &
result)
noexcept
1011 if (!initializationFailed) {
1012 EVP_MD_CTX_ptr
copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
1014 result.resizeForOverwrite(EVP_MD_get_size(algorithm.get()));
1015 EVP_DigestFinal_ex(
copy.get(),
result.data(),
nullptr);
1026 Sha1State
copy = sha1Context;
1027 result.resizeForOverwrite(20);
1028 sha1FinalizeState(&
copy);
1032#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
1034 Q_ASSERT_X(
false,
"QCryptographicHash",
"Method not compiled in");
1039 md4_context
copy = md4Context;
1040 result.resizeForOverwrite(MD4_RESULTLEN);
1045 MD5Context
copy = md5Context;
1046 result.resizeForOverwrite(16);
1051 SHA224Context
copy = sha224Context;
1052 result.resizeForOverwrite(SHA224HashSize);
1057 SHA256Context
copy = sha256Context;
1058 result.resizeForOverwrite(SHA256HashSize);
1063 SHA384Context
copy = sha384Context;
1064 result.resizeForOverwrite(SHA384HashSize);
1069 SHA512Context
copy = sha512Context;
1070 result.resizeForOverwrite(SHA512HashSize);
1093 blake2b_state
copy = blake2bContext;
1103 blake2s_state
copy = blake2sContext;
1125 hash.finalizeUnchecked();
1126 return hash.resultView().toByteArray();
1159#ifdef USING_OPENSSL30
1163 if (useNonOpenSSLFallback(
method))
1166 OSSL_PROVIDER_load(
nullptr,
"legacy");
1167 OSSL_PROVIDER_load(
nullptr,
"default");
1169 const char *restriction =
"-fips";
1170 EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(
method), restriction));
1172 return algorithm !=
nullptr;
1176#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
1211 return SHA1_Message_Block_Size;
1212#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
1218 return SHA224_Message_Block_Size;
1220 return SHA256_Message_Block_Size;
1222 return SHA384_Message_Block_Size;
1224 return SHA512_Message_Block_Size;
1241 return BLAKE2B_BLOCKBYTES;
1246 return BLAKE2S_BLOCKBYTES;
1249#if !defined(Q_GCC_ONLY) || Q_CC_GCC >= 900
1262 for (
int i = 0;
i < A::NumAlgorithms ; ++
i)
1272 for (
int i = 0;
i < A::NumAlgorithms ; ++
i)
1282 for (
int i = 0;
i < A::NumAlgorithms ; ++
i)
1296 result.resizeForOverwrite(block.size());
1314 void initMessageHash() noexcept;
1341 for (
int i = 0;
i < A::NumAlgorithms; ++
i) {
1346 }(),
"this code assumes that a hash's result always fits into that hash's block size");
1348 messageHash.
reset();
1505 d->messageHash.reset();
1527 d->messageHash.addData(
data);
1604#ifndef QT_NO_QOBJECT
1605#include "moc_qcryptographichash.cpp"
IOBluetoothDevice * device
QByteArray toByteArray() const
const QCryptographicHash::Algorithm method
QBasicMutex finalizeMutex
void addData(QByteArrayView bytes) noexcept
~QCryptographicHashPrivate()
QCryptographicHashPrivate(QCryptographicHash::Algorithm method) noexcept
void finalizeUnchecked() noexcept
union QCryptographicHashPrivate::State state
QByteArrayView resultView() const noexcept
static bool supportsAlgorithm(QCryptographicHash::Algorithm method)
~QCryptographicHash()
Destroys the object.
static int hashLength(Algorithm method)
Returns the size of the output of the selected hash method in bytes.
QByteArrayView resultView() const noexcept
static QByteArray hash(QByteArrayView data, Algorithm method)
Returns the hash of data using method.
static bool supportsAlgorithm(Algorithm method)
Returns whether the selected algorithm method is supported and if result() will return a value when t...
void addData(QByteArrayView data) noexcept
Adds the characters in bytes to the cryptographic hash.
QByteArray result() const
Returns the final hash value.
QCryptographicHash(Algorithm method)
Constructs an object that can be used to create a cryptographic hash from data using method.
void reset() noexcept
Resets the object.
Algorithm algorithm() const noexcept
Returns the algorithm used to generate the cryptographic hash.
\inmodule QtCore \reentrant
void initMessageHash() noexcept
QMessageAuthenticationCodePrivate(QCryptographicHash::Algorithm m)
void setKey(QByteArrayView k) noexcept
QCryptographicHashPrivate messageHash
void finalizeUnchecked() noexcept
~QMessageAuthenticationCode()
Destroys the object.
void setKey(QByteArrayView key) noexcept
Sets secret key.
QByteArrayView resultView() const noexcept
void addData(const char *data, qsizetype length)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QByteArray result() const
Returns the final authentication code.
void reset() noexcept
Resets message data.
static QByteArray hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
Returns the authentication code for the message message using the key key and the method method.
QMessageAuthenticationCode(QCryptographicHash::Algorithm method, QByteArrayView key={})
QSmallByteArray()=default
QHash< int, QWidget * > hash
[35multi]
const PluginKeyMapConstIterator cend
Combined button and popup list for selecting options.
static jboolean copy(JNIEnv *, jobject)
static int SHA224_256AddLength(SHA256Context *context, unsigned int length)
static constexpr int hashLengthInternal(QCryptographicHash::Algorithm method) noexcept
constexpr int maxHashBlockSize()
static int SHA384_512AddLength(SHA512Context *context, unsigned int length)
static Q_CONSTINIT SHA3Update *const sha3Update
static constexpr int qt_hash_block_size(QCryptographicHash::Algorithm method)
static HashBlock xored(const HashBlock &block, quint8 val) noexcept
static constexpr int maxHashLength()
constexpr int minHashBlockSize()
constexpr int gcdHashBlockSize()
static Q_CONSTINIT SHA3Init *const sha3Init
unsigned long long DataLength
HashReturn() SHA3Update(hashState *state, const BitSequence *data, DataLength databitlen)
HashReturn() SHA3Final(hashState *state, BitSequence *hashval)
unsigned char BitSequence
HashReturn() SHA3Init(hashState *state, int hashbitlen)
static Q_CONSTINIT SHA3Final *const sha3Final
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char * method
constexpr const T & qMin(const T &a, const T &b)
GLsizei const GLfloat * v
[13]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei length
GLuint GLsizei const GLchar * message
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT_X(cond, x, msg)
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
blake2b_state blake2bContext
SHA384Context sha384Context
SHA224Context sha224Context
void addData(QCryptographicHash::Algorithm method, QByteArrayView data) noexcept
void sha3Finish(HashResult &result, int bitCount, Sha3Variant sha3Variant)
SHA512Context sha512Context
void finalizeUnchecked(QCryptographicHash::Algorithm method, HashResult &result) noexcept
SHA256Context sha256Context
void destroy(QCryptographicHash::Algorithm method)
blake2s_state blake2sContext
State(QCryptographicHash::Algorithm method)
void reset(QCryptographicHash::Algorithm method) noexcept