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())
931 while ((length = device->read(buffer,
sizeof(buffer))) > 0)
934 return device->atEnd();
939
940
941
942
943QByteArray QCryptographicHash::result()
const
945 return resultView().toByteArray();
949
950
951
952
953
954
955
956
957
958QByteArrayView QCryptographicHash::resultView()
const noexcept
963 return d->resultView();
967
968
969
970
973 const auto lock = qt_scoped_lock(finalizeMutex);
975 if (!result.isEmpty())
981
982
983
984
985
988 result.resizeForOverwrite(hashLengthInternal(method));
989 state.finalizeUnchecked(method, result);
993
994
995
996
997
1000 buffer = buffer.first(hashLengthInternal(method));
1001 state.finalizeUnchecked(method, buffer);
1002 Q_ASSERT(result.size() == 0);
1006#ifdef USING_OPENSSL30
1007void QCryptographicHashPrivate::State::finalizeUnchecked(QCryptographicHash::Algorithm method,
1008 QSpan<uchar> result)
noexcept
1011 case QCryptographicHash::Keccak_224:
1012 case QCryptographicHash::Keccak_256:
1013 case QCryptographicHash::Keccak_384:
1014 case QCryptographicHash::Keccak_512: {
1015 SHA3Context copy = sha3Context;
1016 sha3Finish(copy, result, Sha3Variant::Keccak);
1019 case QCryptographicHash::Blake2b_160:
1020 case QCryptographicHash::Blake2b_256:
1021 case QCryptographicHash::Blake2b_384: {
1022 const auto length = hashLengthInternal(method);
1023 blake2b_state copy = blake2bContext;
1024 blake2b_final(©, result.data(), length);
1027 case QCryptographicHash::Blake2s_128:
1028 case QCryptographicHash::Blake2s_160:
1029 case QCryptographicHash::Blake2s_224: {
1030 const auto length = hashLengthInternal(method);
1031 blake2s_state copy = blake2sContext;
1032 blake2s_final(©, result.data(), length);
1035 case QCryptographicHash::Sha1:
1036 case QCryptographicHash::Md4:
1037 case QCryptographicHash::Md5:
1038 case QCryptographicHash::Sha224:
1039 case QCryptographicHash::Sha256:
1040 case QCryptographicHash::Sha384:
1041 case QCryptographicHash::Sha512:
1042 case QCryptographicHash::RealSha3_224:
1043 case QCryptographicHash::RealSha3_256:
1044 case QCryptographicHash::RealSha3_384:
1045 case QCryptographicHash::RealSha3_512:
1046 case QCryptographicHash::Blake2b_512:
1047 case QCryptographicHash::Blake2s_256:
1048 evp.finalizeUnchecked(result);
1050 case QCryptographicHash::NumAlgorithms:
1055void QCryptographicHashPrivate::EVP::finalizeUnchecked(QSpan<uchar> result)
noexcept
1057 if (!initializationFailed) {
1058 EVP_MD_CTX_ptr copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new());
1059 EVP_MD_CTX_copy_ex(copy.get(), context.get());
1060 Q_ASSERT(result.size() == EVP_MD_get_size(algorithm.get()));
1061 EVP_DigestFinal_ex(copy.get(), result.data(),
nullptr);
1068 QSpan<uchar> result)
noexcept
1071 case QCryptographicHash::Sha1: {
1072 Sha1State copy = sha1Context;
1073 sha1FinalizeState(©);
1074 sha1ToHash(©, result.data());
1077 case QCryptographicHash::Md4: {
1078 md4_context copy = md4Context;
1079 md4_final(©, result.data());
1082 case QCryptographicHash::Md5: {
1083 MD5Context copy = md5Context;
1084 MD5Final(©, result.data());
1087 case QCryptographicHash::Sha224: {
1089 SHA224Result(©, result.data());
1092 case QCryptographicHash::Sha256: {
1094 SHA256Result(©, result.data());
1097 case QCryptographicHash::Sha384: {
1099 SHA384Result(©, result.data());
1102 case QCryptographicHash::Sha512: {
1104 SHA512Result(©, result.data());
1107 case QCryptographicHash::RealSha3_224:
1108 case QCryptographicHash::RealSha3_256:
1109 case QCryptographicHash::RealSha3_384:
1110 case QCryptographicHash::RealSha3_512: {
1112 sha3Finish(copy, result, Sha3Variant::Sha3);
1115 case QCryptographicHash::Keccak_224:
1116 case QCryptographicHash::Keccak_256:
1117 case QCryptographicHash::Keccak_384:
1118 case QCryptographicHash::Keccak_512: {
1120 sha3Finish(copy, result, Sha3Variant::Keccak);
1123 case QCryptographicHash::Blake2b_160:
1124 case QCryptographicHash::Blake2b_256:
1125 case QCryptographicHash::Blake2b_384:
1126 case QCryptographicHash::Blake2b_512: {
1127 const auto length = hashLengthInternal(method);
1128 blake2b_state copy = blake2bContext;
1129 blake2b_final(©, result.data(), length);
1132 case QCryptographicHash::Blake2s_128:
1133 case QCryptographicHash::Blake2s_160:
1134 case QCryptographicHash::Blake2s_224:
1135 case QCryptographicHash::Blake2s_256: {
1136 const auto length = hashLengthInternal(method);
1137 blake2s_state copy = blake2sContext;
1138 blake2s_final(©, result.data(), length);
1141 case QCryptographicHash::NumAlgorithms:
1148
1149
1150
1151
1152
1153
1154
1155QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method)
1157 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1158 [[maybe_unused]]
const auto r = hashInto(ba, data, method);
1159 Q_ASSERT(r.size() == ba.size());
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181QByteArrayView QCryptographicHash::hashInto(QSpan<std::byte> buffer,
1182 QSpan<
const QByteArrayView> data,
1183 Algorithm method)
noexcept
1185 if (buffer.size() < hashLengthInternal(method))
1188 QCryptographicHashPrivate hash(method);
1189 for (QByteArrayView part : data)
1191 auto span = QSpan{
reinterpret_cast<uchar *>(buffer.data()), buffer.size()};
1192 return hash.finalizeUnchecked(span);
1196
1197
1198
1199
1200int QCryptographicHash::hashLength(QCryptographicHash::Algorithm method)
1202 return hashLengthInternal(method);
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1218bool QCryptographicHash::supportsAlgorithm(QCryptographicHash::Algorithm method)
1220 return QCryptographicHashPrivate::supportsAlgorithm(method);
1223#ifdef USING_OPENSSL30
1224bool QCryptographicHashPrivate::supportsAlgorithm(QCryptographicHash::Algorithm method)
1230 case QCryptographicHash::Keccak_224:
1231 case QCryptographicHash::Keccak_256:
1232 case QCryptographicHash::Keccak_384:
1233 case QCryptographicHash::Keccak_512:
1234 case QCryptographicHash::Blake2b_160:
1235 case QCryptographicHash::Blake2b_256:
1236 case QCryptographicHash::Blake2b_384:
1237 case QCryptographicHash::Blake2s_128:
1238 case QCryptographicHash::Blake2s_160:
1239 case QCryptographicHash::Blake2s_224:
1241 case QCryptographicHash::Sha1:
1242 case QCryptographicHash::Md4:
1243 case QCryptographicHash::Md5:
1244 case QCryptographicHash::Sha224:
1245 case QCryptographicHash::Sha256:
1246 case QCryptographicHash::Sha384:
1247 case QCryptographicHash::Sha512:
1248 case QCryptographicHash::RealSha3_224:
1249 case QCryptographicHash::RealSha3_256:
1250 case QCryptographicHash::RealSha3_384:
1251 case QCryptographicHash::RealSha3_512:
1252 case QCryptographicHash::Blake2b_512:
1253 case QCryptographicHash::Blake2s_256: {
1256 auto legacyProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_try_load(
nullptr,
"legacy", 1));
1257 auto defaultProvider = OSSL_PROVIDER_ptr(OSSL_PROVIDER_try_load(
nullptr,
"default", 1));
1259 const char *restriction =
"-fips";
1260 EVP_MD_ptr algorithm = EVP_MD_ptr(EVP_MD_fetch(
nullptr, methodToName(method), restriction));
1262 return algorithm !=
nullptr;
1265 case QCryptographicHash::NumAlgorithms:
1275 case QCryptographicHash::Sha1:
1276 case QCryptographicHash::Md4:
1277 case QCryptographicHash::Md5:
1278 case QCryptographicHash::Sha224:
1279 case QCryptographicHash::Sha256:
1280 case QCryptographicHash::Sha384:
1281 case QCryptographicHash::Sha512:
1282 case QCryptographicHash::RealSha3_224:
1283 case QCryptographicHash::Keccak_224:
1284 case QCryptographicHash::RealSha3_256:
1285 case QCryptographicHash::Keccak_256:
1286 case QCryptographicHash::RealSha3_384:
1287 case QCryptographicHash::Keccak_384:
1288 case QCryptographicHash::RealSha3_512:
1289 case QCryptographicHash::Keccak_512:
1290 case QCryptographicHash::Blake2b_160:
1291 case QCryptographicHash::Blake2b_256:
1292 case QCryptographicHash::Blake2b_384:
1293 case QCryptographicHash::Blake2b_512:
1294 case QCryptographicHash::Blake2s_128:
1295 case QCryptographicHash::Blake2s_160:
1296 case QCryptographicHash::Blake2s_224:
1297 case QCryptographicHash::Blake2s_256:
1299 case QCryptographicHash::NumAlgorithms: ;
1308 case QCryptographicHash::Sha1:
1309 return SHA1_Message_Block_Size;
1310 case QCryptographicHash::Md4:
1312 case QCryptographicHash::Md5:
1314 case QCryptographicHash::Sha224:
1315 return SHA224_Message_Block_Size;
1316 case QCryptographicHash::Sha256:
1317 return SHA256_Message_Block_Size;
1318 case QCryptographicHash::Sha384:
1319 return SHA384_Message_Block_Size;
1320 case QCryptographicHash::Sha512:
1321 return SHA512_Message_Block_Size;
1322 case QCryptographicHash::RealSha3_224:
1323 case QCryptographicHash::Keccak_224:
1325 case QCryptographicHash::RealSha3_256:
1326 case QCryptographicHash::Keccak_256:
1328 case QCryptographicHash::RealSha3_384:
1329 case QCryptographicHash::Keccak_384:
1331 case QCryptographicHash::RealSha3_512:
1332 case QCryptographicHash::Keccak_512:
1334 case QCryptographicHash::Blake2b_160:
1335 case QCryptographicHash::Blake2b_256:
1336 case QCryptographicHash::Blake2b_384:
1337 case QCryptographicHash::Blake2b_512:
1338 return BLAKE2B_BLOCKBYTES;
1339 case QCryptographicHash::Blake2s_128:
1340 case QCryptographicHash::Blake2s_160:
1341 case QCryptographicHash::Blake2s_224:
1342 case QCryptographicHash::Blake2s_256:
1343 return BLAKE2S_BLOCKBYTES;
1344 case QCryptographicHash::NumAlgorithms:
1345#if !defined(Q_CC_GNU_ONLY) || Q_CC_GNU >= 900
1357 using A = QCryptographicHash::Algorithm;
1358 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1359 result =
std::max(result, qt_hash_block_size(A(i)));
1366 int result = INT_MAX;
1367 using A = QCryptographicHash::Algorithm;
1368 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1369 result =
std::min(result, qt_hash_block_size(A(i)));
1377 using A = QCryptographicHash::Algorithm;
1378 for (
int i = 0; i < A::NumAlgorithms ; ++i)
1379 result =
std::gcd(result, qt_hash_block_size(A(i)));
1383using HashBlock = QSmallByteArray<maxHashBlockSize()>;
1392 result.resizeForOverwrite(block.size());
1393 for (qsizetype i = 0; i < block.size(); ++i)
1394 result[i] = block[i] ^ val;
1420
1421
1422
1423
1424
1425
1426
1427
1430 const int blockSize = qt_hash_block_size(messageHash.method);
1432 if (newKey.size() > blockSize) {
1433 messageHash.addData(newKey);
1434 messageHash.finalizeUnchecked();
1436 using A = QCryptographicHash::Algorithm;
1437 for (
int i = 0; i < A::NumAlgorithms; ++i) {
1438 if (hashLengthInternal(A(i)) > qt_hash_block_size(A(i)))
1442 }(),
"this code assumes that a hash's result always fits into that hash's block size");
1443 key = messageHash.result;
1444 messageHash.reset();
1449 if (key.size() < blockSize)
1450 key.resize(blockSize,
'\0');
1456
1457
1458
1459
1460
1461
1462
1465 messageHash.addData(xored(key, 0x36));
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
1499
1500
1501
1502
1503
1504
1505
1506
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
1523 : d(
new QMessageAuthenticationCodePrivate(method))
1529
1530
1531QMessageAuthenticationCode::~QMessageAuthenticationCode()
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1561
1562
1563
1564
1567
1568
1569void QMessageAuthenticationCode::reset()
noexcept
1571 d->messageHash.reset();
1572 d->initMessageHash();
1576
1577
1578
1579
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
1607void QMessageAuthenticationCode::setKey(QByteArrayView key)
noexcept
1609 d->messageHash.reset();
1614
1615
1616
1617void QMessageAuthenticationCode::addData(
const char *data, qsizetype length)
1619 d->messageHash.addData({data, length});
1623
1624
1625
1626
1627
1628
1629void QMessageAuthenticationCode::addData(QByteArrayView data)
noexcept
1631 d->messageHash.addData(data);
1635
1636
1637
1638
1639
1640bool QMessageAuthenticationCode::addData(QIODevice *device)
1642 return d->messageHash.addData(device);
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655QByteArrayView QMessageAuthenticationCode::resultView()
const noexcept
1658 return d->messageHash.resultView();
1662
1663
1664
1665
1666QByteArray QMessageAuthenticationCode::result()
const
1668 return resultView().toByteArray();
1673 const auto lock = qt_scoped_lock(messageHash.finalizeMutex);
1674 if (!messageHash.result.isEmpty())
1681 messageHash.finalizeUnchecked();
1682 const HashResult hashedMessage = messageHash.result;
1684 messageHash.reset();
1685 messageHash.addData(xored(key, 0x5c));
1686 messageHash.addData(hashedMessage);
1687 messageHash.finalizeUnchecked();
1691
1692
1693
1694
1695
1696
1697
1698QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key,
1699 QCryptographicHash::Algorithm method)
1701 QByteArray ba(hashLengthInternal(method), Qt::Uninitialized);
1702 [[maybe_unused]]
const auto r = hashInto(ba, message, key, method);
1703 Q_ASSERT(r.size() == ba.size());
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer,
1726 QSpan<
const QByteArrayView> messageParts,
1728 QCryptographicHash::Algorithm method)
noexcept
1730 QMessageAuthenticationCodePrivate mac(method);
1732 for (QByteArrayView part : messageParts)
1733 mac.messageHash.addData(part);
1734 mac.finalizeUnchecked();
1735 auto result = mac.messageHash.resultView();
1736 if (buffer.size() < result.size())
1739 memcpy(buffer.data(), result.data(), result.size());
1740 return buffer.first(result.size());
1745#ifndef QT_NO_QOBJECT
1746#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