sha3 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +8 -5
  4. data/certs/johanns.pem +23 -22
  5. data/ext/sha3/config.h +26 -0
  6. data/ext/sha3/digest.c +6 -6
  7. data/ext/sha3/extconf.rb +40 -10
  8. data/ext/sha3/lib/common/align.h +33 -0
  9. data/ext/sha3/{brg_endian.h → lib/common/brg_endian.h} +8 -7
  10. data/ext/sha3/{KeccakHash.c → lib/high/Keccak/FIPS202/KeccakHash.c} +26 -25
  11. data/ext/sha3/{KeccakHash.h → lib/high/Keccak/FIPS202/KeccakHash.h} +34 -19
  12. data/ext/sha3/lib/high/Keccak/KeccakSponge.c +111 -0
  13. data/ext/sha3/lib/high/Keccak/KeccakSponge.h +76 -0
  14. data/ext/sha3/lib/high/Keccak/KeccakSponge.inc +316 -0
  15. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-64.macros +748 -0
  16. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-unrolling.macros +305 -0
  17. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-SnP.h +44 -0
  18. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference.h +23 -0
  19. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c +625 -0
  20. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-SnP.h +44 -0
  21. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c +444 -0
  22. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.h +23 -0
  23. data/lib/sha3/version.rb +1 -1
  24. data/sha3.gemspec +6 -5
  25. data/tests.sh +8 -7
  26. data.tar.gz.sig +0 -0
  27. metadata +50 -66
  28. metadata.gz.sig +0 -0
  29. data/ext/sha3/KeccakF-1600-interface.h +0 -40
  30. data/ext/sha3/KeccakSponge.c +0 -192
  31. data/ext/sha3/KeccakSponge.h +0 -113
  32. data/ext/sha3/Optimized64/KeccakF-1600-64.macros +0 -2199
  33. data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +0 -3
  34. data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +0 -508
  35. data/ext/sha3/Optimized64/KeccakF-1600-unrolling.macros +0 -126
  36. data/ext/sha3/Optimized64/SnP-interface.h +0 -47
  37. data/ext/sha3/Reference/KeccakF-1600-reference.c +0 -311
  38. data/ext/sha3/Reference/KeccakF-reference.h +0 -26
  39. data/ext/sha3/Reference/SnP-FBWL-default.c +0 -96
  40. data/ext/sha3/Reference/SnP-FBWL-default.h +0 -26
  41. data/ext/sha3/Reference/SnP-interface.h +0 -42
  42. data/ext/sha3/Reference/displayIntermediateValues.c +0 -158
  43. data/ext/sha3/Reference/displayIntermediateValues.h +0 -34
  44. data/ext/sha3/SnP-Relaned.h +0 -249
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae66a849bd5438b3b6d3fa4f5d33505a75e6f64556bfacc21d0efcf03a7d41d8
4
- data.tar.gz: dea6beac49eeaeb12829af4e87b36dfc19ac1b66bb7872a7c6884441a70cf814
3
+ metadata.gz: 16edba407e859ee7afacc8a405666ec66cab0052cd7788e79089bd3f0d295844
4
+ data.tar.gz: 5552886038bbec790d086599cbbe6ad09a0c3561b58a9a2dfff2b9c9834b7154
5
5
  SHA512:
