7#include <qcryptographichash.h>
8#include <qmessageauthenticationcode.h>
10#include <QtCore/private/qsmallbytearray_p.h>
13#include <private/qlocking_p.h>
19#include "../../3rdparty/sha1/sha1.cpp"
22#include "../../3rdparty/rfc6234/sha.h"
24#if !QT_CONFIG(openssl_hash)
25#include "../../3rdparty/md5/md5.h"
26#include "../../3rdparty/md5/md5.cpp"
27#include "../../3rdparty/md4/md4.h"
28#include "../../3rdparty/md4/md4.cpp"
39#include "../../3rdparty/sha3/KeccakSponge.c"
42#include "../../3rdparty/sha3/KeccakNISTInterface.c"
45
46
48typedef HashReturn (SHA3Init)(
hashState *state,
int hashbitlen);
52#if Q_PROCESSOR_WORDSIZE == 8
54#include "../../3rdparty/sha3/KeccakF-1600-opt64.c"
56Q_CONSTINIT
static SHA3Init *
const sha3Init = Init;
57Q_CONSTINIT
static SHA3Update *
const sha3Update = Update;
58Q_CONSTINIT
static SHA3Final *
const sha3Final = Final;
62#include "../../3rdparty/sha3/KeccakF-1600-opt32.c"
64Q_CONSTINIT
static SHA3Init *
const sha3Init = Init;
65Q_CONSTINIT
static SHA3Update *
const sha3Update = Update;
66Q_CONSTINIT
static SHA3Final *
const sha3Final = Final;
70#if !QT_CONFIG(openssl_hash)
72
73
74
75
76
77
78
79
80
81static int SHA224_256AddLength(SHA256Context *context,
unsigned int length);
82static int SHA384_512AddLength(SHA512Context *context,
unsigned int length);
87#include "../../3rdparty/rfc6234/sha224-256.c"
90#include "../../3rdparty/rfc6234/sha384-512.c"
92static inline int SHA224_256AddLength(SHA256Context *context,
unsigned int length)
95 return SHA224_256AddLengthM(context, length);
97static inline int SHA384_512AddLength(SHA512Context *context,
unsigned int length)
100 return SHA384_512AddLengthM(context, length);
104#include "qtcore-config_p.h"
106#if QT_CONFIG(system_libb2)
110QT_WARNING_DISABLE_CLANG(
"-Wunused-function")
111QT_WARNING_DISABLE_GCC(
"-Wunused-function")
112QT_WARNING_DISABLE_MSVC(4505)
113#include "../../3rdparty/blake2/src/blake2b-ref.c"
114#include "../../3rdparty/blake2/src/blake2s-ref.c"
118#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(openssl_hash)
119#define USING_OPENSSL30
120#include <openssl/evp.h>
121#include <openssl/provider.h>
129#define CASE(Enum, Size)
130 case QCryptographicHash:: Enum :
136 CASE(Sha224, SHA224HashSize);
137 CASE(Sha256, SHA256HashSize);
138 CASE(Sha384, SHA384HashSize);
139 CASE(Sha512, SHA512HashSize);
140 CASE(Blake2s_128, 128 / 8);
141 case QCryptographicHash::Blake2b_160:
142 case QCryptographicHash::Blake2s_160:
144 case QCryptographicHash::RealSha3_224:
145 case QCryptographicHash::Keccak_224:
146 case QCryptographicHash::Blake2s_224:
148 case QCryptographicHash::RealSha3_256:
149 case QCryptographicHash::Keccak_256:
150 case QCryptographicHash::Blake2b_256:
151 case QCryptographicHash::Blake2s_256:
153 case QCryptographicHash::RealSha3_384:
154 case QCryptographicHash::Keccak_384:
155 case QCryptographicHash::Blake2b_384:
157 case QCryptographicHash::RealSha3_512:
158 case QCryptographicHash::Keccak_512:
159 case QCryptographicHash::Blake2b_512:
162 case QCryptographicHash::NumAlgorithms: ;
172 using A = QCryptographicHash::Algorithm;
173 for (
int i = 0; i < A::NumAlgorithms; ++i)
174 result = std::max(result, hashLengthInternal(A(i)));
178using HashResult = QSmallByteArray<maxHashLength()>;
180#ifdef USING_OPENSSL30
181static constexpr const char * methodToName(QCryptographicHash::Algorithm method)
noexcept
184#define CASE(Enum, Name)
185 case QCryptographicHash:: Enum :
191 CASE(Sha224,
"SHA224");
192 CASE(Sha256,
"SHA256");
193 CASE(Sha384,
"SHA384");
194 CASE(Sha512,
"SHA512");
195 CASE(RealSha3_224,
"SHA3-224");
196 CASE(RealSha3_256,
"SHA3-256");
197 CASE(RealSha3_384,
"SHA3-384");
198 CASE(RealSha3_512,
"SHA3-512");
199 CASE(Blake2b_512,
"BLAKE2B512");
200 CASE(Blake2s_256,
"BLAKE2S256");
202 CASE(Keccak_224,
nullptr);
203 CASE(Keccak_256,
nullptr);
204 CASE(Keccak_384,
nullptr);
205 CASE(Keccak_512,
nullptr);
206 CASE(Blake2b_160,
nullptr);
207 CASE(Blake2b_256,
nullptr);
208 CASE(Blake2b_384,
nullptr);
209 CASE(Blake2s_128,
nullptr);
210 CASE(Blake2s_160,
nullptr);
211 CASE(Blake2s_224,
nullptr);
212 CASE(NumAlgorithms,
nullptr);
228 state.destroy(method);
242#ifdef USING_OPENSSL30
270 void reset()
noexcept;
278#ifdef USING_OPENSSL30
287#ifdef USING_OPENSSL30
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334 static const unsigned char sha3FinalSuffix = 0x80;
336 switch (sha3Variant) {
338 sha3Update(&ctx,
reinterpret_cast<
const BitSequence *>(&sha3FinalSuffix), 2);
344 sha3Final(&ctx, result.data());
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
404
405
406QCryptographicHash::QCryptographicHash(Algorithm method)
407 : d(
new QCryptographicHashPrivate{method})
412
413
414
415
416
417
418
419
420
421
424
425
426QCryptographicHash::~QCryptographicHash()
432
433
434
435
436
437
438
439
440
441
444
445
446
447
450
451
452void QCryptographicHash::reset()
noexcept
458
459
460
461
462QCryptographicHash::Algorithm QCryptographicHash::algorithm()
const noexcept
467#ifdef USING_OPENSSL30
469QCryptographicHashPrivate::State::State(QCryptographicHash::Algorithm method)
472 case QCryptographicHash::Keccak_224:
473 case QCryptographicHash::Keccak_256:
474 case QCryptographicHash::Keccak_384:
475 case QCryptographicHash::Keccak_512:
476 new (&sha3Context) SHA3Context;
479 case QCryptographicHash::Blake2b_160:
480 case QCryptographicHash::Blake2b_256:
481 case QCryptographicHash::Blake2b_384:
482 new (&blake2bContext) blake2b_state;
485 case QCryptographicHash::Blake2s_128:
486 case QCryptographicHash::Blake2s_160:
487 case QCryptographicHash::Blake2s_224:
488 new (&blake2sContext) blake2s_state;
491 case QCryptographicHash::Sha1:
492 case QCryptographicHash::Md4:
493 case QCryptographicHash::Md5:
494 case QCryptographicHash::Sha224:
495 case QCryptographicHash::Sha256:
496 case QCryptographicHash::Sha384:
497 case QCryptographicHash::Sha512:
498 case QCryptographicHash::RealSha3_224:
499 case QCryptographicHash::RealSha3_256:
500 case QCryptographicHash::RealSha3_384:
501 case QCryptographicHash::RealSha3_512:
502 case QCryptographicHash::Blake2b_512:
503 case QCryptographicHash::Blake2s_256:
504 new (&evp) EVP(method);
506 case QCryptographicHash::NumAlgorithms:
511void QCryptographicHashPrivate::State::destroy(QCryptographicHash::Algorithm method)
514 case QCryptographicHash::Keccak_224:
515 case QCryptographicHash::Keccak_256:
516 case QCryptographicHash::Keccak_384:
517 case QCryptographicHash::Keccak_512:
518 case QCryptographicHash::Blake2b_160:
519 case QCryptographicHash::Blake2b_256:
520 case QCryptographicHash::Blake2b_384:
521 case QCryptographicHash::Blake2s_128:
522 case QCryptographicHash::Blake2s_160:
523 case QCryptographicHash::Blake2s_224:
525 case QCryptographicHash::Sha1:
526 case QCryptographicHash::Md4:
527 case QCryptographicHash::Md5:
528 case QCryptographicHash::Sha224:
529 case QCryptographicHash::Sha256:
530 case QCryptographicHash::Sha384:
531 case QCryptographicHash::Sha512:
532 case QCryptographicHash::RealSha3_224:
533 case QCryptographicHash::RealSha3_256:
534 case QCryptographicHash::RealSha3_384:
535 case QCryptographicHash::RealSha3_512:
536 case QCryptographicHash::Blake2b_512:
537 case QCryptographicHash::Blake2s_256:
540 case QCryptographicHash::NumAlgorithms:
545QCryptographicHashPrivate::EVP::EVP(QCryptographicHash::Algorithm method)
546 : initializationFailed{
true}
548 if (method == QCryptographicHash::Md4) {
550
551
552
553 legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"legacy"));
559 defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"default"));
560 if (!defaultProvider)
563 context = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
570
571
572
573
574 algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method),
"-fips"));
579 initializationFailed = !EVP_DigestInit_ex(context.get(), algorithm.get(),
nullptr);
587 case QCryptographicHash::Sha1:
588 new (&sha1Context) Sha1State;
590 case QCryptographicHash::Md4:
591 new (&md4Context) md4_context;
593 case QCryptographicHash::Md5:
594 new (&md5Context) MD5Context;
596 case QCryptographicHash::Sha224:
599 case QCryptographicHash::Sha256:
602 case QCryptographicHash::Sha384:
605 case QCryptographicHash::Sha512:
608 case QCryptographicHash::RealSha3_224:
609 case QCryptographicHash::Keccak_224:
610 case QCryptographicHash::RealSha3_256:
611 case QCryptographicHash::Keccak_256:
612 case QCryptographicHash::RealSha3_384:
613 case QCryptographicHash::Keccak_384:
614 case QCryptographicHash::RealSha3_512:
615 case QCryptographicHash::Keccak_512:
618 case QCryptographicHash::Blake2b_160:
619 case QCryptographicHash::Blake2b_256:
620 case QCryptographicHash::Blake2b_384:
621 case QCryptographicHash::Blake2b_512:
622 new (&blake2bContext) blake2b_state;
624 case QCryptographicHash::Blake2s_128:
625 case QCryptographicHash::Blake2s_160:
626 case QCryptographicHash::Blake2s_224:
627 case QCryptographicHash::Blake2s_256:
628 new (&blake2sContext) blake2s_state;
630 case QCryptographicHash::NumAlgorithms:
638 static_assert(std::is_trivially_destructible_v<State>);
648#ifdef USING_OPENSSL30
650void QCryptographicHashPrivate::State::reset(QCryptographicHash::Algorithm method)
noexcept
653 case QCryptographicHash::Keccak_224:
654 case QCryptographicHash::Keccak_256:
655 case QCryptographicHash::Keccak_384:
656 case QCryptographicHash::Keccak_512:
657 sha3Init(&sha3Context, hashLengthInternal(method) * 8);
659 case QCryptographicHash::Blake2b_160:
660 case QCryptographicHash::Blake2b_256:
661 case QCryptographicHash::Blake2b_384:
662 blake2b_init(&blake2bContext, hashLengthInternal(method));
664 case QCryptographicHash::Blake2s_128:
665 case QCryptographicHash::Blake2s_160:
666 case QCryptographicHash::Blake2s_224:
667 blake2s_init(&blake2sContext, hashLengthInternal(method));
669 case QCryptographicHash::Sha1:
670 case QCryptographicHash::Md4:
671 case QCryptographicHash::Md5:
672 case QCryptographicHash::Sha224:
673 case QCryptographicHash::Sha256:
674 case QCryptographicHash::Sha384:
675 case QCryptographicHash::Sha512:
676 case QCryptographicHash::RealSha3_224:
677 case QCryptographicHash::RealSha3_256:
678 case QCryptographicHash::RealSha3_384:
679 case QCryptographicHash::RealSha3_512:
680 case QCryptographicHash::Blake2b_512:
681 case QCryptographicHash::Blake2s_256:
684 case QCryptographicHash::NumAlgorithms:
689void QCryptographicHashPrivate::EVP::reset()
noexcept
691 if (!initializationFailed) {
695 EVP_MD_CTX_reset(context.get());
696 initializationFailed = !EVP_DigestInit_ex(context.get(), algorithm.get(),
nullptr);
706 case QCryptographicHash::Sha1:
707 sha1InitState(&sha1Context);
709 case QCryptographicHash::Md4:
710 md4_init(&md4Context);
712 case QCryptographicHash::Md5:
713 MD5Init(&md5Context);
715 case QCryptographicHash::Sha224:
718 case QCryptographicHash::Sha256:
721 case QCryptographicHash::Sha384:
724 case QCryptographicHash::Sha512:
727 case QCryptographicHash::RealSha3_224:
728 case QCryptographicHash::Keccak_224:
729 case QCryptographicHash::RealSha3_256:
730 case QCryptographicHash::Keccak_256:
731 case QCryptographicHash::RealSha3_384:
732 case QCryptographicHash::Keccak_384:
733 case QCryptographicHash::RealSha3_512:
734 case QCryptographicHash::Keccak_512:
735 sha3Init(&sha3Context, hashLengthInternal(method) * 8);
737 case QCryptographicHash::Blake2b_160:
738 case QCryptographicHash::Blake2b_256:
739 case QCryptographicHash::Blake2b_384:
740 case QCryptographicHash::Blake2b_512:
741 blake2b_init(&blake2bContext, hashLengthInternal(method));
743 case QCryptographicHash::Blake2s_128:
744 case QCryptographicHash::Blake2s_160:
745 case QCryptographicHash::Blake2s_224:
746 case QCryptographicHash::Blake2s_256:
747 blake2s_init(&blake2sContext, hashLengthInternal(method));
749 case QCryptographicHash::NumAlgorithms:
756#if QT_DEPRECATED_SINCE(6
, 4
)
758
759
760
761
762
763
764void QCryptographicHash::addData(
const char *data, qsizetype length)
766 Q_ASSERT(length >= 0);
767 addData(QByteArrayView{data, length});
772
773
774
775
776
777void QCryptographicHash::addData(QByteArrayView bytes)
noexcept
784 state.addData(method, bytes);
788#ifdef USING_OPENSSL30
790void QCryptographicHashPrivate::State::addData(QCryptographicHash::Algorithm method,
791 QByteArrayView bytes)
noexcept
793 const char *data = bytes.data();
794 auto length = bytes.size();
797 case QCryptographicHash::Keccak_224:
798 case QCryptographicHash::Keccak_256:
799 case QCryptographicHash::Keccak_384:
800 case QCryptographicHash::Keccak_512:
801 sha3Update(&sha3Context,
reinterpret_cast<
const BitSequence *>(data), uint64_t(length) * 8);
803 case QCryptographicHash::Blake2b_160:
804 case QCryptographicHash::Blake2b_256:
805 case QCryptographicHash::Blake2b_384:
806 blake2b_update(&blake2bContext,
reinterpret_cast<
const uint8_t *>(data), length);
808 case QCryptographicHash::Blake2s_128:
809 case QCryptographicHash::Blake2s_160:
810 case QCryptographicHash::Blake2s_224:
811 blake2s_update(&blake2sContext,
reinterpret_cast<
const uint8_t *>(data), length);
813 case QCryptographicHash::Sha1:
814 case QCryptographicHash::Md4:
815 case QCryptographicHash::Md5:
816 case QCryptographicHash::Sha224:
817 case QCryptographicHash::Sha256:
818 case QCryptographicHash::Sha384:
819 case QCryptographicHash::Sha512:
820 case QCryptographicHash::RealSha3_224:
821 case QCryptographicHash::RealSha3_256:
822 case QCryptographicHash::RealSha3_384:
823 case QCryptographicHash::RealSha3_512:
824 case QCryptographicHash::Blake2b_512:
825 case QCryptographicHash::Blake2s_256:
826 if (!evp.initializationFailed)
827 EVP_DigestUpdate(evp.context.get(), (
const unsigned char *)data, length);
829 case QCryptographicHash::NumAlgorithms:
837 QByteArrayView bytes)
noexcept
839 const char *data = bytes.data();
840 auto length = bytes.size();
842#if QT_POINTER_SIZE == 8
846 for (
auto remaining = length; remaining; remaining -= length, data += length) {
847 length = qMin(qsizetype(std::numeric_limits<uint>::max()), remaining);
852 case QCryptographicHash::Sha1:
853 sha1Update(&sha1Context, (
const unsigned char *)data, length);
855 case QCryptographicHash::Md4:
856 md4_update(&md4Context, (
const unsigned char *)data, length);
858 case QCryptographicHash::Md5:
859 MD5Update(&md5Context, (
const unsigned char *)data, length);
861 case QCryptographicHash::Sha224:
862 SHA224Input(&
sha224Context,
reinterpret_cast<
const unsigned char *>(data), length);
864 case QCryptographicHash::Sha256:
865 SHA256Input(&
sha256Context,
reinterpret_cast<
const unsigned char *>(data), length);
867 case QCryptographicHash::Sha384:
868 SHA384Input(&
sha384Context,
reinterpret_cast<
const unsigned char *>(data), length);
870 case QCryptographicHash::Sha512:
871 SHA512Input(&
sha512Context,
reinterpret_cast<
const unsigned char *>(data), length);
873 case QCryptographicHash::RealSha3_224:
874 case QCryptographicHash::Keccak_224:
875 case QCryptographicHash::RealSha3_256:
876 case QCryptographicHash::Keccak_256:
877 case QCryptographicHash::RealSha3_384:
878 case QCryptographicHash::Keccak_384:
879 case QCryptographicHash::RealSha3_512:
880 case QCryptographicHash::Keccak_512:
883 case QCryptographicHash::Blake2b_160:
884 case QCryptographicHash::Blake2b_256:
885 case QCryptographicHash::Blake2b_384:
886 case QCryptographicHash::Blake2b_512:
887 blake2b_update(&blake2bContext,
reinterpret_cast<
const uint8_t *>(data), length);
889 case QCryptographicHash::Blake2s_128:
890 case QCryptographicHash::Blake2s_160:
891 case QCryptographicHash::Blake2s_224:
892 case QCryptographicHash::Blake2s_256:
893 blake2s_update(&blake2sContext,
reinterpret_cast<
const uint8_t *>(data), length);
895 case QCryptographicHash::NumAlgorithms:
903
904
905
906
907bool QCryptographicHash::addData(QIODevice *device)
909 return d->addData(device);
914 if (!device->isReadable())
917 if (!device->isOpen())
923 while ((length = device->read(buffer,
sizeof(buffer))) > 0)
926 return device->atEnd();
931
932
933
934
935QByteArray QCryptographicHash::result()
const
937 return resultView().toByteArray();
941
942
943
944
945
946
947
948
949
950QByteArrayView QCryptographicHash::resultView()
const noexcept
955 return d->resultView();
959
960
961
962
965 const auto lock = qt_scoped_lock(finalizeMutex);
967 if (!result.isEmpty())
973
974
975
976
977
980 result.resizeForOverwrite(hashLengthInternal(method));
981 state.finalizeUnchecked(method, result);
984#ifdef USING_OPENSSL30
985void QCryptographicHashPrivate::State::finalizeUnchecked(QCryptographicHash::Algorithm method,
986 HashResult &result)
noexcept
989 case QCryptographicHash::Keccak_224:
990 case QCryptographicHash::Keccak_256:
991 case QCryptographicHash::Keccak_384:
992 case QCryptographicHash::Keccak_512: {
993 SHA3Context copy = sha3Context;
994 sha3Finish(copy, result, Sha3Variant::Keccak);
997 case QCryptographicHash::Blake2b_160:
998 case QCryptographicHash::Blake2b_256:
999 case QCryptographicHash::Blake2b_384: {
1000 const auto length = hashLengthInternal(method);
1001 blake2b_state copy = blake2bContext;
1002 blake2b_final(©, result.data(), length);
1005 case QCryptographicHash::Blake2s_128:
1006 case QCryptographicHash::Blake2s_160:
1007 case QCryptographicHash::Blake2s_224: {
1008 const auto length = hashLengthInternal(method);
1009 blake2s_state copy = blake2sContext;
1010 blake2s_final(©, result.data(), length);
1013 case QCryptographicHash::Sha1:
1014 case QCryptographicHash::Md4:
1015 case QCryptographicHash::Md5:
1016 case QCryptographicHash::Sha224:
1017 case QCryptographicHash::Sha256:
1018 case QCryptographicHash::Sha384:
1019 case QCryptographicHash::Sha512:
1020 case QCryptographicHash::RealSha3_224:
1021 case QCryptographicHash::RealSha3_256:
1022 case QCryptographicHash::RealSha3_384:
1023 case QCryptographicHash::RealSha3_512:
1024 case QCryptographicHash::Blake2b_512:
1025 case QCryptographicHash::Blake2s_256:
1026 evp.finalizeUnchecked(result);
1028 case QCryptographicHash::NumAlgorithms:
1033void QCryptographicHashPrivate::EVP::finalizeUnchecked(HashResult &result)
noexcept
1035 if (!initializationFailed) {
1036 EVP_MD_CTX_ptr copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
1037 EVP_MD_CTX_copy_ex(copy.get(), context.get());
1038 Q_ASSERT(result.size() == EVP_MD_get_size(algorithm.get()));
1039 EVP_DigestFinal_ex(copy.get(), result.data(),
nullptr);
1046 HashResult &result)
noexcept
1049 case QCryptographicHash::Sha1: {
1050 Sha1State copy = sha1Context;
1051 sha1FinalizeState(©);
1052 sha1ToHash(©, result.data());
1055 case QCryptographicHash::Md4: {
1056 md4_context copy = md4Context;
1057 md4_final(©, result.data());
1060 case QCryptographicHash::Md5: {
1061 MD5Context copy = md5Context;
1062 MD5Final(©, result.data());
1065 case QCryptographicHash::Sha224: {
1067 SHA224Result(©, result.data());
1070 case QCryptographicHash::Sha256: {
1072 SHA256Result(©, result.data());
1075 case QCryptographicHash::Sha384: {
1077 SHA384Result(©, result.data());
1080 case QCryptographicHash::Sha512: {
1082 SHA512Result(©, result.data());
1085 case QCryptographicHash::RealSha3_224:
1086 case QCryptographicHash::RealSha3_256:
1087 case QCryptographicHash::RealSha3_384:
1088 case QCryptographicHash::RealSha3_512: {
1090 sha3Finish(copy, result, Sha3Variant::Sha3);
1093 case QCryptographicHash::Keccak_224:
1094 case QCryptographicHash::Keccak_256:
1095 case QCryptographicHash::Keccak_384:
1096 case QCryptographicHash::Keccak_512: {
1098 sha3Finish(copy, result, Sha3Variant::Keccak);
1101 case QCryptographicHash::Blake2b_160:
1102 case QCryptographicHash::Blake2b_256:
1103 case QCryptographicHash::Blake2b_384:
1104 case QCryptographicHash::Blake2b_512: {
1105 const auto length = hashLengthInternal(method);
1106 blake2b_state copy = blake2bContext;
1107 blake2b_final(©, result.data(), length);
1110 case QCryptographicHash::Blake2s_128:
1111 case QCryptographicHash::Blake2s_160:
1112 case QCryptographicHash::Blake2s_224:
1113 case QCryptographicHash::Blake2s_256: {
1114 const auto length = hashLengthInternal(method);
1115 blake2s_state copy = blake2sContext;
1116 blake2s_final(©, result.data(), length);
1119 case QCryptographicHash::NumAlgorithms:
1126
1127
1128
1129
1130
1131
1132
1133QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method)
1135 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1136 [[maybe_unused]]
const auto r = hashInto(ba, data, method);
1137 Q_ASSERT(r.size() == ba.size());
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer,
1160 QSpan<
const QByteArrayView> data,
1161 Algorithm method)
noexcept
1163 if (buffer.size() < hashLengthInternal(method))
1166 QCryptographicHashPrivate hash(method);
1167 for (QByteArrayView part : data)
1169 hash.finalizeUnchecked();
1170 auto result = hash.resultView();
1171 Q_ASSERT(buffer.size() >= result.size());
1173 memcpy(buffer.data(), result.data(), result.size());
1174 return buffer.first(result.size());
1178
1179
1180
1181
1182int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
1184 return hashLengthInternal(method);
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1200bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)
1202 return QCryptographicHashPrivate::supportsAlgorithm(method);
1205#ifdef USING_OPENSSL30
1206bool QCryptographicHashPrivate::supportsAlgorithm(QCryptographicHash::Algorithm method)
1212 case QCryptographicHash::Keccak_224:
1213 case QCryptographicHash::Keccak_256:
1214 case QCryptographicHash::Keccak_384:
1215 case QCryptographicHash::Keccak_512:
1216 case QCryptographicHash::Blake2b_160:
1217 case QCryptographicHash::Blake2b_256:
1218 case QCryptographicHash::Blake2b_384:
1219 case QCryptographicHash::Blake2s_128:
1220 case QCryptographicHash::Blake2s_160:
1221 case QCryptographicHash::Blake2s_224:
1223 case QCryptographicHash::Sha1:
1224 case QCryptographicHash::Md4:
1225 case QCryptographicHash::Md5:
1226 case QCryptographicHash::Sha224:
1227 case QCryptographicHash::Sha256:
1228 case QCryptographicHash::Sha384:
1229 case QCryptographicHash::Sha512:
1230 case QCryptographicHash::RealSha3_224:
1231 case QCryptographicHash::RealSha3_256:
1232 case QCryptographicHash::RealSha3_384:
1233 case QCryptographicHash::RealSha3_512:
1234 case QCryptographicHash::Blake2b_512:
1235 case QCryptographicHash::Blake2s_256: {
1236 auto legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"legacy"));
1237 auto defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"default"));
1239 const char *restriction =
"-fips";
1240 EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method), restriction));
1242 return algorithm !=
nullptr;
1245 case QCryptographicHash::NumAlgorithms:
1255 case QCryptographicHash::Sha1:
1256 case QCryptographicHash::Md4:
1257 case QCryptographicHash::Md5:
1258 case QCryptographicHash::Sha224:
1259 case QCryptographicHash::Sha256:
1260 case QCryptographicHash::Sha384:
1261 case QCryptographicHash::Sha512:
1262 case QCryptographicHash::RealSha3_224:
1263 case QCryptographicHash::Keccak_224:
1264 case QCryptographicHash::RealSha3_256:
1265 case QCryptographicHash::Keccak_256:
1266 case QCryptographicHash::RealSha3_384:
1267 case QCryptographicHash::Keccak_384:
1268 case QCryptographicHash::RealSha3_512:
1269 case QCryptographicHash::Keccak_512:
1270 case QCryptographicHash::Blake2b_160:
1271 case QCryptographicHash::Blake2b_256:
1272 case QCryptographicHash::Blake2b_384:
1273 case QCryptographicHash::Blake2b_512:
1274 case QCryptographicHash::Blake2s_128:
1275 case QCryptographicHash::Blake2s_160:
1276 case QCryptographicHash::Blake2s_224:
1277 case QCryptographicHash::Blake2s_256:
1279 case QCryptographicHash::NumAlgorithms: ;
1288 case QCryptographicHash::Sha1:
1289 return SHA1_Message_Block_Size;
1290 case QCryptographicHash::Md4:
1292 case QCryptographicHash::Md5:
1294 case QCryptographicHash::Sha224:
1295 return SHA224_Message_Block_Size;
1296 case QCryptographicHash::Sha256:
1297 return SHA256_Message_Block_Size;
1298 case QCryptographicHash::Sha384:
1299 return SHA384_Message_Block_Size;
1300 case QCryptographicHash::Sha512:
1301 return SHA512_Message_Block_Size;
1302 case QCryptographicHash::RealSha3_224:
1303 case QCryptographicHash::Keccak_224:
1305 case QCryptographicHash::RealSha3_256:
1306 case QCryptographicHash::Keccak_256:
1308 case QCryptographicHash::RealSha3_384:
1309 case QCryptographicHash::Keccak_384:
1311 case QCryptographicHash::RealSha3_512:
1312 case QCryptographicHash::Keccak_512:
1314 case QCryptographicHash::Blake2b_160:
1315 case QCryptographicHash::Blake2b_256:
1316 case QCryptographicHash::Blake2b_384:
1317 case QCryptographicHash::Blake2b_512:
1318 return BLAKE2B_BLOCKBYTES;
1319 case QCryptographicHash::Blake2s_128:
1320 case QCryptographicHash::Blake2s_160:
1321 case QCryptographicHash::Blake2s_224:
1322 case QCryptographicHash::Blake2s_256:
1323 return BLAKE2S_BLOCKBYTES;
1324 case QCryptographicHash::NumAlgorithms:
1325#if !defined(Q_CC_GNU_ONLY) || Q_CC_GNU >= 900
1337 using A = QCryptographicHash::Algorithm;
1338 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1339 result = std::max(result, qt_hash_block_size(A(i)));
1346 int result = INT_MAX;
1347 using A = QCryptographicHash::Algorithm;
1348 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1349 result = std::min(result, qt_hash_block_size(A(i)));
1357 using A = QCryptographicHash::Algorithm;
1358 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1359 result = std::gcd(result, qt_hash_block_size(A(i)));
1363using HashBlock = QSmallByteArray<maxHashBlockSize()>;
1372 result.resizeForOverwrite(block.size());
1373 for (qsizetype i = 0; i < block.size(); ++i)
1374 result[i] = block[i] ^ val;
1400
1401
1402
1403
1404
1405
1406
1407
1410 const int blockSize = qt_hash_block_size(messageHash.method);
1412 if (newKey.size() > blockSize) {
1413 messageHash.addData(newKey);
1414 messageHash.finalizeUnchecked();
1416 using A = QCryptographicHash::Algorithm;
1417 for (
int i = 0; i < A::NumAlgorithms; ++i) {
1418 if (hashLengthInternal(A(i)) > qt_hash_block_size(A(i)))
1422 }(),
"this code assumes that a hash's result always fits into that hash's block size");
1423 key = messageHash.result;
1424 messageHash.reset();
1429 if (key.size() < blockSize)
1430 key.resize(blockSize,
'\0');
1436
1437
1438
1439
1440
1441
1442
1445 messageHash.addData(xored(key, 0x36));
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
1503 : d(
new QMessageAuthenticationCodePrivate(method))
1509
1510
1511QMessageAuthenticationCode::~QMessageAuthenticationCode()
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1541
1542
1543
1544
1547
1548
1549void QMessageAuthenticationCode::reset()
noexcept
1551 d->messageHash.reset();
1552 d->initMessageHash();
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587void QMessageAuthenticationCode::setKey(QByteArrayView key)
noexcept
1589 d->messageHash.reset();
1594
1595
1596
1597void QMessageAuthenticationCode::addData(
const char *data, qsizetype length)
1599 d->messageHash.addData({data, length});
1603
1604
1605
1606
1607
1608
1609void QMessageAuthenticationCode::addData(QByteArrayView data)
noexcept
1611 d->messageHash.addData(data);
1615
1616
1617
1618
1619
1620bool QMessageAuthenticationCode::addData(QIODevice *device)
1622 return d->messageHash.addData(device);
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635QByteArrayView QMessageAuthenticationCode::resultView()
const noexcept
1638 return d->messageHash.resultView();
1642
1643
1644
1645
1646QByteArray QMessageAuthenticationCode::result()
const
1648 return resultView().toByteArray();
1653 const auto lock = qt_scoped_lock(messageHash.finalizeMutex);
1654 if (!messageHash.result.isEmpty())
1661 messageHash.finalizeUnchecked();
1662 const HashResult hashedMessage = messageHash.result;
1664 messageHash.reset();
1665 messageHash.addData(xored(key, 0x5c));
1666 messageHash.addData(hashedMessage);
1667 messageHash.finalizeUnchecked();
1671
1672
1673
1674
1675
1676
1677
1678QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key,
1679 QCryptographicHash::Algorithm method)
1681 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1682 [[maybe_unused]]
const auto r = hashInto(ba, message, key, method);
1683 Q_ASSERT(r.size() == ba.size());
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer,
1706 QSpan<
const QByteArrayView> messageParts,
1708 QCryptographicHash::Algorithm method)
noexcept
1710 QMessageAuthenticationCodePrivate mac(method);
1712 for (QByteArrayView part : messageParts)
1713 mac.messageHash.addData(part);
1714 mac.finalizeUnchecked();
1715 auto result = mac.messageHash.resultView();
1716 if (buffer.size() < result.size())
1719 memcpy(buffer.data(), result.data(), result.size());
1720 return buffer.first(result.size());
1725#ifndef QT_NO_QOBJECT
1726#include "moc_qcryptographichash.cpp"
QBasicMutex finalizeMutex
void addData(QByteArrayView bytes) noexcept
~QCryptographicHashPrivate()
QCryptographicHashPrivate(QCryptographicHash::Algorithm method) noexcept
void finalizeUnchecked() noexcept
QByteArrayView resultView() const noexcept
static bool supportsAlgorithm(QCryptographicHash::Algorithm method)
bool addData(QIODevice *dev)
QMessageAuthenticationCodePrivate(QCryptographicHash::Algorithm m) noexcept
void initMessageHash() noexcept
void setKey(QByteArrayView k) noexcept
QCryptographicHashPrivate messageHash
void finalizeUnchecked() noexcept
constexpr int maxHashBlockSize()
QT_WARNING_PUSH QT_WARNING_POP static QT_BEGIN_NAMESPACE constexpr int hashLengthInternal(QCryptographicHash::Algorithm method) noexcept
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()
unsigned long long DataLength
unsigned char BitSequence
blake2b_state blake2bContext
SHA384Context sha384Context
SHA224Context sha224Context
void addData(QCryptographicHash::Algorithm method, QByteArrayView data) noexcept
SHA512Context sha512Context
static void sha3Finish(SHA3Context &ctx, HashResult &result, Sha3Variant sha3Variant)
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