diff options
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qcryptographichash.cpp | 97 | ||||
-rw-r--r-- | src/corelib/tools/qcryptographichash.h | 7 | ||||
-rw-r--r-- | src/corelib/tools/qmessageauthenticationcode.cpp | 8 | ||||
-rw-r--r-- | src/corelib/tools/tools.pri | 3 |
4 files changed, 109 insertions, 6 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(©, reinterpret_cast<unsigned char *>(d->result.data())); break; } + case Sha3_224: { + SHA3Context copy = d->sha3Context; + d->result.resize(224/8); + sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); + break; + } + case Sha3_256: { + SHA3Context copy = d->sha3Context; + d->result.resize(256/8); + sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); + break; + } + case Sha3_384: { + SHA3Context copy = d->sha3Context; + d->result.resize(384/8); + sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); + break; + } + case Sha3_512: { + SHA3Context copy = d->sha3Context; + d->result.resize(512/8); + sha3Final(©, reinterpret_cast<BitSequence *>(d->result.data())); + break; + } } return d->result; } diff --git a/src/corelib/tools/qcryptographichash.h b/src/corelib/tools/qcryptographichash.h index 847ba9c3f7..d4e75c4667 100644 --- a/src/corelib/tools/qcryptographichash.h +++ b/src/corelib/tools/qcryptographichash.h @@ -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. @@ -60,7 +61,11 @@ public: Sha224, Sha256, Sha384, - Sha512 + Sha512, + Sha3_224, + Sha3_256, + Sha3_384, + Sha3_512 }; explicit QCryptographicHash(Algorithm method); diff --git a/src/corelib/tools/qmessageauthenticationcode.cpp b/src/corelib/tools/qmessageauthenticationcode.cpp index 3950f15502..becaaaa704 100644 --- a/src/corelib/tools/qmessageauthenticationcode.cpp +++ b/src/corelib/tools/qmessageauthenticationcode.cpp @@ -81,6 +81,14 @@ static int qt_hash_block_size(QCryptographicHash::Algorithm method) return SHA384_Message_Block_Size; case QCryptographicHash::Sha512: return SHA512_Message_Block_Size; + case QCryptographicHash::Sha3_224: + return 144; + case QCryptographicHash::Sha3_256: + return 136; + case QCryptographicHash::Sha3_384: + return 104; + case QCryptographicHash::Sha3_512: + return 72; } return 0; } diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri index bed71c6d25..9b80b7c4fe 100644 --- a/src/corelib/tools/tools.pri +++ b/src/corelib/tools/tools.pri @@ -148,7 +148,8 @@ SOURCES += ../3rdparty/harfbuzz/src/harfbuzz-buffer.c \ HEADERS += tools/qharfbuzz_p.h INCLUDEPATH += ../3rdparty/md5 \ - ../3rdparty/md4 + ../3rdparty/md4 \ + ../3rdparty/sha3 # Note: libm should be present by default becaue this is C++ !macx-icc:!vxworks:unix:LIBS_PRIVATE += -lm |