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#if QT_CONFIG(system_libb2)
108QT_WARNING_DISABLE_CLANG(
"-Wunused-function")
109QT_WARNING_DISABLE_GCC(
"-Wunused-function")
110QT_WARNING_DISABLE_MSVC(4505)
111#include "../../3rdparty/blake2/src/blake2b-ref.c"
112#include "../../3rdparty/blake2/src/blake2s-ref.c"
116#if !defined(QT_BOOTSTRAPPED) && QT_CONFIG(openssl_hash)
117#define USING_OPENSSL30
118#include <openssl/evp.h>
119#include <openssl/provider.h>
127#define CASE(Enum, Size)
128 case QCryptographicHash:: Enum :
134 CASE(Sha224, SHA224HashSize);
135 CASE(Sha256, SHA256HashSize);
136 CASE(Sha384, SHA384HashSize);
137 CASE(Sha512, SHA512HashSize);
138 CASE(Blake2s_128, 128 / 8);
139 case QCryptographicHash::Blake2b_160:
140 case QCryptographicHash::Blake2s_160:
142 case QCryptographicHash::RealSha3_224:
143 case QCryptographicHash::Keccak_224:
144 case QCryptographicHash::Blake2s_224:
146 case QCryptographicHash::RealSha3_256:
147 case QCryptographicHash::Keccak_256:
148 case QCryptographicHash::Blake2b_256:
149 case QCryptographicHash::Blake2s_256:
151 case QCryptographicHash::RealSha3_384:
152 case QCryptographicHash::Keccak_384:
153 case QCryptographicHash::Blake2b_384:
155 case QCryptographicHash::RealSha3_512:
156 case QCryptographicHash::Keccak_512:
157 case QCryptographicHash::Blake2b_512:
160 case QCryptographicHash::NumAlgorithms: ;
170 using A = QCryptographicHash::Algorithm;
171 for (
int i = 0; i < A::NumAlgorithms; ++i)
172 result =
std::max(result, hashLengthInternal(A(i)));
176using HashResult = QSmallByteArray<maxHashLength()>;
178#ifdef USING_OPENSSL30
179static constexpr const char * methodToName(QCryptographicHash::Algorithm method)
noexcept
182#define CASE(Enum, Name)
183 case QCryptographicHash:: Enum :
189 CASE(Sha224,
"SHA224");
190 CASE(Sha256,
"SHA256");
191 CASE(Sha384,
"SHA384");
192 CASE(Sha512,
"SHA512");
193 CASE(RealSha3_224,
"SHA3-224");
194 CASE(RealSha3_256,
"SHA3-256");
195 CASE(RealSha3_384,
"SHA3-384");
196 CASE(RealSha3_512,
"SHA3-512");
197 CASE(Blake2b_512,
"BLAKE2B512");
198 CASE(Blake2s_256,
"BLAKE2S256");
200 CASE(Keccak_224,
nullptr);
201 CASE(Keccak_256,
nullptr);
202 CASE(Keccak_384,
nullptr);
203 CASE(Keccak_512,
nullptr);
204 CASE(Blake2b_160,
nullptr);
205 CASE(Blake2b_256,
nullptr);
206 CASE(Blake2b_384,
nullptr);
207 CASE(Blake2s_128,
nullptr);
208 CASE(Blake2s_160,
nullptr);
209 CASE(Blake2s_224,
nullptr);
210 CASE(NumAlgorithms,
nullptr);
226 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_try_load(
nullptr,
"legacy", 1));
560
561
562
563
564
565
566
567 defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_try_load(
nullptr,
"default", 1));
568 if (!defaultProvider)
571 context = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
578
579
580
581
582 algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method),
"-fips"));
587 initializationFailed = !EVP_DigestInit_ex(context.get(), algorithm.get(),
nullptr);
595 case QCryptographicHash::Sha1:
596 new (&sha1Context) Sha1State;
598 case QCryptographicHash::Md4:
599 new (&md4Context) md4_context;
601 case QCryptographicHash::Md5:
602 new (&md5Context) MD5Context;
604 case QCryptographicHash::Sha224:
607 case QCryptographicHash::Sha256:
610 case QCryptographicHash::Sha384:
613 case QCryptographicHash::Sha512:
616 case QCryptographicHash::RealSha3_224:
617 case QCryptographicHash::Keccak_224:
618 case QCryptographicHash::RealSha3_256:
619 case QCryptographicHash::Keccak_256:
620 case QCryptographicHash::RealSha3_384:
621 case QCryptographicHash::Keccak_384:
622 case QCryptographicHash::RealSha3_512:
623 case QCryptographicHash::Keccak_512:
626 case QCryptographicHash::Blake2b_160:
627 case QCryptographicHash::Blake2b_256:
628 case QCryptographicHash::Blake2b_384:
629 case QCryptographicHash::Blake2b_512:
630 new (&blake2bContext) blake2b_state;
632 case QCryptographicHash::Blake2s_128:
633 case QCryptographicHash::Blake2s_160:
634 case QCryptographicHash::Blake2s_224:
635 case QCryptographicHash::Blake2s_256:
636 new (&blake2sContext) blake2s_state;
638 case QCryptographicHash::NumAlgorithms:
646 static_assert(std::is_trivially_destructible_v<State>);
656#ifdef USING_OPENSSL30
658void QCryptographicHashPrivate::State::reset(QCryptographicHash::Algorithm method)
noexcept
661 case QCryptographicHash::Keccak_224:
662 case QCryptographicHash::Keccak_256:
663 case QCryptographicHash::Keccak_384:
664 case QCryptographicHash::Keccak_512:
665 sha3Init(&sha3Context, hashLengthInternal(method) * 8);
667 case QCryptographicHash::Blake2b_160:
668 case QCryptographicHash::Blake2b_256:
669 case QCryptographicHash::Blake2b_384:
670 blake2b_init(&blake2bContext, hashLengthInternal(method));
672 case QCryptographicHash::Blake2s_128:
673 case QCryptographicHash::Blake2s_160:
674 case QCryptographicHash::Blake2s_224:
675 blake2s_init(&blake2sContext, hashLengthInternal(method));
677 case QCryptographicHash::Sha1:
678 case QCryptographicHash::Md4:
679 case QCryptographicHash::Md5:
680 case QCryptographicHash::Sha224:
681 case QCryptographicHash::Sha256:
682 case QCryptographicHash::Sha384:
683 case QCryptographicHash::Sha512:
684 case QCryptographicHash::RealSha3_224:
685 case QCryptographicHash::RealSha3_256:
686 case QCryptographicHash::RealSha3_384:
687 case QCryptographicHash::RealSha3_512:
688 case QCryptographicHash::Blake2b_512:
689 case QCryptographicHash::Blake2s_256:
692 case QCryptographicHash::NumAlgorithms:
697void QCryptographicHashPrivate::EVP::reset()
noexcept
699 if (!initializationFailed) {
703 EVP_MD_CTX_reset(context.get());
704 initializationFailed = !EVP_DigestInit_ex(context.get(), algorithm.get(),
nullptr);
714 case QCryptographicHash::Sha1:
715 sha1InitState(&sha1Context);
717 case QCryptographicHash::Md4:
718 md4_init(&md4Context);
720 case QCryptographicHash::Md5:
721 MD5Init(&md5Context);
723 case QCryptographicHash::Sha224:
726 case QCryptographicHash::Sha256:
729 case QCryptographicHash::Sha384:
732 case QCryptographicHash::Sha512:
735 case QCryptographicHash::RealSha3_224:
736 case QCryptographicHash::Keccak_224:
737 case QCryptographicHash::RealSha3_256:
738 case QCryptographicHash::Keccak_256:
739 case QCryptographicHash::RealSha3_384:
740 case QCryptographicHash::Keccak_384:
741 case QCryptographicHash::RealSha3_512:
742 case QCryptographicHash::Keccak_512:
743 sha3Init(&
sha3Context, hashLengthInternal(method) * 8);
745 case QCryptographicHash::Blake2b_160:
746 case QCryptographicHash::Blake2b_256:
747 case QCryptographicHash::Blake2b_384:
748 case QCryptographicHash::Blake2b_512:
749 blake2b_init(&blake2bContext, hashLengthInternal(method));
751 case QCryptographicHash::Blake2s_128:
752 case QCryptographicHash::Blake2s_160:
753 case QCryptographicHash::Blake2s_224:
754 case QCryptographicHash::Blake2s_256:
755 blake2s_init(&blake2sContext, hashLengthInternal(method));
757 case QCryptographicHash::NumAlgorithms:
764#if QT_DEPRECATED_SINCE(6
, 4
)
766
767
768
769
770
771
772void QCryptographicHash::addData(
const char *data, qsizetype length)
774 Q_ASSERT(length >= 0);
775 addData(QByteArrayView{data, length});
780
781
782
783
784
785void QCryptographicHash::addData(QByteArrayView bytes)
noexcept
792 state.addData(method, bytes);
796#ifdef USING_OPENSSL30
798void QCryptographicHashPrivate::State::addData(QCryptographicHash::Algorithm method,
799 QByteArrayView bytes)
noexcept
801 const char *data = bytes.data();
802 auto length = bytes.size();
805 case QCryptographicHash::Keccak_224:
806 case QCryptographicHash::Keccak_256:
807 case QCryptographicHash::Keccak_384:
808 case QCryptographicHash::Keccak_512:
809 sha3Update(&sha3Context,
reinterpret_cast<
const BitSequence *>(data), uint64_t(length) * 8);
811 case QCryptographicHash::Blake2b_160:
812 case QCryptographicHash::Blake2b_256:
813 case QCryptographicHash::Blake2b_384:
814 blake2b_update(&blake2bContext,
reinterpret_cast<
const uint8_t *>(data), length);
816 case QCryptographicHash::Blake2s_128:
817 case QCryptographicHash::Blake2s_160:
818 case QCryptographicHash::Blake2s_224:
819 blake2s_update(&blake2sContext,
reinterpret_cast<
const uint8_t *>(data), length);
821 case QCryptographicHash::Sha1:
822 case QCryptographicHash::Md4:
823 case QCryptographicHash::Md5:
824 case QCryptographicHash::Sha224:
825 case QCryptographicHash::Sha256:
826 case QCryptographicHash::Sha384:
827 case QCryptographicHash::Sha512:
828 case QCryptographicHash::RealSha3_224:
829 case QCryptographicHash::RealSha3_256:
830 case QCryptographicHash::RealSha3_384:
831 case QCryptographicHash::RealSha3_512:
832 case QCryptographicHash::Blake2b_512:
833 case QCryptographicHash::Blake2s_256:
834 if (!evp.initializationFailed)
835 EVP_DigestUpdate(evp.context.get(), (
const unsigned char *)data, length);
837 case QCryptographicHash::NumAlgorithms:
845 QByteArrayView bytes)
noexcept
847 const char *data = bytes.data();
848 auto length = bytes.size();
850#if QT_POINTER_SIZE == 8
854 for (
auto remaining = length; remaining; remaining -= length, data += length) {
855 length = qMin(qsizetype(std::numeric_limits<uint>::max()), remaining);
860 case QCryptographicHash::Sha1:
861 sha1Update(&sha1Context, (
const unsigned char *)data, length);
863 case QCryptographicHash::Md4:
864 md4_update(&md4Context, (
const unsigned char *)data, length);
866 case QCryptographicHash::Md5:
867 MD5Update(&md5Context, (
const unsigned char *)data, length);
869 case QCryptographicHash::Sha224:
870 SHA224Input(&
sha224Context,
reinterpret_cast<
const unsigned char *>(data), length);
872 case QCryptographicHash::Sha256:
873 SHA256Input(&
sha256Context,
reinterpret_cast<
const unsigned char *>(data), length);
875 case QCryptographicHash::Sha384:
876 SHA384Input(&
sha384Context,
reinterpret_cast<
const unsigned char *>(data), length);
878 case QCryptographicHash::Sha512:
879 SHA512Input(&
sha512Context,
reinterpret_cast<
const unsigned char *>(data), length);
881 case QCryptographicHash::RealSha3_224:
882 case QCryptographicHash::Keccak_224:
883 case QCryptographicHash::RealSha3_256:
884 case QCryptographicHash::Keccak_256:
885 case QCryptographicHash::RealSha3_384:
886 case QCryptographicHash::Keccak_384:
887 case QCryptographicHash::RealSha3_512:
888 case QCryptographicHash::Keccak_512:
891 case QCryptographicHash::Blake2b_160:
892 case QCryptographicHash::Blake2b_256:
893 case QCryptographicHash::Blake2b_384:
894 case QCryptographicHash::Blake2b_512:
895 blake2b_update(&blake2bContext,
reinterpret_cast<
const uint8_t *>(data), length);
897 case QCryptographicHash::Blake2s_128:
898 case QCryptographicHash::Blake2s_160:
899 case QCryptographicHash::Blake2s_224:
900 case QCryptographicHash::Blake2s_256:
901 blake2s_update(&blake2sContext,
reinterpret_cast<
const uint8_t *>(data), length);
903 case QCryptographicHash::NumAlgorithms:
911
912
913
914
915bool QCryptographicHash::addData(QIODevice *device)
917 return d->addData(device);
922 if (!device->isReadable())
925 if (!device->isOpen())
932 while ((length = device->read(buffer,
sizeof(buffer))) > 0)
935 return device->atEnd();
940
941
942
943
944QByteArray QCryptographicHash::result()
const
946 return resultView().toByteArray();
950
951
952
953
954
955
956
957
958
959QByteArrayView QCryptographicHash::resultView()
const noexcept
964 return d->resultView();
968
969
970
971
974 const auto lock = qt_scoped_lock(finalizeMutex);
976 if (!result.isEmpty())
982
983
984
985
986
989 result.resizeForOverwrite(hashLengthInternal(method));
990 state.finalizeUnchecked(method, result);
994
995
996
997
998
1001 buffer = buffer.first(hashLengthInternal(method));
1002 state.finalizeUnchecked(method, buffer);
1003 Q_ASSERT(result.size() == 0);
1007#ifdef USING_OPENSSL30
1008void QCryptographicHashPrivate::State::finalizeUnchecked(QCryptographicHash::Algorithm method,
1009 QSpan<uchar> result)
noexcept
1012 case QCryptographicHash::Keccak_224:
1013 case QCryptographicHash::Keccak_256:
1014 case QCryptographicHash::Keccak_384:
1015 case QCryptographicHash::Keccak_512: {
1016 SHA3Context copy = sha3Context;
1017 sha3Finish(copy, result, Sha3Variant::Keccak);
1020 case QCryptographicHash::Blake2b_160:
1021 case QCryptographicHash::Blake2b_256:
1022 case QCryptographicHash::Blake2b_384: {
1023 const auto length = hashLengthInternal(method);
1024 blake2b_state copy = blake2bContext;
1025 blake2b_final(©, result.data(), length);
1028 case QCryptographicHash::Blake2s_128:
1029 case QCryptographicHash::Blake2s_160:
1030 case QCryptographicHash::Blake2s_224: {
1031 const auto length = hashLengthInternal(method);
1032 blake2s_state copy = blake2sContext;
1033 blake2s_final(©, result.data(), length);
1036 case QCryptographicHash::Sha1:
1037 case QCryptographicHash::Md4:
1038 case QCryptographicHash::Md5:
1039 case QCryptographicHash::Sha224:
1040 case QCryptographicHash::Sha256:
1041 case QCryptographicHash::Sha384:
1042 case QCryptographicHash::Sha512:
1043 case QCryptographicHash::RealSha3_224:
1044 case QCryptographicHash::RealSha3_256:
1045 case QCryptographicHash::RealSha3_384:
1046 case QCryptographicHash::RealSha3_512:
1047 case QCryptographicHash::Blake2b_512:
1048 case QCryptographicHash::Blake2s_256:
1049 evp.finalizeUnchecked(result);
1051 case QCryptographicHash::NumAlgorithms:
1056void QCryptographicHashPrivate::EVP::finalizeUnchecked(QSpan<uchar> result)
noexcept
1058 if (!initializationFailed) {
1059 EVP_MD_CTX_ptr copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
1060 EVP_MD_CTX_copy_ex(copy.get(), context.get());
1061 Q_ASSERT(result.size() == EVP_MD_get_size(algorithm.get()));
1062 EVP_DigestFinal_ex(copy.get(), result.data(),
nullptr);
1069 QSpan<uchar> result)
noexcept
1072 case QCryptographicHash::Sha1: {
1073 Sha1State copy = sha1Context;
1074 sha1FinalizeState(©);
1075 sha1ToHash(©, result.data());
1078 case QCryptographicHash::Md4: {
1079 md4_context copy = md4Context;
1080 md4_final(©, result.data());
1083 case QCryptographicHash::Md5: {
1084 MD5Context copy = md5Context;
1085 MD5Final(©, result.data());
1088 case QCryptographicHash::Sha224: {
1090 SHA224Result(©, result.data());
1093 case QCryptographicHash::Sha256: {
1095 SHA256Result(©, result.data());
1098 case QCryptographicHash::Sha384: {
1100 SHA384Result(©, result.data());
1103 case QCryptographicHash::Sha512: {
1105 SHA512Result(©, result.data());
1108 case QCryptographicHash::RealSha3_224:
1109 case QCryptographicHash::RealSha3_256:
1110 case QCryptographicHash::RealSha3_384:
1111 case QCryptographicHash::RealSha3_512: {
1113 sha3Finish(copy, result, Sha3Variant::Sha3);
1116 case QCryptographicHash::Keccak_224:
1117 case QCryptographicHash::Keccak_256:
1118 case QCryptographicHash::Keccak_384:
1119 case QCryptographicHash::Keccak_512: {
1121 sha3Finish(copy, result, Sha3Variant::Keccak);
1124 case QCryptographicHash::Blake2b_160:
1125 case QCryptographicHash::Blake2b_256:
1126 case QCryptographicHash::Blake2b_384:
1127 case QCryptographicHash::Blake2b_512: {
1128 const auto length = hashLengthInternal(method);
1129 blake2b_state copy = blake2bContext;
1130 blake2b_final(©, result.data(), length);
1133 case QCryptographicHash::Blake2s_128:
1134 case QCryptographicHash::Blake2s_160:
1135 case QCryptographicHash::Blake2s_224:
1136 case QCryptographicHash::Blake2s_256: {
1137 const auto length = hashLengthInternal(method);
1138 blake2s_state copy = blake2sContext;
1139 blake2s_final(©, result.data(), length);
1142 case QCryptographicHash::NumAlgorithms:
1149
1150
1151
1152
1153
1154
1155
1156QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method)
1158 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1159 [[maybe_unused]]
const auto r = hashInto(ba, data, method);
1160 Q_ASSERT(r.size() == ba.size());
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer,
1183 QSpan<
const QByteArrayView> data,
1184 Algorithm method)
noexcept
1186 if (buffer.size() < hashLengthInternal(method))
1189 Q_DECL_UNINITIALIZED
1190 QCryptographicHashPrivate hash(method);
1191 for (QByteArrayView part : data)
1193 auto span = QSpan{
reinterpret_cast<uchar *>(buffer.data()), buffer.size()};
1194 return hash.finalizeUnchecked(span);
1198
1199
1200
1201
1202int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
1204 return hashLengthInternal(method);
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1220bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)
1222 return QCryptographicHashPrivate::supportsAlgorithm(method);
1225#ifdef USING_OPENSSL30
1226bool QCryptographicHashPrivate::supportsAlgorithm(QCryptographicHash::Algorithm method)
1232 case QCryptographicHash::Keccak_224:
1233 case QCryptographicHash::Keccak_256:
1234 case QCryptographicHash::Keccak_384:
1235 case QCryptographicHash::Keccak_512:
1236 case QCryptographicHash::Blake2b_160:
1237 case QCryptographicHash::Blake2b_256:
1238 case QCryptographicHash::Blake2b_384:
1239 case QCryptographicHash::Blake2s_128:
1240 case QCryptographicHash::Blake2s_160:
1241 case QCryptographicHash::Blake2s_224:
1243 case QCryptographicHash::Sha1:
1244 case QCryptographicHash::Md4:
1245 case QCryptographicHash::Md5:
1246 case QCryptographicHash::Sha224:
1247 case QCryptographicHash::Sha256:
1248 case QCryptographicHash::Sha384:
1249 case QCryptographicHash::Sha512:
1250 case QCryptographicHash::RealSha3_224:
1251 case QCryptographicHash::RealSha3_256:
1252 case QCryptographicHash::RealSha3_384:
1253 case QCryptographicHash::RealSha3_512:
1254 case QCryptographicHash::Blake2b_512:
1255 case QCryptographicHash::Blake2s_256: {
1258 auto legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_try_load(
nullptr,
"legacy", 1));
1259 auto defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_try_load(
nullptr,
"default", 1));
1261 const char *restriction =
"-fips";
1262 EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method), restriction));
1264 return algorithm !=
nullptr;
1267 case QCryptographicHash::NumAlgorithms:
1277 case QCryptographicHash::Sha1:
1278 case QCryptographicHash::Md4:
1279 case QCryptographicHash::Md5:
1280 case QCryptographicHash::Sha224:
1281 case QCryptographicHash::Sha256:
1282 case QCryptographicHash::Sha384:
1283 case QCryptographicHash::Sha512:
1284 case QCryptographicHash::RealSha3_224:
1285 case QCryptographicHash::Keccak_224:
1286 case QCryptographicHash::RealSha3_256:
1287 case QCryptographicHash::Keccak_256:
1288 case QCryptographicHash::RealSha3_384:
1289 case QCryptographicHash::Keccak_384:
1290 case QCryptographicHash::RealSha3_512:
1291 case QCryptographicHash::Keccak_512:
1292 case QCryptographicHash::Blake2b_160:
1293 case QCryptographicHash::Blake2b_256:
1294 case QCryptographicHash::Blake2b_384:
1295 case QCryptographicHash::Blake2b_512:
1296 case QCryptographicHash::Blake2s_128:
1297 case QCryptographicHash::Blake2s_160:
1298 case QCryptographicHash::Blake2s_224:
1299 case QCryptographicHash::Blake2s_256:
1301 case QCryptographicHash::NumAlgorithms: ;
1310 case QCryptographicHash::Sha1:
1311 return SHA1_Message_Block_Size;
1312 case QCryptographicHash::Md4:
1314 case QCryptographicHash::Md5:
1316 case QCryptographicHash::Sha224:
1317 return SHA224_Message_Block_Size;
1318 case QCryptographicHash::Sha256:
1319 return SHA256_Message_Block_Size;
1320 case QCryptographicHash::Sha384:
1321 return SHA384_Message_Block_Size;
1322 case QCryptographicHash::Sha512:
1323 return SHA512_Message_Block_Size;
1324 case QCryptographicHash::RealSha3_224:
1325 case QCryptographicHash::Keccak_224:
1327 case QCryptographicHash::RealSha3_256:
1328 case QCryptographicHash::Keccak_256:
1330 case QCryptographicHash::RealSha3_384:
1331 case QCryptographicHash::Keccak_384:
1333 case QCryptographicHash::RealSha3_512:
1334 case QCryptographicHash::Keccak_512:
1336 case QCryptographicHash::Blake2b_160:
1337 case QCryptographicHash::Blake2b_256:
1338 case QCryptographicHash::Blake2b_384:
1339 case QCryptographicHash::Blake2b_512:
1340 return BLAKE2B_BLOCKBYTES;
1341 case QCryptographicHash::Blake2s_128:
1342 case QCryptographicHash::Blake2s_160:
1343 case QCryptographicHash::Blake2s_224:
1344 case QCryptographicHash::Blake2s_256:
1345 return BLAKE2S_BLOCKBYTES;
1346 case QCryptographicHash::NumAlgorithms:
1347#if !defined(Q_CC_GNU_ONLY) || Q_CC_GNU >= 900
1359 using A = QCryptographicHash::Algorithm;
1360 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1361 result =
std::max(result, qt_hash_block_size(A(i)));
1368 int result = INT_MAX;
1369 using A = QCryptographicHash::Algorithm;
1370 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1371 result =
std::min(result, qt_hash_block_size(A(i)));
1379 using A = QCryptographicHash::Algorithm;
1380 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1381 result =
std::gcd(result, qt_hash_block_size(A(i)));
1385using HashBlock = QSmallByteArray<maxHashBlockSize()>;
1394 Q_DECL_UNINITIALIZED
1396 result.resizeForOverwrite(block.size());
1397 for (qsizetype i = 0; i < block.size(); ++i)
1398 result[i] = block[i] ^ val;
1424
1425
1426
1427
1428
1429
1430
1431
1434 const int blockSize = qt_hash_block_size(messageHash.method);
1436 if (newKey.size() > blockSize) {
1437 messageHash.addData(newKey);
1438 messageHash.finalizeUnchecked();
1440 using A = QCryptographicHash::Algorithm;
1441 for (
int i = 0; i < A::NumAlgorithms; ++i) {
1442 if (hashLengthInternal(A(i)) > qt_hash_block_size(A(i)))
1446 }(),
"this code assumes that a hash's result always fits into that hash's block size");
1447 key = messageHash.result;
1448 messageHash.reset();
1453 if (key.size() < blockSize)
1454 key.resize(blockSize,
'\0');
1460
1461
1462
1463
1464
1465
1466
1469 messageHash.addData(xored(key, 0x36));
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
1527 : d(
new QMessageAuthenticationCodePrivate(method))
1533
1534
1535QMessageAuthenticationCode::~QMessageAuthenticationCode()
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1565
1566
1567
1568
1571
1572
1573void QMessageAuthenticationCode::reset()
noexcept
1575 d->messageHash.reset();
1576 d->initMessageHash();
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611void QMessageAuthenticationCode::setKey(QByteArrayView key)
noexcept
1613 d->messageHash.reset();
1618
1619
1620
1621void QMessageAuthenticationCode::addData(
const char *data, qsizetype length)
1623 d->messageHash.addData({data, length});
1627
1628
1629
1630
1631
1632
1633void QMessageAuthenticationCode::addData(QByteArrayView data)
noexcept
1635 d->messageHash.addData(data);
1639
1640
1641
1642
1643
1644bool QMessageAuthenticationCode::addData(QIODevice *device)
1646 return d->messageHash.addData(device);
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659QByteArrayView QMessageAuthenticationCode::resultView()
const noexcept
1662 return d->messageHash.resultView();
1666
1667
1668
1669
1670QByteArray QMessageAuthenticationCode::result()
const
1672 return resultView().toByteArray();
1677 const auto lock = qt_scoped_lock(messageHash.finalizeMutex);
1678 if (!messageHash.result.isEmpty())
1685 messageHash.finalizeUnchecked();
1686 const HashResult hashedMessage = messageHash.result;
1688 messageHash.reset();
1689 messageHash.addData(xored(key, 0x5c));
1690 messageHash.addData(hashedMessage);
1691 messageHash.finalizeUnchecked();
1695
1696
1697
1698
1699
1700
1701
1702QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key,
1703 QCryptographicHash::Algorithm method)
1705 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1706 [[maybe_unused]]
const auto r = hashInto(ba, message, key, method);
1707 Q_ASSERT(r.size() == ba.size());
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer,
1730 QSpan<
const QByteArrayView> messageParts,
1732 QCryptographicHash::Algorithm method)
noexcept
1734 Q_DECL_UNINITIALIZED
1735 QMessageAuthenticationCodePrivate mac(method);
1737 for (QByteArrayView part : messageParts)
1738 mac.messageHash.addData(part);
1739 mac.finalizeUnchecked();
1740 auto result = mac.messageHash.resultView();
1741 if (buffer.size() < result.size())
1744 memcpy(buffer.data(), result.data(), result.size());
1745 return buffer.first(result.size());
1750#ifndef QT_NO_QOBJECT
1751#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)
QSpan< uchar > finalizeUnchecked(QSpan< uchar > buffer) noexcept
QMessageAuthenticationCodePrivate(QCryptographicHash::Algorithm m) noexcept
void initMessageHash() noexcept
void setKey(QByteArrayView k) noexcept
QCryptographicHashPrivate messageHash
void finalizeUnchecked() noexcept
Combined button and popup list for selecting options.
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
static void sha3Finish(SHA3Context &ctx, QSpan< uchar > result, Sha3Variant sha3Variant)
blake2b_state blake2bContext
SHA384Context sha384Context
SHA224Context sha224Context
void addData(QCryptographicHash::Algorithm method, QByteArrayView data) noexcept
SHA512Context sha512Context
SHA256Context sha256Context
void finalizeUnchecked(QCryptographicHash::Algorithm method, QSpan< uchar > buffer) noexcept
void destroy(QCryptographicHash::Algorithm method)
blake2s_state blake2sContext
State(QCryptographicHash::Algorithm method)
void reset(QCryptographicHash::Algorithm method) noexcept