6
- metadata.gz: d374e5f716056d6d888d862f97bc07d433c74f4d721d2342cc09f7865571e47eec50f7b1ecd9b284aedb1c908393b0da249e0c474f5792e215097263928d9195
7
- data.tar.gz: 6ea6442adf3c90ca8849576f8539835630d968a032b385447a9c95e2f4be97861910f510ba528dbe398990c2eb71fac6b3966816807571855b855afa8c6af15b
6
+ metadata.gz: a8e36ae984df177684d4b1a70689a80824e9d9ee70fd07812eaa50c8f0cd90a90f292dca435764842b737fb268084d09fa5f47450bdc9cf93316253c70484945
7
+ data.tar.gz: a643ded4e2f9828d732f428eba1757c6cc383797f32b52ba7427dae8234ac9bc0e158712ccf64d7be77f2538b80904efaf26a0e455ce9515f9a76679929054c0
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/sha3.svg)](https://badge.fury.io/rb/sha3) [![Ruby](https://github.com/johanns/sha3/actions/workflows/main.yml/badge.svg)](https://github.com/johanns/sha3/actions/workflows/main.yml)
4
4
 
5
- **SHA3 for Ruby** is a native (C) binding to SHA3 (Keccak FIPS 202) cryptographic hashing algorithm.
5
+ **SHA3 for Ruby** is a XKCP based native (C) binding to SHA3 (FIPS 202) cryptographic hashing algorithm.
6
6
 
7
7
  - [Home](https://github.com/johanns/sha3#readme)
8
8
  - [Issues](https://github.com/johanns/sha3/issues)
9
9
  - [Documentation](http://rubydoc.info/gems/sha3/frames)
10
+ - [XKCP - eXtended Keccak Code Package](https://github.com/XKCP/XKCP)
10
11
 
11
- ## Warnings
12
+ ## Warning
12
13
 
13
14
  - Please do NOT use SHA3 to hash passwords -- use a slow hashing function instead (e.g.: `pbkdf2`, `argon2`, `bcrypt` or `scrypt`)
14
15
  - Version 1.0 introduces new API and is incompatible with previous versions (0.x).
@@ -92,9 +93,9 @@ s = SHA3::Digest.file("tests.sh")
92
93
  # => #<SHA3::Digest: a9801db49389339...>
93
94
  ```
94
95
 
95
- ## Development
96
+ ### Development Dependencies
96
97
 
97
- * Native build tools (e.g., GCC, Minigw, etc.)
98
+ * Native build tools (e.g., Clang/LLVM, GCC, Minigw, etc.)
98
99
  * Gems: rubygems-tasks, rake, rspec, yard
99
100
 
100
101
  ### Testing
@@ -109,9 +110,11 @@ Only a small subset of test vectors are included in the source repository; howev
109
110
 
110
111
  Supported Ruby versions:
111
112
 
112
- - MRI Ruby 2.4 - 3.1
113
+ - MRI Ruby 2.6 - 3.1
113
114
 
115
+ ## Credits
114
116
 
117
+ XKCP by Keccak team: [https://keccak.team/index.html]()
115
118
 
116
119
  ## Copyright
117
120
 
data/certs/johanns.pem CHANGED
@@ -1,24 +1,25 @@
1
1
  -----BEGIN CERTIFICATE-----
2
- MIIEFDCCAnygAwIBAgIBATANBgkqhkiG9w0BAQsFADAaMRgwFgYDVQQDDA9pby9E
3
- Qz1qc2cvREM9aW8wHhcNMjIwMzA5MDcyODEwWhcNMjMwMzA5MDcyODEwWjAaMRgw
4
- FgYDVQQDDA9pby9EQz1qc2cvREM9aW8wggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAw
5
- ggGKAoIBgQCuPwkDs4uoBV6pQhKp3uA58LkDPRR7VoI4QryuH7PbIwuO3xbuyR6T
6
- xEqwcb9GgVnMxiFyXeVkWiriuGXqZ26Pp5zH/xxQNgPGCIyXuXeJMpASkWjJ9J6h
7
- +jOdaG5jdL6zRcw/5XaJSQTLGO32BNOIezUZCF9JtMoEBQYHRWFjoD5BryjYnU3f
8
- SstXg9nD8KOyk5Vzqk/wvJs5X+b2jcZL0KmSP1UgaCakfTCKf8LA9I5zAbvv2M9n
9
- F0JQfKw4AUlmykzmOMEsITVxtM8lIVxm99KAS+lFn19xEjOs7nr3GktyLJxav8qI
10
- 3yXW/q5wUbOFrj/e1tk6r/W3bM6TQzGFBsBJgXEoFFZ+OQdEW8PXU0H5CIg59SxB
11
- lXwSC1rKlxSeGNcKDXeIN1AU3pqv/PJj2SKg2+uQ9Staya0Mtek/caPDVfIy8AgH
12
- ulTeTVN7m5SdVjVBl3rIehH/j0R/JF7ygsMkJEQAxrwPxqmazXkPAGLNNIqppaxi
13
- VyHhxSnGefUCAwEAAaNlMGMwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
14
- BBYEFBZFuuJYra4ctzMvtfzlTq3pW4idMBQGA1UdEQQNMAuBCWlvQGpzZy5pbzAU
15
- BgNVHRIEDTALgQlpb0Bqc2cuaW8wDQYJKoZIhvcNAQEL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Gk9eL8z
24
25
  -----END CERTIFICATE-----
data/ext/sha3/config.h ADDED
@@ -0,0 +1,26 @@
1
+ /* File generated by ToTargetConfigFile.xsl */
2
+
3
+ #define XKCP_has_Sponge_Keccak
4
+ #define XKCP_has_FIPS202
5
+ #define XKCP_has_KeccakP1600
6
+
7
+ // #define XKCP_has_SP800_185
8
+ // #define XKCP_has_Duplex_Keccak
9
+ // #define XKCP_has_PRG_Keccak
10
+ // #define XKCP_has_Ketje
11
+ // #define XKCP_has_Keyak
12
+ // #define XKCP_has_KangarooTwelve
13
+ // #define XKCP_has_Kravatte
14
+ // #define XKCP_has_Xoofff
15
+ // #define XKCP_has_Xoodyak
16
+ // #define XKCP_has_KeccakP200
17
+ // #define XKCP_has_KeccakP400
18
+ // #define XKCP_has_KeccakP800
19
+ // #define XKCP_has_KeccakP1600
20
+ // #define XKCP_has_KeccakP1600times2
21
+ // #define XKCP_has_KeccakP1600times4
22
+ // #define XKCP_has_KeccakP1600times8
23
+ // #define XKCP_has_Xoodoo
24
+ // #define XKCP_has_Xoodootimes4
25
+ // #define XKCP_has_Xoodootimes8
26
+ // #define XKCP_has_Xoodootimes16
data/ext/sha3/digest.c CHANGED
@@ -64,7 +64,7 @@ static VALUE c_digest_update(VALUE, VALUE);
64
64
 
65
65
  HashReturn c_keccak_hash_initialize(MDX *mdx)
66
66
  {
67
- HashReturn r = FAIL;
67
+ HashReturn r = KECCAK_FAIL;
68
68
 
69
69
  switch (mdx->hashbitlen)
70
70
  {
@@ -103,7 +103,7 @@ static VALUE c_digest_init(int argc, VALUE *argv, VALUE self)
103
103
  mdx->hashbitlen = 256;
104
104
  }
105
105
 
106
- if (c_keccak_hash_initialize(mdx) != SUCCESS)
106
+ if (c_keccak_hash_initialize(mdx) != KECCAK_SUCCESS)
107
107
  {
108
108
  rb_raise(eSHA3DigestError, "failed to initialize algorithm state");
109
109
  }
@@ -120,14 +120,14 @@ static VALUE c_digest_init(int argc, VALUE *argv, VALUE self)
120
120
  static VALUE c_digest_update(VALUE self, VALUE data)
121
121
  {
122
122
  MDX *mdx;
123
- DataLength dlen;
123
+ BitLength dlen;
124
124
 
125
125
  StringValue(data);
126
126
  GETMDX(self, mdx);
127
127
 
128
128
  dlen = (RSTRING_LEN(data) * 8);
129
129
 
130
- if (Keccak_HashUpdate(mdx->state, (BitSequence *)RSTRING_PTR(data), dlen) != SUCCESS)
130
+ if (Keccak_HashUpdate(mdx->state, (BitSequence *)RSTRING_PTR(data), dlen) != KECCAK_SUCCESS)
131
131
  {
132
132
  rb_raise(eSHA3DigestError, "failed to update hash data");
133
133
  }
@@ -144,7 +144,7 @@ static VALUE c_digest_reset(VALUE self)
144
144
 
145
145
  memset(mdx->state, 0, sizeof(Keccak_HashInstance));
146
146
 
147
- if (c_keccak_hash_initialize(mdx) != SUCCESS)
147
+ if (c_keccak_hash_initialize(mdx) != KECCAK_SUCCESS)
148
148
  {
149
149
  rb_raise(eSHA3DigestError, "failed to reset internal state");
150
150
  }
@@ -236,7 +236,7 @@ static VALUE c_digest_finish(int argc, VALUE *argv, VALUE self)
236
236
  rb_str_resize(str, mdx->hashbitlen / 8);
237
237
  }
238
238
 
239
- if (Keccak_HashFinal(mdx->state, (BitSequence *)RSTRING_PTR(str)) != SUCCESS)
239
+ if (Keccak_HashFinal(mdx->state, (BitSequence *)RSTRING_PTR(str)) != KECCAK_SUCCESS)
240
240
  {
241
241
  rb_raise(eSHA3DigestError, "failed to finalize digest");
242
242
  }
data/ext/sha3/extconf.rb CHANGED
@@ -1,20 +1,50 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkmf'
2
4
  require 'rbconfig'
3
5
 
4
- target_cpu = RbConfig::CONFIG['target_cpu']
6
+ # Maintaining XKCP lib directory structure to hopefully simplify
7
+ # future upgrades.
8
+
9
+ keccak_base_files = [
10
+ 'lib/high/Keccak/KeccakSponge.c',
11
+ 'lib/high/Keccak/FIPS202/KeccakHash.c'
12
+ ]
13
+
14
+ if 1.size == 8
15
+ Logging.message "=== Using 64-bit reference ===\n"
5
16
 
6
- if 1.size == 8 and target_cpu =~ /i686|x86_64/
7
- Logging.message "=== Using optimized (64-bit) ===\n"
8
- FileUtils.cp Dir["#{$srcdir}/Optimized64/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
17
+ keccak_base_files << 'lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c'
9
18
  else
10
- Logging.message "=== Using reference ===\n"
11
- FileUtils.cp Dir["#{$srcdir}/Reference/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
19
+ Logging.message "=== Using 32-bit reference ===\n"
20
+
21
+ keccak_base_files << 'lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c'
12
22
  end
13
23
 
24
+ FileUtils.cp keccak_base_files.map { |f| "#{$srcdir}/#{f}" }, $srcdir
25
+
26
+ extension_name = 'sha3_n'
27
+ dir_config(extension_name)
28
+
29
+ $INCFLAGS << [
30
+ ' -I$(src) ',
31
+ ' -I$(srcdir)lib/ ',
32
+ ' -I$(srcdir)/lib/common ',
33
+ ' -I$(srcdir)/lib/high/Keccak ',
34
+ ' -I$(srcdir)/lib/high/Keccak/FIPS202 ',
35
+ ' -I$(srcdir)/lib/low/KeccakP-1600/common ',
36
+ ' -I$(srcdir)/lib/low/KeccakP-1600/ref-32bits ',
37
+ ' -I$(srcdir)/lib/low/KeccakP-1600/ref-64bits '
38
+ ].join
39
+
40
+ $CFLAGS << ' -fomit-frame-pointer -O3 -g0 -fms-extensions '
41
+ $CFLAGS << ' -march=native ' if enable_config('march-tune-native', false)
42
+
14
43
  find_header('sha3.h')
15
44
  find_header('digest.h')
45
+ find_header('align.h')
46
+ find_header('brg_endian.h')
47
+ find_header('KeccakSponge.h')
48
+ find_header('KeccakHash.h')
16
49
 
17
- $CFLAGS += ' -fomit-frame-pointer -O3 -g0 -fms-extensions '
18
- $CFLAGS += ' -march=native ' if enable_config('march-tune-native', false)
19
-
20
- create_makefile 'sha3_n'
50
+ create_makefile extension_name
@@ -0,0 +1,33 @@
1
+ /*
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
4
+
5
+ Implementation by Gilles Van Assche and Ronny Van Keer, hereby denoted as "the implementer".
6
+
7
+ For more information, feedback or questions, please refer to the Keccak Team website:
8
+ https://keccak.team/
9
+
10
+ To the extent possible under law, the implementer has waived all copyright
11
+ and related or neighboring rights to the source code in this file.
12
+ http://creativecommons.org/publicdomain/zero/1.0/
13
+ */
14
+
15
+ #ifndef _align_h_
16
+ #define _align_h_
17
+
18
+ /* on Mac OS-X and possibly others, ALIGN(x) is defined in param.h, and -Werror chokes on the redef. */
19
+ #ifdef ALIGN
20
+ #undef ALIGN
21
+ #endif
22
+
23
+ #if defined(__GNUC__)
24
+ #define ALIGN(x) __attribute__ ((aligned(x)))
25
+ #elif defined(_MSC_VER)
26
+ #define ALIGN(x) __declspec(align(x))
27
+ #elif defined(__ARMCC_VERSION)
28
+ #define ALIGN(x) __align(x)
29
+ #else
30
+ #define ALIGN(x)
31
+ #endif
32
+
33
+ #endif
@@ -114,13 +114,14 @@
114
114
  defined( __VMS ) || defined( _M_X64 )
115
115
  # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
116
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 )
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
+ defined( __s390__ ) || defined( __s390x__ ) || defined( __zarch__ )
124
125
  # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
125
126
 
126
127
  #elif defined(__arm__)
@@ -1,12 +1,13 @@
1
1
  /*
2
- Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
- Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
- denoted as "the implementer".
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
5
4
 
6
- For more information, feedback or questions, please refer to our websites:
7
- http://keccak.noekeon.org/
8
- http://keyak.noekeon.org/
9
- http://ketje.noekeon.org/
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by the designers, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
10
11
 
11
12
  To the extent possible under law, the implementer has waived all copyright
12
13
  and related or neighboring rights to the source code in this file.
@@ -23,35 +24,35 @@ HashReturn Keccak_HashInitialize(Keccak_HashInstance *instance, unsigned int rat
23
24
  HashReturn result;
24
25
 
25
26
  if (delimitedSuffix == 0)
26
- return FAIL;
27
- result = (HashReturn)Keccak_SpongeInitialize(&instance->sponge, rate, capacity);
28
- if (result != SUCCESS)
27
+ return KECCAK_FAIL;
28
+ result = (HashReturn)KeccakWidth1600_SpongeInitialize(&instance->sponge, rate, capacity);
29
+ if (result != KECCAK_SUCCESS)
29
30
  return result;
30
31
  instance->fixedOutputLength = hashbitlen;
31
32
  instance->delimitedSuffix = delimitedSuffix;
32
- return SUCCESS;
33
+ return KECCAK_SUCCESS;
33
34
  }
34
35
 
35
36
  /* ---------------------------------------------------------------- */
36
37
 
37
- HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *data, DataLength databitlen)
38
+ HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *data, BitLength databitlen)
38
39
  {
39
40
  if ((databitlen % 8) == 0)
40
- return (HashReturn)Keccak_SpongeAbsorb(&instance->sponge, data, databitlen/8);
41
+ return (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, data, databitlen/8);
41
42
  else {
42
- HashReturn ret = (HashReturn)Keccak_SpongeAbsorb(&instance->sponge, data, databitlen/8);
43
- if (ret == SUCCESS) {
44
- // The last partial byte is assumed to be aligned on the least significant bits
43
+ HashReturn ret = (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, data, databitlen/8);
44
+ if (ret == KECCAK_SUCCESS) {
45
+ /* The last partial byte is assumed to be aligned on the least significant bits */
45
46
  unsigned char lastByte = data[databitlen/8];
46
- // Concatenate the last few bits provided here with those of the suffix
47
- unsigned short delimitedLastBytes = (unsigned short)lastByte | ((unsigned short)instance->delimitedSuffix << (databitlen % 8));
47
+ /* Concatenate the last few bits provided here with those of the suffix */
48
+ unsigned short delimitedLastBytes = (unsigned short)((unsigned short)(lastByte & ((1 << (databitlen % 8)) - 1)) | ((unsigned short)instance->delimitedSuffix << (databitlen % 8)));
48
49
  if ((delimitedLastBytes & 0xFF00) == 0x0000) {
49
50
  instance->delimitedSuffix = delimitedLastBytes & 0xFF;
50
51
  }
51
52
  else {
52
53
  unsigned char oneByte[1];
53
54
  oneByte[0] = delimitedLastBytes & 0xFF;
54
- ret = (HashReturn)Keccak_SpongeAbsorb(&instance->sponge, oneByte, 1);
55
+ ret = (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, oneByte, 1);
55
56
  instance->delimitedSuffix = (delimitedLastBytes >> 8) & 0xFF;
56
57
  }
57
58
  }
@@ -63,18 +64,18 @@ HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *d
63
64
 
64
65
  HashReturn Keccak_HashFinal(Keccak_HashInstance *instance, BitSequence *hashval)
65
66
  {
66
- HashReturn ret = (HashReturn)Keccak_SpongeAbsorbLastFewBits(&instance->sponge, instance->delimitedSuffix);
67
- if (ret == SUCCESS)
68
- return (HashReturn)Keccak_SpongeSqueeze(&instance->sponge, hashval, instance->fixedOutputLength/8);
67
+ HashReturn ret = (HashReturn)KeccakWidth1600_SpongeAbsorbLastFewBits(&instance->sponge, instance->delimitedSuffix);
68
+ if (ret == KECCAK_SUCCESS)
69
+ return (HashReturn)KeccakWidth1600_SpongeSqueeze(&instance->sponge, hashval, instance->fixedOutputLength/8);
69
70
  else
70
71
  return ret;
71
72
  }
72
73
 
73
74
  /* ---------------------------------------------------------------- */
74
75
 
75
- HashReturn Keccak_HashSqueeze(Keccak_HashInstance *instance, BitSequence *data, DataLength databitlen)
76
+ HashReturn Keccak_HashSqueeze(Keccak_HashInstance *instance, BitSequence *data, BitLength databitlen)
76
77
  {
77
78
  if ((databitlen % 8) != 0)
78
- return FAIL;
79
- return (HashReturn)Keccak_SpongeSqueeze(&instance->sponge, data, databitlen/8);
79
+ return KECCAK_FAIL;
80
+ return (HashReturn)KeccakWidth1600_SpongeSqueeze(&instance->sponge, data, databitlen/8);
80
81
  }
@@ -1,12 +1,13 @@
1
1
  /*
2
- Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
- Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
- denoted as "the implementer".
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
5
4
 
6
- For more information, feedback or questions, please refer to our websites:
7
- http://keccak.noekeon.org/
8
- http://keyak.noekeon.org/
9
- http://ketje.noekeon.org/
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by the designers, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
10
11
 
11
12
  To the extent possible under law, the implementer has waived all copyright
12
13
  and related or neighboring rights to the source code in this file.
@@ -16,15 +17,24 @@ http://creativecommons.org/publicdomain/zero/1.0/
16
17
  #ifndef _KeccakHashInterface_h_
17
18
  #define _KeccakHashInterface_h_
18
19
 
19
- #include "KeccakSponge.h"
20
+ #include "config.h"
21
+ #ifdef XKCP_has_KeccakP1600
22
+
23
+ #include <stdint.h>
20
24
  #include <string.h>
25
+ #include "KeccakSponge.h"
21
26
 
22
- typedef unsigned char BitSequence;
23
- typedef size_t DataLength;
24
- typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;
27
+ #ifndef _Keccak_BitTypes_
28
+ #define _Keccak_BitTypes_
29
+ typedef uint8_t BitSequence;
30
+
31
+ typedef size_t BitLength;
32
+ #endif
33
+
34
+ typedef enum { KECCAK_SUCCESS = 0, KECCAK_FAIL = 1, KECCAK_BAD_HASHLEN = 2 } HashReturn;
25
35
 
26
36
  typedef struct {
27
- Keccak_SpongeInstance sponge;
37
+ KeccakWidth1600_SpongeInstance sponge;
28
38
  unsigned int fixedOutputLength;
29
39
  unsigned char delimitedSuffix;
30
40
  } Keccak_HashInstance;
@@ -42,7 +52,7 @@ typedef struct {
42
52
  * formatted like the @a delimitedData parameter of
43
53
  * the Keccak_SpongeAbsorbLastFewBits() function.
44
54
  * @pre One must have r+c=1600 and the rate a multiple of 8 bits in this implementation.
45
- * @return SUCCESS if successful, FAIL otherwise.
55
+ * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
46
56
  */
47
57
  HashReturn Keccak_HashInitialize(Keccak_HashInstance *hashInstance, unsigned int rate, unsigned int capacity, unsigned int hashbitlen, unsigned char delimitedSuffix);
48
58
 
@@ -76,11 +86,13 @@ HashReturn Keccak_HashInitialize(Keccak_HashInstance *hashInstance, unsigned int
76
86
  * @param data Pointer to the input data.
77
87
  * When @a databitLen is not a multiple of 8, the last bits of data must be
78
88
  * in the least significant bits of the last byte (little-endian convention).
89
+ * In this case, the (8 - @a databitLen mod 8) most significant bits
90
+ * of the last byte are ignored.
79
91
  * @param databitLen The number of input bits provided in the input data.
80
92
  * @pre In the previous call to Keccak_HashUpdate(), databitlen was a multiple of 8.
81
- * @return SUCCESS if successful, FAIL otherwise.
93
+ * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
82
94
  */
83
- HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequence *data, DataLength databitlen);
95
+ HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequence *data, BitLength databitlen);
84
96
 
85
97
  /**
86
98
  * Function to call after all input blocks have been input and to get
@@ -90,9 +102,8 @@ HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequenc
90
102
  * output bits is equal to @a hashbitlen.
91
103
  * If @a hashbitlen was 0 in the call to Keccak_HashInitialize(), the output bits
92
104
  * must be extracted using the Keccak_HashSqueeze() function.
93
- * @param state Pointer to the state of the sponge function initialized by Init().
94
105
  * @param hashval Pointer to the buffer where to store the output data.
95
- * @return SUCCESS if successful, FAIL otherwise.
106
+ * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
96
107
  */
97
108
  HashReturn Keccak_HashFinal(Keccak_HashInstance *hashInstance, BitSequence *hashval);
98
109
 
@@ -103,8 +114,12 @@ HashReturn Keccak_HashFinal(Keccak_HashInstance *hashInstance, BitSequence *hash
103
114
  * @param databitlen The number of output bits desired (must be a multiple of 8).
104
115
  * @pre Keccak_HashFinal() must have been already called.
105
116
  * @pre @a databitlen is a multiple of 8.
106
- * @return SUCCESS if successful, FAIL otherwise.
117
+ * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
107
118
  */
108
- HashReturn Keccak_HashSqueeze(Keccak_HashInstance *hashInstance, BitSequence *data, DataLength databitlen);
119
+ HashReturn Keccak_HashSqueeze(Keccak_HashInstance *hashInstance, BitSequence *data, BitLength databitlen);
120
+
121
+ #else
122
+ #error This requires an implementation of Keccak-p[1600]
123
+ #endif
109
124
 
110
125
  #endif
@@ -0,0 +1,111 @@
1
+ /*
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
4
+
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by the designers, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
11
+
12
+ To the extent possible under law, the implementer has waived all copyright
13
+ and related or neighboring rights to the source code in this file.
14
+ http://creativecommons.org/publicdomain/zero/1.0/
15
+ */
16
+
17
+ #include "KeccakSponge.h"
18
+
19
+ #ifdef KeccakReference
20
+ #include "displayIntermediateValues.h"
21
+ #endif
22
+
23
+ #ifdef XKCP_has_KeccakP200
24
+ #include "KeccakP-200-SnP.h"
25
+
26
+ #define prefix KeccakWidth200
27
+ #define SnP KeccakP200
28
+ #define SnP_width 200
29
+ #define SnP_Permute KeccakP200_Permute_18rounds
30
+ #if defined(KeccakF200_FastLoop_supported)
31
+ #define SnP_FastLoop_Absorb KeccakF200_FastLoop_Absorb
32
+ #endif
33
+ #include "KeccakSponge.inc"
34
+ #undef prefix
35
+ #undef SnP
36
+ #undef SnP_width
37
+ #undef SnP_Permute
38
+ #undef SnP_FastLoop_Absorb
39
+ #endif
40
+
41
+ #ifdef XKCP_has_KeccakP400
42
+ #include "KeccakP-400-SnP.h"
43
+
44
+ #define prefix KeccakWidth400
45
+ #define SnP KeccakP400
46
+ #define SnP_width 400
47
+ #define SnP_Permute KeccakP400_Permute_20rounds
48
+ #if defined(KeccakF400_FastLoop_supported)
49
+ #define SnP_FastLoop_Absorb KeccakF400_FastLoop_Absorb
50
+ #endif
51
+ #include "KeccakSponge.inc"
52
+ #undef prefix
53
+ #undef SnP
54
+ #undef SnP_width
55
+ #undef SnP_Permute
56
+ #undef SnP_FastLoop_Absorb
57
+ #endif
58
+
59
+ #ifdef XKCP_has_KeccakP800
60
+ #include "KeccakP-800-SnP.h"
61
+
62
+ #define prefix KeccakWidth800
63
+ #define SnP KeccakP800
64
+ #define SnP_width 800
65
+ #define SnP_Permute KeccakP800_Permute_22rounds
66
+ #if defined(KeccakF800_FastLoop_supported)
67
+ #define SnP_FastLoop_Absorb KeccakF800_FastLoop_Absorb
68
+ #endif
69
+ #include "KeccakSponge.inc"
70
+ #undef prefix
71
+ #undef SnP
72
+ #undef SnP_width
73
+ #undef SnP_Permute
74
+ #undef SnP_FastLoop_Absorb
75
+ #endif
76
+
77
+ #ifdef XKCP_has_KeccakP1600
78
+ #include "KeccakP-1600-SnP.h"
79
+
80
+ #define prefix KeccakWidth1600
81
+ #define SnP KeccakP1600
82
+ #define SnP_width 1600
83
+ #define SnP_Permute KeccakP1600_Permute_24rounds
84
+ #if defined(KeccakF1600_FastLoop_supported)
85
+ #define SnP_FastLoop_Absorb KeccakF1600_FastLoop_Absorb
86
+ #endif
87
+ #include "KeccakSponge.inc"
88
+ #undef prefix
89
+ #undef SnP
90
+ #undef SnP_width
91
+ #undef SnP_Permute
92
+ #undef SnP_FastLoop_Absorb
93
+ #endif
94
+
95
+ #ifdef XKCP_has_KeccakP1600
96
+ #include "KeccakP-1600-SnP.h"
97
+
98
+ #define prefix KeccakWidth1600_12rounds
99
+ #define SnP KeccakP1600
100
+ #define SnP_width 1600
101
+ #define SnP_Permute KeccakP1600_Permute_12rounds
102
+ #if defined(KeccakP1600_12rounds_FastLoop_supported)
103
+ #define SnP_FastLoop_Absorb KeccakP1600_12rounds_FastLoop_Absorb
104
+ #endif
105
+ #include "KeccakSponge.inc"
106
+ #undef prefix
107
+ #undef SnP
108
+ #undef SnP_width
109
+ #undef SnP_Permute
110
+ #undef SnP_FastLoop_Absorb
111
+ #endif