sha3 1.0.3 → 1.0.5

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 (56) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.rspec +3 -1
  4. data/.rubocop.yml +6 -0
  5. data/Gemfile +3 -1
  6. data/README.md +10 -7
  7. data/Rakefile +10 -37
  8. data/certs/johanns.pem +25 -0
  9. data/ext/sha3/config.h +26 -0
  10. data/ext/sha3/digest.c +6 -6
  11. data/ext/sha3/extconf.rb +40 -16
  12. data/ext/sha3/lib/common/align.h +33 -0
  13. data/ext/sha3/{brg_endian.h → lib/common/brg_endian.h} +8 -7
  14. data/ext/sha3/{KeccakHash.c → lib/high/Keccak/FIPS202/KeccakHash.c} +26 -25
  15. data/ext/sha3/{KeccakHash.h → lib/high/Keccak/FIPS202/KeccakHash.h} +34 -19
  16. data/ext/sha3/lib/high/Keccak/KeccakSponge.c +111 -0
  17. data/ext/sha3/lib/high/Keccak/KeccakSponge.h +76 -0
  18. data/ext/sha3/lib/high/Keccak/KeccakSponge.inc +316 -0
  19. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-64.macros +748 -0
  20. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-unrolling.macros +305 -0
  21. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-SnP.h +44 -0
  22. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference.h +23 -0
  23. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c +625 -0
  24. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-SnP.h +44 -0
  25. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c +444 -0
  26. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.h +23 -0
  27. data/lib/sha3/doc.rb +4 -4
  28. data/lib/sha3/version.rb +4 -4
  29. data/lib/sha3.rb +15 -13
  30. data/sha3.gemspec +53 -22
  31. data/tests.sh +8 -7
  32. data.tar.gz.sig +0 -0
  33. metadata +100 -51
  34. metadata.gz.sig +0 -0
  35. data/.gitignore +0 -234
  36. data/.travis.yml +0 -21
  37. data/Gemfile.ci +0 -8
  38. data/ext/sha3/KeccakF-1600-interface.h +0 -40
  39. data/ext/sha3/KeccakSponge.c +0 -192
  40. data/ext/sha3/KeccakSponge.h +0 -113
  41. data/ext/sha3/Optimized64/KeccakF-1600-64.macros +0 -2199
  42. data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +0 -3
  43. data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +0 -508
  44. data/ext/sha3/Optimized64/KeccakF-1600-unrolling.macros +0 -126
  45. data/ext/sha3/Optimized64/SnP-interface.h +0 -47
  46. data/ext/sha3/Reference/KeccakF-1600-reference.c +0 -311
  47. data/ext/sha3/Reference/KeccakF-reference.h +0 -26
  48. data/ext/sha3/Reference/SnP-FBWL-default.c +0 -96
  49. data/ext/sha3/Reference/SnP-FBWL-default.h +0 -26
  50. data/ext/sha3/Reference/SnP-interface.h +0 -42
  51. data/ext/sha3/Reference/displayIntermediateValues.c +0 -158
  52. data/ext/sha3/Reference/displayIntermediateValues.h +0 -34
  53. data/ext/sha3/SnP-Relaned.h +0 -249
  54. data/spec/generate_tests.rb +0 -49
  55. data/spec/sha3_core_spec.rb +0 -148
  56. data/spec/spec_helper.rb +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2abc34b466bf79618721bac7202c2e4c2617175a74843ae4e059e698da1bb813
4
- data.tar.gz: c805b46df9491e191022164606330d1b4d8bb709470399558da0d8272041e210
3
+ metadata.gz: 16edba407e859ee7afacc8a405666ec66cab0052cd7788e79089bd3f0d295844
4
+ data.tar.gz: 5552886038bbec790d086599cbbe6ad09a0c3561b58a9a2dfff2b9c9834b7154
5
5
  SHA512:
