sha3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sha3 might be problematic. Click here for more details.

@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.rdoc
3
+ LICENSE.txt
@@ -0,0 +1,26 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg/
9
+ rdoc/
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp/
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ install.sh
21
+ *.lock
22
+
23
+ *.bundle
24
+
25
+ spec/bench.rb
26
+ .rbx/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1 @@
1
+ --markup rdoc --title "sha3 Documentation" --protected
@@ -0,0 +1,4 @@
1
+ === 0.1.0 / 2012-10-04
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'rubygems-tasks', '> 0.2'
6
+ gem 'rake', '> 0.9'
7
+ gem 'rspec', '~> 2.11'
8
+ gem 'yard', '~> 0.8'
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Johanns Gregorian
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ = sha3
2
+
3
+ * {Homepage}[https://github.com/johanns/sha3#readme]
4
+ * {Issues}[https://github.com/johanns/sha3/issues]
5
+ * {Documentation}[http://rubydoc.info/gems/sha3/frames]
6
+ * {Email}[mailto:io+sha3 at jsani.com]
7
+
8
+ == Description
9
+
10
+ TO-DO: A Ruby SHA3 wrapper
11
+
12
+ == Features
13
+
14
+ == Examples
15
+
16
+ require 'sha3'
17
+
18
+ # Basic Usage
19
+ SHA3::digest_224("Crazy Man's Utopia")
20
+ => "\xF8\x1A4\xACW6\v\x01A \x99\x11i\x91\xB3q\xB5\xF5\x04\xD4\x88fn\xC6\xA7\xEA\xBBb"
21
+
22
+ # A bit more advanced; allows you to specify data length (2nd param), and desired hash bit length (3rd param)
23
+ SHA3::digest("Crazy Man's Utopia", 36, 512)
24
+ => "\xBC\x9Dl\xEA \x83\xC2\x19\xC8\r/<!\xE4\xE3\x9E3(\xA1\x04\xB6\xBA6C\xE6\xA3\xC1\xCE\xF1EG/\xA9\xAD\xF1\x89\x97yA3\x98\x9FtX\xE7\x7F\xF4a\xD1\x1C\xFF\xCE\xA8\x84\x05\x91\x0310@\xBC\xF75b"
25
+
26
+ == Requirements
27
+
28
+ == Install
29
+
30
+ $ gem install sha3
31
+
32
+ == Copyright
33
+
34
+ Copyright (c) 2012 Johanns Gregorian
35
+
36
+ See LICENSE.txt for details.
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ gem 'rubygems-tasks', '~> 0.2'
8
+ require 'rubygems/tasks'
9
+
10
+ Gem::Tasks.new
11
+ rescue LoadError => e
12
+ warn e.message
13
+ warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
14
+ end
15
+
16
+ begin
17
+ gem 'rspec', '~> 2.4'
18
+ require 'rspec/core/rake_task'
19
+
20
+ RSpec::Core::RakeTask.new
21
+ rescue LoadError => e
22
+ task :spec do
23
+ abort "Please run `gem install rspec` to install RSpec."
24
+ end
25
+ end
26
+
27
+ task :test => :spec
28
+ task :default => [:compile, :spec]
29
+
30
+ begin
31
+ gem 'yard', '~> 0.8'
32
+ require 'yard'
33
+
34
+ YARD::Rake::YardocTask.new
35
+ rescue LoadError => e
36
+ task :yard do
37
+ abort "Please run `gem install yard` to install YARD."
38
+ end
39
+ end
40
+ task :doc => :yard
41
+
42
+ begin
43
+ gem 'rake-compiler'
44
+ require 'rake/extensiontask'
45
+
46
+ Rake::ExtensionTask.new do |ext|
47
+ ext.name = 'sha3_n'
48
+ ext.ext_dir = 'ext/sha3'
49
+ ext.tmp_dir = 'tmp'
50
+ ext.source_pattern = "*.{c,cpp}"
51
+ end
52
+ rescue LoadError => e
53
+ task :compile do
54
+ abort "Please run `gem install rake-compiler` to install Rake-Compiler."
55
+ end
56
+ end
57
+
@@ -0,0 +1,6 @@
1
+ #define ProvideFast576
2
+ #define ProvideFast832
3
+ #define ProvideFast1024
4
+ #define ProvideFast1088
5
+ #define ProvideFast1152
6
+ #define ProvideFast1344
@@ -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
+ }