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);
244#ifdef USING_OPENSSL30
272 void reset()
noexcept;
280#ifdef USING_OPENSSL30
289#ifdef USING_OPENSSL30
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 static const unsigned char sha3FinalSuffix = 0x80;
338 switch (sha3Variant) {
340 sha3Update(&ctx,
reinterpret_cast<
const BitSequence *>(&sha3FinalSuffix), 2);
346 sha3Final(&ctx, result.data());
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
402
403
406
407
408QCryptographicHash::QCryptographicHash(Algorithm method)
409 : d(
new QCryptographicHashPrivate{method})
414
415
416
417
418
419
420
421
422
423
426
427
428QCryptographicHash::~QCryptographicHash()
434
435
436
437
438
439
440
441
442
443
446
447
448
449
452
453
454void QCryptographicHash::reset()
noexcept
460
461
462
463
464QCryptographicHash::Algorithm QCryptographicHash::algorithm()
const noexcept
469#ifdef USING_OPENSSL30
471QCryptographicHashPrivate::State::State(QCryptographicHash::Algorithm method)
474 case QCryptographicHash::Keccak_224:
475 case QCryptographicHash::Keccak_256:
476 case QCryptographicHash::Keccak_384:
477 case QCryptographicHash::Keccak_512:
478 new (&sha3Context) SHA3Context;
481 case QCryptographicHash::Blake2b_160:
482 case QCryptographicHash::Blake2b_256:
483 case QCryptographicHash::Blake2b_384:
484 new (&blake2bContext) blake2b_state;
487 case QCryptographicHash::Blake2s_128:
488 case QCryptographicHash::Blake2s_160:
489 case QCryptographicHash::Blake2s_224:
490 new (&blake2sContext) blake2s_state;
493 case QCryptographicHash::Sha1:
494 case QCryptographicHash::Md4:
495 case QCryptographicHash::Md5:
496 case QCryptographicHash::Sha224:
497 case QCryptographicHash::Sha256:
498 case QCryptographicHash::Sha384:
499 case QCryptographicHash::Sha512:
500 case QCryptographicHash::RealSha3_224:
501 case QCryptographicHash::RealSha3_256:
502 case QCryptographicHash::RealSha3_384:
503 case QCryptographicHash::RealSha3_512:
504 case QCryptographicHash::Blake2b_512:
505 case QCryptographicHash::Blake2s_256:
506 new (&evp) EVP(method);
508 case QCryptographicHash::NumAlgorithms:
513void QCryptographicHashPrivate::State::destroy(QCryptographicHash::Algorithm method)
516 case QCryptographicHash::Keccak_224:
517 case QCryptographicHash::Keccak_256:
518 case QCryptographicHash::Keccak_384:
519 case QCryptographicHash::Keccak_512:
520 case QCryptographicHash::Blake2b_160:
521 case QCryptographicHash::Blake2b_256:
522 case QCryptographicHash::Blake2b_384:
523 case QCryptographicHash::Blake2s_128:
524 case QCryptographicHash::Blake2s_160:
525 case QCryptographicHash::Blake2s_224:
527 case QCryptographicHash::Sha1:
528 case QCryptographicHash::Md4:
529 case QCryptographicHash::Md5:
530 case QCryptographicHash::Sha224:
531 case QCryptographicHash::Sha256:
532 case QCryptographicHash::Sha384:
533 case QCryptographicHash::Sha512:
534 case QCryptographicHash::RealSha3_224:
535 case QCryptographicHash::RealSha3_256:
536 case QCryptographicHash::RealSha3_384:
537 case QCryptographicHash::RealSha3_512:
538 case QCryptographicHash::Blake2b_512:
539 case QCryptographicHash::Blake2s_256:
542 case QCryptographicHash::NumAlgorithms:
547QCryptographicHashPrivate::EVP::EVP(QCryptographicHash::Algorithm method)
548 : initializationFailed{
true}
550 if (method == QCryptographicHash::Md4) {
552
553
554
555 legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"legacy"));
561 defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"default"));
562 if (!defaultProvider)
565 context = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
572
573
574
575
576 algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method),
"-fips"));
581 initializationFailed = !EVP_DigestInit_ex(context.get(), algorithm.get(),
nullptr);
589 case QCryptographicHash::Sha1:
590 new (&sha1Context) Sha1State;
592 case QCryptographicHash::Md4:
593 new (&md4Context) md4_context;
595 case QCryptographicHash::Md5:
596 new (&md5Context) MD5Context;
598 case QCryptographicHash::Sha224:
601 case QCryptographicHash::Sha256:
604 case QCryptographicHash::Sha384:
607 case QCryptographicHash::Sha512:
610 case QCryptographicHash::RealSha3_224:
611 case QCryptographicHash::Keccak_224:
612 case QCryptographicHash::RealSha3_256:
613 case QCryptographicHash::Keccak_256:
614 case QCryptographicHash::RealSha3_384:
615 case QCryptographicHash::Keccak_384:
616 case QCryptographicHash::RealSha3_512:
617 case QCryptographicHash::Keccak_512:
620 case QCryptographicHash::Blake2b_160:
621 case QCryptographicHash::Blake2b_256:
622 case QCryptographicHash::Blake2b_384:
623 case QCryptographicHash::Blake2b_512:
624 new (&blake2bContext) blake2b_state;
626 case QCryptographicHash::Blake2s_128:
627 case QCryptographicHash::Blake2s_160:
628 case QCryptographicHash::Blake2s_224:
629 case QCryptographicHash::Blake2s_256:
630 new (&blake2sContext) blake2s_state;
632 case QCryptographicHash::NumAlgorithms:
640 static_assert(std::is_trivially_destructible_v<State>);
650#ifdef USING_OPENSSL30
652void QCryptographicHashPrivate::State::reset(QCryptographicHash::Algorithm method)
noexcept
655 case QCryptographicHash::Keccak_224:
656 case QCryptographicHash::Keccak_256:
657 case QCryptographicHash::Keccak_384:
658 case QCryptographicHash::Keccak_512:
659 sha3Init(&sha3Context, hashLengthInternal(method) * 8);
661 case QCryptographicHash::Blake2b_160:
662 case QCryptographicHash::Blake2b_256:
663 case QCryptographicHash::Blake2b_384:
664 blake2b_init(&blake2bContext, hashLengthInternal(method));
666 case QCryptographicHash::Blake2s_128:
667 case QCryptographicHash::Blake2s_160:
668 case QCryptographicHash::Blake2s_224:
669 blake2s_init(&blake2sContext, hashLengthInternal(method));
671 case QCryptographicHash::Sha1:
672 case QCryptographicHash::Md4:
673 case QCryptographicHash::Md5:
674 case QCryptographicHash::Sha224:
675 case QCryptographicHash::Sha256:
676 case QCryptographicHash::Sha384:
677 case QCryptographicHash::Sha512:
678 case QCryptographicHash::RealSha3_224:
679 case QCryptographicHash::RealSha3_256:
680 case QCryptographicHash::RealSha3_384:
681 case QCryptographicHash::RealSha3_512:
682 case QCryptographicHash::Blake2b_512:
683 case QCryptographicHash::Blake2s_256:
686 case QCryptographicHash::NumAlgorithms:
691void QCryptographicHashPrivate::EVP::reset()
noexcept
693 if (!initializationFailed) {
697 EVP_MD_CTX_reset(context.get());
698 initializationFailed = !EVP_DigestInit_ex(context.get(), algorithm.get(),
nullptr);
708 case QCryptographicHash::Sha1:
709 sha1InitState(&sha1Context);
711 case QCryptographicHash::Md4:
712 md4_init(&md4Context);
714 case QCryptographicHash::Md5:
715 MD5Init(&md5Context);
717 case QCryptographicHash::Sha224:
720 case QCryptographicHash::Sha256:
723 case QCryptographicHash::Sha384:
726 case QCryptographicHash::Sha512:
729 case QCryptographicHash::RealSha3_224:
730 case QCryptographicHash::Keccak_224:
731 case QCryptographicHash::RealSha3_256:
732 case QCryptographicHash::Keccak_256:
733 case QCryptographicHash::RealSha3_384:
734 case QCryptographicHash::Keccak_384:
735 case QCryptographicHash::RealSha3_512:
736 case QCryptographicHash::Keccak_512:
737 sha3Init(&sha3Context, hashLengthInternal(method) * 8);
739 case QCryptographicHash::Blake2b_160:
740 case QCryptographicHash::Blake2b_256:
741 case QCryptographicHash::Blake2b_384:
742 case QCryptographicHash::Blake2b_512:
743 blake2b_init(&blake2bContext, hashLengthInternal(method));
745 case QCryptographicHash::Blake2s_128:
746 case QCryptographicHash::Blake2s_160:
747 case QCryptographicHash::Blake2s_224:
748 case QCryptographicHash::Blake2s_256:
749 blake2s_init(&blake2sContext, hashLengthInternal(method));
751 case QCryptographicHash::NumAlgorithms:
758#if QT_DEPRECATED_SINCE(6
, 4
)
760
761
762
763
764
765
766void QCryptographicHash::addData(
const char *data, qsizetype length)
768 Q_ASSERT(length >= 0);
769 addData(QByteArrayView{data, length});
774
775
776
777
778
779void QCryptographicHash::addData(QByteArrayView bytes)
noexcept
786 state.addData(method, bytes);
790#ifdef USING_OPENSSL30
792void QCryptographicHashPrivate::State::addData(QCryptographicHash::Algorithm method,
793 QByteArrayView bytes)
noexcept
795 const char *data = bytes.data();
796 auto length = bytes.size();
799 case QCryptographicHash::Keccak_224:
800 case QCryptographicHash::Keccak_256:
801 case QCryptographicHash::Keccak_384:
802 case QCryptographicHash::Keccak_512:
803 sha3Update(&sha3Context,
reinterpret_cast<
const BitSequence *>(data), uint64_t(length) * 8);
805 case QCryptographicHash::Blake2b_160:
806 case QCryptographicHash::Blake2b_256:
807 case QCryptographicHash::Blake2b_384:
808 blake2b_update(&blake2bContext,
reinterpret_cast<
const uint8_t *>(data), length);
810 case QCryptographicHash::Blake2s_128:
811 case QCryptographicHash::Blake2s_160:
812 case QCryptographicHash::Blake2s_224:
813 blake2s_update(&blake2sContext,
reinterpret_cast<
const uint8_t *>(data), length);
815 case QCryptographicHash::Sha1:
816 case QCryptographicHash::Md4:
817 case QCryptographicHash::Md5:
818 case QCryptographicHash::Sha224:
819 case QCryptographicHash::Sha256:
820 case QCryptographicHash::Sha384:
821 case QCryptographicHash::Sha512:
822 case QCryptographicHash::RealSha3_224:
823 case QCryptographicHash::RealSha3_256:
824 case QCryptographicHash::RealSha3_384:
825 case QCryptographicHash::RealSha3_512:
826 case QCryptographicHash::Blake2b_512:
827 case QCryptographicHash::Blake2s_256:
828 if (!evp.initializationFailed)
829 EVP_DigestUpdate(evp.context.get(), (
const unsigned char *)data, length);
831 case QCryptographicHash::NumAlgorithms:
839 QByteArrayView bytes)
noexcept
841 const char *data = bytes.data();
842 auto length = bytes.size();
844#if QT_POINTER_SIZE == 8
848 for (
auto remaining = length; remaining; remaining -= length, data += length) {
849 length = qMin(qsizetype(std::numeric_limits<uint>::max()), remaining);
854 case QCryptographicHash::Sha1:
855 sha1Update(&sha1Context, (
const unsigned char *)data, length);
857 case QCryptographicHash::Md4:
858 md4_update(&md4Context, (
const unsigned char *)data, length);
860 case QCryptographicHash::Md5:
861 MD5Update(&md5Context, (
const unsigned char *)data, length);
863 case QCryptographicHash::Sha224:
864 SHA224Input(&
sha224Context,
reinterpret_cast<
const unsigned char *>(data), length);
866 case QCryptographicHash::Sha256:
867 SHA256Input(&
sha256Context,
reinterpret_cast<
const unsigned char *>(data), length);
869 case QCryptographicHash::Sha384:
870 SHA384Input(&
sha384Context,
reinterpret_cast<
const unsigned char *>(data), length);
872 case QCryptographicHash::Sha512:
873 SHA512Input(&
sha512Context,
reinterpret_cast<
const unsigned char *>(data), length);
875 case QCryptographicHash::RealSha3_224:
876 case QCryptographicHash::Keccak_224:
877 case QCryptographicHash::RealSha3_256:
878 case QCryptographicHash::Keccak_256:
879 case QCryptographicHash::RealSha3_384:
880 case QCryptographicHash::Keccak_384:
881 case QCryptographicHash::RealSha3_512:
882 case QCryptographicHash::Keccak_512:
885 case QCryptographicHash::Blake2b_160:
886 case QCryptographicHash::Blake2b_256:
887 case QCryptographicHash::Blake2b_384:
888 case QCryptographicHash::Blake2b_512:
889 blake2b_update(&blake2bContext,
reinterpret_cast<
const uint8_t *>(data), length);
891 case QCryptographicHash::Blake2s_128:
892 case QCryptographicHash::Blake2s_160:
893 case QCryptographicHash::Blake2s_224:
894 case QCryptographicHash::Blake2s_256:
895 blake2s_update(&blake2sContext,
reinterpret_cast<
const uint8_t *>(data), length);
897 case QCryptographicHash::NumAlgorithms:
905
906
907
908
909bool QCryptographicHash::addData(QIODevice *device)
911 return d->addData(device);
916 if (!device->isReadable())
919 if (!device->isOpen())
925 while ((length = device->read(buffer,
sizeof(buffer))) > 0)
928 return device->atEnd();
933
934
935
936
937QByteArray QCryptographicHash::result()
const
939 return resultView().toByteArray();
943
944
945
946
947
948
949
950
951
952QByteArrayView QCryptographicHash::resultView()
const noexcept
957 return d->resultView();
961
962
963
964
967 const auto lock = qt_scoped_lock(finalizeMutex);
969 if (!result.isEmpty())
975
976
977
978
979
982 result.resizeForOverwrite(hashLengthInternal(method));
983 state.finalizeUnchecked(method, result);
987
988
989
990
991
994 buffer = buffer.first(hashLengthInternal(method));
995 state.finalizeUnchecked(method, buffer);
996 Q_ASSERT(result.size() == 0);
1000#ifdef USING_OPENSSL30
1001void QCryptographicHashPrivate::State::finalizeUnchecked(QCryptographicHash::Algorithm method,
1002 QSpan<uchar> result)
noexcept
1005 case QCryptographicHash::Keccak_224:
1006 case QCryptographicHash::Keccak_256:
1007 case QCryptographicHash::Keccak_384:
1008 case QCryptographicHash::Keccak_512: {
1009 SHA3Context copy = sha3Context;
1010 sha3Finish(copy, result, Sha3Variant::Keccak);
1013 case QCryptographicHash::Blake2b_160:
1014 case QCryptographicHash::Blake2b_256:
1015 case QCryptographicHash::Blake2b_384: {
1016 const auto length = hashLengthInternal(method);
1017 blake2b_state copy = blake2bContext;
1018 blake2b_final(©, result.data(), length);
1021 case QCryptographicHash::Blake2s_128:
1022 case QCryptographicHash::Blake2s_160:
1023 case QCryptographicHash::Blake2s_224: {
1024 const auto length = hashLengthInternal(method);
1025 blake2s_state copy = blake2sContext;
1026 blake2s_final(©, result.data(), length);
1029 case QCryptographicHash::Sha1:
1030 case QCryptographicHash::Md4:
1031 case QCryptographicHash::Md5:
1032 case QCryptographicHash::Sha224:
1033 case QCryptographicHash::Sha256:
1034 case QCryptographicHash::Sha384:
1035 case QCryptographicHash::Sha512:
1036 case QCryptographicHash::RealSha3_224:
1037 case QCryptographicHash::RealSha3_256:
1038 case QCryptographicHash::RealSha3_384:
1039 case QCryptographicHash::RealSha3_512:
1040 case QCryptographicHash::Blake2b_512:
1041 case QCryptographicHash::Blake2s_256:
1042 evp.finalizeUnchecked(result);
1044 case QCryptographicHash::NumAlgorithms:
1049void QCryptographicHashPrivate::EVP::finalizeUnchecked(QSpan<uchar> result)
noexcept
1051 if (!initializationFailed) {
1052 EVP_MD_CTX_ptr copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
1053 EVP_MD_CTX_copy_ex(copy.get(), context.get());
1054 Q_ASSERT(result.size() == EVP_MD_get_size(algorithm.get()));
1055 EVP_DigestFinal_ex(copy.get(), result.data(),
nullptr);
1062 QSpan<uchar> result)
noexcept
1065 case QCryptographicHash::Sha1: {
1066 Sha1State copy = sha1Context;
1067 sha1FinalizeState(©);
1068 sha1ToHash(©, result.data());
1071 case QCryptographicHash::Md4: {
1072 md4_context copy = md4Context;
1073 md4_final(©, result.data());
1076 case QCryptographicHash::Md5: {
1077 MD5Context copy = md5Context;
1078 MD5Final(©, result.data());
1081 case QCryptographicHash::Sha224: {
1083 SHA224Result(©, result.data());
1086 case QCryptographicHash::Sha256: {
1088 SHA256Result(©, result.data());
1091 case QCryptographicHash::Sha384: {
1093 SHA384Result(©, result.data());
1096 case QCryptographicHash::Sha512: {
1098 SHA512Result(©, result.data());
1101 case QCryptographicHash::RealSha3_224:
1102 case QCryptographicHash::RealSha3_256:
1103 case QCryptographicHash::RealSha3_384:
1104 case QCryptographicHash::RealSha3_512: {
1106 sha3Finish(copy, result, Sha3Variant::Sha3);
1109 case QCryptographicHash::Keccak_224:
1110 case QCryptographicHash::Keccak_256:
1111 case QCryptographicHash::Keccak_384:
1112 case QCryptographicHash::Keccak_512: {
1114 sha3Finish(copy, result, Sha3Variant::Keccak);
1117 case QCryptographicHash::Blake2b_160:
1118 case QCryptographicHash::Blake2b_256:
1119 case QCryptographicHash::Blake2b_384:
1120 case QCryptographicHash::Blake2b_512: {
1121 const auto length = hashLengthInternal(method);
1122 blake2b_state copy = blake2bContext;
1123 blake2b_final(©, result.data(), length);
1126 case QCryptographicHash::Blake2s_128:
1127 case QCryptographicHash::Blake2s_160:
1128 case QCryptographicHash::Blake2s_224:
1129 case QCryptographicHash::Blake2s_256: {
1130 const auto length = hashLengthInternal(method);
1131 blake2s_state copy = blake2sContext;
1132 blake2s_final(©, result.data(), length);
1135 case QCryptographicHash::NumAlgorithms:
1142
1143
1144
1145
1146
1147
1148
1149QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method)
1151 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1152 [[maybe_unused]]
const auto r = hashInto(ba, data, method);
1153 Q_ASSERT(r.size() == ba.size());
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer,
1176 QSpan<
const QByteArrayView> data,
1177 Algorithm method)
noexcept
1179 if (buffer.size() < hashLengthInternal(method))
1182 QCryptographicHashPrivate hash(method);
1183 for (QByteArrayView part : data)
1185 auto span = QSpan{
reinterpret_cast<uchar *>(buffer.data()), buffer.size()};
1186 return hash.finalizeUnchecked(span);
1190
1191
1192
1193
1194int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
1196 return hashLengthInternal(method);
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1212bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)
1214 return QCryptographicHashPrivate::supportsAlgorithm(method);
1217#ifdef USING_OPENSSL30
1218bool QCryptographicHashPrivate::supportsAlgorithm(QCryptographicHash::Algorithm method)
1224 case QCryptographicHash::Keccak_224:
1225 case QCryptographicHash::Keccak_256:
1226 case QCryptographicHash::Keccak_384:
1227 case QCryptographicHash::Keccak_512:
1228 case QCryptographicHash::Blake2b_160:
1229 case QCryptographicHash::Blake2b_256:
1230 case QCryptographicHash::Blake2b_384:
1231 case QCryptographicHash::Blake2s_128:
1232 case QCryptographicHash::Blake2s_160:
1233 case QCryptographicHash::Blake2s_224:
1235 case QCryptographicHash::Sha1:
1236 case QCryptographicHash::Md4:
1237 case QCryptographicHash::Md5:
1238 case QCryptographicHash::Sha224:
1239 case QCryptographicHash::Sha256:
1240 case QCryptographicHash::Sha384:
1241 case QCryptographicHash::Sha512:
1242 case QCryptographicHash::RealSha3_224:
1243 case QCryptographicHash::RealSha3_256:
1244 case QCryptographicHash::RealSha3_384:
1245 case QCryptographicHash::RealSha3_512:
1246 case QCryptographicHash::Blake2b_512:
1247 case QCryptographicHash::Blake2s_256: {
1248 auto legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"legacy"));
1249 auto defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_load(
nullptr,
"default"));
1251 const char *restriction =
"-fips";
1252 EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method), restriction));
1254 return algorithm !=
nullptr;
1257 case QCryptographicHash::NumAlgorithms:
1267 case QCryptographicHash::Sha1:
1268 case QCryptographicHash::Md4:
1269 case QCryptographicHash::Md5:
1270 case QCryptographicHash::Sha224:
1271 case QCryptographicHash::Sha256:
1272 case QCryptographicHash::Sha384:
1273 case QCryptographicHash::Sha512:
1274 case QCryptographicHash::RealSha3_224:
1275 case QCryptographicHash::Keccak_224:
1276 case QCryptographicHash::RealSha3_256:
1277 case QCryptographicHash::Keccak_256:
1278 case QCryptographicHash::RealSha3_384:
1279 case QCryptographicHash::Keccak_384:
1280 case QCryptographicHash::RealSha3_512:
1281 case QCryptographicHash::Keccak_512:
1282 case QCryptographicHash::Blake2b_160:
1283 case QCryptographicHash::Blake2b_256:
1284 case QCryptographicHash::Blake2b_384:
1285 case QCryptographicHash::Blake2b_512:
1286 case QCryptographicHash::Blake2s_128:
1287 case QCryptographicHash::Blake2s_160:
1288 case QCryptographicHash::Blake2s_224:
1289 case QCryptographicHash::Blake2s_256:
1291 case QCryptographicHash::NumAlgorithms: ;
1300 case QCryptographicHash::Sha1:
1301 return SHA1_Message_Block_Size;
1302 case QCryptographicHash::Md4:
1304 case QCryptographicHash::Md5:
1306 case QCryptographicHash::Sha224:
1307 return SHA224_Message_Block_Size;
1308 case QCryptographicHash::Sha256:
1309 return SHA256_Message_Block_Size;
1310 case QCryptographicHash::Sha384:
1311 return SHA384_Message_Block_Size;
1312 case QCryptographicHash::Sha512:
1313 return SHA512_Message_Block_Size;
1314 case QCryptographicHash::RealSha3_224:
1315 case QCryptographicHash::Keccak_224:
1317 case QCryptographicHash::RealSha3_256:
1318 case QCryptographicHash::Keccak_256:
1320 case QCryptographicHash::RealSha3_384:
1321 case QCryptographicHash::Keccak_384:
1323 case QCryptographicHash::RealSha3_512:
1324 case QCryptographicHash::Keccak_512:
1326 case QCryptographicHash::Blake2b_160:
1327 case QCryptographicHash::Blake2b_256:
1328 case QCryptographicHash::Blake2b_384:
1329 case QCryptographicHash::Blake2b_512:
1330 return BLAKE2B_BLOCKBYTES;
1331 case QCryptographicHash::Blake2s_128:
1332 case QCryptographicHash::Blake2s_160:
1333 case QCryptographicHash::Blake2s_224:
1334 case QCryptographicHash::Blake2s_256:
1335 return BLAKE2S_BLOCKBYTES;
1336 case QCryptographicHash::NumAlgorithms:
1337#if !defined(Q_CC_GNU_ONLY) || Q_CC_GNU >= 900
1349 using A = QCryptographicHash::Algorithm;
1350 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1351 result = std::max(result, qt_hash_block_size(A(i)));
1358 int result = INT_MAX;
1359 using A = QCryptographicHash::Algorithm;
1360 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1361 result = std::min(result, qt_hash_block_size(A(i)));
1369 using A = QCryptographicHash::Algorithm;
1370 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1371 result = std::gcd(result, qt_hash_block_size(A(i)));
1375using HashBlock = QSmallByteArray<maxHashBlockSize()>;
1384 result.resizeForOverwrite(block.size());
1385 for (qsizetype i = 0; i < block.size(); ++i)
1386 result[i] = block[i] ^ val;
1412
1413
1414
1415
1416
1417
1418
1419
1422 const int blockSize = qt_hash_block_size(messageHash.method);
1424 if (newKey.size() > blockSize) {
1425 messageHash.addData(newKey);
1426 messageHash.finalizeUnchecked();
1428 using A = QCryptographicHash::Algorithm;
1429 for (
int i = 0; i < A::NumAlgorithms; ++i) {
1430 if (hashLengthInternal(A(i)) > qt_hash_block_size(A(i)))
1434 }(),
"this code assumes that a hash's result always fits into that hash's block size");
1435 key = messageHash.result;
1436 messageHash.reset();
1441 if (key.size() < blockSize)
1442 key.resize(blockSize,
'\0');
1448
1449
1450
1451
1452
1453
1454
1457 messageHash.addData(xored(key, 0x36));
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
1497
1498
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
1515 : d(
new QMessageAuthenticationCodePrivate(method))
1521
1522
1523QMessageAuthenticationCode::~QMessageAuthenticationCode()
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1553
1554
1555
1556
1559
1560
1561void QMessageAuthenticationCode::reset()
noexcept
1563 d->messageHash.reset();
1564 d->initMessageHash();
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
1597
1598
1599void QMessageAuthenticationCode::setKey(QByteArrayView key)
noexcept
1601 d->messageHash.reset();
1606
1607
1608
1609void QMessageAuthenticationCode::addData(
const char *data, qsizetype length)
1611 d->messageHash.addData({data, length});
1615
1616
1617
1618
1619
1620
1621void QMessageAuthenticationCode::addData(QByteArrayView data)
noexcept
1623 d->messageHash.addData(data);
1627
1628
1629
1630
1631
1632bool QMessageAuthenticationCode::addData(QIODevice *device)
1634 return d->messageHash.addData(device);
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647QByteArrayView QMessageAuthenticationCode::resultView()
const noexcept
1650 return d->messageHash.resultView();
1654
1655
1656
1657
1658QByteArray QMessageAuthenticationCode::result()
const
1660 return resultView().toByteArray();
1665 const auto lock = qt_scoped_lock(messageHash.finalizeMutex);
1666 if (!messageHash.result.isEmpty())
1673 messageHash.finalizeUnchecked();
1674 const HashResult hashedMessage = messageHash.result;
1676 messageHash.reset();
1677 messageHash.addData(xored(key, 0x5c));
1678 messageHash.addData(hashedMessage);
1679 messageHash.finalizeUnchecked();
1683
1684
1685
1686
1687
1688
1689
1690QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key,
1691 QCryptographicHash::Algorithm method)
1693 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1694 [[maybe_unused]]
const auto r = hashInto(ba, message, key, method);
1695 Q_ASSERT(r.size() == ba.size());
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer,
1718 QSpan<
const QByteArrayView> messageParts,
1720 QCryptographicHash::Algorithm method)
noexcept
1722 QMessageAuthenticationCodePrivate mac(method);
1724 for (QByteArrayView part : messageParts)
1725 mac.messageHash.addData(part);
1726 mac.finalizeUnchecked();
1727 auto result = mac.messageHash.resultView();
1728 if (buffer.size() < result.size())
1731 memcpy(buffer.data(), result.data(), result.size());
1732 return buffer.first(result.size());
1737#ifndef QT_NO_QOBJECT
1738#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