6
- metadata.gz: 61f430b210275ae079d8ce0baf13841b02fe817ff4ee80215eb99578920d1bff3131b453ec7bf80426b11607302404705cc55a017a0e19bded58485755f9d492
7
- data.tar.gz: 475662e0c8a0d74b9222152a9b98bfed02bcb385551f212cac5cdc23e5e9a2eccc99ffb1c7bb101b1ca50fb55c3614d4e17d63b267a1ef4864339998df90f2c7
6
+ metadata.gz: a8e36ae984df177684d4b1a70689a80824e9d9ee70fd07812eaa50c8f0cd90a90f292dca435764842b737fb268084d09fa5f47450bdc9cf93316253c70484945
7
+ data.tar.gz: a643ded4e2f9828d732f428eba1757c6cc383797f32b52ba7427dae8234ac9bc0e158712ccf64d7be77f2538b80904efaf26a0e455ce9515f9a76679929054c0
checksums.yaml.gz.sig ADDED
Binary file
data/.rspec CHANGED
@@ -1 +1,3 @@
1
- --colour --format documentation
1
+ --format documentation
2
+ --color
3
+ --require ./spec/spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6
4
+
5
+ Layout/LineLength:
6
+ Max: 120
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
- source 'http://rubygems.org'
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  gemspec
data/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # sha3
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/sha3.svg)](https://badge.fury.io/rb/sha3) [![CI](https://secure.travis-ci.org/johanns/sha3.png)](https://secure.travis-ci.org/johanns/sha3) [![CodeClimate](https://codeclimate.com/github/johanns/sha3.png)](https://codeclimate.com/github/johanns/sha3)
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,12 +110,14 @@ 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.0
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
 
118
- Copyright (c) 2012 - 2020 Johanns Gregorian (https://github.com/johanns)
121
+ Copyright (c) 2012 - 2022 Johanns Gregorian (https://github.com/johanns)
119
122
 
120
123
  **See LICENSE.txt for details.**
data/Rakefile CHANGED
@@ -1,47 +1,18 @@
1
- require 'rubygems'
2
- require 'rake'
1
+ # frozen_string_literal: true
3
2
 
4
- begin
5
- gem 'rubygems-tasks'
6
- require 'rubygems/tasks'
7
-
8
- Gem::Tasks.new
9
- rescue LoadError => e
10
- warn e.message
11
- warn 'Run `gem install rubygems-tasks` to install Gem::Tasks.'
12
- end
13
-
14
- begin
15
- gem 'rspec'
16
- require 'rspec/core/rake_task'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
17
5
 
18
- RSpec::Core::RakeTask.new
19
- rescue LoadError
20
- task :spec do
21
- abort 'Please run `gem install rspec` to install RSpec.'
22
- end
23
- end
6
+ RSpec::Core::RakeTask.new(:spec)
24
7
 
25
- task test: :spec
26
- task default: %i[compile spec]
8
+ require 'rubocop/rake_task'
27
9
 
28
- begin
29
- gem 'yard'
30
- require 'yard'
10
+ RuboCop::RakeTask.new
31
11
 
32
- YARD::Rake::YardocTask.new
33
- rescue LoadError
34
- task :yard do
35
- abort 'Please run `gem install yard` to install YARD.'
36
- end
37
- end
38
- task doc: :yard
12
+ require 'rake/extensiontask'
39
13
 
40
14
  begin
41
- gem 'rake-compiler'
42
- require 'rake/extensiontask'
43
-
44
- Rake::ExtensionTask.new do |ext|
15
+ Rake::ExtensionTask.new :compile do |ext|
45
16
  ext.name = 'sha3_n'
46
17
  ext.ext_dir = 'ext/sha3'
47
18
  ext.tmp_dir = 'tmp'
@@ -52,3 +23,5 @@ rescue LoadError
52
23
  abort 'Please run `gem install rake-compiler` to install Rake-Compiler.'
53
24
  end
54
25
  end
26
+
27
+ task default: %i[compile spec]
data/certs/johanns.pem ADDED
@@ -0,0 +1,25 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIETDCCArSgAwIBAgIBATANBgkqhkiG9w0BAQsFADA2MQswCQYDVQQDDAJpbzET
3
+ MBEGCgmSJomT8ixkARkWA2pzZzESMBAGCgmSJomT8ixkARkWAmlvMB4XDTIyMTAy
4
+ NDA2MzcxMVoXDTIzMTAyNDA2MzcxMVowNjELMAkGA1UEAwwCaW8xEzARBgoJkiaJ
5
+ k/IsZAEZFgNqc2cxEjAQBgoJkiaJk/IsZAEZFgJpbzCCAaIwDQYJKoZIhvcNAQEB
6
+ BQADggGPADCCAYoCggGBALQTl5BGmtYGvljWwOTxe2Uul7RoBcSOwFUh03qUvHJf
7
+ 1LmWr6y1j97ogl/VffBXpbtTU4adZa+qTxfMs7GpfKDjikSIieZ7SrMNB68zCH0e
8
+ undHx+bMutN7919rviHfGyaXlQK4SsuWUl4AOlgT69VPQp6dOBYY9T78jbr/ZcG6
9
+ +mDlRpNfPVg5i67euvpR5dO9SpO1HNoHmzx5L4wYNr9QykIft1oA+Ne+SAF66ykn
10
+ agugF/R0Q7s+5Bpt3gr6SF2CvKsNJ2IS5TJO9unhLZ+h8FO7dcQw1EuJ31uHQKiD
11
+ rWUv2tnKCvLkHg0S69VeQtQv58dklJ3iFJcSen4VRtC7r5JMEd1VrdpXU4JQ54gY
12
+ tWrqWmazF9SOErbgvDlJgmlkMMkX6aoZ21/f1s6Z2myzP3KkRBjCf51BrgHTXTJD
13
+ 28ANp21H0o0HhrpVFJVDjToXRLczsw0O9lnL+khzkeZoc+YTZuvJDLKokavXhb4a
14
+ vESgRttXjyN5jBKY7yFhKQIDAQABo2UwYzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIE
15
+ sDAdBgNVHQ4EFgQUKmyX3Q2uwTPM9S5+K/5kg7qe3ugwFAYDVR0RBA0wC4EJaW9A
16
+ anNnLmlvMBQGA1UdEgQNMAuBCWlvQGpzZy5pbzANBgkqhkiG9w0BAQsFAAOCAYEA
17
+ TexWHx3uLVObT+ylm3OE8Iue3cHdrDVE3zSjo8VlU3u1WBznH9MdoiPB7wux61Zx
18
+ jXUzBUaT7y7JnDaVGnECkpHXhfvPOYHBgkqEws6i79lAk/Va2U7EVPj0moM9d4Hv
19
+ 12V8YVM1Z9QnfwBVo7YGb5o7W8lr01jj1gT+Qcw+kln7M3Y9RB+jQ4DwySHVIEMc
20
+ Ow7//MF7bhCz6T5uAOXlGe88wTHKW+fO1AmW5MIQZUojR5Ioxm80v2YdW/JnQZ1l
21
+ 3VFpCutilnhDuzSw3DhgxReX7AK42aXFFclIzi11twW4KUPdt1KIvaoL/DgbZivl
22
+ PVG86dx4gfax2Mc2PiM+d1DiSllh+chh4dqRkIyhj0S4V7McQHkwW1ZBJ3kDf5rt
23
+ 1O/udKquzj7egb6uceqzBB40W/1/CsNkGNpNZ8Bk8lrTmKw+3bJpj+nKWxovmF2p
24
+ VhzZDQf2jkcjBXKNA9Z5ku7g0TCR1/Y1V3ODgkTLqhw+kQZmlbQEVzcwxGk9eL8z
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,26 +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
+ ]
5
13
 
6
- if 1.size == 4 and target_cpu =~ /i386|x86_32/ # x86 32bit optimized code
7
- Logging::message "=== Using reference ===\n"
8
- FileUtils.cp Dir["#{$srcdir}/Reference/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
9
- elsif 1.size == 8 and target_cpu =~ /i686|x86_64/
10
- Logging::message "=== Using optimized (64-bit) ===\n"
11
- FileUtils.cp Dir["#{$srcdir}/Optimized64/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
14
+ if 1.size == 8
15
+ Logging.message "=== Using 64-bit reference ===\n"
16
+
17
+ keccak_base_files << 'lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c'
12
18
  else
13
- Logging::message "=== Using reference ===\n"
14
- 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'
15
22
  end
16
23
 
17
- find_header("sha3.h")
18
- find_header("digest.h")
24
+ FileUtils.cp keccak_base_files.map { |f| "#{$srcdir}/#{f}" }, $srcdir
19
25
 
20
- $CFLAGS = ' -fomit-frame-pointer -O3 -g0'
26
+ extension_name = 'sha3_n'
27
+ dir_config(extension_name)
21
28
 
22
- if enable_config('march-tune-native', false)
23
- $CFLAGS += ' -march=native'
24
- end
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
+
43
+ find_header('sha3.h')
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')
25
49
 
26
- 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