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,300 @@
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 the designers,
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 <stdio.h>
15
+ #include <string.h>
16
+ #include "brg_endian.h"
17
+ #include "displayIntermediateValues.h"
18
+ #include "KeccakNISTInterface.h"
19
+ #include "KeccakF-1600-interface.h"
20
+
21
+ typedef unsigned char UINT8;
22
+ typedef unsigned long long int UINT64;
23
+
24
+ #define nrRounds 24
25
+ UINT64 KeccakRoundConstants[nrRounds];
26
+ #define nrLanes 25
27
+ unsigned int KeccakRhoOffsets[nrLanes];
28
+
29
+ void KeccakPermutationOnWords(UINT64 *state);
30
+ void theta(UINT64 *A);
31
+ void rho(UINT64 *A);
32
+ void pi(UINT64 *A);
33
+ void chi(UINT64 *A);
34
+ void iota(UINT64 *A, unsigned int indexRound);
35
+
36
+ void fromBytesToWords(UINT64 *stateAsWords, const unsigned char *state)
37
+ {
38
+ unsigned int i, j;
39
+
40
+ for(i=0; i<(KeccakPermutationSize/64); i++) {
41
+ stateAsWords[i] = 0;
42
+ for(j=0; j<(64/8); j++)
43
+ stateAsWords[i] |= (UINT64)(state[i*(64/8)+j]) << (8*j);
44
+ }
45
+ }
46
+
47
+ void fromWordsToBytes(unsigned char *state, const UINT64 *stateAsWords)
48
+ {
49
+ unsigned int i, j;
50
+
51
+ for(i=0; i<(KeccakPermutationSize/64); i++)
52
+ for(j=0; j<(64/8); j++)
53
+ state[i*(64/8)+j] = (stateAsWords[i] >> (8*j)) & 0xFF;
54
+ }
55
+
56
+ void KeccakPermutation(unsigned char *state)
57
+ {
58
+ #if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
59
+ UINT64 stateAsWords[KeccakPermutationSize/64];
60
+ #endif
61
+
62
+ displayStateAsBytes(1, "Input of permutation", state);
63
+ #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
64
+ KeccakPermutationOnWords((UINT64*)state);
65
+ #else
66
+ fromBytesToWords(stateAsWords, state);
67
+ KeccakPermutationOnWords(stateAsWords);
68
+ fromWordsToBytes(state, stateAsWords);
69
+ #endif
70
+ displayStateAsBytes(1, "State after permutation", state);
71
+ }
72
+
73
+ void KeccakPermutationAfterXor(unsigned char *state, const unsigned char *data, unsigned int dataLengthInBytes)
74
+ {
75
+ unsigned int i;
76
+
77
+ for(i=0; i<dataLengthInBytes; i++)
78
+ state[i] ^= data[i];
79
+ KeccakPermutation(state);
80
+ }
81
+
82
+ void KeccakPermutationOnWords(UINT64 *state)
83
+ {
84
+ unsigned int i;
85
+
86
+ displayStateAs64bitWords(3, "Same, with lanes as 64-bit words", state);
87
+
88
+ for(i=0; i<nrRounds; i++) {
89
+ displayRoundNumber(3, i);
90
+
91
+ theta(state);
92
+ displayStateAs64bitWords(3, "After theta", state);
93
+
94
+ rho(state);
95
+ displayStateAs64bitWords(3, "After rho", state);
96
+
97
+ pi(state);
98
+ displayStateAs64bitWords(3, "After pi", state);
99
+
100
+ chi(state);
101
+ displayStateAs64bitWords(3, "After chi", state);
102
+
103
+ iota(state, i);
104
+ displayStateAs64bitWords(3, "After iota", state);
105
+ }
106
+ }
107
+
108
+ #define index(x, y) (((x)%5)+5*((y)%5))
109
+ #define ROL64(a, offset) ((offset != 0) ? ((((UINT64)a) << offset) ^ (((UINT64)a) >> (64-offset))) : a)
110
+
111
+ void theta(UINT64 *A)
112
+ {
113
+ unsigned int x, y;
114
+ UINT64 C[5], D[5];
115
+
116
+ for(x=0; x<5; x++) {
117
+ C[x] = 0;
118
+ for(y=0; y<5; y++)
119
+ C[x] ^= A[index(x, y)];
120
+ }
121
+ for(x=0; x<5; x++)
122
+ D[x] = ROL64(C[(x+1)%5], 1) ^ C[(x+4)%5];
123
+ for(x=0; x<5; x++)
124
+ for(y=0; y<5; y++)
125
+ A[index(x, y)] ^= D[x];
126
+ }
127
+
128
+ void rho(UINT64 *A)
129
+ {
130
+ unsigned int x, y;
131
+
132
+ for(x=0; x<5; x++) for(y=0; y<5; y++)
133
+ A[index(x, y)] = ROL64(A[index(x, y)], KeccakRhoOffsets[index(x, y)]);
134
+ }
135
+
136
+ void pi(UINT64 *A)
137
+ {
138
+ unsigned int x, y;
139
+ UINT64 tempA[25];
140
+
141
+ for(x=0; x<5; x++) for(y=0; y<5; y++)
142
+ tempA[index(x, y)] = A[index(x, y)];
143
+ for(x=0; x<5; x++) for(y=0; y<5; y++)
144
+ A[index(0*x+1*y, 2*x+3*y)] = tempA[index(x, y)];
145
+ }
146
+
147
+ void chi(UINT64 *A)
148
+ {
149
+ unsigned int x, y;
150
+ UINT64 C[5];
151
+
152
+ for(y=0; y<5; y++) {
153
+ for(x=0; x<5; x++)
154
+ C[x] = A[index(x, y)] ^ ((~A[index(x+1, y)]) & A[index(x+2, y)]);
155
+ for(x=0; x<5; x++)
156
+ A[index(x, y)] = C[x];
157
+ }
158
+ }
159
+
160
+ void iota(UINT64 *A, unsigned int indexRound)
161
+ {
162
+ A[index(0, 0)] ^= KeccakRoundConstants[indexRound];
163
+ }
164
+
165
+ int LFSR86540(UINT8 *LFSR)
166
+ {
167
+ int result = ((*LFSR) & 0x01) != 0;
168
+ if (((*LFSR) & 0x80) != 0)
169
+ // Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1
170
+ (*LFSR) = ((*LFSR) << 1) ^ 0x71;
171
+ else
172
+ (*LFSR) <<= 1;
173
+ return result;
174
+ }
175
+
176
+ void KeccakInitializeRoundConstants()
177
+ {
178
+ UINT8 LFSRstate = 0x01;
179
+ unsigned int i, j, bitPosition;
180
+
181
+ for(i=0; i<nrRounds; i++) {
182
+ KeccakRoundConstants[i] = 0;
183
+ for(j=0; j<7; j++) {
184
+ bitPosition = (1<<j)-1; //2^j-1
185
+ if (LFSR86540(&LFSRstate))
186
+ KeccakRoundConstants[i] ^= (UINT64)1<<bitPosition;
187
+ }
188
+ }
189
+ }
190
+
191
+ void KeccakInitializeRhoOffsets()
192
+ {
193
+ unsigned int x, y, t, newX, newY;
194
+
195
+ KeccakRhoOffsets[index(0, 0)] = 0;
196
+ x = 1;
197
+ y = 0;
198
+ for(t=0; t<24; t++) {
199
+ KeccakRhoOffsets[index(x, y)] = ((t+1)*(t+2)/2) % 64;
200
+ newX = (0*x+1*y) % 5;
201
+ newY = (2*x+3*y) % 5;
202
+ x = newX;
203
+ y = newY;
204
+ }
205
+ }
206
+
207
+ void KeccakInitialize()
208
+ {
209
+ KeccakInitializeRoundConstants();
210
+ KeccakInitializeRhoOffsets();
211
+ }
212
+
213
+ void displayRoundConstants(FILE *f)
214
+ {
215
+ unsigned int i;
216
+
217
+ for(i=0; i<nrRounds; i++) {
218
+ fprintf(f, "RC[%02i][0][0] = ", i);
219
+ fprintf(f, "%08X", (unsigned int)(KeccakRoundConstants[i] >> 32));
220
+ fprintf(f, "%08X", (unsigned int)(KeccakRoundConstants[i] & 0xFFFFFFFFULL));
221
+ fprintf(f, "\n");
222
+ }
223
+ fprintf(f, "\n");
224
+ }
225
+
226
+ void displayRhoOffsets(FILE *f)
227
+ {
228
+ unsigned int x, y;
229
+
230
+ for(y=0; y<5; y++) for(x=0; x<5; x++) {
231
+ fprintf(f, "RhoOffset[%i][%i] = ", x, y);
232
+ fprintf(f, "%2i", KeccakRhoOffsets[index(x, y)]);
233
+ fprintf(f, "\n");
234
+ }
235
+ fprintf(f, "\n");
236
+ }
237
+
238
+ void KeccakInitializeState(unsigned char *state)
239
+ {
240
+ memset(state, 0, KeccakPermutationSizeInBytes);
241
+ }
242
+
243
+ #ifdef ProvideFast576
244
+ void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
245
+ {
246
+ KeccakPermutationAfterXor(state, data, 72);
247
+ }
248
+ #endif
249
+
250
+ #ifdef ProvideFast832
251
+ void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
252
+ {
253
+ KeccakPermutationAfterXor(state, data, 104);
254
+ }
255
+ #endif
256
+
257
+ #ifdef ProvideFast1024
258
+ void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
259
+ {
260
+ KeccakPermutationAfterXor(state, data, 128);
261
+ }
262
+ #endif
263
+
264
+ #ifdef ProvideFast1088
265
+ void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
266
+ {
267
+ KeccakPermutationAfterXor(state, data, 136);
268
+ }
269
+ #endif
270
+
271
+ #ifdef ProvideFast1152
272
+ void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
273
+ {
274
+ KeccakPermutationAfterXor(state, data, 144);
275
+ }
276
+ #endif
277
+
278
+ #ifdef ProvideFast1344
279
+ void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
280
+ {
281
+ KeccakPermutationAfterXor(state, data, 168);
282
+ }
283
+ #endif
284
+
285
+ void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
286
+ {
287
+ KeccakPermutationAfterXor(state, data, laneCount*8);
288
+ }
289
+
290
+ #ifdef ProvideFast1024
291
+ void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
292
+ {
293
+ memcpy(data, state, 128);
294
+ }
295
+ #endif
296
+
297
+ void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
298
+ {
299
+ memcpy(data, state, laneCount*8);
300
+ }
@@ -0,0 +1,20 @@
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 the designers,
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 _KeccakPermutationReference_h_
15
+ #define _KeccakPermutationReference_h_
16
+
17
+ void displayRoundConstants(FILE *f);
18
+ void displayRhoOffsets(FILE *f);
19
+
20
+ #endif
@@ -0,0 +1,371 @@
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 the designers,
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 <stdio.h>
15
+ #include <string.h>
16
+ #include "brg_endian.h"
17
+ #include "displayIntermediateValues.h"
18
+ #include "KeccakNISTInterface.h"
19
+ #include "KeccakF-1600-interface.h"
20
+
21
+ typedef unsigned char UINT8;
22
+ typedef unsigned int UINT32;
23
+
24
+ #define nrRounds 24
25
+ UINT32 KeccakRoundConstants[nrRounds][2];
26
+ #define nrLanes 25
27
+ unsigned int KeccakRhoOffsets[nrLanes];
28
+
29
+ void KeccakPermutationOnWords(UINT32 *state);
30
+ void theta(UINT32 *A);
31
+ void rho(UINT32 *A);
32
+ void pi(UINT32 *A);
33
+ void chi(UINT32 *A);
34
+ void iota(UINT32 *A, unsigned int indexRound);
35
+
36
+ void toBitInterleaving(UINT32 low, UINT32 high, UINT32 *even, UINT32 *odd)
37
+ {
38
+ unsigned int i;
39
+
40
+ *even = 0;
41
+ *odd = 0;
42
+ for(i=0; i<64; i++) {
43
+ unsigned int inBit;
44
+ if (i < 32)
45
+ inBit = (low >> i) & 1;
46
+ else
47
+ inBit = (high >> (i-32)) & 1;
48
+ if ((i % 2) == 0)
49
+ *even |= inBit << (i/2);
50
+ else
51
+ *odd |= inBit << ((i-1)/2);
52
+ }
53
+ }
54
+
55
+ void fromBitInterleaving(UINT32 even, UINT32 odd, UINT32 *low, UINT32 *high)
56
+ {
57
+ unsigned int i;
58
+
59
+ *low = 0;
60
+ *high = 0;
61
+ for(i=0; i<64; i++) {
62
+ unsigned int inBit;
63
+ if ((i % 2) == 0)
64
+ inBit = (even >> (i/2)) & 1;
65
+ else
66
+ inBit = (odd >> ((i-1)/2)) & 1;
67
+ if (i < 32)
68
+ *low |= inBit << i;
69
+ else
70
+ *high |= inBit << (i-32);
71
+ }
72
+ }
73
+
74
+ void fromBytesToWords(UINT32 *stateAsWords, const unsigned char *state)
75
+ {
76
+ unsigned int i, j;
77
+ UINT32 low, high;
78
+ UINT32 even, odd;
79
+
80
+ for(i=0; i<(KeccakPermutationSize/64); i++) {
81
+ low = 0;
82
+ high = 0;
83
+ for(j=0; j<(32/8); j++)
84
+ low |= (UINT32)(state[i*(64/8)+j]) << (8*j);
85
+ for(j=(32/8); j<(64/8); j++)
86
+ high |= (UINT32)(state[i*(64/8)+j]) << (8*j-32);
87
+ toBitInterleaving(low, high, &even, &odd);
88
+ stateAsWords[2*i+0] = even;
89
+ stateAsWords[2*i+1] = odd;
90
+ }
91
+ }
92
+
93
+ void fromWordsToBytes(unsigned char *state, const UINT32 *stateAsWords)
94
+ {
95
+ unsigned int i, j;
96
+ UINT32 low, high;
97
+
98
+ for(i=0; i<(KeccakPermutationSize/64); i++) {
99
+ fromBitInterleaving(stateAsWords[2*i+0], stateAsWords[2*i+1], &low, &high);
100
+ for(j=0; j<(32/8); j++)
101
+ state[i*(64/8)+j] = (low >> (8*j)) & 0xFF;
102
+ for(j=32/8; j<(64/8); j++)
103
+ state[i*(64/8)+j] = (high >> (8*j-32)) & 0xFF;
104
+ }
105
+ }
106
+
107
+ void KeccakPermutation(unsigned char *state)
108
+ {
109
+ UINT32 stateAsWords[KeccakPermutationSize/32];
110
+
111
+ displayStateAsBytes(1, "Input of permutation", state);
112
+ fromBytesToWords(stateAsWords, state);
113
+ KeccakPermutationOnWords(stateAsWords);
114
+ fromWordsToBytes(state, stateAsWords);
115
+ displayStateAsBytes(1, "State after permutation", state);
116
+ }
117
+
118
+ void KeccakPermutationAfterXor(unsigned char *state, const unsigned char *data, unsigned int dataLengthInBytes)
119
+ {
120
+ unsigned int i;
121
+
122
+ for(i=0; i<dataLengthInBytes; i++)
123
+ state[i] ^= data[i];
124
+ KeccakPermutation(state);
125
+ }
126
+
127
+ void KeccakPermutationOnWords(UINT32 *state)
128
+ {
129
+ unsigned int i;
130
+
131
+ displayStateAs32bitWords(3, "Same, with lanes as pairs of 32-bit words (bit interleaving)", state);
132
+
133
+ for(i=0; i<nrRounds; i++) {
134
+ displayRoundNumber(3, i);
135
+
136
+ theta(state);
137
+ displayStateAs32bitWords(3, "After theta", state);
138
+
139
+ rho(state);
140
+ displayStateAs32bitWords(3, "After rho", state);
141
+
142
+ pi(state);
143
+ displayStateAs32bitWords(3, "After pi", state);
144
+
145
+ chi(state);
146
+ displayStateAs32bitWords(3, "After chi", state);
147
+
148
+ iota(state, i);
149
+ displayStateAs32bitWords(3, "After iota", state);
150
+ }
151
+ }
152
+
153
+ #define index(x, y,z) ((((x)%5)+5*((y)%5))*2 + z)
154
+ #define ROL32(a, offset) ((offset != 0) ? ((((UINT32)a) << offset) ^ (((UINT32)a) >> (32-offset))) : a)
155
+
156
+ void ROL64(UINT32 inEven, UINT32 inOdd, UINT32 *outEven, UINT32 *outOdd, unsigned int offset)
157
+ {
158
+ if ((offset % 2) == 0) {
159
+ *outEven = ROL32(inEven, offset/2);
160
+ *outOdd = ROL32(inOdd, offset/2);
161
+ }
162
+ else {
163
+ *outEven = ROL32(inOdd, (offset+1)/2);
164
+ *outOdd = ROL32(inEven, (offset-1)/2);
165
+ }
166
+ }
167
+
168
+ void theta(UINT32 *A)
169
+ {
170
+ unsigned int x, y, z;
171
+ UINT32 C[5][2], D[5][2];
172
+
173
+ for(x=0; x<5; x++) {
174
+ for(z=0; z<2; z++) {
175
+ C[x][z] = 0;
176
+ for(y=0; y<5; y++)
177
+ C[x][z] ^= A[index(x, y, z)];
178
+ }
179
+ }
180
+ for(x=0; x<5; x++) {
181
+ ROL64(C[(x+1)%5][0], C[(x+1)%5][1], &(D[x][0]), &(D[x][1]), 1);
182
+ for(z=0; z<2; z++)
183
+ D[x][z] ^= C[(x+4)%5][z];
184
+ }
185
+ for(x=0; x<5; x++)
186
+ for(y=0; y<5; y++)
187
+ for(z=0; z<2; z++)
188
+ A[index(x, y, z)] ^= D[x][z];
189
+ }
190
+
191
+ void rho(UINT32 *A)
192
+ {
193
+ unsigned int x, y;
194
+
195
+ for(x=0; x<5; x++) for(y=0; y<5; y++)
196
+ ROL64(A[index(x, y, 0)], A[index(x, y, 1)], &(A[index(x, y, 0)]), &(A[index(x, y, 1)]), KeccakRhoOffsets[5*y+x]);
197
+ }
198
+
199
+ void pi(UINT32 *A)
200
+ {
201
+ unsigned int x, y, z;
202
+ UINT32 tempA[50];
203
+
204
+ for(x=0; x<5; x++) for(y=0; y<5; y++) for(z=0; z<2; z++)
205
+ tempA[index(x, y, z)] = A[index(x, y, z)];
206
+ for(x=0; x<5; x++) for(y=0; y<5; y++) for(z=0; z<2; z++)
207
+ A[index(0*x+1*y, 2*x+3*y, z)] = tempA[index(x, y, z)];
208
+ }
209
+
210
+ void chi(UINT32 *A)
211
+ {
212
+ unsigned int x, y, z;
213
+ UINT32 C[5][2];
214
+
215
+ for(y=0; y<5; y++) {
216
+ for(x=0; x<5; x++)
217
+ for(z=0; z<2; z++)
218
+ C[x][z] = A[index(x, y, z)] ^ ((~A[index(x+1, y, z)]) & A[index(x+2, y, z)]);
219
+ for(x=0; x<5; x++)
220
+ for(z=0; z<2; z++)
221
+ A[index(x, y, z)] = C[x][z];
222
+ }
223
+ }
224
+
225
+ void iota(UINT32 *A, unsigned int indexRound)
226
+ {
227
+ A[index(0, 0, 0)] ^= KeccakRoundConstants[indexRound][0];
228
+ A[index(0, 0, 1)] ^= KeccakRoundConstants[indexRound][1];
229
+ }
230
+
231
+ int LFSR86540(UINT8 *LFSR)
232
+ {
233
+ int result = ((*LFSR) & 0x01) != 0;
234
+ if (((*LFSR) & 0x80) != 0)
235
+ // Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1
236
+ (*LFSR) = ((*LFSR) << 1) ^ 0x71;
237
+ else
238
+ (*LFSR) <<= 1;
239
+ return result;
240
+ }
241
+
242
+ void KeccakInitializeRoundConstants()
243
+ {
244
+ UINT8 LFSRstate = 0x01;
245
+ unsigned int i, j, bitPosition;
246
+ UINT32 low, high;
247
+
248
+ for(i=0; i<nrRounds; i++) {
249
+ low = high = 0;
250
+ for(j=0; j<7; j++) {
251
+ bitPosition = (1<<j)-1; //2^j-1
252
+ if (LFSR86540(&LFSRstate)) {
253
+ if (bitPosition < 32)
254
+ low ^= (UINT32)1 << bitPosition;
255
+ else
256
+ high ^= (UINT32)1 << (bitPosition-32);
257
+ }
258
+ }
259
+ toBitInterleaving(low, high, &(KeccakRoundConstants[i][0]), &(KeccakRoundConstants[i][1]));
260
+ }
261
+ }
262
+
263
+ void KeccakInitializeRhoOffsets()
264
+ {
265
+ unsigned int x, y, t, newX, newY;
266
+
267
+ KeccakRhoOffsets[0] = 0;
268
+ x = 1;
269
+ y = 0;
270
+ for(t=0; t<24; t++) {
271
+ KeccakRhoOffsets[5*y+x] = ((t+1)*(t+2)/2) % 64;
272
+ newX = (0*x+1*y) % 5;
273
+ newY = (2*x+3*y) % 5;
274
+ x = newX;
275
+ y = newY;
276
+ }
277
+ }
278
+
279
+ void KeccakInitialize()
280
+ {
281
+ KeccakInitializeRoundConstants();
282
+ KeccakInitializeRhoOffsets();
283
+ }
284
+
285
+ void displayRoundConstants(FILE *f)
286
+ {
287
+ unsigned int i;
288
+
289
+ for(i=0; i<nrRounds; i++) {
290
+ fprintf(f, "RC[%02i][0][0] = ", i);
291
+ fprintf(f, "%08X:%08X", (unsigned int)(KeccakRoundConstants[i][0]), (unsigned int)(KeccakRoundConstants[i][1]));
292
+ fprintf(f, "\n");
293
+ }
294
+ fprintf(f, "\n");
295
+ }
296
+
297
+ void displayRhoOffsets(FILE *f)
298
+ {
299
+ unsigned int x, y;
300
+
301
+ for(y=0; y<5; y++) for(x=0; x<5; x++) {
302
+ fprintf(f, "RhoOffset[%i][%i] = ", x, y);
303
+ fprintf(f, "%2i", KeccakRhoOffsets[5*y+x]);
304
+ fprintf(f, "\n");
305
+ }
306
+ fprintf(f, "\n");
307
+ }
308
+
309
+ void KeccakInitializeState(unsigned char *state)
310
+ {
311
+ memset(state, 0, KeccakPermutationSizeInBytes);
312
+ }
313
+
314
+ #ifdef ProvideFast576
315
+ void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
316
+ {
317
+ KeccakPermutationAfterXor(state, data, 72);
318
+ }
319
+ #endif
320
+
321
+ #ifdef ProvideFast832
322
+ void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
323
+ {
324
+ KeccakPermutationAfterXor(state, data, 104);
325
+ }
326
+ #endif
327
+
328
+ #ifdef ProvideFast1024
329
+ void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
330
+ {
331
+ KeccakPermutationAfterXor(state, data, 128);
332
+ }
333
+ #endif
334
+
335
+ #ifdef ProvideFast1088
336
+ void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
337
+ {
338
+ KeccakPermutationAfterXor(state, data, 136);
339
+ }
340
+ #endif
341
+
342
+ #ifdef ProvideFast1152
343
+ void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
344
+ {
345
+ KeccakPermutationAfterXor(state, data, 144);
346
+ }
347
+ #endif
348
+
349
+ #ifdef ProvideFast1344
350
+ void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
351
+ {
352
+ KeccakPermutationAfterXor(state, data, 168);
353
+ }
354
+ #endif
355
+
356
+ void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
357
+ {
358
+ KeccakPermutationAfterXor(state, data, laneCount*8);
359
+ }
360
+
361
+ #ifdef ProvideFast1024
362
+ void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
363
+ {
364
+ memcpy(data, state, 128);
365
+ }
366
+ #endif
367
+
368
+ void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
369
+ {
370
+ memcpy(data, state, laneCount*8);
371
+ }