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_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);
985
986
987
988
989
992 buffer = buffer.first(hashLengthInternal(method));
993 state.finalizeUnchecked(method, buffer);
994 Q_ASSERT(result.size() == 0);
998#ifdef USING_OPENSSL30
999void QCryptographicHashPrivate::State::finalizeUnchecked(QCryptographicHash::Algorithm method,
1000 QSpan<uchar> result)
noexcept
1003 case QCryptographicHash::Keccak_224:
1004 case QCryptographicHash::Keccak_256:
1005 case QCryptographicHash::Keccak_384:
1006 case QCryptographicHash::Keccak_512: {
1007 SHA3Context copy = sha3Context;
1008 sha3Finish(copy, result, Sha3Variant::Keccak);
1011 case QCryptographicHash::Blake2b_160:
1012 case QCryptographicHash::Blake2b_256:
1013 case QCryptographicHash::Blake2b_384: {
1014 const auto length = hashLengthInternal(method);
1015 blake2b_state copy = blake2bContext;
1016 blake2b_final(©, result.data(), length);
1019 case QCryptographicHash::Blake2s_128:
1020 case QCryptographicHash::Blake2s_160:
1021 case QCryptographicHash::Blake2s_224: {
1022 const auto length = hashLengthInternal(method);
1023 blake2s_state copy = blake2sContext;
1024 blake2s_final(©, result.data(), length);
1027 case QCryptographicHash::Sha1:
1028 case QCryptographicHash::Md4:
1029 case QCryptographicHash::Md5:
1030 case QCryptographicHash::Sha224:
1031 case QCryptographicHash::Sha256:
1032 case QCryptographicHash::Sha384:
1033 case QCryptographicHash::Sha512:
1034 case QCryptographicHash::RealSha3_224:
1035 case QCryptographicHash::RealSha3_256:
1036 case QCryptographicHash::RealSha3_384:
1037 case QCryptographicHash::RealSha3_512:
1038 case QCryptographicHash::Blake2b_512:
1039 case QCryptographicHash::Blake2s_256:
1040 evp.finalizeUnchecked(result);
1042 case QCryptographicHash::NumAlgorithms:
1047void QCryptographicHashPrivate::EVP::finalizeUnchecked(QSpan<uchar> result)
noexcept
1049 if (!initializationFailed) {
1050 EVP_MD_CTX_ptr copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
1051 EVP_MD_CTX_copy_ex(copy.get(), context.get());
1052 Q_ASSERT(result.size() == EVP_MD_get_size(algorithm.get()));
1053 EVP_DigestFinal_ex(copy.get(), result.data(),
nullptr);
1060 QSpan<uchar> result)
noexcept
1063 case QCryptographicHash::Sha1: {
1064 Sha1State copy = sha1Context;
1065 sha1FinalizeState(©);
1066 sha1ToHash(©, result.data());
1069 case QCryptographicHash::Md4: {
1070 md4_context copy = md4Context;
1071 md4_final(©, result.data());
1074 case QCryptographicHash::Md5: {
1075 MD5Context copy = md5Context;
1076 MD5Final(©, result.data());
1079 case QCryptographicHash::Sha224: {
1081 SHA224Result(©, result.data());
1084 case QCryptographicHash::Sha256: {
1086 SHA256Result(©, result.data());
1089 case QCryptographicHash::Sha384: {
1091 SHA384Result(©, result.data());
1094 case QCryptographicHash::Sha512: {
1096 SHA512Result(©, result.data());
1099 case QCryptographicHash::RealSha3_224:
1100 case QCryptographicHash::RealSha3_256:
1101 case QCryptographicHash::RealSha3_384:
1102 case QCryptographicHash::RealSha3_512: {
1104 sha3Finish(copy, result, Sha3Variant::Sha3);
1107 case QCryptographicHash::Keccak_224:
1108 case QCryptographicHash::Keccak_256:
1109 case QCryptographicHash::Keccak_384:
1110 case QCryptographicHash::Keccak_512: {
1112 sha3Finish(copy, result, Sha3Variant::Keccak);
1115 case QCryptographicHash::Blake2b_160:
1116 case QCryptographicHash::Blake2b_256:
1117 case QCryptographicHash::Blake2b_384:
1118 case QCryptographicHash::Blake2b_512: {
1119 const auto length = hashLengthInternal(method);
1120 blake2b_state copy = blake2bContext;
1121 blake2b_final(©, result.data(), length);
1124 case QCryptographicHash::Blake2s_128:
1125 case QCryptographicHash::Blake2s_160:
1126 case QCryptographicHash::Blake2s_224:
1127 case QCryptographicHash::Blake2s_256: {
1128 const auto length = hashLengthInternal(method);
1129 blake2s_state copy = blake2sContext;
1130 blake2s_final(©, result.data(), length);
1133 case QCryptographicHash::NumAlgorithms:
1140
1141
1142
1143
1144
1145
1146
1147QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method)
1149 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1150 [[maybe_unused]]
const auto r = hashInto(ba, data, method);
1151 Q_ASSERT(r.size() == ba.size());
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer,
1174 QSpan<
const QByteArrayView> data,
1175 Algorithm method)
noexcept
1177 if (buffer.size() < hashLengthInternal(method))
1180 QCryptographicHashPrivate hash(method);
1181 for (QByteArrayView part : data)
1183 auto span = QSpan{
reinterpret_cast<uchar *>(buffer.data()), buffer.size()};
1184 return hash.finalizeUnchecked(span);
1188
1189
1190
1191
1192int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
1194 return hashLengthInternal(method);
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1210bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)
1212 return QCryptographicHashPrivate::supportsAlgorithm(method);
1215#ifdef USING_OPENSSL30
1216bool QCryptographicHashPrivate::supportsAlgorithm(QCryptographicHash::Algorithm method)
1222 case QCryptographicHash::Keccak_224:
1223 case QCryptographicHash::Keccak_256:
1224 case QCryptographicHash::Keccak_384:
1225 case QCryptographicHash::Keccak_512:
1226 case QCryptographicHash::Blake2b_160:
1227 case QCryptographicHash::Blake2b_256:
1228 case QCryptographicHash::Blake2b_384:
1229 case QCryptographicHash::Blake2s_128:
1230 case QCryptographicHash::Blake2s_160:
1231 case QCryptographicHash::Blake2s_224:
1233 case QCryptographicHash::Sha1:
1234 case QCryptographicHash::Md4:
1235 case QCryptographicHash::Md5:
1236 case QCryptographicHash::Sha224:
1237 case QCryptographicHash::Sha256:
1238 case QCryptographicHash::Sha384:
1239 case QCryptographicHash::Sha512:
1240 case QCryptographicHash::RealSha3_224:
1241 case QCryptographicHash::RealSha3_256:
1242 case QCryptographicHash::RealSha3_384:
1243 case QCryptographicHash::RealSha3_512:
1244 case QCryptographicHash::Blake2b_512:
1245 case QCryptographicHash::Blake2s_256: {
1246 auto legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"legacy"));
1247 auto defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"default"));
1249 const char *restriction =
"-fips";
1250 EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method), restriction));
1252 return algorithm !=
nullptr;
1255 case QCryptographicHash::NumAlgorithms:
1265 case QCryptographicHash::Sha1:
1266 case QCryptographicHash::Md4:
1267 case QCryptographicHash::Md5:
1268 case QCryptographicHash::Sha224:
1269 case QCryptographicHash::Sha256:
1270 case QCryptographicHash::Sha384:
1271 case QCryptographicHash::Sha512:
1272 case QCryptographicHash::RealSha3_224:
1273 case QCryptographicHash::Keccak_224:
1274 case QCryptographicHash::RealSha3_256:
1275 case QCryptographicHash::Keccak_256:
1276 case QCryptographicHash::RealSha3_384:
1277 case QCryptographicHash::Keccak_384:
1278 case QCryptographicHash::RealSha3_512:
1279 case QCryptographicHash::Keccak_512:
1280 case QCryptographicHash::Blake2b_160:
1281 case QCryptographicHash::Blake2b_256:
1282 case QCryptographicHash::Blake2b_384:
1283 case QCryptographicHash::Blake2b_512:
1284 case QCryptographicHash::Blake2s_128:
1285 case QCryptographicHash::Blake2s_160:
1286 case QCryptographicHash::Blake2s_224:
1287 case QCryptographicHash::Blake2s_256:
1289 case QCryptographicHash::NumAlgorithms: ;
1298 case QCryptographicHash::Sha1:
1299 return SHA1_Message_Block_Size;
1300 case QCryptographicHash::Md4:
1302 case QCryptographicHash::Md5:
1304 case QCryptographicHash::Sha224:
1305 return SHA224_Message_Block_Size;
1306 case QCryptographicHash::Sha256:
1307 return SHA256_Message_Block_Size;
1308 case QCryptographicHash::Sha384:
1309 return SHA384_Message_Block_Size;
1310 case QCryptographicHash::Sha512:
1311 return SHA512_Message_Block_Size;
1312 case QCryptographicHash::RealSha3_224:
1313 case QCryptographicHash::Keccak_224:
1315 case QCryptographicHash::RealSha3_256:
1316 case QCryptographicHash::Keccak_256:
1318 case QCryptographicHash::RealSha3_384:
1319 case QCryptographicHash::Keccak_384:
1321 case QCryptographicHash::RealSha3_512:
1322 case QCryptographicHash::Keccak_512:
1324 case QCryptographicHash::Blake2b_160:
1325 case QCryptographicHash::Blake2b_256:
1326 case QCryptographicHash::Blake2b_384:
1327 case QCryptographicHash::Blake2b_512:
1328 return BLAKE2B_BLOCKBYTES;
1329 case QCryptographicHash::Blake2s_128:
1330 case QCryptographicHash::Blake2s_160:
1331 case QCryptographicHash::Blake2s_224:
1332 case QCryptographicHash::Blake2s_256:
1333 return BLAKE2S_BLOCKBYTES;
1334 case QCryptographicHash::NumAlgorithms:
1335#if !defined(Q_CC_GNU_ONLY) || Q_CC_GNU >= 900
1347 using A = QCryptographicHash::Algorithm;
1348 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1349 result =
std::max(result, qt_hash_block_size(A(i)));
1356 int result = INT_MAX;
1357 using A = QCryptographicHash::Algorithm;
1358 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1359 result =
std::min(result, qt_hash_block_size(A(i)));
1367 using A = QCryptographicHash::Algorithm;
1368 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1369 result =
std::gcd(result, qt_hash_block_size(A(i)));
1373using HashBlock = QSmallByteArray<maxHashBlockSize()>;
1382 result.resizeForOverwrite(block.size());
1383 for (qsizetype i = 0; i < block.size(); ++i)
1384 result[i] = block[i] ^ val;
1410
1411
1412
1413
1414
1415
1416
1417
1420 const int blockSize = qt_hash_block_size(messageHash.method);
1422 if (newKey.size() > blockSize) {
1423 messageHash.addData(newKey);
1424 messageHash.finalizeUnchecked();
1426 using A = QCryptographicHash::Algorithm;
1427 for (
int i = 0; i < A::NumAlgorithms; ++i) {
1428 if (hashLengthInternal(A(i)) > qt_hash_block_size(A(i)))
1432 }(),
"this code assumes that a hash's result always fits into that hash's block size");
1433 key = messageHash.result;
1434 messageHash.reset();
1439 if (key.size() < blockSize)
1440 key.resize(blockSize,
'\0');
1446
1447
1448
1449
1450
1451
1452
1455 messageHash.addData(xored(key, 0x36));
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
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
1513 : d(
new QMessageAuthenticationCodePrivate(method))
1519
1520
1521QMessageAuthenticationCode::~QMessageAuthenticationCode()
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1551
1552
1553
1554
1557
1558
1559void QMessageAuthenticationCode::reset()
noexcept
1561 d->messageHash.reset();
1562 d->initMessageHash();
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597void QMessageAuthenticationCode::setKey(QByteArrayView key)
noexcept
1599 d->messageHash.reset();
1604
1605
1606
1607void QMessageAuthenticationCode::addData(
const char *data, qsizetype length)
1609 d->messageHash.addData({data, length});
1613
1614
1615
1616
1617
1618
1619void QMessageAuthenticationCode::addData(QByteArrayView data)
noexcept
1621 d->messageHash.addData(data);
1625
1626
1627
1628
1629
1630bool QMessageAuthenticationCode::addData(QIODevice *device)
1632 return d->messageHash.addData(device);
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645QByteArrayView QMessageAuthenticationCode::resultView()
const noexcept
1648 return d->messageHash.resultView();
1652
1653
1654
1655
1656QByteArray QMessageAuthenticationCode::result()
const
1658 return resultView().toByteArray();
1663 const auto lock = qt_scoped_lock(messageHash.finalizeMutex);
1664 if (!messageHash.result.isEmpty())
1671 messageHash.finalizeUnchecked();
1672 const HashResult hashedMessage = messageHash.result;
1674 messageHash.reset();
1675 messageHash.addData(xored(key, 0x5c));
1676 messageHash.addData(hashedMessage);
1677 messageHash.finalizeUnchecked();
1681
1682
1683
1684
1685
1686
1687
1688QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key,
1689 QCryptographicHash::Algorithm method)
1691 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1692 [[maybe_unused]]
const auto r = hashInto(ba, message, key, method);
1693 Q_ASSERT(r.size() == ba.size());
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer,
1716 QSpan<
const QByteArrayView> messageParts,
1718 QCryptographicHash::Algorithm method)
noexcept
1720 QMessageAuthenticationCodePrivate mac(method);
1722 for (QByteArrayView part : messageParts)
1723 mac.messageHash.addData(part);
1724 mac.finalizeUnchecked();
1725 auto result = mac.messageHash.resultView();
1726 if (buffer.size() < result.size())
1729 memcpy(buffer.data(), result.data(), result.size());
1730 return buffer.first(result.size());
1735#ifndef QT_NO_QOBJECT
1736#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