keccak 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYRIGHT +14 -0
- data/LICENSE +202 -0
- data/Makefile +18 -0
- data/README.md +75 -0
- data/ext/digest/KeccakF-1600-int-set.h +6 -0
- data/ext/digest/KeccakF-1600-interface.h +46 -0
- data/ext/digest/KeccakF-1600-reference.c +300 -0
- data/ext/digest/KeccakNISTInterface.c +81 -0
- data/ext/digest/KeccakNISTInterface.h +70 -0
- data/ext/digest/KeccakSponge.c +266 -0
- data/ext/digest/KeccakSponge.h +76 -0
- data/ext/digest/brg_endian.h +142 -0
- data/ext/digest/displayIntermediateValues.c +117 -0
- data/ext/digest/displayIntermediateValues.h +29 -0
- data/ext/digest/extconf.rb +7 -0
- data/ext/digest/sha3.c +153 -0
- data/keccak.gemspec +24 -0
- data/lib/digest/sha3/version.rb +7 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8aceb5b5671d06e3d0db69e7818d8cbf878a6519605beb75e13216bea26d7dce
|
4
|
+
data.tar.gz: 7bb0533a1e1f8f81a6b8ab21e555d0eb54fbd3c803a69a3f8a34bf90aa31bb90
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3a55a66d4b3ee22cc992ab3b5b30e584dd5c1a9781f160384f66bd7c378d4c81015fc9d9c935a81dce7c76e1fd42209e04d7dc70aa326ef98263923211cc92b
|
7
|
+
data.tar.gz: e6cf1e8fbe26d116182b462f95dd9371bc9421331b4a8176dc0cd83489846396375c5d5df7dc138d1127edf5d377096899b209ca8a9930f18de831d28f9fd061
|
data/COPYRIGHT
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
This package contains code written by the Keccak authors,
|
2
|
+
which is licensed CC0 (Public Domain).
|
3
|
+
|
4
|
+
Copyright (c) 2012-2015 Phusion B.V.; licensed under MIT License
|
5
|
+
https://github.com/phusion/digest-sha3-ruby
|
6
|
+
|
7
|
+
Copyright (c) 2016-2018 Chris Metcalfe; licensed under MIT License
|
8
|
+
https://github.com/teamhedge/digest-sha3-ruby
|
9
|
+
|
10
|
+
Copyright (c) 2018-2021 Seb's; licensed under MIT License
|
11
|
+
https://github.com/sydneyitguy/digest-sha3-ruby
|
12
|
+
|
13
|
+
Copyright (c) 2021-2022 Afri Schoedon; re-licensed under Apache 2.0
|
14
|
+
https://github.com/q9f/digest-sha3-ruby
|
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright 2019 @q9f
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/Makefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
.phony: all clean test
|
2
|
+
|
3
|
+
all: ext/digest/Makefile
|
4
|
+
make -C ext/digest
|
5
|
+
|
6
|
+
ext/digest/Makefile: ext/digest/extconf.rb
|
7
|
+
cd ext/digest && ruby extconf.rb
|
8
|
+
|
9
|
+
clean:
|
10
|
+
if [ -f ext/digest/Makefile ]; then make -C ext/digest clean; fi
|
11
|
+
rm -f ext/digest/Makefile
|
12
|
+
rm -f test/test_vectors.rb
|
13
|
+
|
14
|
+
test: all test/test_vectors.rb
|
15
|
+
ruby test/test_all.rb
|
16
|
+
|
17
|
+
test/test_vectors.rb: test/generate_tests.rb test/data/*
|
18
|
+
ruby test/generate_tests.rb > test/test_vectors.rb
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# The Keccak (SHA-3) digest for Ruby
|
2
|
+
|
3
|
+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/q9f/keccak.rb/Build/main)
|
4
|
+
![GitHub top language](https://img.shields.io/github/languages/top/q9f/keccak.rb)
|
5
|
+
![GitHub release](https://img.shields.io/github/v/release/q9f/keccak.rb)
|
6
|
+
![Gem](https://img.shields.io/gem/v/keccak)
|
7
|
+
|
8
|
+
This Ruby extension exposes the [Keccak](http://keccak.noekeon.org/) (SHA-3) digest C bindings in the non-final version used by Ethereum. It is based on the reference `C` implementation, version 3.2. The exposed interface is almost identical to that of the `digest` standard library.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
```bash
|
13
|
+
gem install digest-keccak
|
14
|
+
```
|
15
|
+
|
16
|
+
**Note**: as of version `v1.1.0`, `digest-keccak` requires Ruby 2.2. `digest-keccak`version `v1.2.0` now also supports Ruby 3.0.
|
17
|
+
|
18
|
+
The last version that worked on older Ruby (1.x) versions was `v1.0.2`. It can be found at the no longer maintained [digest-sha3 repository from 2015](https://github.com/phusion/digest-sha3-ruby/releases/tag/release-1.0.2).
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Keccak supports five hash lengths: 224-bit, 256-bit, 384-bit, 512-bit and variable length. Variable length is not supported by this Ruby extension. Unless the user specifies otherwise, this Ruby extension assumes 512-bit.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'digest/sha3'
|
26
|
+
|
27
|
+
# Generate 512-bit digest.
|
28
|
+
Digest::SHA3.digest("foo") # => "\025\227\204*..."
|
29
|
+
Digest::SHA3.hexdigest("foo") # => "1597842a..."
|
30
|
+
|
31
|
+
# Generate 224-bit digest.
|
32
|
+
Digest::SHA3.digest("foo", 224) # => "\332\251M\247..."
|
33
|
+
Digest::SHA3.hexdigest("foo", 224) # => "daa94da7..."
|
34
|
+
|
35
|
+
# Use this interface to feed data in chunks. 512-bit by default.
|
36
|
+
digest = Digest::SHA3.new
|
37
|
+
digest.update("f")
|
38
|
+
digest.update("o")
|
39
|
+
digest.update("o")
|
40
|
+
digest.digest # => "\025\227\204*..."
|
41
|
+
digest.hexdigest # => "1597842a..."
|
42
|
+
|
43
|
+
# You can pass a hash length to the constructor.
|
44
|
+
digest = Digest::SHA3.new(224)
|
45
|
+
```
|
46
|
+
|
47
|
+
## Running the test suite
|
48
|
+
|
49
|
+
Run the test suite as follows:
|
50
|
+
|
51
|
+
```bash
|
52
|
+
gem install test-unit
|
53
|
+
make test
|
54
|
+
```
|
55
|
+
|
56
|
+
A part of the test suite is automatically generated from Keccak's reference test suite.
|
57
|
+
|
58
|
+
## Warning: Keccak vs. SHA-3
|
59
|
+
|
60
|
+
**Note:** This gem still uses the `Digest::SHA3` namespace for reasons of backwards compatibility and long-term maintainability. See history section below.
|
61
|
+
|
62
|
+
:warning: This gem does **not** implement the final FIPS202 standard, today known as SHA-3 but rather an early version, commonly referred to as Keccak. The reason why this is kept around, is that Ethereum uses this earler version of Keccak. See also: [Ethereum: Difference between keccak256 and sha3](https://ethereum.stackexchange.com/questions/30369/difference-between-keccak256-and-sha3)
|
63
|
+
|
64
|
+
If you are looking for the final SHA-3 gem, please use the following: https://rubygems.org/gems/sha3
|
65
|
+
|
66
|
+
|
67
|
+
## History
|
68
|
+
|
69
|
+
This gem was initially developed and published as `digest-sha3`: https://github.com/phusion/digest-sha3-ruby
|
70
|
+
|
71
|
+
This gem was later patched multiple times:
|
72
|
+
|
73
|
+
* https://github.com/teamhedge/digest-sha3-ruby (KECCAK, as `digest-sha3-patched`)
|
74
|
+
* https://github.com/sydneyitguy/digest-sha3-ruby (KECCAK, as `digest-sha3-patched-ruby-3`)
|
75
|
+
* https://github.com/steakknife/digest-sha3-ruby (actual SHA-3, do not use for Ethereum)
|
@@ -0,0 +1,46 @@
|
|
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 _KeccakPermutationInterface_h_
|
15
|
+
#define _KeccakPermutationInterface_h_
|
16
|
+
|
17
|
+
#include "KeccakF-1600-int-set.h"
|
18
|
+
|
19
|
+
void KeccakInitialize( void );
|
20
|
+
void KeccakInitializeState(unsigned char *state);
|
21
|
+
void KeccakPermutation(unsigned char *state);
|
22
|
+
#ifdef ProvideFast576
|
23
|
+
void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data);
|
24
|
+
#endif
|
25
|
+
#ifdef ProvideFast832
|
26
|
+
void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data);
|
27
|
+
#endif
|
28
|
+
#ifdef ProvideFast1024
|
29
|
+
void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data);
|
30
|
+
#endif
|
31
|
+
#ifdef ProvideFast1088
|
32
|
+
void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data);
|
33
|
+
#endif
|
34
|
+
#ifdef ProvideFast1152
|
35
|
+
void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data);
|
36
|
+
#endif
|
37
|
+
#ifdef ProvideFast1344
|
38
|
+
void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data);
|
39
|
+
#endif
|
40
|
+
void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount);
|
41
|
+
#ifdef ProvideFast1024
|
42
|
+
void KeccakExtract1024bits(const unsigned char *state, unsigned char *data);
|
43
|
+
#endif
|
44
|
+
void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount);
|
45
|
+
|
46
|
+
#endif
|
@@ -0,0 +1,300 @@
|
|
1
|
+
/*
|
2
|
+
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
|
3
|
+
Michaël Peeters and Gilles Van Assche. For more information, feedback or
|
4
|
+
questions, please refer to our website: http://keccak.noekeon.org/
|
5
|
+
|
6
|
+
Implementation by the designers,
|
7
|
+
hereby denoted as "the implementer".
|
8
|
+
|
9
|
+
To the extent possible under law, the implementer has waived all copyright
|
10
|
+
and related or neighboring rights to the source code in this file.
|
11
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
12
|
+
*/
|
13
|
+
|
14
|
+
#include <stdio.h>
|
15
|
+
#include <string.h>
|
16
|
+
#include "brg_endian.h"
|
17
|
+
#include "displayIntermediateValues.h"
|
18
|
+
#include "KeccakNISTInterface.h"
|
19
|
+
#include "KeccakF-1600-interface.h"
|
20
|
+
|
21
|
+
typedef unsigned char UINT8;
|
22
|
+
typedef unsigned long long int UINT64;
|
23
|
+
|
24
|
+
#define nrRounds 24
|
25
|
+
UINT64 KeccakRoundConstants[nrRounds];
|
26
|
+
#define nrLanes 25
|
27
|
+
unsigned int KeccakRhoOffsets[nrLanes];
|
28
|
+
|
29
|
+
void KeccakPermutationOnWords(UINT64 *state);
|
30
|
+
void theta(UINT64 *A);
|
31
|
+
void rho(UINT64 *A);
|
32
|
+
void pi(UINT64 *A);
|
33
|
+
void chi(UINT64 *A);
|
34
|
+
void iota(UINT64 *A, unsigned int indexRound);
|
35
|
+
|
36
|
+
void fromBytesToWords(UINT64 *stateAsWords, const unsigned char *state)
|
37
|
+
{
|
38
|
+
unsigned int i, j;
|
39
|
+
|
40
|
+
for(i=0; i<(KeccakPermutationSize/64); i++) {
|
41
|
+
stateAsWords[i] = 0;
|
42
|
+
for(j=0; j<(64/8); j++)
|
43
|
+
stateAsWords[i] |= (UINT64)(state[i*(64/8)+j]) << (8*j);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
void fromWordsToBytes(unsigned char *state, const UINT64 *stateAsWords)
|
48
|
+
{
|
49
|
+
unsigned int i, j;
|
50
|
+
|
51
|
+
for(i=0; i<(KeccakPermutationSize/64); i++)
|
52
|
+
for(j=0; j<(64/8); j++)
|
53
|
+
state[i*(64/8)+j] = (stateAsWords[i] >> (8*j)) & 0xFF;
|
54
|
+
}
|
55
|
+
|
56
|
+
void KeccakPermutation(unsigned char *state)
|
57
|
+
{
|
58
|
+
#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
|
59
|
+
UINT64 stateAsWords[KeccakPermutationSize/64];
|
60
|
+
#endif
|
61
|
+
|
62
|
+
displayStateAsBytes(1, "Input of permutation", state);
|
63
|
+
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
|
64
|
+
KeccakPermutationOnWords((UINT64*)state);
|
65
|
+
#else
|
66
|
+
fromBytesToWords(stateAsWords, state);
|
67
|
+
KeccakPermutationOnWords(stateAsWords);
|
68
|
+
fromWordsToBytes(state, stateAsWords);
|
69
|
+
#endif
|
70
|
+
displayStateAsBytes(1, "State after permutation", state);
|
71
|
+
}
|
72
|
+
|
73
|
+
void KeccakPermutationAfterXor(unsigned char *state, const unsigned char *data, unsigned int dataLengthInBytes)
|
74
|
+
{
|
75
|
+
unsigned int i;
|
76
|
+
|
77
|
+
for(i=0; i<dataLengthInBytes; i++)
|
78
|
+
state[i] ^= data[i];
|
79
|
+
KeccakPermutation(state);
|
80
|
+
}
|
81
|
+
|
82
|
+
void KeccakPermutationOnWords(UINT64 *state)
|
83
|
+
{
|
84
|
+
unsigned int i;
|
85
|
+
|
86
|
+
displayStateAs64bitWords(3, "Same, with lanes as 64-bit words", state);
|
87
|
+
|
88
|
+
for(i=0; i<nrRounds; i++) {
|
89
|
+
displayRoundNumber(3, i);
|
90
|
+
|
91
|
+
theta(state);
|
92
|
+
displayStateAs64bitWords(3, "After theta", state);
|
93
|
+
|
94
|
+
rho(state);
|
95
|
+
displayStateAs64bitWords(3, "After rho", state);
|
96
|
+
|
97
|
+
pi(state);
|
98
|
+
displayStateAs64bitWords(3, "After pi", state);
|
99
|
+
|
100
|
+
chi(state);
|
101
|
+
displayStateAs64bitWords(3, "After chi", state);
|
102
|
+
|
103
|
+
iota(state, i);
|
104
|
+
displayStateAs64bitWords(3, "After iota", state);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
#define index(x, y) (((x)%5)+5*((y)%5))
|
109
|
+
#define ROL64(a, offset) ((offset != 0) ? ((((UINT64)a) << offset) ^ (((UINT64)a) >> (64-offset))) : a)
|
110
|
+
|
111
|
+
void theta(UINT64 *A)
|
112
|
+
{
|
113
|
+
unsigned int x, y;
|
114
|
+
UINT64 C[5], D[5];
|
115
|
+
|
116
|
+
for(x=0; x<5; x++) {
|
117
|
+
C[x] = 0;
|
118
|
+
for(y=0; y<5; y++)
|
119
|
+
C[x] ^= A[index(x, y)];
|
120
|
+
}
|
121
|
+
for(x=0; x<5; x++)
|
122
|
+
D[x] = ROL64(C[(x+1)%5], 1) ^ C[(x+4)%5];
|
123
|
+
for(x=0; x<5; x++)
|
124
|
+
for(y=0; y<5; y++)
|
125
|
+
A[index(x, y)] ^= D[x];
|
126
|
+
}
|
127
|
+
|
128
|
+
void rho(UINT64 *A)
|
129
|
+
{
|
130
|
+
unsigned int x, y;
|
131
|
+
|
132
|
+
for(x=0; x<5; x++) for(y=0; y<5; y++)
|
133
|
+
A[index(x, y)] = ROL64(A[index(x, y)], KeccakRhoOffsets[index(x, y)]);
|
134
|
+
}
|
135
|
+
|
136
|
+
void pi(UINT64 *A)
|
137
|
+
{
|
138
|
+
unsigned int x, y;
|
139
|
+
UINT64 tempA[25];
|
140
|
+
|
141
|
+
for(x=0; x<5; x++) for(y=0; y<5; y++)
|
142
|
+
tempA[index(x, y)] = A[index(x, y)];
|
143
|
+
for(x=0; x<5; x++) for(y=0; y<5; y++)
|
144
|
+
A[index(0*x+1*y, 2*x+3*y)] = tempA[index(x, y)];
|
145
|
+
}
|
146
|
+
|
147
|
+
void chi(UINT64 *A)
|
148
|
+
{
|
149
|
+
unsigned int x, y;
|
150
|
+
UINT64 C[5];
|
151
|
+
|
152
|
+
for(y=0; y<5; y++) {
|
153
|
+
for(x=0; x<5; x++)
|
154
|
+
C[x] = A[index(x, y)] ^ ((~A[index(x+1, y)]) & A[index(x+2, y)]);
|
155
|
+
for(x=0; x<5; x++)
|
156
|
+
A[index(x, y)] = C[x];
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
void iota(UINT64 *A, unsigned int indexRound)
|
161
|
+
{
|
162
|
+
A[index(0, 0)] ^= KeccakRoundConstants[indexRound];
|
163
|
+
}
|
164
|
+
|
165
|
+
int LFSR86540(UINT8 *LFSR)
|
166
|
+
{
|
167
|
+
int result = ((*LFSR) & 0x01) != 0;
|
168
|
+
if (((*LFSR) & 0x80) != 0)
|
169
|
+
// Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1
|
170
|
+
(*LFSR) = ((*LFSR) << 1) ^ 0x71;
|
171
|
+
else
|
172
|
+
(*LFSR) <<= 1;
|
173
|
+
return result;
|
174
|
+
}
|
175
|
+
|
176
|
+
void KeccakInitializeRoundConstants()
|
177
|
+
{
|
178
|
+
UINT8 LFSRstate = 0x01;
|
179
|
+
unsigned int i, j, bitPosition;
|
180
|
+
|
181
|
+
for(i=0; i<nrRounds; i++) {
|
182
|
+
KeccakRoundConstants[i] = 0;
|
183
|
+
for(j=0; j<7; j++) {
|
184
|
+
bitPosition = (1<<j)-1; //2^j-1
|
185
|
+
if (LFSR86540(&LFSRstate))
|
186
|
+
KeccakRoundConstants[i] ^= (UINT64)1<<bitPosition;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
void KeccakInitializeRhoOffsets()
|
192
|
+
{
|
193
|
+
unsigned int x, y, t, newX, newY;
|
194
|
+
|
195
|
+
KeccakRhoOffsets[index(0, 0)] = 0;
|
196
|
+
x = 1;
|
197
|
+
y = 0;
|
198
|
+
for(t=0; t<24; t++) {
|
199
|
+
KeccakRhoOffsets[index(x, y)] = ((t+1)*(t+2)/2) % 64;
|
200
|
+
newX = (0*x+1*y) % 5;
|
201
|
+
newY = (2*x+3*y) % 5;
|
202
|
+
x = newX;
|
203
|
+
y = newY;
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
void KeccakInitialize()
|
208
|
+
{
|
209
|
+
KeccakInitializeRoundConstants();
|
210
|
+
KeccakInitializeRhoOffsets();
|
211
|
+
}
|
212
|
+
|
213
|
+
void displayRoundConstants(FILE *f)
|
214
|
+
{
|
215
|
+
unsigned int i;
|
216
|
+
|
217
|
+
for(i=0; i<nrRounds; i++) {
|
218
|
+
fprintf(f, "RC[%02i][0][0] = ", i);
|
219
|
+
fprintf(f, "%08X", (unsigned int)(KeccakRoundConstants[i] >> 32));
|
220
|
+
fprintf(f, "%08X", (unsigned int)(KeccakRoundConstants[i] & 0xFFFFFFFFULL));
|
221
|
+
fprintf(f, "\n");
|
222
|
+
}
|
223
|
+
fprintf(f, "\n");
|
224
|
+
}
|
225
|
+
|
226
|
+
void displayRhoOffsets(FILE *f)
|
227
|
+
{
|
228
|
+
unsigned int x, y;
|
229
|
+
|
230
|
+
for(y=0; y<5; y++) for(x=0; x<5; x++) {
|
231
|
+
fprintf(f, "RhoOffset[%i][%i] = ", x, y);
|
232
|
+
fprintf(f, "%2i", KeccakRhoOffsets[index(x, y)]);
|
233
|
+
fprintf(f, "\n");
|
234
|
+
}
|
235
|
+
fprintf(f, "\n");
|
236
|
+
}
|
237
|
+
|
238
|
+
void KeccakInitializeState(unsigned char *state)
|
239
|
+
{
|
240
|
+
memset(state, 0, KeccakPermutationSizeInBytes);
|
241
|
+
}
|
242
|
+
|
243
|
+
#ifdef ProvideFast576
|
244
|
+
void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
|
245
|
+
{
|
246
|
+
KeccakPermutationAfterXor(state, data, 72);
|
247
|
+
}
|
248
|
+
#endif
|
249
|
+
|
250
|
+
#ifdef ProvideFast832
|
251
|
+
void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
|
252
|
+
{
|
253
|
+
KeccakPermutationAfterXor(state, data, 104);
|
254
|
+
}
|
255
|
+
#endif
|
256
|
+
|
257
|
+
#ifdef ProvideFast1024
|
258
|
+
void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
|
259
|
+
{
|
260
|
+
KeccakPermutationAfterXor(state, data, 128);
|
261
|
+
}
|
262
|
+
#endif
|
263
|
+
|
264
|
+
#ifdef ProvideFast1088
|
265
|
+
void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
|
266
|
+
{
|
267
|
+
KeccakPermutationAfterXor(state, data, 136);
|
268
|
+
}
|
269
|
+
#endif
|
270
|
+
|
271
|
+
#ifdef ProvideFast1152
|
272
|
+
void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
|
273
|
+
{
|
274
|
+
KeccakPermutationAfterXor(state, data, 144);
|
275
|
+
}
|
276
|
+
#endif
|
277
|
+
|
278
|
+
#ifdef ProvideFast1344
|
279
|
+
void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
|
280
|
+
{
|
281
|
+
KeccakPermutationAfterXor(state, data, 168);
|
282
|
+
}
|
283
|
+
#endif
|
284
|
+
|
285
|
+
void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
|
286
|
+
{
|
287
|
+
KeccakPermutationAfterXor(state, data, laneCount*8);
|
288
|
+
}
|
289
|
+
|
290
|
+
#ifdef ProvideFast1024
|
291
|
+
void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
|
292
|
+
{
|
293
|
+
memcpy(data, state, 128);
|
294
|
+
}
|
295
|
+
#endif
|
296
|
+
|
297
|
+
void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
|
298
|
+
{
|
299
|
+
memcpy(data, state, laneCount*8);
|
300
|
+
}
|