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.
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,142 @@
1
+ /*
2
+ ---------------------------------------------------------------------------
3
+ Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
4
+
5
+ LICENSE TERMS
6
+
7
+ The redistribution and use of this software (with or without changes)
8
+ is allowed without the payment of fees or royalties provided that:
9
+
10
+ 1. source code distributions include the above copyright notice, this
11
+ list of conditions and the following disclaimer;
12
+
13
+ 2. binary distributions include the above copyright notice, this list
14
+ of conditions and the following disclaimer in their documentation;
15
+
16
+ 3. the name of the copyright holder is not used to endorse products
17
+ built using this software without specific written permission.
18
+
19
+ DISCLAIMER
20
+
21
+ This software is provided 'as is' with no explicit or implied warranties
22
+ in respect of its properties, including, but not limited to, correctness
23
+ and/or fitness for purpose.
24
+ ---------------------------------------------------------------------------
25
+ Issue Date: 20/12/2007
26
+ Changes for ARM 9/9/2010
27
+ */
28
+
29
+ #ifndef _BRG_ENDIAN_H
30
+ #define _BRG_ENDIAN_H
31
+
32
+ #define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
33
+ #define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
34
+
35
+ #if 0
36
+ /* Include files where endian defines and byteswap functions may reside */
37
+ #if defined( __sun )
38
+ # include <sys/isa_defs.h>
39
+ #elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ )
40
+ # include <sys/endian.h>
41
+ #elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \
42
+ defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ )
43
+ # include <machine/endian.h>
44
+ #elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
45
+ # if !defined( __MINGW32__ ) && !defined( _AIX )
46
+ # include <endian.h>
47
+ # if !defined( __BEOS__ )
48
+ # include <byteswap.h>
49
+ # endif
50
+ # endif
51
+ #endif
52
+ #endif
53
+
54
+ /* Now attempt to set the define for platform byte order using any */
55
+ /* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which */
56
+ /* seem to encompass most endian symbol definitions */
57
+
58
+ #if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN )
59
+ # if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN
60
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
61
+ # elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN
62
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
63
+ # endif
64
+ #elif defined( BIG_ENDIAN )
65
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
66
+ #elif defined( LITTLE_ENDIAN )
67
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
68
+ #endif
69
+
70
+ #if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN )
71
+ # if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN
72
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
73
+ # elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN
74
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
75
+ # endif
76
+ #elif defined( _BIG_ENDIAN )
77
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
78
+ #elif defined( _LITTLE_ENDIAN )
79
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
80
+ #endif
81
+
82
+ #if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN )
83
+ # if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN
84
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
85
+ # elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN
86
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
87
+ # endif
88
+ #elif defined( __BIG_ENDIAN )
89
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
90
+ #elif defined( __LITTLE_ENDIAN )
91
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
92
+ #endif
93
+
94
+ #if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ )
95
+ # if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__
96
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
97
+ # elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__
98
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
99
+ # endif
100
+ #elif defined( __BIG_ENDIAN__ )
101
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
102
+ #elif defined( __LITTLE_ENDIAN__ )
103
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
104
+ #endif
105
+
106
+ /* if the platform byte order could not be determined, then try to */
107
+ /* set this define using common machine defines */
108
+ #if !defined(PLATFORM_BYTE_ORDER)
109
+
110
+ #if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \
111
+ defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \
112
+ defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \
113
+ defined( vax ) || defined( vms ) || defined( VMS ) || \
114
+ defined( __VMS ) || defined( _M_X64 )
115
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
116
+
117
+ #elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
118
+ defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
119
+ defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
120
+ defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
121
+ defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
122
+ defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM ) || \
123
+ defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX )
124
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
125
+
126
+ #elif defined(__arm__)
127
+ # ifdef __BIG_ENDIAN
128
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
129
+ # else
130
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
131
+ # endif
132
+ #elif 1 /* **** EDIT HERE IF NECESSARY **** */
133
+ # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
134
+ #elif 0 /* **** EDIT HERE IF NECESSARY **** */
135
+ # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
136
+ #else
137
+ # error Please edit lines 132 or 134 in brg_endian.h to set the platform byte order
138
+ #endif
139
+
140
+ #endif
141
+
142
+ #endif
@@ -0,0 +1,117 @@
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 "displayIntermediateValues.h"
16
+ #include "KeccakNISTInterface.h"
17
+
18
+ FILE *intermediateValueFile = 0;
19
+ int displayLevel = 0;
20
+
21
+ void displaySetIntermediateValueFile(FILE *f)
22
+ {
23
+ intermediateValueFile = f;
24
+ }
25
+
26
+ void displaySetLevel(int level)
27
+ {
28
+ displayLevel = level;
29
+ }
30
+
31
+ void displayBytes(int level, const char *text, const unsigned char *bytes, unsigned int size)
32
+ {
33
+ unsigned int i;
34
+
35
+ if ((intermediateValueFile) && (level <= displayLevel)) {
36
+ fprintf(intermediateValueFile, "%s:\n", text);
37
+ for(i=0; i<size; i++)
38
+ fprintf(intermediateValueFile, "%02X ", bytes[i]);
39
+ fprintf(intermediateValueFile, "\n");
40
+ fprintf(intermediateValueFile, "\n");
41
+ }
42
+ }
43
+
44
+ void displayBits(int level, const char *text, const unsigned char *data, unsigned int size, int MSBfirst)
45
+ {
46
+ unsigned int i, iByte, iBit;
47
+
48
+ if ((intermediateValueFile) && (level <= displayLevel)) {
49
+ fprintf(intermediateValueFile, "%s:\n", text);
50
+ for(i=0; i<size; i++) {
51
+ iByte = i/8;
52
+ iBit = i%8;
53
+ if (MSBfirst)
54
+ fprintf(intermediateValueFile, "%d ", ((data[iByte] << iBit) & 0x80) != 0);
55
+ else
56
+ fprintf(intermediateValueFile, "%d ", ((data[iByte] >> iBit) & 0x01) != 0);
57
+ }
58
+ fprintf(intermediateValueFile, "\n");
59
+ fprintf(intermediateValueFile, "\n");
60
+ }
61
+ }
62
+
63
+ void displayStateAsBytes(int level, const char *text, const unsigned char *state)
64
+ {
65
+ displayBytes(level, text, state, KeccakPermutationSizeInBytes);
66
+ }
67
+
68
+ void displayStateAs32bitWords(int level, const char *text, const unsigned int *state)
69
+ {
70
+ unsigned int i;
71
+
72
+ if ((intermediateValueFile) && (level <= displayLevel)) {
73
+ fprintf(intermediateValueFile, "%s:\n", text);
74
+ for(i=0; i<KeccakPermutationSize/64; i++) {
75
+ fprintf(intermediateValueFile, "%08X:%08X", (unsigned int)state[2*i+0], (unsigned int)state[2*i+1]);
76
+ if ((i%5) == 4)
77
+ fprintf(intermediateValueFile, "\n");
78
+ else
79
+ fprintf(intermediateValueFile, " ");
80
+ }
81
+ }
82
+ }
83
+
84
+ void displayStateAs64bitWords(int level, const char *text, const unsigned long long int *state)
85
+ {
86
+ unsigned int i;
87
+
88
+ if ((intermediateValueFile) && (level <= displayLevel)) {
89
+ fprintf(intermediateValueFile, "%s:\n", text);
90
+ for(i=0; i<KeccakPermutationSize/64; i++) {
91
+ fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] >> 32));
92
+ fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] & 0xFFFFFFFFULL));
93
+ if ((i%5) == 4)
94
+ fprintf(intermediateValueFile, "\n");
95
+ else
96
+ fprintf(intermediateValueFile, " ");
97
+ }
98
+ }
99
+ }
100
+
101
+ void displayRoundNumber(int level, unsigned int i)
102
+ {
103
+ if ((intermediateValueFile) && (level <= displayLevel)) {
104
+ fprintf(intermediateValueFile, "\n");
105
+ fprintf(intermediateValueFile, "--- Round %d ---\n", i);
106
+ fprintf(intermediateValueFile, "\n");
107
+ }
108
+ }
109
+
110
+ void displayText(int level, const char *text)
111
+ {
112
+ if ((intermediateValueFile) && (level <= displayLevel)) {
113
+ fprintf(intermediateValueFile, text);
114
+ fprintf(intermediateValueFile, "\n");
115
+ fprintf(intermediateValueFile, "\n");
116
+ }
117
+ }
@@ -0,0 +1,29 @@
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 _displayIntermediateValues_h_
15
+ #define _displayIntermediateValues_h_
16
+
17
+ #include <stdio.h>
18
+
19
+ void displaySetIntermediateValueFile(FILE *f);
20
+ void displaySetLevel(int level);
21
+ void displayBytes(int level, const char *text, const unsigned char *bytes, unsigned int size);
22
+ void displayBits(int level, const char *text, const unsigned char *data, unsigned int size, int MSBfirst);
23
+ void displayStateAsBytes(int level, const char *text, const unsigned char *state);
24
+ void displayStateAs32bitWords(int level, const char *text, const unsigned int *state);
25
+ void displayStateAs64bitWords(int level, const char *text, const unsigned long long int *state);
26
+ void displayRoundNumber(int level, unsigned int i);
27
+ void displayText(int level, const char *text);
28
+
29
+ #endif
@@ -0,0 +1,692 @@
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
+
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+ #include <time.h>
11
+ #include <ctype.h>
12
+
13
+ #include "KeccakDuplex.h"
14
+ #include "KeccakNISTInterface.h"
15
+ #include "KeccakSponge.h"
16
+
17
+ #define MAX_MARKER_LEN 50
18
+ #define SUBMITTER_INFO_LEN 128
19
+
20
+ typedef enum { KAT_SUCCESS = 0, KAT_FILE_OPEN_ERROR = 1, KAT_HEADER_ERROR = 2, KAT_DATA_ERROR = 3, KAT_HASH_ERROR = 4 } STATUS_CODES;
21
+
22
+ #define AllowExtendedFunctions
23
+ #define ExcludeExtremelyLong
24
+
25
+ #ifdef AllowExtendedFunctions
26
+ #define SqueezingOutputLength 4096
27
+ #endif
28
+
29
+ STATUS_CODES genShortMsg(int hashbitlen);
30
+ STATUS_CODES genLongMsg(int hashbitlen);
31
+ STATUS_CODES genExtremelyLongMsg(int hashbitlen);
32
+ STATUS_CODES genMonteCarlo(int hashbitlen);
33
+ #ifdef AllowExtendedFunctions
34
+ STATUS_CODES genMonteCarloSqueezing(int hashbitlen);
35
+ STATUS_CODES genShortMsgSponge(unsigned int rate, unsigned int capacity, int outputLength, const char *fileName);
36
+ STATUS_CODES genDuplexKAT(unsigned int rate, unsigned int capacity, const char *fileName);
37
+ #endif
38
+ int FindMarker(FILE *infile, const char *marker);
39
+ int ReadHex(FILE *infile, BitSequence *A, int Length, char *str);
40
+ void fprintBstr(FILE *fp, char *S, BitSequence *A, int L);
41
+
42
+
43
+ STATUS_CODES
44
+ genKAT_main()
45
+ {
46
+ int i, ret_val, bitlens[4] = { 224, 256, 384, 512 };
47
+
48
+ #ifdef AllowExtendedFunctions
49
+ if ( (ret_val = genShortMsgSponge(1024, 576, 4096, "ShortMsgKAT_0.txt")) != KAT_SUCCESS )
50
+ return (STATUS_CODES)ret_val;
51
+ if ( (ret_val = genLongMsg(0)) != KAT_SUCCESS )
52
+ return (STATUS_CODES)ret_val;
53
+ #ifndef ExcludeExtremelyLong
54
+ if ( (ret_val = genExtremelyLongMsg(0)) != KAT_SUCCESS )
55
+ return (STATUS_CODES)ret_val;
56
+ #endif
57
+ if ( (ret_val = genMonteCarloSqueezing(0)) != KAT_SUCCESS )
58
+ return (STATUS_CODES)ret_val;
59
+ #endif
60
+
61
+ for ( i=0; i<4; i++ ) {
62
+ if ( (ret_val = genShortMsg(bitlens[i])) != KAT_SUCCESS )
63
+ return (STATUS_CODES)ret_val;
64
+ if ( (ret_val = genLongMsg(bitlens[i])) != KAT_SUCCESS )
65
+ return (STATUS_CODES)ret_val;
66
+ #ifndef ExcludeExtremelyLong
67
+ if ( (ret_val = genExtremelyLongMsg(bitlens[i])) != KAT_SUCCESS )
68
+ return (STATUS_CODES)ret_val;
69
+ #endif
70
+ if ( (ret_val = genMonteCarlo(bitlens[i])) != KAT_SUCCESS )
71
+ return (STATUS_CODES)ret_val;
72
+ }
73
+
74
+ #ifdef AllowExtendedFunctions
75
+ /* Other case examples */
76
+ genShortMsgSponge(1344, 256, 4096, "ShortMsgKAT_r1344c256.txt");
77
+ /* Duplexing */
78
+ //genDuplexKAT(1024, 576, "DuplexKAT_r1024c576.txt");
79
+ //genDuplexKAT(1025, 575, "DuplexKAT_r1025c575.txt");
80
+ genDuplexKAT(1026, 574, "DuplexKAT_r1026c574.txt");
81
+ genDuplexKAT(1027, 573, "DuplexKAT_r1027c573.txt");
82
+ //genDuplexKAT(1028, 572, "DuplexKAT_r1028c572.txt");
83
+ //genDuplexKAT(1029, 571, "DuplexKAT_r1029c571.txt");
84
+ //genDuplexKAT(1030, 570, "DuplexKAT_r1030c570.txt");
85
+ //genDuplexKAT(1031, 569, "DuplexKAT_r1031c569.txt");
86
+ //genDuplexKAT(1032, 568, "DuplexKAT_r1032c568.txt");
87
+ #endif
88
+
89
+ return KAT_SUCCESS;
90
+ }
91
+
92
+ STATUS_CODES
93
+ genShortMsg(int hashbitlen)
94
+ {
95
+ char fn[32], line[SUBMITTER_INFO_LEN];
96
+ int msglen, msgbytelen, done;
97
+ BitSequence Msg[256], MD[64];
98
+ FILE *fp_in, *fp_out;
99
+
100
+ if ( (fp_in = fopen("ShortMsgKAT.txt", "r")) == NULL ) {
101
+ printf("Couldn't open <ShortMsgKAT.txt> for read\n");
102
+ return KAT_FILE_OPEN_ERROR;
103
+ }
104
+
105
+ sprintf(fn, "ShortMsgKAT_%d.txt", hashbitlen);
106
+ if ( (fp_out = fopen(fn, "w")) == NULL ) {
107
+ printf("Couldn't open <%s> for write\n", fn);
108
+ return KAT_FILE_OPEN_ERROR;
109
+ }
110
+ fprintf(fp_out, "# %s\n", fn);
111
+ if ( FindMarker(fp_in, "# Algorithm Name:") ) {
112
+ fscanf(fp_in, "%[^\n]\n", line);
113
+ fprintf(fp_out, "# Algorithm Name:%s\n", line);
114
+ }
115
+ else {
116
+ printf("genShortMsg: Couldn't read Algorithm Name\n");
117
+ return KAT_HEADER_ERROR;
118
+ }
119
+ if ( FindMarker(fp_in, "# Principal Submitter:") ) {
120
+ fscanf(fp_in, "%[^\n]\n", line);
121
+ fprintf(fp_out, "# Principal Submitter:%s\n", line);
122
+ }
123
+ else {
124
+ printf("genShortMsg: Couldn't read Principal Submitter\n");
125
+ return KAT_HEADER_ERROR;
126
+ }
127
+
128
+ done = 0;
129
+ do {
130
+ if ( FindMarker(fp_in, "Len = ") )
131
+ fscanf(fp_in, "%d", &msglen);
132
+ else {
133
+ done = 1;
134
+ break;
135
+ }
136
+ msgbytelen = (msglen+7)/8;
137
+
138
+ if ( !ReadHex(fp_in, Msg, msgbytelen, "Msg = ") ) {
139
+ printf("ERROR: unable to read 'Msg' from <ShortMsgKAT.txt>\n");
140
+ return KAT_DATA_ERROR;
141
+ }
142
+ Hash(hashbitlen, Msg, msglen, MD);
143
+ fprintf(fp_out, "\nLen = %d\n", msglen);
144
+ fprintBstr(fp_out, "Msg = ", Msg, msgbytelen);
145
+ fprintBstr(fp_out, "MD = ", MD, hashbitlen/8);
146
+ } while ( !done );
147
+ printf("finished ShortMsgKAT for <%d>\n", hashbitlen);
148
+
149
+ fclose(fp_in);
150
+ fclose(fp_out);
151
+
152
+ return KAT_SUCCESS;
153
+ }
154
+
155
+ #ifdef AllowExtendedFunctions
156
+ STATUS_CODES
157
+ genShortMsgSponge(unsigned int rate, unsigned int capacity, int outputLength, const char *fileName)
158
+ {
159
+ char line[SUBMITTER_INFO_LEN];
160
+ int msglen, msgbytelen, done;
161
+ BitSequence Msg[256];
162
+ BitSequence Squeezed[SqueezingOutputLength/8];
163
+ spongeState state;
164
+ FILE *fp_in, *fp_out;
165
+
166
+ if (outputLength > SqueezingOutputLength) {
167
+ printf("Requested output length too long.\n");
168
+ return KAT_HASH_ERROR;
169
+ }
170
+
171
+ if ( (fp_in = fopen("ShortMsgKAT.txt", "r")) == NULL ) {
172
+ printf("Couldn't open <ShortMsgKAT.txt> for read\n");
173
+ return KAT_FILE_OPEN_ERROR;
174
+ }
175
+
176
+ if ( (fp_out = fopen(fileName, "w")) == NULL ) {
177
+ printf("Couldn't open <%s> for write\n", fileName);
178
+ return KAT_FILE_OPEN_ERROR;
179
+ }
180
+ fprintf(fp_out, "# %s\n", fileName);
181
+ if ( FindMarker(fp_in, "# Algorithm Name:") ) {
182
+ fscanf(fp_in, "%[^\n]\n", line);
183
+ fprintf(fp_out, "# Algorithm Name:%s\n", line);
184
+ }
185
+ else {
186
+ printf("genShortMsg: Couldn't read Algorithm Name\n");
187
+ return KAT_HEADER_ERROR;
188
+ }
189
+ if ( FindMarker(fp_in, "# Principal Submitter:") ) {
190
+ fscanf(fp_in, "%[^\n]\n", line);
191
+ fprintf(fp_out, "# Principal Submitter:%s\n", line);
192
+ }
193
+ else {
194
+ printf("genShortMsg: Couldn't read Principal Submitter\n");
195
+ return KAT_HEADER_ERROR;
196
+ }
197
+
198
+ done = 0;
199
+ do {
200
+ if ( FindMarker(fp_in, "Len = ") )
201
+ fscanf(fp_in, "%d", &msglen);
202
+ else {
203
+ done = 1;
204
+ break;
205
+ }
206
+ msgbytelen = (msglen+7)/8;
207
+
208
+ if ( !ReadHex(fp_in, Msg, msgbytelen, "Msg = ") ) {
209
+ printf("ERROR: unable to read 'Msg' from <ShortMsgKAT.txt>\n");
210
+ return KAT_DATA_ERROR;
211
+ }
212
+ fprintf(fp_out, "\nLen = %d\n", msglen);
213
+ fprintBstr(fp_out, "Msg = ", Msg, msgbytelen);
214
+ InitSponge(&state, rate, capacity);
215
+ if ((msglen % 8 ) != 0)
216
+ // From NIST convention to internal convention for last byte
217
+ Msg[msgbytelen - 1] >>= 8 - (msglen % 8);
218
+ Absorb(&state, Msg, msglen);
219
+ Squeeze(&state, Squeezed, outputLength);
220
+ fprintBstr(fp_out, "Squeezed = ", Squeezed, SqueezingOutputLength/8);
221
+ } while ( !done );
222
+ printf("finished ShortMsgKAT for <%s>\n", fileName);
223
+
224
+ fclose(fp_in);
225
+ fclose(fp_out);
226
+
227
+ return KAT_SUCCESS;
228
+ }
229
+ #endif
230
+
231
+ STATUS_CODES
232
+ genLongMsg(int hashbitlen)
233
+ {
234
+ char fn[32], line[SUBMITTER_INFO_LEN];
235
+ int msglen, msgbytelen, done;
236
+ BitSequence Msg[4288], MD[64];
237
+ #ifdef AllowExtendedFunctions
238
+ BitSequence Squeezed[SqueezingOutputLength/8];
239
+ hashState state;
240
+ #endif
241
+ FILE *fp_in, *fp_out;
242
+
243
+ if ( (fp_in = fopen("LongMsgKAT.txt", "r")) == NULL ) {
244
+ printf("Couldn't open <LongMsgKAT.txt> for read\n");
245
+ return KAT_FILE_OPEN_ERROR;
246
+ }
247
+
248
+ sprintf(fn, "LongMsgKAT_%d.txt", hashbitlen);
249
+ if ( (fp_out = fopen(fn, "w")) == NULL ) {
250
+ printf("Couldn't open <%s> for write\n", fn);
251
+ return KAT_FILE_OPEN_ERROR;
252
+ }
253
+ fprintf(fp_out, "# %s\n", fn);
254
+ if ( FindMarker(fp_in, "# Algorithm Name:") ) {
255
+ fscanf(fp_in, "%[^\n]\n", line);
256
+ fprintf(fp_out, "# Algorithm Name:%s\n", line);
257
+ }
258
+ else {
259
+ printf("genLongMsg: Couldn't read Algorithm Name\n");
260
+ return KAT_HEADER_ERROR;
261
+ }
262
+ if ( FindMarker(fp_in, "# Principal Submitter:") ) {
263
+ fscanf(fp_in, "%[^\n]\n", line);
264
+ fprintf(fp_out, "# Principal Submitter:%s\n\n", line);
265
+ }
266
+ else {
267
+ printf("genLongMsg: Couldn't read Principal Submitter\n");
268
+ return KAT_HEADER_ERROR;
269
+ }
270
+
271
+ done = 0;
272
+ do {
273
+ if ( FindMarker(fp_in, "Len = ") )
274
+ fscanf(fp_in, "%d", &msglen);
275
+ else
276
+ break;
277
+ msgbytelen = (msglen+7)/8;
278
+
279
+ if ( !ReadHex(fp_in, Msg, msgbytelen, "Msg = ") ) {
280
+ printf("ERROR: unable to read 'Msg' from <LongMsgKAT.txt>\n");
281
+ return KAT_DATA_ERROR;
282
+ }
283
+ #ifdef AllowExtendedFunctions
284
+ if (hashbitlen > 0)
285
+ Hash(hashbitlen, Msg, msglen, MD);
286
+ else {
287
+ Init(&state, hashbitlen);
288
+ Update(&state, Msg, msglen);
289
+ Final(&state, 0);
290
+ Squeeze(&state, Squeezed, SqueezingOutputLength);
291
+ }
292
+ #else
293
+ Hash(hashbitlen, Msg, msglen, MD);
294
+ #endif
295
+ fprintf(fp_out, "Len = %d\n", msglen);
296
+ fprintBstr(fp_out, "Msg = ", Msg, msgbytelen);
297
+ #ifdef AllowExtendedFunctions
298
+ if (hashbitlen > 0)
299
+ fprintBstr(fp_out, "MD = ", MD, hashbitlen/8);
300
+ else
301
+ fprintBstr(fp_out, "Squeezed = ", Squeezed, SqueezingOutputLength/8);
302
+ #else
303
+ fprintBstr(fp_out, "MD = ", MD, hashbitlen/8);
304
+ #endif
305
+ } while ( !done );
306
+ printf("finished LongMsgKAT for <%d>\n", hashbitlen);
307
+
308
+ fclose(fp_in);
309
+ fclose(fp_out);
310
+
311
+ return KAT_SUCCESS;
312
+ }
313
+
314
+ STATUS_CODES
315
+ genExtremelyLongMsg(int hashbitlen)
316
+ {
317
+ char fn[32], line[SUBMITTER_INFO_LEN];
318
+ BitSequence Text[65], MD[64];
319
+ #ifdef AllowExtendedFunctions
320
+ BitSequence Squeezed[SqueezingOutputLength/8];
321
+ #endif
322
+ int i, repeat;
323
+ FILE *fp_in, *fp_out;
324
+ hashState state;
325
+ HashReturn retval;
326
+
327
+ if ( (fp_in = fopen("ExtremelyLongMsgKAT.txt", "r")) == NULL ) {
328
+ printf("Couldn't open <ExtremelyLongMsgKAT.txt> for read\n");
329
+ return KAT_FILE_OPEN_ERROR;
330
+ }
331
+
332
+ sprintf(fn, "ExtremelyLongMsgKAT_%d.txt", hashbitlen);
333
+ if ( (fp_out = fopen(fn, "w")) == NULL ) {
334
+ printf("Couldn't open <%s> for write\n", fn);
335
+ return KAT_FILE_OPEN_ERROR;
336
+ }
337
+ fprintf(fp_out, "# %s\n", fn);
338
+ if ( FindMarker(fp_in, "# Algorithm Name:") ) {
339
+ fscanf(fp_in, "%[^\n]\n", line);
340
+ fprintf(fp_out, "# Algorithm Name:%s\n", line);
341
+ }
342
+ else {
343
+ printf("genExtremelyLongMsg: Couldn't read Algorithm Name\n");
344
+ return KAT_HEADER_ERROR;
345
+ }
346
+ if ( FindMarker(fp_in, "# Principal Submitter:") ) {
347
+ fscanf(fp_in, "%[^\n]\n", line);
348
+ fprintf(fp_out, "# Principal Submitter:%s\n\n", line);
349
+ }
350
+ else {
351
+ printf("genExtremelyLongMsg: Couldn't read Principal Submitter\n");
352
+ return KAT_HEADER_ERROR;
353
+ }
354
+
355
+ if ( FindMarker(fp_in, "Repeat = ") )
356
+ fscanf(fp_in, "%d", &repeat);
357
+ else {
358
+ printf("ERROR: unable to read 'Repeat' from <ExtremelyLongMsgKAT.txt>\n");
359
+ return KAT_DATA_ERROR;
360
+ }
361
+
362
+ if ( FindMarker(fp_in, "Text = ") )
363
+ fscanf(fp_in, "%s", Text);
364
+ else {
365
+ printf("ERROR: unable to read 'Text' from <ExtremelyLongMsgKAT.txt>\n");
366
+ return KAT_DATA_ERROR;
367
+ }
368
+
369
+ // memcpy(Text, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno", 64);
370
+
371
+ if ( (retval = Init(&state, hashbitlen)) != KAT_SUCCESS ) {
372
+ printf("Init returned <%d> in genExtremelyLongMsg\n", retval);
373
+ return KAT_HASH_ERROR;
374
+ }
375
+ for ( i=0; i<repeat; i++ )
376
+ if ( (retval = Update(&state, Text, 512)) != KAT_SUCCESS ) {
377
+ printf("Update returned <%d> in genExtremelyLongMsg\n", retval);
378
+ return KAT_HASH_ERROR;
379
+ }
380
+ if ( (retval = Final(&state, MD)) != KAT_SUCCESS ) {
381
+ printf("Final returned <%d> in genExtremelyLongMsg\n", retval);
382
+ return KAT_HASH_ERROR;
383
+ }
384
+ #ifdef AllowExtendedFunctions
385
+ if (hashbitlen == 0)
386
+ Squeeze(&state, Squeezed, SqueezingOutputLength);
387
+ #endif
388
+ fprintf(fp_out, "Repeat = %d\n", repeat);
389
+ fprintf(fp_out, "Text = %s\n", Text);
390
+ #ifdef AllowExtendedFunctions
391
+ if (hashbitlen > 0)
392
+ fprintBstr(fp_out, "MD = ", MD, hashbitlen/8);
393
+ else
394
+ fprintBstr(fp_out, "Squeezed = ", Squeezed, SqueezingOutputLength/8);
395
+ #else
396
+ fprintBstr(fp_out, "MD = ", MD, hashbitlen/8);
397
+ #endif
398
+ printf("finished ExtremelyLongMsgKAT for <%d>\n", hashbitlen);
399
+
400
+ fclose(fp_in);
401
+ fclose(fp_out);
402
+
403
+ return KAT_SUCCESS;
404
+ }
405
+
406
+ STATUS_CODES
407
+ genMonteCarlo(int hashbitlen)
408
+ {
409
+ char fn[32], line[SUBMITTER_INFO_LEN];
410
+ BitSequence Seed[128], Msg[128], MD[64], Temp[128];
411
+ int i, j, bytelen;
412
+ FILE *fp_in, *fp_out;
413
+
414
+ if ( (fp_in = fopen("MonteCarlo.txt", "r")) == NULL ) {
415
+ printf("Couldn't open <MonteCarlo.txt> for read\n");
416
+ return KAT_FILE_OPEN_ERROR;
417
+ }
418
+
419
+ sprintf(fn, "MonteCarlo_%d.txt", hashbitlen);
420
+ if ( (fp_out = fopen(fn, "w")) == NULL ) {
421
+ printf("Couldn't open <%s> for write\n", fn);
422
+ return KAT_FILE_OPEN_ERROR;
423
+ }
424
+ fprintf(fp_out, "# %s\n", fn);
425
+ if ( FindMarker(fp_in, "# Algorithm Name:") ) {
426
+ fscanf(fp_in, "%[^\n]\n", line);
427
+ fprintf(fp_out, "# Algorithm Name:%s\n", line);
428
+ }
429
+ else {
430
+ printf("genMonteCarlo: Couldn't read Algorithm Name\n");
431
+ return KAT_HEADER_ERROR;
432
+ }
433
+ if ( FindMarker(fp_in, "# Principal Submitter:") ) {
434
+ fscanf(fp_in, "%[^\n]\n", line);
435
+ fprintf(fp_out, "# Principal Submitter:%s\n\n", line);
436
+ }
437
+ else {
438
+ printf("genMonteCarlo: Couldn't read Principal Submitter\n");
439
+ return KAT_HEADER_ERROR;
440
+ }
441
+
442
+ if ( !ReadHex(fp_in, Seed, 128, "Seed = ") ) {
443
+ printf("ERROR: unable to read 'Seed' from <MonteCarlo.txt>\n");
444
+ return KAT_DATA_ERROR;
445
+ }
446
+
447
+ bytelen = hashbitlen / 8;
448
+ memcpy(Msg, Seed, 128);
449
+ fprintBstr(fp_out, "Seed = ", Seed, 128);
450
+ for ( j=0; j<100; j++ ) {
451
+ for ( i=0; i<1000; i++ ) {
452
+ Hash(hashbitlen, Msg, 1024, MD);
453
+ memcpy(Temp, Msg, 128-bytelen);
454
+ memcpy(Msg, MD, bytelen);
455
+ memcpy(Msg+bytelen, Temp, 128-bytelen);
456
+ }
457
+ fprintf(fp_out, "\nj = %d\n", j);
458
+ fprintBstr(fp_out, "MD = ", MD, bytelen);
459
+ }
460
+ printf("finished MonteCarloKAT for <%d>\n", hashbitlen);
461
+
462
+ fclose(fp_in);
463
+ fclose(fp_out);
464
+
465
+ return KAT_SUCCESS;
466
+ }
467
+
468
+ #ifdef AllowExtendedFunctions
469
+ STATUS_CODES
470
+ genMonteCarloSqueezing(int hashbitlen)
471
+ {
472
+ char fn[32], line[SUBMITTER_INFO_LEN];
473
+ BitSequence Seed[128], MD[64];
474
+ int i, j, bytelen;
475
+ FILE *fp_in, *fp_out;
476
+ hashState state;
477
+ HashReturn retval;
478
+
479
+ if ( (fp_in = fopen("MonteCarlo.txt", "r")) == NULL ) {
480
+ printf("Couldn't open <MonteCarlo.txt> for read\n");
481
+ return KAT_FILE_OPEN_ERROR;
482
+ }
483
+
484
+ sprintf(fn, "MonteCarlo_%d.txt", hashbitlen);
485
+ if ( (fp_out = fopen(fn, "w")) == NULL ) {
486
+ printf("Couldn't open <%s> for write\n", fn);
487
+ return KAT_FILE_OPEN_ERROR;
488
+ }
489
+ fprintf(fp_out, "# %s\n", fn);
490
+ if ( FindMarker(fp_in, "# Algorithm Name:") ) {
491
+ fscanf(fp_in, "%[^\n]\n", line);
492
+ fprintf(fp_out, "# Algorithm Name:%s\n", line);
493
+ }
494
+ else {
495
+ printf("genMonteCarlo: Couldn't read Algorithm Name\n");
496
+ return KAT_HEADER_ERROR;
497
+ }
498
+ if ( FindMarker(fp_in, "# Principal Submitter:") ) {
499
+ fscanf(fp_in, "%[^\n]\n", line);
500
+ fprintf(fp_out, "# Principal Submitter:%s\n\n", line);
501
+ }
502
+ else {
503
+ printf("genMonteCarlo: Couldn't read Principal Submitter\n");
504
+ return KAT_HEADER_ERROR;
505
+ }
506
+
507
+ if ( !ReadHex(fp_in, Seed, 128, "Seed = ") ) {
508
+ printf("ERROR: unable to read 'Seed' from <MonteCarlo.txt>\n");
509
+ return KAT_DATA_ERROR;
510
+ }
511
+
512
+ fprintBstr(fp_out, "Seed = ", Seed, 128);
513
+
514
+ if ( (retval = Init(&state, hashbitlen)) != KAT_SUCCESS ) {
515
+ printf("Init returned <%d> in genMonteCarloSqueezing\n", retval);
516
+ return KAT_HASH_ERROR;
517
+ }
518
+ if ( (retval = Update(&state, Seed, 128*8)) != KAT_SUCCESS ) {
519
+ printf("Update returned <%d> in genMonteCarloSqueezing\n", retval);
520
+ return KAT_HASH_ERROR;
521
+ }
522
+ if ( (retval = Final(&state, 0)) != KAT_SUCCESS ) {
523
+ printf("Final returned <%d> in genMonteCarloSqueezing\n", retval);
524
+ return KAT_HASH_ERROR;
525
+ }
526
+ bytelen = 64;
527
+ for ( j=0; j<100; j++ ) {
528
+ for ( i=0; i<1000; i++ ) {
529
+ if ( (retval = Squeeze(&state, MD, bytelen*8)) != KAT_SUCCESS ) {
530
+ printf("Squeeze returned <%d> in genMonteCarloSqueezing\n", retval);
531
+ return KAT_HASH_ERROR;
532
+ }
533
+ }
534
+ fprintf(fp_out, "\nj = %d\n", j);
535
+ fprintBstr(fp_out, "MD = ", MD, bytelen);
536
+ }
537
+ printf("finished MonteCarloKAT for <%d>\n", hashbitlen);
538
+
539
+ fclose(fp_in);
540
+ fclose(fp_out);
541
+
542
+ return KAT_SUCCESS;
543
+ }
544
+
545
+ STATUS_CODES
546
+ genDuplexKAT(unsigned int rate, unsigned int capacity, const char *fileName)
547
+ {
548
+ int inLen, inByteLen, outLen, outByteLen, done;
549
+ BitSequence in[256];
550
+ BitSequence out[256];
551
+ FILE *fp_in, *fp_out;
552
+ duplexState state;
553
+
554
+ if ( (fp_in = fopen("DuplexKAT.txt", "r")) == NULL ) {
555
+ printf("Couldn't open <DuplexKAT.txt> for read\n");
556
+ return KAT_FILE_OPEN_ERROR;
557
+ }
558
+
559
+ if ( (fp_out = fopen(fileName, "w")) == NULL ) {
560
+ printf("Couldn't open <%s> for write\n", fileName);
561
+ return KAT_FILE_OPEN_ERROR;
562
+ }
563
+ fprintf(fp_out, "# %s\n", fileName);
564
+ fprintf(fp_out, "# Algorithm: Duplex[f=Keccak-f[1600], pad=pad10*1, r=%d, c=%d, \xCF\x81max=%d]\n", rate, capacity, rate-2);
565
+
566
+ InitDuplex(&state, rate, capacity);
567
+ done = 0;
568
+ outLen = rate;
569
+ outByteLen = (outLen+7)/8;
570
+ do {
571
+ if ( FindMarker(fp_in, "InLen = ") )
572
+ fscanf(fp_in, "%d", &inLen);
573
+ else {
574
+ done = 1;
575
+ break;
576
+ }
577
+ inByteLen = (inLen+7)/8;
578
+
579
+ if ( !ReadHex(fp_in, in, inByteLen, "In = ") ) {
580
+ printf("ERROR: unable to read 'In' from <DuplexKAT.txt>\n");
581
+ return KAT_DATA_ERROR;
582
+ }
583
+ if (inLen <= rate-2) {
584
+ fprintf(fp_out, "\nInLen = %d\n", inLen);
585
+ fprintBstr(fp_out, "In = ", in, inByteLen);
586
+ Duplexing(&state, in, inLen, out, outLen);
587
+ fprintf(fp_out, "OutLen = %d\n", outLen);
588
+ fprintBstr(fp_out, "Out = ", out, outByteLen);
589
+ }
590
+ } while ( !done );
591
+ printf("finished DuplexKAT for <%s>\n", fileName);
592
+
593
+ fclose(fp_in);
594
+ fclose(fp_out);
595
+
596
+ return KAT_SUCCESS;
597
+ }
598
+ #endif
599
+
600
+ //
601
+ // ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
602
+ //
603
+ int
604
+ FindMarker(FILE *infile, const char *marker)
605
+ {
606
+ char line[MAX_MARKER_LEN];
607
+ int i, len;
608
+
609
+ len = (int)strlen(marker);
610
+ if ( len > MAX_MARKER_LEN-1 )
611
+ len = MAX_MARKER_LEN-1;
612
+
613
+ for ( i=0; i<len; i++ )
614
+ if ( (line[i] = fgetc(infile)) == EOF )
615
+ return 0;
616
+ line[len] = '\0';
617
+
618
+ while ( 1 ) {
619
+ if ( !strncmp(line, marker, len) )
620
+ return 1;
621
+
622
+ for ( i=0; i<len-1; i++ )
623
+ line[i] = line[i+1];
624
+ if ( (line[len-1] = fgetc(infile)) == EOF )
625
+ return 0;
626
+ line[len] = '\0';
627
+ }
628
+
629
+ // shouldn't get here
630
+ return 0;
631
+ }
632
+
633
+ //
634
+ // ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
635
+ //
636
+ int
637
+ ReadHex(FILE *infile, BitSequence *A, int Length, char *str)
638
+ {
639
+ int i, ch, started;
640
+ BitSequence ich;
641
+
642
+ if ( Length == 0 ) {
643
+ A[0] = 0x00;
644
+ return 1;
645
+ }
646
+ memset(A, 0x00, Length);
647
+ started = 0;
648
+ if ( FindMarker(infile, str) )
649
+ while ( (ch = fgetc(infile)) != EOF ) {
650
+ if ( !isxdigit(ch) ) {
651
+ if ( !started ) {
652
+ if ( ch == '\n' )
653
+ break;
654
+ else
655
+ continue;
656
+ }
657
+ else
658
+ break;
659
+ }
660
+ started = 1;
661
+ if ( (ch >= '0') && (ch <= '9') )
662
+ ich = ch - '0';
663
+ else if ( (ch >= 'A') && (ch <= 'F') )
664
+ ich = ch - 'A' + 10;
665
+ else if ( (ch >= 'a') && (ch <= 'f') )
666
+ ich = ch - 'a' + 10;
667
+
668
+ for ( i=0; i<Length-1; i++ )
669
+ A[i] = (A[i] << 4) | (A[i+1] >> 4);
670
+ A[Length-1] = (A[Length-1] << 4) | ich;
671
+ }
672
+ else
673
+ return 0;
674
+
675
+ return 1;
676
+ }
677
+
678
+ void
679
+ fprintBstr(FILE *fp, char *S, BitSequence *A, int L)
680
+ {
681
+ int i;
682
+
683
+ fprintf(fp, "%s", S);
684
+
685
+ for ( i=0; i<L; i++ )
686
+ fprintf(fp, "%02X", A[i]);
687
+
688
+ if ( L == 0 )
689
+ fprintf(fp, "00");
690
+
691
+ fprintf(fp, "\n");
692
+ }