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>
This commit is contained in:
Richard Moore 2013-02-03 11:33:26 +00:00 committed by The Qt Project
parent d1acaf2b1c
commit 20bde28448
12 changed files with 270 additions and 96 deletions

View File

@ -16,31 +16,31 @@ http://creativecommons.org/publicdomain/zero/1.0/
#include "KeccakF-1600-int-set.h"
void KeccakInitialize( void );
void KeccakInitializeState(unsigned char *state);
void KeccakPermutation(unsigned char *state);
static void KeccakInitialize( void );
static void KeccakInitializeState(unsigned char *state);
static void KeccakPermutation(unsigned char *state);
#ifdef ProvideFast576
void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data);
static void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data);
#endif
#ifdef ProvideFast832
void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data);
static void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data);
#endif
#ifdef ProvideFast1024
void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data);
static void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data);
#endif
#ifdef ProvideFast1088
void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data);
static void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data);
#endif
#ifdef ProvideFast1152
void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data);
static void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data);
#endif
#ifdef ProvideFast1344
void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data);
static void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data);
#endif
void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount);
static void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount);
#ifdef ProvideFast1024
void KeccakExtract1024bits(const unsigned char *state, unsigned char *data);
static void KeccakExtract1024bits(const unsigned char *state, unsigned char *data);
#endif
void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount);
static void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount);
#endif

View File

