sha3-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +7 -0
  6. data/ext/sha3/KeccakReferenceAndOptimized/KeccakCompact.vcproj +207 -0
  7. data/ext/sha3/KeccakReferenceAndOptimized/KeccakCompact8.vcproj +207 -0
  8. data/ext/sha3/KeccakReferenceAndOptimized/KeccakInplace.vcproj +203 -0
  9. data/ext/sha3/KeccakReferenceAndOptimized/KeccakInplace32BI.vcproj +201 -0
  10. data/ext/sha3/KeccakReferenceAndOptimized/KeccakOptimized32.vcproj +267 -0
  11. data/ext/sha3/KeccakReferenceAndOptimized/KeccakOptimized64.vcproj +267 -0
  12. data/ext/sha3/KeccakReferenceAndOptimized/KeccakReference.vcproj +243 -0
  13. data/ext/sha3/KeccakReferenceAndOptimized/KeccakReference32BI.vcproj +243 -0
  14. data/ext/sha3/KeccakReferenceAndOptimized/KeccakReferenceAndOptimized.sln +62 -0
  15. data/ext/sha3/KeccakReferenceAndOptimized/KeccakSimple.vcproj +203 -0
  16. data/ext/sha3/KeccakReferenceAndOptimized/KeccakSimple32BI.vcproj +201 -0
  17. data/ext/sha3/KeccakReferenceAndOptimized/Sources/AVR8-rotate64.h +27 -0
  18. data/ext/sha3/KeccakReferenceAndOptimized/Sources/AVR8-rotate64.s +285 -0
  19. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-settings.h +2 -0
  20. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-test.c +142 -0
  21. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-util.h +15 -0
  22. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8-util.s +119 -0
  23. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8.c +184 -0
  24. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-avr8.h +25 -0
  25. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact-settings.h +3 -0
  26. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact-test.c +317 -0
  27. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact.c +341 -0
  28. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact.h +50 -0
  29. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8-settings.h +2 -0
  30. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8-test.c +192 -0
  31. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8.c +375 -0
  32. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-compact8.h +47 -0
  33. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-crypto_hash-inplace-armgcc-ARMv7A-NEON.s +406 -0
  34. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace-minimal-test.c +231 -0
  35. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace-settings.h +3 -0
  36. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace-test.c +221 -0
  37. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace.c +445 -0
  38. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI-armgcc-ARMv6M.s +844 -0
  39. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI-armgcc-ARMv7A.s +687 -0
  40. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI-armgcc-ARMv7M.s +687 -0
  41. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-inplace32BI.c +849 -0
  42. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple-settings.h +3 -0
  43. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple-test.c +221 -0
  44. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple.c +403 -0
  45. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccak-simple32BI.c +673 -0
  46. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakDuplex.c +68 -0
  47. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakDuplex.h +59 -0
  48. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32-rvk.macros +555 -0
  49. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32-s1.macros +1187 -0
  50. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32-s2.macros +1187 -0
  51. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-32.macros +26 -0
  52. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-64.macros +728 -0
  53. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-arm.c +123 -0
  54. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-armcc.s +653 -0
  55. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-armgcc.s +686 -0
  56. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-avr8.c +163 -0
  57. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-avr8asm-compact.s +647 -0
  58. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-avr8asm-fast.s +934 -0
  59. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-inplace-armgcc-ARMv7A-NEON.s +446 -0
  60. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-int-set.h +6 -0
  61. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-interface.h +46 -0
  62. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt32-settings.h +4 -0
  63. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt32.c +524 -0
  64. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt64-settings.h +7 -0
  65. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-opt64.c +504 -0
  66. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference.c +300 -0
  67. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference.h +20 -0
  68. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference.o +0 -0
  69. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-reference32BI.c +371 -0
  70. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-simd128.macros +651 -0
  71. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-simd64.macros +517 -0
  72. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-unrolling.macros +124 -0
  73. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-x86-64-asm.c +62 -0
  74. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-x86-64-gas.s +766 -0
  75. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-x86-64-shld-gas.s +766 -0
  76. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakF-1600-xop.macros +573 -0
  77. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.c +81 -0
  78. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.h +70 -0
  79. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakNISTInterface.o +0 -0
  80. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakSponge.c +266 -0
  81. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakSponge.h +76 -0
  82. data/ext/sha3/KeccakReferenceAndOptimized/Sources/KeccakSponge.o +0 -0
  83. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccakc1024-crypto_hash-inplace-armgcc-ARMv7A-NEON.s +296 -0
  84. data/ext/sha3/KeccakReferenceAndOptimized/Sources/Keccakc512-crypto_hash-inplace-armgcc-ARMv7A-NEON.s +429 -0
  85. data/ext/sha3/KeccakReferenceAndOptimized/Sources/brg_endian.h +142 -0
  86. data/ext/sha3/KeccakReferenceAndOptimized/Sources/crypto_hash.h +0 -0
  87. data/ext/sha3/KeccakReferenceAndOptimized/Sources/displayIntermediateValues.c +117 -0
  88. data/ext/sha3/KeccakReferenceAndOptimized/Sources/displayIntermediateValues.h +29 -0
  89. data/ext/sha3/KeccakReferenceAndOptimized/Sources/displayIntermediateValues.o +0 -0
  90. data/ext/sha3/KeccakReferenceAndOptimized/Sources/genKAT.c +692 -0
  91. data/ext/sha3/KeccakReferenceAndOptimized/Sources/mainARM.c +88 -0
  92. data/ext/sha3/KeccakReferenceAndOptimized/Sources/mainOptimized.c +23 -0
  93. data/ext/sha3/KeccakReferenceAndOptimized/Sources/mainReference.c +381 -0
  94. data/ext/sha3/KeccakReferenceAndOptimized/Sources/timing.c +436 -0
  95. data/ext/sha3/KeccakReferenceAndOptimized/Sources/timing.h +13 -0
  96. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/DoublePermutation-config.h +2 -0
  97. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/DoublePermutation.c +572 -0
  98. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/DoublePermutation.h +38 -0
  99. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/KeccakF-1600-unrolling.macros +124 -0
  100. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc256TreeD2.c +81 -0
  101. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc256TreeD2.h +18 -0
  102. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc512TreeD2.c +81 -0
  103. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/Keccakc512TreeD2.h +18 -0
  104. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/crypto_hash.h +0 -0
  105. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/mainOptimized.c +112 -0
  106. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/timing-Double.c +225 -0
  107. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/Sources/timing-Double.h +20 -0
  108. data/ext/sha3/KeccakReferenceAndOptimized/TreeHashing/makefile +68 -0
  109. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakCompact +0 -0
  110. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakOptimized32 +0 -0
  111. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakOptimized64 +0 -0
  112. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakReference +0 -0
  113. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakReference32BI +0 -0
  114. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakSimple +0 -0
  115. data/ext/sha3/KeccakReferenceAndOptimized/bin/KeccakSimple32BI +0 -0
  116. data/ext/sha3/KeccakReferenceAndOptimized/bin/compact/Keccak-compact-test.o +0 -0
  117. data/ext/sha3/KeccakReferenceAndOptimized/bin/compact/Keccak-compact.o +0 -0
  118. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakDuplex.o +0 -0
  119. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakF-1600-opt32.o +0 -0
  120. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakNISTInterface.o +0 -0
  121. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/KeccakSponge.o +0 -0
  122. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/genKAT.o +0 -0
  123. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/mainOptimized.o +0 -0
  124. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized32/timing.o +0 -0
  125. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakDuplex.o +0 -0
  126. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakF-1600-opt64.o +0 -0
  127. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakNISTInterface.o +0 -0
  128. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/KeccakSponge.o +0 -0
  129. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/genKAT.o +0 -0
  130. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/mainOptimized.o +0 -0
  131. data/ext/sha3/KeccakReferenceAndOptimized/bin/optimized64/timing.o +0 -0
  132. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakDuplex.o +0 -0
  133. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakF-1600-reference.o +0 -0
  134. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakNISTInterface.o +0 -0
  135. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/KeccakSponge.o +0 -0
  136. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/displayIntermediateValues.o +0 -0
  137. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/genKAT.o +0 -0
  138. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference/mainReference.o +0 -0
  139. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakDuplex.o +0 -0
  140. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakF-1600-reference32BI.o +0 -0
  141. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakNISTInterface.o +0 -0
  142. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/KeccakSponge.o +0 -0
  143. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/displayIntermediateValues.o +0 -0
  144. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/genKAT.o +0 -0
  145. data/ext/sha3/KeccakReferenceAndOptimized/bin/reference32bi/mainReference.o +0 -0
  146. data/ext/sha3/KeccakReferenceAndOptimized/bin/simple/Keccak-simple-test.o +0 -0
  147. data/ext/sha3/KeccakReferenceAndOptimized/bin/simple/Keccak-simple.o +0 -0
  148. data/ext/sha3/KeccakReferenceAndOptimized/bin/simple32BI/Keccak-simple-test.o +0 -0
  149. data/ext/sha3/KeccakReferenceAndOptimized/bin/simple32BI/Keccak-simple32BI.o +0 -0
  150. data/ext/sha3/KeccakReferenceAndOptimized/compile64.bat +1 -0
  151. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccak +1 -0
  152. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc1024 +1 -0
  153. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc256 +1 -0
  154. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc256treed2 +1 -0
  155. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc448 +1 -0
  156. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc512 +1 -0
  157. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc512treed2 +1 -0
  158. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/checksum-keccakc768 +1 -0
  159. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccak.c +11 -0
  160. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc1024.c +11 -0
  161. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc256.c +11 -0
  162. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc448.c +11 -0
  163. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc512.c +11 -0
  164. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/hash-keccakc768.c +11 -0
  165. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccak.h +1 -0
  166. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc1024.h +1 -0
  167. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc256.h +1 -0
  168. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc448.h +1 -0
  169. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc512.h +1 -0
  170. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/int-set-keccakc768.h +1 -0
  171. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/populate.py +506 -0
  172. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccak.h +2 -0
  173. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc1024.h +2 -0
  174. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc256.h +2 -0
  175. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc448.h +2 -0
  176. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc512.h +2 -0
  177. data/ext/sha3/KeccakReferenceAndOptimized/eBASH/simple-keccakc768.h +2 -0
  178. data/ext/sha3/KeccakReferenceAndOptimized/makefile +327 -0
  179. data/ext/sha3/Makefile +240 -0
  180. data/ext/sha3/depend +28 -0
  181. data/ext/sha3/extconf.rb +21 -0
  182. data/ext/sha3/sha3.c +95 -0
  183. data/lib/sha3-ruby.rb +27 -0
  184. data/lib/sha3-ruby/version.rb +5 -0
  185. data/sha3-ruby.gemspec +21 -0
  186. metadata +233 -0
