summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qcryptographichash.cpp
diff options
context:
space:
mode:
authorRichard Moore <rich@kde.org>2013-02-03 11:33:26 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-09 15:58:09 +0100
commit20bde28448583eecb1e37b9492eb4dc73b1409fe (patch)
tree27a086a50f7b2f5ab1fcb2f366e3114921dc1dd3 /src/corelib/tools/qcryptographichash.cpp
parentd1acaf2b1c60f276dfbe4ed33eb3b6800d99fcbb (diff)
Add support for SHA3 to QCryptographicHash.
This commit adds SHA3 support to QCryptographicHash. Two implementations are provided, one optimised for 32 bit and one for 64 bits. The code has been written to make it easy to add further implementations, for example ones using NEON instructions on ARM. Change-Id: I3be9c45bbd4fcc2771d697e7f7ae74e48a831e8f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/corelib/tools/qcryptographichash.cpp')
-rw-r--r--src/corelib/tools/qcryptographichash.cpp97
1 files changed, 93 insertions, 4 deletions
diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp
index 3f17fe15aa..6704f14eb1 100644
--- a/src/corelib/tools/qcryptographichash.cpp
+++ b/src/corelib/tools/qcryptographichash.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2013 Richard J. Moore <rich@kde.org>.
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -47,6 +48,41 @@
#include "../../3rdparty/md4/md4.cpp"
#include "../../3rdparty/sha1/sha1.cpp"
+typedef unsigned char BitSequence;
+typedef unsigned long long DataLength;
+typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;
+
+#include "../../3rdparty/sha3/KeccakSponge.c"
+typedef spongeState hashState;
+
+#include "../../3rdparty/sha3/KeccakNISTInterface.c"
+
+/*
+ This lets us choose between SHA3 implementations at build time.
+ */
+typedef spongeState SHA3Context;
+typedef HashReturn (SHA3Init)(hashState *state, int hashbitlen);
+typedef HashReturn (SHA3Update)(hashState *state, const BitSequence *data, DataLength databitlen);
+typedef HashReturn (SHA3Final)(hashState *state, BitSequence *hashval);
+
+#if QT_POINTER_SIZE == 8 // 64 bit version
+
+#include "../../3rdparty/sha3/KeccakF-1600-opt64.c"
+
+static SHA3Init * const sha3Init = Init;
+static SHA3Update * const sha3Update = Update;
+static SHA3Final * const sha3Final = Final;
+
+#else // 32 bit optimised fallback
+
+#include "../../3rdparty/sha3/KeccakF-1600-opt32.c"
+
+static SHA3Init * const sha3Init = Init;
+static SHA3Update * const sha3Update = Update;
+static SHA3Final * const sha3Final = Final;
+
+#endif
+
/*
These #defines replace the typedefs needed by the RFC6234 code. Normally
the typedefs would come from from stdint.h, but since this header is not
@@ -115,6 +151,7 @@ public:
SHA256Context sha256Context;
SHA384Context sha384Context;
SHA512Context sha512Context;
+ SHA3Context sha3Context;
};
QByteArray result;
};
@@ -141,10 +178,14 @@ public:
\value Md4 Generate an MD4 hash sum
\value Md5 Generate an MD5 hash sum
\value Sha1 Generate an SHA-1 hash sum
- \value Sha224 Generate an SHA-224 hash sum. Introduced in Qt 5.0
- \value Sha256 Generate an SHA-256 hash sum. Introduced in Qt 5.0
- \value Sha384 Generate an SHA-384 hash sum. Introduced in Qt 5.0
- \value Sha512 Generate an SHA-512 hash sum. Introduced in Qt 5.0
+ \value Sha224 Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0
+ \value Sha256 Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0
+ \value Sha384 Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0
+ \value Sha512 Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0
+ \value Sha3_224 Generate an SHA3-224 hash sum. Introduced in Qt 5.1
+ \value Sha3_256 Generate an SHA3-256 hash sum. Introduced in Qt 5.1
+ \value Sha3_384 Generate an SHA3-384 hash sum. Introduced in Qt 5.1
+ \value Sha3_512 Generate an SHA3-512 hash sum. Introduced in Qt 5.1
*/
/*!
@@ -192,6 +233,18 @@ void QCryptographicHash::reset()
case Sha512:
SHA512Reset(&d->sha512Context);
break;
+ case Sha3_224:
+ sha3Init(&d->sha3Context, 224);
+ break;
+ case Sha3_256:
+ sha3Init(&d->sha3Context, 256);
+ break;
+ case Sha3_384:
+ sha3Init(&d->sha3Context, 384);
+ break;
+ case Sha3_512:
+ sha3Init(&d->sha3Context, 512);
+ break;
}
d->result.clear();
}
@@ -224,6 +277,18 @@ void QCryptographicHash::addData(const char *data, int length)
case Sha512:
SHA512Input(&d->sha512Context, reinterpret_cast<const unsigned char *>(data), length);
break;
+ case Sha3_224:
+ sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8);
+ break;
+ case Sha3_256:
+ sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8);
+ break;
+ case Sha3_384:
+ sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8);
+ break;
+ case Sha3_512:
+ sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), length*8);
+ break;
}
d->result.clear();
}
@@ -313,6 +378,30 @@ QByteArray QCryptographicHash::result() const
SHA512Result(&copy, reinterpret_cast<unsigned char *>(d->result.data()));
break;
}
+ case Sha3_224: {
+ SHA3Context copy = d->sha3Context;
+ d->result.resize(224/8);
+ sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ break;
+ }
+ case Sha3_256: {
+ SHA3Context copy = d->sha3Context;
+ d->result.resize(256/8);
+ sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ break;
+ }
+ case Sha3_384: {
+ SHA3Context copy = d->sha3Context;
+ d->result.resize(384/8);
+ sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ break;
+ }
+ case Sha3_512: {
+ SHA3Context copy = d->sha3Context;
+ d->result.resize(512/8);
+ sha3Final(&copy, reinterpret_cast<BitSequence *>(d->result.data()));
+ break;
+ }
}
return d->result;
}