@ -26,7 +26,7 @@ int interleaveTablesBuilt = 0;
UINT16 interleaveTable[65536];
UINT16 deinterleaveTable[65536];
void buildInterleaveTables()
static void buildInterleaveTables()
{
UINT32 i, j;
UINT16 x;
@ -70,7 +70,7 @@ void buildInterleaveTables()
#endif // Endianness
void xor8bytesIntoInterleavedWords(UINT32 *even, UINT32 *odd, const UINT8* source)
static void xor8bytesIntoInterleavedWords(UINT32 *even, UINT32 *odd, const UINT8* source)
{
UINT16 i0, i1, i2, i3;
@ -87,7 +87,7 @@ void xor8bytesIntoInterleavedWords(UINT32 *even, UINT32 *odd, const UINT8* sourc
xor8bytesIntoInterleavedWords(state+i*2, state+i*2+1, input+i*8); \
}
void setInterleavedWordsInto8bytes(UINT8* dest, UINT32 even, UINT32 odd)
static void setInterleavedWordsInto8bytes(UINT8* dest, UINT32 even, UINT32 odd)
{
UINT16 d0, d1, d2, d3;
@ -138,7 +138,7 @@ void setInterleavedWordsInto8bytes(UINT8* dest, UINT32 even, UINT32 odd)
#else // (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
// Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002
UINT64 toInterleaving(UINT64 x)
static UINT64 toInterleaving(UINT64 x)
{
UINT64 t;
@ -151,7 +151,7 @@ UINT64 toInterleaving(UINT64 x)
return x;
}
void xor8bytesIntoInterleavedWords(UINT32* evenAndOdd, const UINT8* source)
static void xor8bytesIntoInterleavedWords(UINT32* evenAndOdd, const UINT8* source)
{
// This can be optimized
UINT64 sourceWord =
@ -178,7 +178,7 @@ void xor8bytesIntoInterleavedWords(UINT32* evenAndOdd, const UINT8* source)
#endif // Endianness
// Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002
UINT64 fromInterleaving(UINT64 x)
static UINT64 fromInterleaving(UINT64 x)
{
UINT64 t;
@ -191,7 +191,7 @@ UINT64 fromInterleaving(UINT64 x)
return x;
}
void setInterleavedWordsInto8bytes(UINT8* dest, UINT32* evenAndOdd)
static void setInterleavedWordsInto8bytes(UINT8* dest, UINT32* evenAndOdd)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
((UINT64*)dest)[0] = fromInterleaving(*(UINT64*)evenAndOdd);
@ -240,19 +240,19 @@ void setInterleavedWordsInto8bytes(UINT8* dest, UINT32* evenAndOdd)
#error "Only unrolling 2 is supported by schedule 3."
#endif
void KeccakPermutationOnWords(UINT32 *state)
static void KeccakPermutationOnWords(UINT32 *state)
{
rounds
}
void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsigned int laneCount)
static void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsigned int laneCount)
{
xorLanesIntoState(laneCount, state, input)
rounds
}
#ifdef ProvideFast576
void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *input)
{
xorLanesIntoState(9, state, input)
rounds
@ -260,7 +260,7 @@ void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *inpu
#endif
#ifdef ProvideFast832
void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *input)
{
xorLanesIntoState(13, state, input)
rounds
@ -268,7 +268,7 @@ void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *inpu
#endif
#ifdef ProvideFast1024
void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *input)
{
xorLanesIntoState(16, state, input)
rounds
@ -276,7 +276,7 @@ void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *inp
#endif
#ifdef ProvideFast1088
void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *input)
{
xorLanesIntoState(17, state, input)
rounds
@ -284,7 +284,7 @@ void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *inp
#endif
#ifdef ProvideFast1152
void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *input)
{
xorLanesIntoState(18, state, input)
rounds
@ -292,7 +292,7 @@ void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *inp
#endif
#ifdef ProvideFast1344
void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *input)
{
xorLanesIntoState(21, state, input)
rounds
@ -301,7 +301,7 @@ void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *inp
#else // (Schedule != 3)
void KeccakPermutationOnWords(UINT32 *state)
static void KeccakPermutationOnWords(UINT32 *state)
{
declareABCDE
#if (Unrolling != 24)
@ -312,7 +312,7 @@ void KeccakPermutationOnWords(UINT32 *state)
rounds
}
void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsigned int laneCount)
static void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsigned int laneCount)
{
declareABCDE
unsigned int i;
@ -323,7 +323,7 @@ void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsi
}
#ifdef ProvideFast576
void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *input)
{
declareABCDE
unsigned int i;
@ -335,7 +335,7 @@ void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *inpu
#endif
#ifdef ProvideFast832
void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *input)
{
declareABCDE
unsigned int i;
@ -347,7 +347,7 @@ void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *inpu
#endif
#ifdef ProvideFast1024
void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *input)
{
declareABCDE
unsigned int i;
@ -359,7 +359,7 @@ void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *inp
#endif
#ifdef ProvideFast1088
void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *input)
{
declareABCDE
unsigned int i;
@ -371,7 +371,7 @@ void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *inp
#endif
#ifdef ProvideFast1152
void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *input)
{
declareABCDE
unsigned int i;
@ -383,7 +383,7 @@ void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *inp
#endif
#ifdef ProvideFast1344
void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *input)
static void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *input)
{
declareABCDE
unsigned int i;
@ -396,14 +396,14 @@ void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *inp
#endif
void KeccakInitialize()
static void KeccakInitialize()
{
#ifdef UseInterleaveTables
buildInterleaveTables();
#endif
}
void KeccakInitializeState(unsigned char *state)
static void KeccakInitializeState(unsigned char *state)
{
memset(state, 0, 200);
#ifdef UseBebigokimisa
@ -422,61 +422,61 @@ void KeccakInitializeState(unsigned char *state)
#endif
}
void KeccakPermutation(unsigned char *state)
static void KeccakPermutation(unsigned char *state)
{
// We assume the state is always stored as interleaved 32-bit words
KeccakPermutationOnWords((UINT32*)state);
}
#ifdef ProvideFast576
void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
{
KeccakPermutationOnWordsAfterXoring576bits((UINT32*)state, data);
}
#endif
#ifdef ProvideFast832
void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
{
KeccakPermutationOnWordsAfterXoring832bits((UINT32*)state, data);
}
#endif
#ifdef ProvideFast1024
void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
{
KeccakPermutationOnWordsAfterXoring1024bits((UINT32*)state, data);
}
#endif
#ifdef ProvideFast1088
void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
{
KeccakPermutationOnWordsAfterXoring1088bits((UINT32*)state, data);
}
#endif
#ifdef ProvideFast1152
void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
{
KeccakPermutationOnWordsAfterXoring1152bits((UINT32*)state, data);
}
#endif
#ifdef ProvideFast1344
void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
{
KeccakPermutationOnWordsAfterXoring1344bits((UINT32*)state, data);
}
#endif
void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
static void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
{
KeccakPermutationOnWordsAfterXoring((UINT32*)state, data, laneCount);
}
#ifdef ProvideFast1024
void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
static void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
{
extractLanes(16, state, data)
#ifdef UseBebigokimisa
@ -492,7 +492,7 @@ void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
}
#endif
void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
static void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
{
extractLanes(laneCount, state, data)
#ifdef UseBebigokimisa

View File

@ -174,7 +174,7 @@ ALIGN const UINT64 rot_39_41[2] = {39, 41};
#include "KeccakF-1600-unrolling.macros"
void KeccakPermutationOnWords(UINT64 *state)
static void KeccakPermutationOnWords(UINT64 *state)
{
declareABCDE
#if (Unrolling != 24)
@ -188,7 +188,7 @@ void KeccakPermutationOnWords(UINT64 *state)
#endif
}
void KeccakPermutationOnWordsAfterXoring(UINT64 *state, const UINT64 *input, unsigned int laneCount)
static void KeccakPermutationOnWordsAfterXoring(UINT64 *state, const UINT64 *input, unsigned int laneCount)
{
declareABCDE
#if (Unrolling != 24)
@ -206,7 +206,7 @@ void KeccakPermutationOnWordsAfterXoring(UINT64 *state, const UINT64 *input, uns
}
#ifdef ProvideFast576
void KeccakPermutationOnWordsAfterXoring576bits(UINT64 *state, const UINT64 *input)
static void KeccakPermutationOnWordsAfterXoring576bits(UINT64 *state, const UINT64 *input)
{
declareABCDE
#if (Unrolling != 24)
@ -222,7 +222,7 @@ void KeccakPermutationOnWordsAfterXoring576bits(UINT64 *state, const UINT64 *inp
#endif
#ifdef ProvideFast832
void KeccakPermutationOnWordsAfterXoring832bits(UINT64 *state, const UINT64 *input)
static void KeccakPermutationOnWordsAfterXoring832bits(UINT64 *state, const UINT64 *input)
{
declareABCDE
#if (Unrolling != 24)
@ -238,7 +238,7 @@ void KeccakPermutationOnWordsAfterXoring832bits(UINT64 *state, const UINT64 *inp
#endif
#ifdef ProvideFast1024
void KeccakPermutationOnWordsAfterXoring1024bits(UINT64 *state, const UINT64 *input)
static void KeccakPermutationOnWordsAfterXoring1024bits(UINT64 *state, const UINT64 *input)
{
declareABCDE
#if (Unrolling != 24)
@ -254,7 +254,7 @@ void KeccakPermutationOnWordsAfterXoring1024bits(UINT64 *state, const UINT64 *in
#endif
#ifdef ProvideFast1088
void KeccakPermutationOnWordsAfterXoring1088bits(UINT64 *state, const UINT64 *input)
static void KeccakPermutationOnWordsAfterXoring1088bits(UINT64 *state, const UINT64 *input)
{
declareABCDE
#if (Unrolling != 24)
@ -270,7 +270,7 @@ void KeccakPermutationOnWordsAfterXoring1088bits(UINT64 *state, const UINT64 *in
#endif
#ifdef ProvideFast1152
void KeccakPermutationOnWordsAfterXoring1152bits(UINT64 *state, const UINT64 *input)
static void KeccakPermutationOnWordsAfterXoring1152bits(UINT64 *state, const UINT64 *input)
{
declareABCDE
#if (Unrolling != 24)
@ -286,7 +286,7 @@ void KeccakPermutationOnWordsAfterXoring1152bits(UINT64 *state, const UINT64 *in
#endif
#ifdef ProvideFast1344
void KeccakPermutationOnWordsAfterXoring1344bits(UINT64 *state, const UINT64 *input)
static void KeccakPermutationOnWordsAfterXoring1344bits(UINT64 *state, const UINT64 *input)
{
declareABCDE
#if (Unrolling != 24)
@ -301,11 +301,11 @@ void KeccakPermutationOnWordsAfterXoring1344bits(UINT64 *state, const UINT64 *in
}
#endif
void KeccakInitialize()
static void KeccakInitialize()
{
}
void KeccakInitializeState(unsigned char *state)
static void KeccakInitializeState(unsigned char *state)
{
memset(state, 0, 200);
#ifdef UseBebigokimisa
@ -318,13 +318,14 @@ void KeccakInitializeState(unsigned char *state)
#endif
}
void KeccakPermutation(unsigned char *state)
static void KeccakPermutation(unsigned char *state)
{
// We assume the state is always stored as words
KeccakPermutationOnWords((UINT64*)state);
}
void fromBytesToWord(UINT64 *word, const UINT8 *bytes)
#if 0 // Unused in the Qt configuration
static void fromBytesToWord(UINT64 *word, const UINT8 *bytes)
{
unsigned int i;
@ -332,9 +333,10 @@ void fromBytesToWord(UINT64 *word, const UINT8 *bytes)
for(i=0; i<(64/8); i++)
*word |= (UINT64)(bytes[i]) << (8*i);
}
#endif
#ifdef ProvideFast576
void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring576bits((UINT64*)state, (const UINT64*)data);
@ -350,7 +352,7 @@ void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
#endif
#ifdef ProvideFast832
void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring832bits((UINT64*)state, (const UINT64*)data);
@ -366,7 +368,7 @@ void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
#endif
#ifdef ProvideFast1024
void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring1024bits((UINT64*)state, (const UINT64*)data);
@ -382,7 +384,7 @@ void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
#endif
#ifdef ProvideFast1088
void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring1088bits((UINT64*)state, (const UINT64*)data);
@ -398,7 +400,7 @@ void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
#endif
#ifdef ProvideFast1152
void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring1152bits((UINT64*)state, (const UINT64*)data);
@ -414,7 +416,7 @@ void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
#endif
#ifdef ProvideFast1344
void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
static void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring1344bits((UINT64*)state, (const UINT64*)data);
@ -429,7 +431,7 @@ void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
}
#endif
void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
static void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
KeccakPermutationOnWordsAfterXoring((UINT64*)state, (const UINT64*)data, laneCount);
@ -443,16 +445,18 @@ void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int
#endif
}
void fromWordToBytes(UINT8 *bytes, const UINT64 word)
#if 0 // Unused in the Qt configuration
static void fromWordToBytes(UINT8 *bytes, const UINT64 word)
{
unsigned int i;
for(i=0; i<(64/8); i++)
bytes[i] = (word >> (8*i)) & 0xFF;
}
#endif
#ifdef ProvideFast1024
void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
static void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
memcpy(data, state, 128);
@ -471,7 +475,7 @@ void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
}
#endif
void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
static void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
memcpy(data, state, laneCount*8);

View File

@ -12,10 +12,10 @@ http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <string.h>
#include "KeccakNISTInterface.h"
//#include "KeccakNISTInterface.h"
#include "KeccakF-1600-interface.h"
HashReturn Init(hashState *state, int hashbitlen)
static HashReturn Init(hashState *state, int hashbitlen)
{
switch(hashbitlen) {
case 0: // Default parameters, arbitrary length output
@ -40,29 +40,29 @@ HashReturn Init(hashState *state, int hashbitlen)
return SUCCESS;
}
HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen)
static HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen)
{
if ((databitlen % 8) == 0)
return Absorb((spongeState*)state, data, databitlen);
return (HashReturn) Absorb((spongeState*)state, data, databitlen);
else {
HashReturn ret = Absorb((spongeState*)state, data, databitlen - (databitlen % 8));
HashReturn ret = (HashReturn) Absorb((spongeState*)state, data, databitlen - (databitlen % 8));
if (ret == SUCCESS) {
unsigned char lastByte;
// Align the last partial byte to the least significant bits
lastByte = data[databitlen/8] >> (8 - (databitlen % 8));
return Absorb((spongeState*)state, &lastByte, databitlen % 8);
return (HashReturn) Absorb((spongeState*)state, &lastByte, databitlen % 8);
}
else
return ret;
}
}
HashReturn Final(hashState *state, BitSequence *hashval)
static HashReturn Final(hashState *state, BitSequence *hashval)
{
return Squeeze(state, hashval, state->fixedOutputLength);
return (HashReturn) Squeeze(state, hashval, state->fixedOutputLength);
}
HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval)
static HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval)
{
hashState state;
HashReturn result;

View File

@ -32,7 +32,7 @@ typedef spongeState hashState;
* @pre The value of hashbitlen must be one of 0, 224, 256, 384 and 512.
* @return SUCCESS if successful, BAD_HASHLEN if the value of hashbitlen is incorrect.
*/
HashReturn Init(hashState *state, int hashbitlen);
static HashReturn Init(hashState *state, int hashbitlen);
/**
* Function to give input data for the sponge function to absorb.
* @param state Pointer to the state of the sponge function initialized by Init().
@ -43,7 +43,7 @@ HashReturn Init(hashState *state, int hashbitlen);
* @pre In the previous call to Absorb(), databitLen was a multiple of 8.
* @return SUCCESS if successful, FAIL otherwise.
*/
HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen);
static HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen);
/**
* Function to squeeze output data from the sponge function.
* If @a hashbitlen was not 0 in the call to Init(), the number of output bits is equal to @a hashbitlen.
@ -52,7 +52,7 @@ HashReturn Update(hashState *state, const BitSequence *data, DataLength databitl
* @param hashval Pointer to the buffer where to store the output data.
* @return SUCCESS if successful, FAIL otherwise.
*/
HashReturn Final(hashState *state, BitSequence *hashval);
static HashReturn Final(hashState *state, BitSequence *hashval);
/**
* Function to compute a hash using the Keccak[r, c] sponge function.
* The rate r and capacity c values are determined from @a hashbitlen.
@ -65,6 +65,6 @@ HashReturn Final(hashState *state, BitSequence *hashval);
* @pre The value of hashbitlen must be one of 224, 256, 384 and 512.
* @return SUCCESS if successful, BAD_HASHLEN if the value of hashbitlen is incorrect.
*/
HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval);
static HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval);
#endif

View File

@ -18,7 +18,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
#include "displayIntermediateValues.h"
#endif
int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity)
static int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity)
{
if (rate+capacity != 1600)
return 1;
@ -37,7 +37,7 @@ int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity)
return 0;
}
void AbsorbQueue(spongeState *state)
static void AbsorbQueue(spongeState *state)
{
// state->bitsInQueue is assumed to be equal to state->rate
#ifdef KeccakReference
@ -77,7 +77,7 @@ void AbsorbQueue(spongeState *state)
state->bitsInQueue = 0;
}
int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen)
static int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen)
{
unsigned long long i, j, wholeBlocks;
unsigned int partialBlock, partialByte;
@ -191,7 +191,7 @@ int Absorb(spongeState *state, const unsigned char *data, unsigned long long dat
return 0;
}
void PadAndSwitchToSqueezingPhase(spongeState *state)
static void PadAndSwitchToSqueezingPhase(spongeState *state)
{
// Note: the bits are numbered from 0=LSB to 7=MSB
if (state->bitsInQueue + 1 == state->rate) {
@ -226,7 +226,7 @@ void PadAndSwitchToSqueezingPhase(spongeState *state)
state->squeezing = 1;
}
int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength)
static int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength)
{
unsigned long long i;
unsigned int partialBlock;

View File

@ -47,7 +47,7 @@ ALIGN typedef struct spongeStateStruct {
* @pre One must have r+c=1600 and the rate a multiple of 64 bits in this implementation.
* @return Zero if successful, 1 otherwise.
*/
int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity);
static int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity);
/**
* Function to give input data for the sponge function to absorb.
* @param state Pointer to the state of the sponge function initialized by InitSponge().
@ -60,7 +60,7 @@ int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity);
* i.e., Squeeze() must not have been called before.
* @return Zero if successful, 1 otherwise.
*/
int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen);
static int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen);
/**
* Function to squeeze output data from the sponge function.
* If the sponge function was in the absorbing phase, this function
@ -71,6 +71,6 @@ int Absorb(spongeState *state, const unsigned char *data, unsigned long long dat
* It must be a multiple of 8.
* @return Zero if successful, 1 otherwise.
*/
int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength);
static int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength);
#endif

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -52,6 +52,7 @@ private slots:
void intermediary_result_data();
void intermediary_result();
void sha1();
void sha3();
void files_data();
void files();
};
@ -118,6 +119,23 @@ void tst_QCryptographicHash::intermediary_result_data()
<< QByteArray("abc") << QByteArray("abc")
<< QByteArray::fromHex("DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F")
<< QByteArray::fromHex("F3C41E7B63EE869596FC28BAD64120612C520F65928AB4D126C72C6998B551B8FF1CEDDFED4373E6717554DC89D1EEE6F0AB22FD3675E561ABA9AE26A3EEC53B");
QTest::newRow("sha3_224") << int(QCryptographicHash::Sha3_224)
<< QByteArray("abc") << QByteArray("abc")
<< QByteArray::fromHex("C30411768506EBE1C2871B1EE2E87D38DF342317300A9B97A95EC6A8")
<< QByteArray::fromHex("048330E7C7C8B4A41AB713B3A6F958D77B8CF3EE969930F1584DD550");
QTest::newRow("sha3_256") << int(QCryptographicHash::Sha3_256)
<< QByteArray("abc") << QByteArray("abc")
<< QByteArray::fromHex("4E03657AEA45A94FC7D47BA826C8D667C0D1E6E33A64A036EC44F58FA12D6C45")
<< QByteArray::fromHex("9F0ADAD0A59B05D2E04A1373342B10B9EB16C57C164C8A3BFCBF46DCCEE39A21");
QTest::newRow("sha3_384") << int(QCryptographicHash::Sha3_384)
<< QByteArray("abc") << QByteArray("abc")
<< QByteArray::fromHex("F7DF1165F033337BE098E7D288AD6A2F74409D7A60B49C36642218DE161B1F99F8C681E4AFAF31A34DB29FB763E3C28E")
<< QByteArray::fromHex("D733B87D392D270889D3DA23AE113F349E25574B445F319CDE4CD3F877C753E9E3C65980421339B3A131457FF393939F");
QTest::newRow("sha3_512") << int(QCryptographicHash::Sha3_512)
<< QByteArray("abc") << QByteArray("abc")
<< QByteArray::fromHex("18587DC2EA106B9A1563E32B3312421CA164C7F1F07BC922A9C83D77CEA3A1E5D0C69910739025372DC14AC9642629379540C17E2A65B19D77AA511A9D00BB96")
<< QByteArray::fromHex("A7C392D2A42155761CA76BDDDE1C47D55486B007EDF465397BFB9DFA74D11C8F0D7C86CD29415283F1B5E7F655CEC25B869C9E9C33A8986F0B38542FB12BFB93");
}
void tst_QCryptographicHash::intermediary_result()
@ -167,6 +185,55 @@ void tst_QCryptographicHash::sha1()
QByteArray("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F"));
}
void tst_QCryptographicHash::sha3()
{
// SHA3-224("The quick brown fox jumps over the lazy dog")
// 10aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog",
QCryptographicHash::Sha3_224).toHex(),
QByteArray("310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe"));
// SHA3-224("The quick brown fox jumps over the lazy dog.")
// c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog.",
QCryptographicHash::Sha3_224).toHex(),
QByteArray("c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab"));
// SHA3-256("The quick brown fox jumps over the lazy dog")
// 4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog",
QCryptographicHash::Sha3_256).toHex(),
QByteArray("4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15"));
// SHA3-256("The quick brown fox jumps over the lazy dog.")
// 578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog.",
QCryptographicHash::Sha3_256).toHex(),
QByteArray("578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d"));
// SHA3-384("The quick brown fox jumps over the lazy dog")
// 283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog",
QCryptographicHash::Sha3_384).toHex(),
QByteArray("283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3"));
// SHA3-384("The quick brown fox jumps over the lazy dog.")
// 9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog.",
QCryptographicHash::Sha3_384).toHex(),
QByteArray("9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b"));
// SHA3-512("The quick brown fox jumps over the lazy dog")
// d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog",
QCryptographicHash::Sha3_512).toHex(),
QByteArray("d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609"));
// SHA3-512("The quick brown fox jumps over the lazy dog.")
// ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760
QCOMPARE(QCryptographicHash::hash("The quick brown fox jumps over the lazy dog.",
QCryptographicHash::Sha3_512).toHex(),
QByteArray("ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760"));
}
Q_DECLARE_METATYPE(QCryptographicHash::Algorithm);