summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qcryptographichash.cpp
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2017-04-07 10:18:50 +0200
committerLiang Qi <liang.qi@qt.io>2017-04-07 10:24:33 +0200
commit5d6073be27cb951b692bbcc4dc0dd54cc09c9fd4 (patch)
treec87c236cd1b9c15eb5f40df3d86915687ab6c59a /src/corelib/tools/qcryptographichash.cpp
parent97d7d80e7355019bb23bb03c2a82908e4436deb0 (diff)
parentbbb67ca32cebad312f02e916dff54e591b92af24 (diff)
Merge remote-tracking branch 'origin/5.9' into dev
Conflicts: mkspecs/linux-icc/qmake.conf mkspecs/macx-icc/qmake.conf mkspecs/win32-icc/qmake.conf src/gui/painting/qgrayraster.c Change-Id: Ib08c45ea3215be05f986ecb3e1f4b37d209aa775
Diffstat (limited to 'src/corelib/tools/qcryptographichash.cpp')
-rw-r--r--src/corelib/tools/qcryptographichash.cpp55
1 files changed, 42 insertions, 13 deletions
diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp
index 08f89d2f02..963a91b9a9 100644
--- a/src/corelib/tools/qcryptographichash.cpp
+++ b/src/corelib/tools/qcryptographichash.cpp
@@ -180,9 +180,45 @@ public:
SHA3Context sha3Context;
#endif
};
+#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
+ void sha3Finish(int bitCount);
+#endif
QByteArray result;
};
+#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
+void QCryptographicHashPrivate::sha3Finish(int bitCount)
+{
+ /*
+ FIPS 202 ยง6.1 defines SHA-3 in terms of calculating the Keccak function
+ over the original message with the two-bit suffix "01" appended to it.
+ This variable stores that suffix (and it's fed into the calculations
+ when the hash is returned to users).
+
+ Only 2 bits of this variable are actually used (see the call to sha3Update
+ below). The Keccak implementation we're using will actually use the
+ *leftmost* 2 bits, and interpret them right-to-left. In other words, the
+ bits must appear in order of *increasing* significance; and as the two most
+ significant bits of the byte -- the rightmost 6 are ignored. (Yes, this
+ seems self-contradictory, but it's the way it is...)
+
+ Overall, this means:
+ * the leftmost two bits must be "10" (not "01"!);
+ * we don't care what the other six bits are set to (they can be set to
+ any value), but we arbitrarily set them to 0;
+
+ and for an unsigned char this gives us 0b10'00'00'00, or 0x80.
+ */
+ static const unsigned char sha3FinalSuffix = 0x80;
+
+ result.resize(bitCount / 8);
+
+ SHA3Context copy = sha3Context;
+ sha3Update(&copy, reinterpret_cast<const BitSequence *>(&sha3FinalSuffix), 2);
+ sha3Final(&copy, reinterpret_cast<BitSequence *>(result.data()));
+}
+#endif
+
/*!
\class QCryptographicHash
\inmodule QtCore
@@ -196,7 +232,8 @@ public:
QCryptographicHash can be used to generate cryptographic hashes of binary or text data.
- Currently MD4, MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are supported.
+ Refer to the documentation of the \l QCryptographicHash::Algorithm enum for a
+ list of the supported algorithms.
*/
/*!
@@ -426,27 +463,19 @@ QByteArray QCryptographicHash::result() const
break;
}
case Sha3_224: {
- SHA3Context copy = d->sha3Context;
- d->result.resize(224/8);
- sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ d->sha3Finish(224);
break;
}
case Sha3_256: {
- SHA3Context copy = d->sha3Context;
- d->result.resize(256/8);
- sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ d->sha3Finish(256);
break;
}
case Sha3_384: {
- SHA3Context copy = d->sha3Context;
- d->result.resize(384/8);
- sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ d->sha3Finish(384);
break;
}
case Sha3_512: {
- SHA3Context copy = d->sha3Context;
- d->result.resize(512/8);
- sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ d->sha3Finish(512);
break;
}
#endif