@@ -0,0 +1,50 @@
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
+ #ifndef _Keccak_compact_h_
15
+ #define _Keccak_compact_h_
16
+
17
+ #include "Keccak-compact-settings.h"
18
+
19
+ int crypto_hash( unsigned char *out, const unsigned char *in, unsigned long long inlen );
20
+
21
+ /*
22
+ ** API with message queue (inspired from NIST's API)
23
+ */
24
+
25
+ typedef unsigned char BitSequence;
26
+ typedef unsigned long long DataLength;
27
+ typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;
28
+
29
+ #if defined(__GNUC__)
30
+ #define ALIGN __attribute__ ((aligned(32)))
31
+ #elif defined(_MSC_VER)
32
+ #define ALIGN __declspec(align(32))
33
+ #else
34
+ #define ALIGN
35
+ #endif
36
+
37
+ ALIGN typedef struct hashStateStruct
38
+ {
39
+ ALIGN unsigned char state[cKeccakB / 8];
40
+ int bitsInQueue;
41
+ } hashState;
42
+
43
+
44
+ HashReturn Init(hashState *state);
45
+
46
+ HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen);
47
+
48
+ HashReturn Final(hashState *state, BitSequence *hashval, int hashbytelen);
49
+
50
+ #endif
@@ -0,0 +1,2 @@
1
+ #define cKeccakR 1024
2
+ //#define cKeccakFixedOutputLengthInBytes 64
@@ -0,0 +1,192 @@
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-compact8.h"
15
+
16
+ #define cKeccakR_SizeInBytes (cKeccakR / 8)
17
+ #include "crypto_hash.h"
18
+ #ifndef crypto_hash_BYTES
19
+ #ifdef cKeccakFixedOutputLengthInBytes
20
+ #define crypto_hash_BYTES cKeccakFixedOutputLengthInBytes
21
+ #else
22
+ #define crypto_hash_BYTES cKeccakR_SizeInBytes
23
+ #endif
24
+ #endif
25
+
26
+ #include <string.h>
27
+ #include <stdio.h>
28
+ #include <stdlib.h>
29
+ #include <ctype.h>
30
+
31
+ const char testVectorMessage[] =
32
+ "\x3A\x3A\x81\x9C\x48\xEF\xDE\x2A\xD9\x14\xFB\xF0\x0E\x18\xAB\x6B\xC4\xF1\x45\x13\xAB\x27\xD0\xC1\x78\xA1\x88\xB6\x14\x31\xE7\xF5"
33
+ "\x62\x3C\xB6\x6B\x23\x34\x67\x75\xD3\x86\xB5\x0E\x98\x2C\x49\x3A\xDB\xBF\xC5\x4B\x9A\x3C\xD3\x83\x38\x23\x36\xA1\xA0\xB2\x15\x0A"
34
+ "\x15\x35\x8F\x33\x6D\x03\xAE\x18\xF6\x66\xC7\x57\x3D\x55\xC4\xFD\x18\x1C\x29\xE6\xCC\xFD\xE6\x3E\xA3\x5F\x0A\xDF\x58\x85\xCF\xC0"
35
+ "\xA3\xD8\x4A\x2B\x2E\x4D\xD2\x44\x96\xDB\x78\x9E\x66\x31\x70\xCE\xF7\x47\x98\xAA\x1B\xBC\xD4\x57\x4E\xA0\xBB\xA4\x04\x89\xD7\x64"
36
+ "\xB2\xF8\x3A\xAD\xC6\x6B\x14\x8B\x4A\x0C\xD9\x52\x46\xC1\x27\xD5\x87\x1C\x4F\x11\x41\x86\x90\xA5\xDD\xF0\x12\x46\xA0\xC8\x0A\x43"
37
+ "\xC7\x00\x88\xB6\x18\x36\x39\xDC\xFD\xA4\x12\x5B\xD1\x13\xA8\xF4\x9E\xE2\x3E\xD3\x06\xFA\xAC\x57\x6C\x3F\xB0\xC1\xE2\x56\x67\x1D"
38
+ "\x81\x7F\xC2\x53\x4A\x52\xF5\xB4\x39\xF7\x2E\x42\x4D\xE3\x76\xF4\xC5\x65\xCC\xA8\x23\x07\xDD\x9E\xF7\x6D\xA5\xB7\xC4\xEB\x7E\x08"
39
+ "\x51\x72\xE3\x28\x80\x7C\x02\xD0\x11\xFF\xBF\x33\x78\x53\x78\xD7\x9D\xC2\x66\xF6\xA5\xBE\x6B\xB0\xE4\xA9\x2E\xCE\xEB\xAE\xB1";
40
+
41
+ #if (cKeccakB == 1600) && (cKeccakR == 1024)
42
+
43
+ const char testVectorSqueezed[] =
44
+ "\x94\x35\xFC\x67\x1D\xFC\xFC\xDA\xC1\x49\x27\x7E\x2C\xAA\xA8\x0E\xD3\xD4\xA2\x35\x93\x00\xDB\x89\x2B\x80\x93\xDF\xFA\x94\x42\xBB\x5C\x08\xF2\x42\xF2\xFC\x2C\xB5\xF8\x38\x80\x32\x29\x9F\x1D\xF4\x7A\x57\x48\x9A\x4F\xC0\xD6\x6D\x88\xE4\x83\x09\x23\x20\xA4\x71\x89\x7F\xB6\xAD\xE6\x78\x97\xE5\x13\x8C\x45\xF1\x91\x74\xA4\xB1\xAE\x0E\x51\x0F\xA3\x90\x82\x5D\x17\x56\x89\x89\xC3\x65\x9F\xC5\x7B\x93\x45\xD7\xD9\x3E\xE5\x88\xCB\x26\x29\xC5\x77\x08\x08\x19\x52\x57\xBB\xF4\x2B\x06\x95\x76\xD9\x40\x11\x98\x9D\xC6\xEB\xC4";
45
+
46
+ const char testVectorAccumulated[] =
47
+ "\x82\xCE\xAA\x06\xEE\x14\x0F\x60\xEC\x60\x19\x58\x23\x9A\x74\xB5\x70\xF6\xDC\x5B\x73\xC0\xCB\xD4\x23\x2B\xAA\x45\xD7\x18\x1C\x93\x08\x7A\x6A\x4E\xD3\x87\x9C\x1E\xC7\xDA\x28\x51\x20\x7D\x54\x9D\x01\x71\x3B\xFF\x05\x88\xDB\x3A\xBC\x0D\xA6\xE1\xE4\xC5\xD2\x17\x8D\xB8\x43\xD8\xA9\x06\xF4\xA6\x1B\x28\x6B\x13\xC5\xC3\x39\x8E\xB6\xD2\x3C\x4F\x1C\xDD\xD3\x96\x01\xE5\x1F\xE4\x6C\x91\x5D\x09\xCF\xC3\x48\xED\x02\x2F\xC8\x85\x0B\xE5\x2E\x56\x17\xFC\xE3\xA7\xE3\xD0\x54\x48\x75\x28\xFD\x8D\xF6\xA3\x1F\x8C\x73\x8D\xC4\x79";
48
+
49
+ #elif (cKeccakB == 1600) && (cKeccakR == 1152)
50
+
51
+ const char testVectorSqueezed[] =
52
+ "\x5A\xF5\x69\x87\xEA\x9C\xF1\x1F\xCD\x0E\xAC\x5E\xBC\x14\xB0\x37\x36\x5E\x9B\x11\x23\xE3\x1C\xB2\xDF\xC7\x92\x9A\xE2\x96\x29\x71\x05\xE9\xC9\x2D\x30\x90\xC5\xA1\x1E\x03\xB4\x8C\x56\x75\x54\xB0\x08\xC3\xC7\x4C\xD4\xD0\x0E\x92\xFB\x53\x22\x9B\x91\xBB\x29\x5D\x1E\x76\xF7\xD8\xD9\xDF\x56\xF3\xB3\x87\x15\xBD\x1E\x7C\xEF\x2D\xDB\x33\x5A\x3D\xD4\xC2\x86\xE2\x40\x04\x09\xAA\x9C\x2E\x3B\xD9\x2F\x9D\xCF\xAF\x53\xEA\x34\x44\x14\x4B\xBB\x87\xFF\x09\x6E\xD2\xD7\xAE\xF0\x5F\xE9\xC5\x36\x8F\xB0\x18\x41\x06\x04\xD9\xAF\x38";
53
+
54
+ const char testVectorAccumulated[] =
55
+ "\x58\x54\x97\x2E\x1A\x8F\x2B\x47\x11\x7A\xC5\x8B\x17\x84\xD0\x70\xC6\x55\xCE\x5F\x81\x9F\x5D\x76\x89\xD6\xD3\x9F\x09\x5B\x98\x0F\x85\x70\xFE\xC8\x12\x62\x4A\x28\x5C\xF4\x25\x47\x37\xA1\xFD\x70\x63\xD0\xCA\x74\x1F\x8F\x49\x22\xF8\xF2\x36\x76\x33\x8C\x7D\xBA\x07\x1F\xF9\xFD\x17\x00\xFE\x96\xEC\x0E\xD1\x97\x6C\x04\x62\x68\x9B\x1D\x2B\x18\x5F\x7B\xBF\x73\xEB\xC3\x7F\xD3\x2C\xEC\xFB\xE1\x30\x00\xC4\x2D\x70\x9F\x81\xC7\xE1\x1C\xA8\x07\x04\xEB\xCF\x91\x40\xE7\xBF\x03\xD7\x54\xA3\xA9\xFC\x5F\x6F\x27\x11\xD8\xA5\xBA";
56
+
57
+ #elif (cKeccakB == 1600) && (cKeccakR == 1088)
58
+
59
+ const char testVectorSqueezed[] =
60
+ "\x34\x8F\xB7\x74\xAD\xC9\x70\xA1\x6B\x11\x05\x66\x94\x42\x62\x5E\x6A\xDA\xA8\x25\x7A\x89\xEF\xFD\xB5\xA8\x02\xF1\x61\xB8\x62\xEA\xD4\xB9\x38\xBE\x13\xC4\x0C\x95\x48\x44\x42\x75\xEE\x98\x9A\xF2\x38\x6D\x9C\x4C\x6B\x01\xDF\x21\xC9\x07\x5D\x20\x50\xB7\x36\x45\x19\x5C\x37\xB4\x27\x18\x79\x32\xC4\x28\x66\x0B\x6D\xFE\x00\x82\xE0\x74\x27\x76\x42\x4B\x0A\x1F\x9D\x9F\x3A\x16\x9E\x85\x76\x68\x96\x9D\xFB\x39\x82\xBC\x45\xB0\x4B\x39\x57\xA8\x7A\xDF\xF7\xAE\x2B\x8F\xC7\xD2\xB1\x53\x0B\xCD\x0D\x84\x05\xEF\x75\x93\x26\x33";
61
+
62
+ const char testVectorAccumulated[] =
63
+ "\xDC\x8D\x2C\x9F\xC2\x42\x56\xF5\xBF\x51\xF0\xC5\x60\xF6\x74\x98\x8B\xF5\x36\x97\x5A\xE8\x62\x3E\xB1\x9E\x9A\x30\x16\x7B\x8F\x03\x56\xFF\x7E\x0C\xE0\xF4\xDD\x99\x92\xE2\xE1\x1F\xE8\x55\xED\xB9\x44\xE4\x01\x34\xEF\xCC\x41\xCF\x2E\x6C\xE1\x88\x13\xEA\xEE\xAF\xC1\x77\xB4\xE0\x76\xC1\x48\x54\x0B\x55\x9C\x76\x53\x44\x11\x59\xA1\x42\x72\xB8\x99\x6C\xE2\x0A\xAF\x41\x7B\x53\xB1\x80\x3C\xCA\x78\x33\x3E\x1C\x2E\x17\xCF\x75\xE8\x52\x82\xBD\xC5\xA8\x3C\xE5\xF5\xDA\xBE\x4B\xD6\x53\xE7\xD1\x47\xBF\x41\x92\x4F\x12\xD2\x22";
64
+
65
+ #elif (cKeccakB == 1600) && (cKeccakR == 832)
66
+
67
+ const char testVectorSqueezed[] =
68
+ "\x6B\xFF\x1C\x84\x05\xA3\xFE\x59\x4E\x36\x0E\x3B\xCC\xEA\x1E\xBC\xD5\x09\x31\x0D\xC7\x9B\x9E\x45\xC2\x63\x78\x3D\x7A\x5D\xD6\x62\xC6\x78\x9B\x18\xBD\x56\x7D\xBD\xDA\x15\x54\xF5\xBE\xE6\xA8\x60\x16\x28\xC0\x22\xB7\x87\x92\x0C\xA0\x09\x63\x86\x2B\x42\x7A\x55\xDF\x89\x67\xF9\xE7\x6C\xD5\xA6\x3A\x70\xDA\xF4\xC5\x52\x36\x0B\x0D\x0F\x79\xF3\x8B\x4D\x12\x01\x1C\xD0\xC3\x1D\xAD\x23\x72\x9C\xE3\xB0\x87\x2E\xD6\xDC\x4B\x61\xF6\x9A\xD7\x26\x4F\x58\xA5\xFF\xE0\x8A\x88\x76\x56\x78\x3D\xA0\xC6\x91\xE1\x34\x89\xD6\x07\xFC";
69
+
70
+ const char testVectorAccumulated[] =
71
+ "\x04\xB1\xDF\x77\xF8\xD1\x1B\x96\xE0\xFB\x79\x87\xD1\xF5\x7A\xAC\x05\x6F\x38\xEF\x70\x1A\x91\x68\xE1\xA2\x2B\x99\xEB\x9D\xBB\x8E\x05\x91\xFD\x82\x76\xD7\xBB\x40\x42\xFE\x79\xEE\x6F\xD0\x18\x7D\x87\xF2\xF7\x9A\x4F\x14\x3D\x1A\x13\xF3\xA2\x9C\x20\x92\xF2\x0E\x48\x6F\x9F\x27\x00\x3A\xD5\xFB\xFC\xEA\x5A\xCA\xE1\x1C\x5B\xBF\x06\xE6\xDD\x50\xFF\x2D\x96\xE5\x12\xE5\x8B\xE1\xDB\x32\x20\x7B\xA4\x27\x79\xEE\x02\x77\xFE\xBC\x75\x55\x6A\x04\xC1\xCE\xA8\x0B\xE6\xE8\x36\x89\x29\xF4\x13\x4C\x79\xD6\x06\xFC\xF0\x12\xC8\x0D";
72
+
73
+ #elif (cKeccakB == 1600) && (cKeccakR == 576)
74
+
75
+ const char testVectorSqueezed[] =
76
+ "\x81\x95\x0E\x70\x96\xD3\x1D\x4F\x22\xE3\xDB\x71\xCA\xC7\x25\xBF\x59\xE8\x1A\xF5\x4C\x7C\xA9\xE6\xAE\xEE\x71\xC0\x10\xFC\x54\x67\x46\x63\x12\xA0\x1A\xA5\xC1\x37\xCF\xB1\x40\x64\x69\x41\x55\x67\x96\xF6\x12\xC9\x35\x12\x68\x73\x7C\x7E\x9A\x2B\x96\x31\xD1\xFA\x71\x83\x46\x26\x3A\x08\x73\x24\x43\x22\x67\x61\x86\x99\x14\xD4\x4B\x99\x40\xD0\x41\x05\x53\x6A\x35\xD4\x56\xFA\x65\xB4\x1D\x2E\x33\xC9\x85\x60\x18\x40\x05\x15\xDA\xB7\xB9\x68\xF6\x51\x00\xC8\x06\x46\x66\x61\x5A\xE6\x95\xBB\xB0\x88\x3A\xBD\xEB\x59\x33\xE6";
77
+
78
+ const char testVectorAccumulated[] =
79
+ "\x56\x1E\x2E\x43\x33\xD2\xB1\xC5\x46\x6A\x8D\x49\x36\x9C\x02\xA2\x7E\x41\xC2\xAE\x84\xAD\x17\x02\x9E\x3B\xB7\xE2\x0B\x90\x7F\x27\x51\x9A\xBF\x76\x20\x73\x54\xDD\xAD\xC2\xC6\xEA\x45\x3E\xC9\x11\x67\xDA\xA3\x05\xF1\xAC\x52\x85\xCC\xD8\x7F\x72\x70\x38\x8E\x47\x95\x84\x5F\x06\xB5\x81\x2A\x5D\xF8\x2B\x55\x2E\xCC\x4E\x20\x89\x21\x2B\xF9\x01\x6D\xF0\xE9\x90\xC6\x0A\xD6\x0D\x18\xF1\x75\xC9\xDA\x53\xB2\x53\x41\xF8\xD6\x81\x70\xF0\x8C\xB9\xEC\x2E\x77\xA8\x2A\xEB\x4B\x9B\x20\x85\x29\xB9\xED\xD3\x7F\xED\xB8\x6A\x9B\x75";
80
+
81
+ #endif
82
+
83
+ #define cKeccakMessageSizeInBytes (sizeof(testVectorMessage) - 1)
84
+ #define cKeccakHashSizeInBytes (sizeof(testVectorSqueezed) - 1)
85
+
86
+ unsigned char input[(cKeccakMessageSizeInBytes > 512) ? cKeccakMessageSizeInBytes : 512];
87
+ unsigned char output[cKeccakHashSizeInBytes];
88
+ unsigned char ref[cKeccakHashSizeInBytes];
89
+
90
+ int main( void )
91
+ {
92
+ {
93
+ unsigned char i;
94
+
95
+ // One test vector for crypto_hash()
96
+ for ( i = 0; i < cKeccakMessageSizeInBytes; i++ )
97
+ {
98
+ input[i] = (unsigned char)testVectorMessage[i];
99
+ }
100
+
101
+ crypto_hash( output, input, cKeccakMessageSizeInBytes );
102
+
103
+ for ( i = 0; (i < cKeccakHashSizeInBytes) && (i < crypto_hash_BYTES); i++ )
104
+ {
105
+ if ( output[i] != (unsigned char)testVectorSqueezed[i] )
106
+ {
107
+ for ( ; ; );
108
+ }
109
+ }
110
+ }
111
+
112
+ {
113
+ #define refLen cKeccakHashSizeInBytes
114
+ unsigned int inlen, offset, size;
115
+ int result;
116
+ hashState state;
117
+
118
+ // Acumulated test vector for crypto_hash()
119
+ memset( ref, 0x00, sizeof(ref) );
120
+
121
+ for ( inlen = 0; inlen <= (sizeof(input) * 8); (inlen < 2*8) ? inlen++ : ((inlen < 32*8) ? (inlen += 8) : (inlen <<= 1)) )
122
+ {
123
+ unsigned int i;
124
+ unsigned int bytesize = (unsigned int)((inlen + 7) / 8);
125
+
126
+ for ( i = 0; i < bytesize; ++i )
127
+ {
128
+ input[i] = (unsigned char)(i - bytesize);
129
+ }
130
+
131
+ result = Init( &state );
132
+ if ( result != 0 )
133
+ {
134
+ for (;;);
135
+ }
136
+
137
+ for ( offset = 0; offset < inlen; offset += size )
138
+ {
139
+ // vary sizes for Update()
140
+ if ( (inlen %8) < 2 )
141
+ {
142
+ // byte per byte
143
+ size = 8;
144
+ }
145
+ else if ( (inlen %8) < 4 )
146
+ {
147
+ // incremental
148
+ size = offset + 8;
149
+ }
150
+ else
151
+ {
152
+ // random
153
+ size = ((rand() % ((inlen + 8) / 8)) + 1) * 8;
154
+ }
155
+
156
+ if ( size > (inlen - offset) )
157
+ {
158
+ size = inlen - offset;
159
+ }
160
+ result = Update( &state, input + offset / 8, size );
161
+ if ( result != 0 )
162
+ {
163
+ for (;;);
164
+ }
165
+ }
166
+ result = Final( &state, output, refLen );
167
+ if ( result != 0 )
168
+ {
169
+ for (;;);
170
+ }
171
+
172
+ for ( i = 0; i < (unsigned int)refLen; ++i )
173
+ {
174
+ ref[i] ^= output[i];
175
+ }
176
+ }
177
+
178
+ {
179
+ unsigned char i;
180
+ for ( i = 0; i < (unsigned char)refLen; i++ )
181
+ {
182
+ if ( ref[i] != (unsigned char)testVectorAccumulated[i] )
183
+ {
184
+ for ( ; ; );
185
+ }
186
+ }
187
+ }
188
+ }
189
+
190
+ //printf("OK!\n");
191
+ return ( 0 );
192
+ }
@@ -0,0 +1,375 @@
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 <string.h>
15
+ #include "Keccak-compact8.h"
16
+
17
+ #define cKeccakR_SizeInBytes (cKeccakR / 8)
18
+ #include "crypto_hash.h"
19
+ #ifndef crypto_hash_BYTES
20
+ #ifdef cKeccakFixedOutputLengthInBytes
21
+ #define crypto_hash_BYTES cKeccakFixedOutputLengthInBytes
22
+ #else
23
+ #define crypto_hash_BYTES cKeccakR_SizeInBytes
24
+ #endif
25
+ #endif
26
+
27
+ typedef unsigned char UINT8;
28
+ typedef UINT8 tSmallUInt;
29
+ typedef unsigned long long UINT64;
30
+ typedef UINT64 tKeccakLane;
31
+
32
+ #define cKeccakLaneSizeInBits (sizeof(tKeccakLane) * 8)
33
+
34
+ #define cKeccakNumberOfRounds 24
35
+
36
+ void KeccakF( tKeccakLane * state );
37
+
38
+
39
+ const void * xorBytes( void* dest, const void* src, tSmallUInt n )
40
+ {
41
+ tSmallUInt i;
42
+ UINT8 *pDest = (UINT8*)dest;
43
+ const UINT8 *pSrc = (const UINT8*)src;
44
+ for(i=0; i<n; i++)
45
+ *(pDest++) ^= *(pSrc++);
46
+ return pSrc;
47
+ }
48
+
49
+ const void * xorLanes( void* dest, const void* src, tSmallUInt n )
50
+ {
51
+ tSmallUInt i;
52
+ UINT64 *pDest = (UINT64*)dest;
53
+ const UINT64 *pSrc = (const UINT64*)src;
54
+ for(i=0; i<n; i++)
55
+ *(pDest++) ^= *(pSrc++);
56
+ return pSrc;
57
+ }
58
+
59
+ UINT64 rotate64_1bit_left( UINT64 a )
60
+ {
61
+ return (a << 1) | (a >> 63);
62
+ }
63
+
64
+ UINT64 rotate64left( UINT64 a, tSmallUInt r )
65
+ {
66
+ return (a << r) | (a >> (64-r));
67
+ }
68
+
69
+ int crypto_hash( unsigned char *out, const unsigned char *in, unsigned long long inlen )
70
+ {
71
+ #if 1
72
+
73
+ hashState state;
74
+
75
+ Init( &state );
76
+ Update( &state, in, inlen * 8 );
77
+ return (Final( &state, out, crypto_hash_BYTES ) );
78
+
79
+ #else
80
+
81
+ tSmallUInt i;
82
+ tSmallUInt len;
83
+ unsigned char * pState;
84
+ tKeccakLane state[5 * 5];
85
+
86
+ memset( state, 0, sizeof(state) );
87
+
88
+ #if 1
89
+ /* Full blocks */
90
+ for ( /* empty */; inlen >= cKeccakR_SizeInBytes; inlen -= cKeccakR_SizeInBytes )
91
+ {
92
+ in = xorLanes( state, in, cKeccakR_SizeInBytes / 8 );
93
+ KeccakF( state );
94
+ }
95
+
96
+ /* Last uncomplete block */
97
+ len = (tSmallUInt)inlen;
98
+ xorBytes( state, in, len );
99
+
100
+ pState = (unsigned char*)state + len;
101
+
102
+ #else
103
+
104
+ /* Full blocks */
105
+ for ( /* empty */; inlen >= cKeccakR_SizeInBytes; inlen -= cKeccakR_SizeInBytes )
106
+ {
107
+ pState = (unsigned char*)state;
108
+ for ( i = cKeccakR_SizeInBytes; i != 0; --i )
109
+ {
110
+ *(pState++) ^= *(in++);
111
+ }
112
+ KeccakF( state );
113
+ }
114
+
115
+ /* Last uncomplete block */
116
+ pState = (unsigned char*)state;
117
+ for ( i = len = (tSmallUInt)inlen; i != 0; --i )
118
+ {
119
+ *(pState++) ^= *(in++);
120
+ }
121
+
122
+ #endif
123
+
124
+
125
+ /* Padding */
126
+ *pState ^= 1;
127
+ ((UINT8*)state)[cKeccakR_SizeInBytes-1] ^= 0x80;
128
+ KeccakF( state );
129
+
130
+ #if (crypto_hash_BYTES > cKeccakR_SizeInBytes)
131
+ #error "Full squeezing not yet implemented"
132
+ #endif
133
+ memcpy( out, state, crypto_hash_BYTES );
134
+
135
+ return ( 0 );
136
+
137
+ #endif
138
+ }
139
+
140
+
141
+ static tKeccakLane KeccakF_RoundConstants[cKeccakNumberOfRounds] =
142
+ {
143
+ (tKeccakLane)0x0000000000000001ULL,
144
+ (tKeccakLane)0x0000000000008082ULL,
145
+ (tKeccakLane)0x800000000000808aULL,
146
+ (tKeccakLane)0x8000000080008000ULL,
147
+ (tKeccakLane)0x000000000000808bULL,
148
+ (tKeccakLane)0x0000000080000001ULL,
149
+ (tKeccakLane)0x8000000080008081ULL,
150
+ (tKeccakLane)0x8000000000008009ULL,
151
+ (tKeccakLane)0x000000000000008aULL,
152
+ (tKeccakLane)0x0000000000000088ULL,
153
+ (tKeccakLane)0x0000000080008009ULL,
154
+ (tKeccakLane)0x000000008000000aULL,
155
+ (tKeccakLane)0x000000008000808bULL,
156
+ (tKeccakLane)0x800000000000008bULL,
157
+ (tKeccakLane)0x8000000000008089ULL,
158
+ (tKeccakLane)0x8000000000008003ULL,
159
+ (tKeccakLane)0x8000000000008002ULL,
160
+ (tKeccakLane)0x8000000000000080ULL,
161
+ (tKeccakLane)0x000000000000800aULL,
162
+ (tKeccakLane)0x800000008000000aULL,
163
+ (tKeccakLane)0x8000000080008081ULL,
164
+ (tKeccakLane)0x8000000000008080ULL,
165
+ (tKeccakLane)0x0000000080000001ULL,
166
+ (tKeccakLane)0x8000000080008008ULL
167
+ };
168
+
169
+ static tSmallUInt KeccakF_RotationConstants[24] =
170
+ {
171
+ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
172
+ };
173
+
174
+ static tSmallUInt KeccakF_PiLane[24] =
175
+ {
176
+ 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
177
+ };
178
+
179
+ static tSmallUInt KeccakF_Mod5[10] =
180
+ {
181
+ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
182
+ };
183
+
184
+
185
+ void KeccakF( tKeccakLane * state )
186
+ {
187
+ tSmallUInt round;
188
+ tKeccakLane C[5];
189
+
190
+ // prepare Theta
191
+ {
192
+ tSmallUInt x;
193
+ tKeccakLane * pC;
194
+ for ( x = 0, pC = C; x < 5; ++x, ++pC )
195
+ {
196
+ *pC = state[x] ^ state[5 + x] ^ state[10 + x] ^ state[15 + x] ^ state[20 + x];
197
+ }
198
+ }
199
+
200
+ for( round = 0; round < cKeccakNumberOfRounds; ++round )
201
+ {
202
+ // Theta
203
+ {
204
+ tSmallUInt x;
205
+ for ( x = 0; x < 5; ++x )
206
+ {
207
+ tKeccakLane temp;
208
+ tSmallUInt y;
209
+ temp = rotate64_1bit_left( C[KeccakF_Mod5[x+1]] );
210
+ temp ^= C[KeccakF_Mod5[x+4]];
211
+ for ( y = 0; y < 25; y += 5 )
212
+ {
213
+ state[y + x] ^= temp;
214
+ }
215
+ }
216
+ }
217
+
218
+ // Rho Pi
219
+ {
220
+ tKeccakLane temp;
221
+ tSmallUInt x;
222
+
223
+ temp = state[1];
224
+ for ( x = 0; x < 24; ++x )
225
+ {
226
+ tSmallUInt t;
227
+ tKeccakLane T[1];
228
+ t = KeccakF_PiLane[x];
229
+ T[0] = state[t];
230
+ state[t] = rotate64left( temp, KeccakF_RotationConstants[x] );
231
+ temp = T[0];
232
+ }
233
+ }
234
+
235
+ // Chi Iota Prepare Theta
236
+ {
237
+ tSmallUInt z;
238
+ UINT8 * p = (unsigned char *)state;
239
+ UINT8 * pC = (unsigned char *)C;
240
+
241
+ for( z = 0; z < 8; ++z, ++p, ++pC )
242
+ {
243
+ tSmallUInt y;
244
+ UINT8 c0, c1, c2, c3, c4, t;
245
+
246
+ c0 = c1 = c2 = c3 = c4 = 0;
247
+ for( y = 5; y != 0; --y, p += 40 )
248
+ {
249
+ UINT8 a0 = *p;
250
+ UINT8 a1 = *(p+8);
251
+ UINT8 a2 = *(p+16);
252
+ UINT8 a3 = *(p+24);
253
+ UINT8 a4 = *(p+32);
254
+
255
+ *p = t = a0 ^ ((~a1) & a2); c0 ^= t;
256
+ *(p+8) = t = a1 ^ ((~a2) & a3); c1 ^= t;
257
+ *(p+16) = a2 ^= ((~a3) & a4); c2 ^= a2;
258
+ *(p+24) = a3 ^= ((~a4) & a0); c3 ^= a3;
259
+ *(p+32) = a4 ^= ((~a0) & a1); c4 ^= a4;
260
+ }
261
+ p -= 5 * 5 * 8;
262
+ y = ((UINT8 *)(KeccakF_RoundConstants+round))[z];
263
+ *p ^= y;
264
+ *pC = c0 ^ y;
265
+ *(pC+ 8) = c1;
266
+ *(pC+16) = c2;
267
+ *(pC+24) = c3;
268
+ *(pC+32) = c4;
269
+ }
270
+ }
271
+ }
272
+
273
+ }
274
+
275
+ /*
276
+ ** API with a message queue
277
+ */
278
+
279
+ HashReturn Init(hashState *state)
280
+ {
281
+ memset( state, 0, sizeof(hashState) );
282
+ return ( SUCCESS );
283
+ }
284
+
285
+ HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen)
286
+ {
287
+ tSmallUInt trailingBits;
288
+ tSmallUInt len;
289
+
290
+ if ( (state->bytesInQueue == 0xFF) || (state->trailingBitsInQueue != 0) )
291
+ {
292
+ /* Final() already called or bits already in queue not modulo 8. */
293
+ return ( FAIL );
294
+ }
295
+
296
+ trailingBits = (unsigned char)databitlen & 7;
297
+ databitlen >>= 3; /* becomes byte length */
298
+
299
+ /* If already data in queue, continue queuing first */
300
+ if ( (state->bytesInQueue != 0) && (databitlen != 0) )
301
+ {
302
+ len = cKeccakR_SizeInBytes - state->bytesInQueue;
303
+ if ( databitlen < len )
304
+ {
305
+ len = (unsigned char)databitlen;
306
+ }
307
+ data = xorBytes( state->state + state->bytesInQueue, data, len );
308
+ databitlen -= len;
309
+ if ( (state->bytesInQueue += len) == cKeccakR_SizeInBytes )
310
+ {
311
+ KeccakF( (tKeccakLane *)state->state );
312
+ state->bytesInQueue = 0;
313
+ }
314
+ }
315
+
316
+ /* Absorb complete blocks */
317
+ for ( /* */; databitlen >= cKeccakR_SizeInBytes; databitlen -= cKeccakR_SizeInBytes )
318
+ {
319
+ data = xorLanes( state->state, data, cKeccakR_SizeInBytes / 8 );
320
+ KeccakF( (tKeccakLane *)state->state );
321
+ }
322
+
323
+ /* Queue remaining data bytes */
324
+ if ( (unsigned char)databitlen != 0 )
325
+ {
326
+ data = xorBytes( state->state, data, (unsigned char)databitlen );
327
+ state->bytesInQueue = (unsigned char)databitlen;
328
+ }
329
+
330
+ /* Queue eventual remaining data bits plus add first padding bit */
331
+ if ( trailingBits != 0 )
332
+ {
333
+ state->trailingBitsInQueue = trailingBits;
334
+ state->state[state->bytesInQueue] ^= (*data >> (8 - trailingBits));
335
+ }
336
+ return ( SUCCESS );
337
+ }
338
+
339
+ HashReturn Final(hashState *state, BitSequence *hashval, int hashbytelen)
340
+ {
341
+ tSmallUInt i;
342
+
343
+ if ( state->bytesInQueue < 0 )
344
+ {
345
+ /* Final() already called. */
346
+ return ( FAIL );
347
+ }
348
+
349
+ // Padding
350
+ if ((state->bytesInQueue == (cKeccakR_SizeInBytes - 1)) && (state->trailingBitsInQueue == 7)) {
351
+ state->state[cKeccakR_SizeInBytes-1] ^= 0x80;
352
+ KeccakF( (tKeccakLane *)state->state );
353
+ }
354
+ else {
355
+ state->state[state->bytesInQueue] ^= 1 << state->trailingBitsInQueue;
356
+ }
357
+ state->state[cKeccakR_SizeInBytes-1] ^= 0x80;
358
+ KeccakF( (tKeccakLane *)state->state );
359
+
360
+ // Output
361
+ for ( /* empty */; hashbytelen != 0; hashval += i, hashbytelen -= i )
362
+ {
363
+ i = (hashbytelen < cKeccakR_SizeInBytes) ? hashbytelen : cKeccakR_SizeInBytes;
364
+
365
+ memcpy( hashval, state->state, i );
366
+
367
+ if ( i != hashbytelen )
368
+ {
369
+ KeccakF( (tKeccakLane *)state->state );
370
+ }
371
+ }
372
+
373
+ state->bytesInQueue = 0xFF; /* flag final state */
374
+ return ( SUCCESS );
375
+ }