sha3-ruby 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakCompact.vcproj +207 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakCompact8.vcproj +207 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakInplace.vcproj +203 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakInplace32BI.vcproj +201 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakOptimized32.vcproj +267 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakOptimized64.vcproj +267 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakReference.vcproj +243 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakReference32BI.vcproj +243 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakReferenceAndOptimized.sln +62 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakSimple.vcproj +203 -0
- data/ext/sha3/KeccakReferenceAndOptimized/KeccakSimple32BI.vcproj +201 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/AVR8-rotate64.h +27 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/AVR8-rotate64.s +285 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-settings.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-test.c +142 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-util.h +15 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-util.s +119 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8.c +184 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8.h +25 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact-settings.h +3 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact-test.c +317 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact.c +341 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact.h +50 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8-settings.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8-test.c +192 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8.c +375 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8.h +47 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-crypto_hash-inplace-armgcc-ARMv7A-NEON.s +406 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace-minimal-test.c +231 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace-settings.h +3 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace-test.c +221 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace.c +445 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI-armgcc-ARMv6M.s +844 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI-armgcc-ARMv7A.s +687 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI-armgcc-ARMv7M.s +687 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI.c +849 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple-settings.h +3 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple-test.c +221 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple.c +403 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple32BI.c +673 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakDuplex.c +68 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakDuplex.h +59 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32-rvk.macros +555 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32-s1.macros +1187 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32-s2.macros +1187 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32.macros +26 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-64.macros +728 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-arm.c +123 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-armcc.s +653 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-armgcc.s +686 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-avr8.c +163 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-avr8asm-compact.s +647 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-avr8asm-fast.s +934 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-inplace-armgcc-ARMv7A-NEON.s +446 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-int-set.h +6 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-interface.h +46 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt32-settings.h +4 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt32.c +524 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt64-settings.h +7 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt64.c +504 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference.c +300 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference.h +20 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference32BI.c +371 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-simd128.macros +651 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-simd64.macros +517 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-unrolling.macros +124 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-x86-64-asm.c +62 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-x86-64-gas.s +766 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-x86-64-shld-gas.s +766 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-xop.macros +573 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.c +81 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.h +70 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakSponge.c +266 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakSponge.h +76 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakSponge.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccakc1024-crypto_hash-inplace-armgcc-ARMv7A-NEON.s +296 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccakc512-crypto_hash-inplace-armgcc-ARMv7A-NEON.s +429 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/brg_endian.h +142 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/crypto_hash.h +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/displayIntermediateValues.c +117 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/displayIntermediateValues.h +29 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/displayIntermediateValues.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/genKAT.c +692 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/mainARM.c +88 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/mainOptimized.c +23 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/mainReference.c +381 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/timing.c +436 -0
- data/ext/sha3/KeccakReferenceAndOptimized/Sources/timing.h +13 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/DoublePermutation-config.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/DoublePermutation.c +572 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/DoublePermutation.h +38 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/KeccakF-1600-unrolling.macros +124 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc256TreeD2.c +81 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc256TreeD2.h +18 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc512TreeD2.c +81 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc512TreeD2.h +18 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/crypto_hash.h +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/mainOptimized.c +112 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/timing-Double.c +225 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/timing-Double.h +20 -0
- data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/makefile +68 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakCompact +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakOptimized32 +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakOptimized64 +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakReference +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakReference32BI +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakSimple +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakSimple32BI +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/compact/Keccak-compact-test.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/compact/Keccak-compact.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakDuplex.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakF-1600-opt32.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakNISTInterface.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakSponge.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/genKAT.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/mainOptimized.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/timing.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakDuplex.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakF-1600-opt64.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakNISTInterface.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakSponge.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/genKAT.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/mainOptimized.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/timing.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakDuplex.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakF-1600-reference.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakNISTInterface.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakSponge.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/displayIntermediateValues.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/genKAT.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/mainReference.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakDuplex.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakF-1600-reference32BI.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakNISTInterface.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakSponge.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/displayIntermediateValues.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/genKAT.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/mainReference.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/simple/Keccak-simple-test.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/simple/Keccak-simple.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/simple32BI/Keccak-simple-test.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/bin/simple32BI/Keccak-simple32BI.o +0 -0
- data/ext/sha3/KeccakReferenceAndOptimized/compile64.bat +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccak +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc1024 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc256 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc256treed2 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc448 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc512 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc512treed2 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc768 +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccak.c +11 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc1024.c +11 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc256.c +11 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc448.c +11 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc512.c +11 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc768.c +11 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccak.h +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc1024.h +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc256.h +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc448.h +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc512.h +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc768.h +1 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/populate.py +506 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccak.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc1024.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc256.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc448.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc512.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc768.h +2 -0
- data/ext/sha3/KeccakReferenceAndOptimized/makefile +327 -0
- data/ext/sha3/Makefile +240 -0
- data/ext/sha3/depend +28 -0
- data/ext/sha3/extconf.rb +21 -0
- data/ext/sha3/sha3.c +95 -0
- data/lib/sha3-ruby.rb +27 -0
- data/lib/sha3-ruby/version.rb +5 -0
- data/sha3-ruby.gemspec +21 -0
- metadata +233 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
|
|
3
|
+
Michaël Peeters and Gilles Van Assche. For more information, feedback or
|
|
4
|
+
questions, please refer to our website: http://keccak.noekeon.org/
|
|
5
|
+
|
|
6
|
+
Implementation by Ronny Van Keer, hereby denoted as "the implementer".
|
|
7
|
+
|
|
8
|
+
To the extent possible under law, the implementer has waived all copyright
|
|
9
|
+
and related or neighboring rights to the source code in this file.
|
|
10
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
#ifndef _Keccak_AVR_h_
|
|
14
|
+
#define _Keccak_AVR_h_
|
|
15
|
+
|
|
16
|
+
#include "Keccak-avr8-settings.h"
|
|
17
|
+
|
|
18
|
+
#ifndef cKeccakB
|
|
19
|
+
/* Only Keccak-F1600 is supported */
|
|
20
|
+
#define cKeccakB 1600
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
int crypto_hash( unsigned char *out, const unsigned char *in, unsigned long long inlen );
|
|
24
|
+
|
|
25
|
+
#endif
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
|
|
3
|
+
Michaël Peeters and Gilles Van Assche. For more information, feedback or
|
|
4
|
+
questions, please refer to our website: http://keccak.noekeon.org/
|
|
5
|
+
|
|
6
|
+
Implementation by Ronny Van Keer,
|
|
7
|
+
hereby denoted as "the implementer".
|
|
8
|
+
|
|
9
|
+
To the extent possible under law, the implementer has waived all copyright
|
|
10
|
+
and related or neighboring rights to the source code in this file.
|
|
11
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
#include "Keccak-compact.h"
|
|
15
|
+
|
|
16
|
+
#define cKeccakR_SizeInBytes (cKeccakR / 8)
|
|
17
|
+
#include <string.h>
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
#include <ctype.h>
|
|
20
|
+
|
|
21
|
+
#if (cKeccakB == 1600) && (cKeccakR == 1024)
|
|
22
|
+
const char * testVectorFile = "ShortMsgKAT_0.txt";
|
|
23
|
+
#elif (cKeccakB == 1600) && (cKeccakR == 1152) && (cKeccakFixedOutputLengthInBytes == 28)
|
|
24
|
+
const char * testVectorFile = "ShortMsgKAT_224.txt";
|
|
25
|
+
#elif (cKeccakB == 1600) && (cKeccakR == 1088) && (cKeccakFixedOutputLengthInBytes == 32)
|
|
26
|
+
const char * testVectorFile = "ShortMsgKAT_256.txt";
|
|
27
|
+
#elif (cKeccakB == 1600) && (cKeccakR == 832) && (cKeccakFixedOutputLengthInBytes == 48)
|
|
28
|
+
const char * testVectorFile = "ShortMsgKAT_384.txt";
|
|
29
|
+
#elif (cKeccakB == 1600) && (cKeccakR == 576) && (cKeccakFixedOutputLengthInBytes == 64)
|
|
30
|
+
const char * testVectorFile = "ShortMsgKAT_512.txt";
|
|
31
|
+
#elif (cKeccakB == 1600) && (cKeccakR == 1344)
|
|
32
|
+
const char * testVectorFile = "ShortMsgKAT_r1344c256.txt";
|
|
33
|
+
#elif (cKeccakB == 800) && (cKeccakR == 544)
|
|
34
|
+
const char * testVectorFile = "ShortMsgKAT_r544c256.txt";
|
|
35
|
+
#elif (cKeccakB == 800) && (cKeccakR == 512)
|
|
36
|
+
const char * testVectorFile = "ShortMsgKAT_r512c288.txt";
|
|
37
|
+
#elif (cKeccakB == 800) && (cKeccakR == 256)
|
|
38
|
+
const char * testVectorFile = "ShortMsgKAT_r256c544.txt";
|
|
39
|
+
#elif (cKeccakB == 400) && (cKeccakR == 144)
|
|
40
|
+
const char * testVectorFile = "ShortMsgKAT_r144c256.txt";
|
|
41
|
+
#elif (cKeccakB == 400) && (cKeccakR == 128)
|
|
42
|
+
const char * testVectorFile = "ShortMsgKAT_r128c272.txt";
|
|
43
|
+
#elif (cKeccakB == 200) && (cKeccakR == 40)
|
|
44
|
+
const char * testVectorFile = "ShortMsgKAT_r40c160.txt";
|
|
45
|
+
#else
|
|
46
|
+
#error "Please set the vector file name here."
|
|
47
|
+
#endif
|
|
48
|
+
|
|
49
|
+
#define cKeccakMaxMessageSizeInBits 2047
|
|
50
|
+
#define cKeccakMaxMessageSizeInBytes (cKeccakMaxMessageSizeInBits/8)
|
|
51
|
+
|
|
52
|
+
#ifdef cKeccakFixedOutputLengthInBytes
|
|
53
|
+
#define cKeccakHashRefSizeInBytes cKeccakFixedOutputLengthInBytes
|
|
54
|
+
#elif (cKeccakB == 1600)
|
|
55
|
+
#define cKeccakHashRefSizeInBytes 512
|
|
56
|
+
#else
|
|
57
|
+
#define cKeccakHashRefSizeInBytes cKeccakR_SizeInBytes
|
|
58
|
+
#endif
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
ALIGN unsigned char input[cKeccakMaxMessageSizeInBytes];
|
|
62
|
+
ALIGN unsigned char output[cKeccakHashRefSizeInBytes];
|
|
63
|
+
ALIGN unsigned char ref[cKeccakHashRefSizeInBytes];
|
|
64
|
+
|
|
65
|
+
//
|
|
66
|
+
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
|
|
67
|
+
//
|
|
68
|
+
#define MAX_MARKER_LEN 50
|
|
69
|
+
|
|
70
|
+
int FindMarker(FILE *infile, const char *marker);
|
|
71
|
+
int FindMarker(FILE *infile, const char *marker)
|
|
72
|
+
{
|
|
73
|
+
char line[MAX_MARKER_LEN];
|
|
74
|
+
int i, len;
|
|
75
|
+
|
|
76
|
+
len = (int)strlen(marker);
|
|
77
|
+
if ( len > MAX_MARKER_LEN-1 )
|
|
78
|
+
len = MAX_MARKER_LEN-1;
|
|
79
|
+
|
|
80
|
+
for ( i=0; i<len; i++ )
|
|
81
|
+
if ( (line[i] = fgetc(infile)) == EOF )
|
|
82
|
+
return 0;
|
|
83
|
+
line[len] = '\0';
|
|
84
|
+
|
|
85
|
+
while ( 1 ) {
|
|
86
|
+
if ( !strncmp(line, marker, len) )
|
|
87
|
+
return 1;
|
|
88
|
+
|
|
89
|
+
for ( i=0; i<len-1; i++ )
|
|
90
|
+
line[i] = line[i+1];
|
|
91
|
+
if ( (line[len-1] = fgetc(infile)) == EOF )
|
|
92
|
+
return 0;
|
|
93
|
+
line[len] = '\0';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// shouldn't get here
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//
|
|
101
|
+
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
|
|
102
|
+
//
|
|
103
|
+
int ReadHex(FILE *infile, BitSequence *A, int Length, char *str);
|
|
104
|
+
int ReadHex(FILE *infile, BitSequence *A, int Length, char *str)
|
|
105
|
+
{
|
|
106
|
+
int i, ch, started;
|
|
107
|
+
BitSequence ich;
|
|
108
|
+
|
|
109
|
+
if ( Length == 0 ) {
|
|
110
|
+
A[0] = 0x00;
|
|
111
|
+
return 1;
|
|
112
|
+
}
|
|
113
|
+
memset(A, 0x00, Length);
|
|
114
|
+
started = 0;
|
|
115
|
+
i = 0;
|
|
116
|
+
if ( FindMarker(infile, str) )
|
|
117
|
+
while ( (ch = fgetc(infile)) != EOF )
|
|
118
|
+
{
|
|
119
|
+
if ( !isxdigit(ch) ) {
|
|
120
|
+
if ( !started ) {
|
|
121
|
+
if ( ch == '\n' )
|
|
122
|
+
break;
|
|
123
|
+
else
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
else
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
started = 1;
|
|
130
|
+
if ( (ch >= '0') && (ch <= '9') )
|
|
131
|
+
ich = ch - '0';
|
|
132
|
+
else if ( (ch >= 'A') && (ch <= 'F') )
|
|
133
|
+
ich = ch - 'A' + 10;
|
|
134
|
+
else if ( (ch >= 'a') && (ch <= 'f') )
|
|
135
|
+
ich = ch - 'a' + 10;
|
|
136
|
+
|
|
137
|
+
A[i / 2] = (A[i / 2] << 4) | ich;
|
|
138
|
+
if ( (++i / 2) == Length )
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
else
|
|
142
|
+
return 0;
|
|
143
|
+
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
int main( void )
|
|
148
|
+
{
|
|
149
|
+
unsigned long long inlen;
|
|
150
|
+
unsigned long long offset;
|
|
151
|
+
unsigned long long size;
|
|
152
|
+
int result = 0;
|
|
153
|
+
FILE *fp_in;
|
|
154
|
+
char marker[20];
|
|
155
|
+
int refLen;
|
|
156
|
+
hashState state;
|
|
157
|
+
|
|
158
|
+
#ifdef cKeccakFixedOutputLengthInBytes
|
|
159
|
+
refLen = cKeccakFixedOutputLengthInBytes;
|
|
160
|
+
#else
|
|
161
|
+
refLen = cKeccakR_SizeInBytes;
|
|
162
|
+
#endif
|
|
163
|
+
|
|
164
|
+
printf( "Testing Keccak[r=%u, c=%u] using crypto_hash() against %s over %d squeezed bytes\n", cKeccakR, cKeccakB - cKeccakR, testVectorFile, refLen );
|
|
165
|
+
if ( (fp_in = fopen(testVectorFile, "r")) == NULL )
|
|
166
|
+
{
|
|
167
|
+
printf("Couldn't open <%s> for read\n", testVectorFile);
|
|
168
|
+
return 1;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
for ( inlen = 0; inlen <= cKeccakMaxMessageSizeInBytes; ++inlen )
|
|
172
|
+
{
|
|
173
|
+
sprintf( marker, "Len = %u", inlen * 8 );
|
|
174
|
+
if ( !FindMarker(fp_in, marker) )
|
|
175
|
+
{
|
|
176
|
+
printf("ERROR: no test vector found (%u bytes)\n", inlen );
|
|
177
|
+
result = 1;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
if ( !ReadHex(fp_in, input, (int)inlen, "Msg = ") )
|
|
181
|
+
{
|
|
182
|
+
printf("ERROR: unable to read 'Msg' (%u bytes)\n", inlen );
|
|
183
|
+
result = 1;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
result = crypto_hash( output, input, inlen );
|
|
188
|
+
if ( result != 0 )
|
|
189
|
+
{
|
|
190
|
+
printf("ERROR: crypto_hash() (%u bytes)\n", inlen);
|
|
191
|
+
result = 1;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
#ifdef cKeccakFixedOutputLengthInBytes
|
|
196
|
+
if ( !ReadHex(fp_in, input, refLen, "MD = ") )
|
|
197
|
+
#else
|
|
198
|
+
if ( !ReadHex(fp_in, input, refLen, "Squeezed = ") )
|
|
199
|
+
#endif
|
|
200
|
+
{
|
|
201
|
+
printf("ERROR: unable to read 'Squeezed/MD' (%u bytes)\n", inlen );
|
|
202
|
+
result = 1;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
if ( memcmp( output, input, refLen ) != 0)
|
|
206
|
+
{
|
|
207
|
+
printf("ERROR: hash verification (%u bytes)\n", inlen );
|
|
208
|
+
for(result=0; result<refLen; result++)
|
|
209
|
+
printf("%02X ", output[result]);
|
|
210
|
+
printf("\n");
|
|
211
|
+
result = 1;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if ( !result )
|
|
216
|
+
printf( "\nSuccess!\n");
|
|
217
|
+
result = 0;
|
|
218
|
+
|
|
219
|
+
refLen = cKeccakHashRefSizeInBytes;
|
|
220
|
+
printf( "\nTesting Keccak[r=%u, c=%u] using Init/Update/Final() against %s over %d squeezed bytes\n", cKeccakR, cKeccakB - cKeccakR, testVectorFile, refLen );
|
|
221
|
+
fseek( fp_in, 0, SEEK_SET );
|
|
222
|
+
for ( inlen = 0; inlen <= cKeccakMaxMessageSizeInBits; ++inlen )
|
|
223
|
+
{
|
|
224
|
+
sprintf( marker, "Len = %u", inlen );
|
|
225
|
+
if ( !FindMarker(fp_in, marker) )
|
|
226
|
+
{
|
|
227
|
+
printf("ERROR: no test vector found (%u bits)\n", inlen );
|
|
228
|
+
result = 1;
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
if ( !ReadHex(fp_in, input, (int)inlen, "Msg = ") )
|
|
232
|
+
{
|
|
233
|
+
printf("ERROR: unable to read 'Msg' (%u bits)\n", inlen );
|
|
234
|
+
result = 1;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
result = Init( &state );
|
|
239
|
+
if ( result != 0 )
|
|
240
|
+
{
|
|
241
|
+
printf("ERROR: Init() (%u bits)\n", inlen);
|
|
242
|
+
result = 1;
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
for ( offset = 0; offset < inlen; offset += size )
|
|
247
|
+
{
|
|
248
|
+
// vary sizes for Update()
|
|
249
|
+
if ( (inlen %8) < 2 )
|
|
250
|
+
{
|
|
251
|
+
// byte per byte
|
|
252
|
+
size = 8;
|
|
253
|
+
}
|
|
254
|
+
else if ( (inlen %8) < 4 )
|
|
255
|
+
{
|
|
256
|
+
// incremental
|
|
257
|
+
size = offset + 8;
|
|
258
|
+
}
|
|
259
|
+
else
|
|
260
|
+
{
|
|
261
|
+
// random
|
|
262
|
+
size = ((rand() % ((inlen + 8) / 8)) + 1) * 8;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if ( size > (inlen - offset) )
|
|
266
|
+
{
|
|
267
|
+
size = inlen - offset;
|
|
268
|
+
}
|
|
269
|
+
//printf("Update() inlen %u, size %u, offset %u\n", (unsigned int)inlen, (unsigned int)size, (unsigned int)offset );
|
|
270
|
+
result = Update( &state, input + offset / 8, size );
|
|
271
|
+
if ( result != 0 )
|
|
272
|
+
{
|
|
273
|
+
printf("ERROR: Update() (%u bits)\n", inlen);
|
|
274
|
+
result = 1;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
result = Final( &state, output, refLen );
|
|
279
|
+
if ( result != 0 )
|
|
280
|
+
{
|
|
281
|
+
printf("ERROR: Final() (%u bits)\n", inlen);
|
|
282
|
+
result = 1;
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
#ifdef cKeccakFixedOutputLengthInBytes
|
|
287
|
+
if ( !ReadHex(fp_in, input, refLen, "MD = ") )
|
|
288
|
+
#else
|
|
289
|
+
if ( !ReadHex(fp_in, input, refLen, "Squeezed = ") )
|
|
290
|
+
#endif
|
|
291
|
+
{
|
|
292
|
+
printf("ERROR: unable to read 'Squeezed/MD' (%u bits)\n", inlen );
|
|
293
|
+
result = 1;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
if ( memcmp( output, input, refLen ) != 0)
|
|
297
|
+
{
|
|
298
|
+
printf("ERROR: hash verification (%u bits)\n", inlen );
|
|
299
|
+
for(result=0; result<refLen; result++)
|
|
300
|
+
printf("%02X ", output[result]);
|
|
301
|
+
printf("\n");
|
|
302
|
+
result = 1;
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
fclose( fp_in );
|
|
308
|
+
if ( !result )
|
|
309
|
+
printf( "\nSuccess!\n");
|
|
310
|
+
|
|
311
|
+
//printf( "\nPress a key ...");
|
|
312
|
+
//getchar();
|
|
313
|
+
//printf( "\n");
|
|
314
|
+
return ( result );
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
|
|
3
|
+
Michaël Peeters and Gilles Van Assche. For more information, feedback or
|
|
4
|
+
questions, please refer to our website: http://keccak.noekeon.org/
|
|
5
|
+
|
|
6
|
+
Implementation by Ronny Van Keer,
|
|
7
|
+
hereby denoted as "the implementer".
|
|
8
|
+
|
|
9
|
+
To the extent possible under law, the implementer has waived all copyright
|
|
10
|
+
and related or neighboring rights to the source code in this file.
|
|
11
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
#include <string.h>
|
|
16
|
+
#include "Keccak-compact-settings.h"
|
|
17
|
+
#include "Keccak-compact.h"
|
|
18
|
+
#define cKeccakR_SizeInBytes (cKeccakR / 8)
|
|
19
|
+
#include "crypto_hash.h"
|
|
20
|
+
#ifndef crypto_hash_BYTES
|
|
21
|
+
#ifdef cKeccakFixedOutputLengthInBytes
|
|
22
|
+
#define crypto_hash_BYTES cKeccakFixedOutputLengthInBytes
|
|
23
|
+
#else
|
|
24
|
+
#define crypto_hash_BYTES cKeccakR_SizeInBytes
|
|
25
|
+
#endif
|
|
26
|
+
#endif
|
|
27
|
+
#if (crypto_hash_BYTES > cKeccakR_SizeInBytes)
|
|
28
|
+
#error "Full squeezing not yet implemented"
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
|
|
32
|
+
#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
|
|
33
|
+
#define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN /* WARNING: This implementation works on little-endian platform. Support for big-endinanness is implemented, but not tested. */
|
|
34
|
+
|
|
35
|
+
#if (cKeccakB == 1600)
|
|
36
|
+
typedef unsigned long long UINT64;
|
|
37
|
+
typedef UINT64 tKeccakLane;
|
|
38
|
+
#define cKeccakNumberOfRounds 24
|
|
39
|
+
#elif (cKeccakB == 800)
|
|
40
|
+
typedef unsigned int UINT32;
|
|
41
|
+
// WARNING: on 8-bit and 16-bit platforms, this should be replaced by:
|
|
42
|
+
//typedef unsigned long UINT32;
|
|
43
|
+
typedef UINT32 tKeccakLane;
|
|
44
|
+
#define cKeccakNumberOfRounds 22
|
|
45
|
+
#elif (cKeccakB == 400)
|
|
46
|
+
typedef unsigned short UINT16;
|
|
47
|
+
typedef UINT16 tKeccakLane;
|
|
48
|
+
#define cKeccakNumberOfRounds 20
|
|
49
|
+
#elif (cKeccakB == 200)
|
|
50
|
+
typedef unsigned char UINT8;
|
|
51
|
+
typedef UINT8 tKeccakLane;
|
|
52
|
+
#define cKeccakNumberOfRounds 18
|
|
53
|
+
#else
|
|
54
|
+
#error "Unsupported Keccak-f width"
|
|
55
|
+
#endif
|
|
56
|
+
|
|
57
|
+
typedef unsigned int tSmallUInt; /*INFO It could be more optimized to use "unsigned char" on an 8-bit CPU */
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
#define cKeccakLaneSizeInBits (sizeof(tKeccakLane) * 8)
|
|
61
|
+
|
|
62
|
+
#define ROL(a, offset) (tKeccakLane)((((tKeccakLane)a) << ((offset) % cKeccakLaneSizeInBits)) ^ (((tKeccakLane)a) >> (cKeccakLaneSizeInBits-((offset) % cKeccakLaneSizeInBits))))
|
|
63
|
+
|
|
64
|
+
void KeccakF( tKeccakLane * state, const tKeccakLane *in, int laneCount );
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
int crypto_hash( unsigned char *out, const unsigned char *in, unsigned long long inlen )
|
|
68
|
+
{
|
|
69
|
+
tKeccakLane state[5 * 5];
|
|
70
|
+
#if (crypto_hash_BYTES >= cKeccakR_SizeInBytes)
|
|
71
|
+
#define temp out
|
|
72
|
+
#else
|
|
73
|
+
unsigned char temp[cKeccakR_SizeInBytes];
|
|
74
|
+
#endif
|
|
75
|
+
|
|
76
|
+
memset( state, 0, sizeof(state) );
|
|
77
|
+
|
|
78
|
+
for ( /* empty */; inlen >= cKeccakR_SizeInBytes; inlen -= cKeccakR_SizeInBytes, in += cKeccakR_SizeInBytes )
|
|
79
|
+
{
|
|
80
|
+
KeccakF( state, (const tKeccakLane*)in, cKeccakR_SizeInBytes / sizeof(tKeccakLane) );
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* Last data and padding */
|
|
84
|
+
memcpy( temp, in, (size_t)inlen );
|
|
85
|
+
temp[inlen++] = 1;
|
|
86
|
+
memset( temp+inlen, 0, cKeccakR_SizeInBytes - (size_t)inlen );
|
|
87
|
+
temp[cKeccakR_SizeInBytes-1] |= 0x80;
|
|
88
|
+
KeccakF( state, (const tKeccakLane*)temp, cKeccakR_SizeInBytes / sizeof(tKeccakLane) );
|
|
89
|
+
|
|
90
|
+
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) || (cKeccakB == 200)
|
|
91
|
+
|
|
92
|
+
memcpy( out, state, crypto_hash_BYTES );
|
|
93
|
+
|
|
94
|
+
#else
|
|
95
|
+
|
|
96
|
+
for ( i = 0; i < (crypto_hash_BYTES / sizeof(tKeccakLane)); ++i )
|
|
97
|
+
{
|
|
98
|
+
tSmallUInt j;
|
|
99
|
+
tKeccakLane t;
|
|
100
|
+
|
|
101
|
+
t = state[i];
|
|
102
|
+
for ( j = 0; j < sizeof(tKeccakLane); ++j )
|
|
103
|
+
{
|
|
104
|
+
*(out++) = (unsigned char)t;
|
|
105
|
+
t >>= 8;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#endif
|
|
110
|
+
#if (crypto_hash_BYTES >= cKeccakR_SizeInBytes)
|
|
111
|
+
#undef temp
|
|
112
|
+
#endif
|
|
113
|
+
|
|
114
|
+
return ( 0 );
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
const tKeccakLane KeccakF_RoundConstants[cKeccakNumberOfRounds] =
|
|
119
|
+
{
|
|
120
|
+
(tKeccakLane)0x0000000000000001ULL,
|
|
121
|
+
(tKeccakLane)0x0000000000008082ULL,
|
|
122
|
+
(tKeccakLane)0x800000000000808aULL,
|
|
123
|
+
(tKeccakLane)0x8000000080008000ULL,
|
|
124
|
+
(tKeccakLane)0x000000000000808bULL,
|
|
125
|
+
(tKeccakLane)0x0000000080000001ULL,
|
|
126
|
+
(tKeccakLane)0x8000000080008081ULL,
|
|
127
|
+
(tKeccakLane)0x8000000000008009ULL,
|
|
128
|
+
(tKeccakLane)0x000000000000008aULL,
|
|
129
|
+
(tKeccakLane)0x0000000000000088ULL,
|
|
130
|
+
(tKeccakLane)0x0000000080008009ULL,
|
|
131
|
+
(tKeccakLane)0x000000008000000aULL,
|
|
132
|
+
(tKeccakLane)0x000000008000808bULL,
|
|
133
|
+
(tKeccakLane)0x800000000000008bULL,
|
|
134
|
+
(tKeccakLane)0x8000000000008089ULL,
|
|
135
|
+
(tKeccakLane)0x8000000000008003ULL,
|
|
136
|
+
(tKeccakLane)0x8000000000008002ULL,
|
|
137
|
+
(tKeccakLane)0x8000000000000080ULL
|
|
138
|
+
#if (cKeccakB >= 400)
|
|
139
|
+
, (tKeccakLane)0x000000000000800aULL,
|
|
140
|
+
(tKeccakLane)0x800000008000000aULL
|
|
141
|
+
#if (cKeccakB >= 800)
|
|
142
|
+
, (tKeccakLane)0x8000000080008081ULL,
|
|
143
|
+
(tKeccakLane)0x8000000000008080ULL
|
|
144
|
+
#if (cKeccakB == 1600)
|
|
145
|
+
, (tKeccakLane)0x0000000080000001ULL,
|
|
146
|
+
(tKeccakLane)0x8000000080008008ULL
|
|
147
|
+
#endif
|
|
148
|
+
#endif
|
|
149
|
+
#endif
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const tSmallUInt KeccakF_RotationConstants[25] =
|
|
153
|
+
{
|
|
154
|
+
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const tSmallUInt KeccakF_PiLane[25] =
|
|
158
|
+
{
|
|
159
|
+
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const tSmallUInt KeccakF_Mod5[10] =
|
|
163
|
+
{
|
|
164
|
+
0, 1, 2, 3, 4, 0, 1, 2, 3, 4
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
void KeccakF( tKeccakLane * state, const tKeccakLane *in, int laneCount )
|
|
169
|
+
{
|
|
170
|
+
tSmallUInt x, y;
|
|
171
|
+
tKeccakLane temp;
|
|
172
|
+
tKeccakLane BC[5];
|
|
173
|
+
|
|
174
|
+
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) || (cKeccakB == 200)
|
|
175
|
+
while ( --laneCount >= 0 )
|
|
176
|
+
{
|
|
177
|
+
state[laneCount] ^= in[laneCount];
|
|
178
|
+
}
|
|
179
|
+
#else
|
|
180
|
+
temp = 0; /* please compiler */
|
|
181
|
+
while ( --laneCount >= 0 )
|
|
182
|
+
{
|
|
183
|
+
for ( x = 0; x < sizeof(tKeccakLane); ++x )
|
|
184
|
+
{
|
|
185
|
+
temp <<= 8;
|
|
186
|
+
temp |= ((char*)&in[laneCount])[x];
|
|
187
|
+
}
|
|
188
|
+
state[laneCount] = temp;
|
|
189
|
+
}
|
|
190
|
+
#endif
|
|
191
|
+
|
|
192
|
+
#define round laneCount
|
|
193
|
+
for( round = 0; round < cKeccakNumberOfRounds; ++round )
|
|
194
|
+
{
|
|
195
|
+
// Theta
|
|
196
|
+
for ( x = 0; x < 5; ++x )
|
|
197
|
+
{
|
|
198
|
+
BC[x] = state[x] ^ state[5 + x] ^ state[10 + x] ^ state[15 + x] ^ state[20 + x];
|
|
199
|
+
}
|
|
200
|
+
for ( x = 0; x < 5; ++x )
|
|
201
|
+
{
|
|
202
|
+
temp = BC[KeccakF_Mod5[x+4]] ^ ROL(BC[KeccakF_Mod5[x+1]], 1);
|
|
203
|
+
for ( y = 0; y < 25; y += 5 )
|
|
204
|
+
{
|
|
205
|
+
state[y + x] ^= temp;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Rho Pi
|
|
210
|
+
temp = state[1];
|
|
211
|
+
for ( x = 0; x < 24; ++x )
|
|
212
|
+
{
|
|
213
|
+
BC[0] = state[KeccakF_PiLane[x]];
|
|
214
|
+
state[KeccakF_PiLane[x]] = ROL( temp, KeccakF_RotationConstants[x] );
|
|
215
|
+
temp = BC[0];
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Chi
|
|
219
|
+
for ( y = 0; y < 25; y += 5 )
|
|
220
|
+
{
|
|
221
|
+
BC[0] = state[y + 0];
|
|
222
|
+
BC[1] = state[y + 1];
|
|
223
|
+
BC[2] = state[y + 2];
|
|
224
|
+
BC[3] = state[y + 3];
|
|
225
|
+
BC[4] = state[y + 4];
|
|
226
|
+
for ( x = 0; x < 5; ++x )
|
|
227
|
+
{
|
|
228
|
+
state[y + x] = BC[x] ^((~BC[KeccakF_Mod5[x+1]]) & BC[KeccakF_Mod5[x+2]]);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Iota
|
|
233
|
+
state[0] ^= KeccakF_RoundConstants[round];
|
|
234
|
+
}
|
|
235
|
+
#undef round
|
|
236
|
+
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
HashReturn Init(hashState *state)
|
|
241
|
+
{
|
|
242
|
+
|
|
243
|
+
state->bitsInQueue = 0;
|
|
244
|
+
memset( state->state, 0, sizeof(state->state) );
|
|
245
|
+
|
|
246
|
+
return ( SUCCESS );
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen)
|
|
250
|
+
{
|
|
251
|
+
|
|
252
|
+
if ( (state->bitsInQueue < 0) || ((state->bitsInQueue % 8) != 0) )
|
|
253
|
+
{
|
|
254
|
+
/* Final() already called or bits already in queue not modulo 8. */
|
|
255
|
+
return ( FAIL );
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/* If already data in queue, continue queuing first */
|
|
259
|
+
for ( /* empty */; (databitlen >= 8) && (state->bitsInQueue != 0); databitlen -= 8 )
|
|
260
|
+
{
|
|
261
|
+
state->state[state->bitsInQueue / 8] ^= *(data++);
|
|
262
|
+
if ( (state->bitsInQueue += 8) == cKeccakR )
|
|
263
|
+
{
|
|
264
|
+
KeccakF( (tKeccakLane *)state->state, 0, 0 );
|
|
265
|
+
state->bitsInQueue = 0;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/* Absorb complete blocks */
|
|
270
|
+
for ( /* */; databitlen >= cKeccakR; databitlen -= cKeccakR, data += cKeccakR_SizeInBytes )
|
|
271
|
+
{
|
|
272
|
+
KeccakF( (tKeccakLane *)state->state, (const tKeccakLane *)data, cKeccakR_SizeInBytes / sizeof(tKeccakLane) );
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* Queue remaining data bytes */
|
|
276
|
+
for ( /* empty */; databitlen >=8; databitlen -= 8, state->bitsInQueue += 8 )
|
|
277
|
+
{
|
|
278
|
+
state->state[state->bitsInQueue / 8] ^= *(data++);
|
|
279
|
+
}
|
|
280
|
+
/* Queue eventual remaining data bits plus add first padding bit */
|
|
281
|
+
if ( databitlen != 0 )
|
|
282
|
+
{
|
|
283
|
+
state->state[state->bitsInQueue / 8] ^= (*data >> (8 - databitlen));
|
|
284
|
+
state->bitsInQueue += (int)databitlen;
|
|
285
|
+
}
|
|
286
|
+
return ( SUCCESS );
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
HashReturn Final(hashState *state, BitSequence *hashval, int hashbytelen)
|
|
290
|
+
{
|
|
291
|
+
tSmallUInt i;
|
|
292
|
+
|
|
293
|
+
if ( state->bitsInQueue < 0 )
|
|
294
|
+
{
|
|
295
|
+
/* Final() already called. */
|
|
296
|
+
return ( FAIL );
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Padding
|
|
300
|
+
if (state->bitsInQueue + 1 == cKeccakR_SizeInBytes*8) {
|
|
301
|
+
state->state[cKeccakR_SizeInBytes-1] ^= 0x80;
|
|
302
|
+
KeccakF( (tKeccakLane *)state->state, 0, 0 );
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
state->state[state->bitsInQueue/8] ^= 1 << (state->bitsInQueue % 8);
|
|
306
|
+
}
|
|
307
|
+
state->state[cKeccakR_SizeInBytes-1] ^= 0x80;
|
|
308
|
+
KeccakF( (tKeccakLane *)state->state, 0, 0 );
|
|
309
|
+
|
|
310
|
+
// Output
|
|
311
|
+
for ( /* empty */; hashbytelen != 0; hashval += i, hashbytelen -= i )
|
|
312
|
+
{
|
|
313
|
+
i = (hashbytelen < cKeccakR_SizeInBytes) ? hashbytelen : cKeccakR_SizeInBytes;
|
|
314
|
+
|
|
315
|
+
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) || (cKeccakB == 200)
|
|
316
|
+
|
|
317
|
+
memcpy( hashval, state->state, i );
|
|
318
|
+
|
|
319
|
+
#else
|
|
320
|
+
|
|
321
|
+
for ( offset = 0; offset < i; offset += sizeof(tKeccakLane) )
|
|
322
|
+
{
|
|
323
|
+
tSmallUInt j;
|
|
324
|
+
|
|
325
|
+
for ( j = 0; j < sizeof(tKeccakLane); ++j )
|
|
326
|
+
{
|
|
327
|
+
hashval[offset + j] = state->state[offset + (sizeof(tKeccakLane) - 1) - j];
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
#endif
|
|
332
|
+
|
|
333
|
+
if ( i != hashbytelen )
|
|
334
|
+
{
|
|
335
|
+
KeccakF( (tKeccakLane *)state->state, 0, 0 );
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
state->bitsInQueue = -1; /* flag final state */
|
|
340
|
+
return ( SUCCESS );
|
|
341
|
+
}
|