sha3 0.2.2 → 1.0.2

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.

Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +232 -17
  3. data/.travis.yml +21 -12
  4. data/.yardopts +1 -1
  5. data/ChangeLog.rdoc +16 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.ci +5 -5
  8. data/LICENSE.txt +1 -1
  9. data/README.md +120 -0
  10. data/Rakefile +15 -18
  11. data/ext/sha3/KeccakF-1600-interface.h +28 -34
  12. data/ext/sha3/KeccakHash.c +80 -0
  13. data/ext/sha3/KeccakHash.h +110 -0
  14. data/ext/sha3/KeccakSponge.c +127 -201
  15. data/ext/sha3/KeccakSponge.h +74 -37
  16. data/ext/sha3/Optimized64/KeccakF-1600-64.macros +2199 -0
  17. data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +3 -0
  18. data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +508 -0
  19. data/ext/sha3/{KeccakF-1600-unrolling.macros → Optimized64/KeccakF-1600-unrolling.macros} +16 -14
  20. data/ext/sha3/Optimized64/SnP-interface.h +47 -0
  21. data/ext/sha3/Reference/KeccakF-1600-reference.c +311 -0
  22. data/ext/sha3/Reference/KeccakF-reference.h +26 -0
  23. data/ext/sha3/Reference/SnP-FBWL-default.c +96 -0
  24. data/ext/sha3/Reference/SnP-FBWL-default.h +26 -0
  25. data/ext/sha3/Reference/SnP-interface.h +42 -0
  26. data/ext/sha3/{displayIntermediateValues.c → Reference/displayIntermediateValues.c} +52 -11
  27. data/ext/sha3/{displayIntermediateValues.h → Reference/displayIntermediateValues.h} +11 -6
  28. data/ext/sha3/SnP-Relaned.h +249 -0
  29. data/ext/sha3/brg_endian.h +0 -0
  30. data/ext/sha3/digest.c +270 -0
  31. data/ext/sha3/digest.h +48 -0
  32. data/ext/sha3/extconf.rb +16 -9
  33. data/ext/sha3/sha3.c +62 -0
  34. data/ext/sha3/sha3.h +26 -0
  35. data/lib/sha3.rb +1 -1
  36. data/lib/sha3/doc.rb +121 -0
  37. data/lib/sha3/version.rb +6 -5
  38. data/sha3.gemspec +13 -15
  39. data/spec/generate_tests.rb +22 -56
  40. data/spec/sha3_core_spec.rb +113 -133
  41. data/spec/spec_helper.rb +2 -2
  42. data/tests.sh +11 -9
  43. metadata +53 -65
  44. data/README.rdoc +0 -133
  45. data/ext/sha3/KeccakF-1600-32-rvk.macros +0 -555
  46. data/ext/sha3/KeccakF-1600-32-s1.macros +0 -1187
  47. data/ext/sha3/KeccakF-1600-32-s2.macros +0 -1187
  48. data/ext/sha3/KeccakF-1600-32.macros +0 -26
  49. data/ext/sha3/KeccakF-1600-64.macros +0 -728
  50. data/ext/sha3/KeccakF-1600-int-set.h +0 -6
  51. data/ext/sha3/KeccakF-1600-opt.c +0 -504
  52. data/ext/sha3/KeccakF-1600-opt32-settings.h +0 -4
  53. data/ext/sha3/KeccakF-1600-opt32.c-arch +0 -524
  54. data/ext/sha3/KeccakF-1600-opt64-settings.h +0 -7
  55. data/ext/sha3/KeccakF-1600-opt64.c-arch +0 -504
  56. data/ext/sha3/KeccakF-1600-reference.c-arch +0 -300
  57. data/ext/sha3/KeccakF-1600-x86-64-gas.s +0 -766
  58. data/ext/sha3/KeccakF-1600-x86-64-shld-gas.s +0 -766
  59. data/ext/sha3/KeccakNISTInterface.c +0 -81
  60. data/ext/sha3/KeccakNISTInterface.h +0 -70
  61. data/ext/sha3/_sha3.c +0 -309
  62. data/ext/sha3/_sha3.h +0 -32
@@ -1,81 +0,0 @@
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 <string.h>
15
- #include "KeccakNISTInterface.h"
16
- #include "KeccakF-1600-interface.h"
17
-
18
- HashReturn Init(hashState *state, int hashbitlen)
19
- {
20
- switch(hashbitlen) {
21
- case 0: // Default parameters, arbitrary length output
22
- InitSponge((spongeState*)state, 1024, 576);
23
- break;
24
- case 224:
25
- InitSponge((spongeState*)state, 1152, 448);
26
- break;
27
- case 256:
28
- InitSponge((spongeState*)state, 1088, 512);
29
- break;
30
- case 384:
31
- InitSponge((spongeState*)state, 832, 768);
32
- break;
33
- case 512:
34
- InitSponge((spongeState*)state, 576, 1024);
35
- break;
36
- default:
37
- return BAD_HASHLEN;
38
- }
39
- state->fixedOutputLength = hashbitlen;
40
- return SUCCESS;
41
- }
42
-
43
- HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen)
44
- {
45
- if ((databitlen % 8) == 0)
46
- return Absorb((spongeState*)state, data, databitlen);
47
- else {
48
- HashReturn ret = Absorb((spongeState*)state, data, databitlen - (databitlen % 8));
49
- if (ret == SUCCESS) {
50
- unsigned char lastByte;
51
- // Align the last partial byte to the least significant bits
52
- lastByte = data[databitlen/8] >> (8 - (databitlen % 8));
53
- return Absorb((spongeState*)state, &lastByte, databitlen % 8);
54
- }
55
- else
56
- return ret;
57
- }
58
- }
59
-
60
- HashReturn Final(hashState *state, BitSequence *hashval)
61
- {
62
- return Squeeze(state, hashval, state->fixedOutputLength);
63
- }
64
-
65
- HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval)
66
- {
67
- hashState state;
68
- HashReturn result;
69
-
70
- if ((hashbitlen != 224) && (hashbitlen != 256) && (hashbitlen != 384) && (hashbitlen != 512))
71
- return BAD_HASHLEN; // Only the four fixed output lengths available through this API
72
- result = Init(&state, hashbitlen);
73
- if (result != SUCCESS)
74
- return result;
75
- result = Update(&state, data, databitlen);
76
- if (result != SUCCESS)
77
- return result;
78
- result = Final(&state, hashval);
79
- return result;
80
- }
81
-
@@ -1,70 +0,0 @@
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 _KeccakNISTInterface_h_
15
- #define _KeccakNISTInterface_h_
16
-
17
- #include "KeccakSponge.h"
18
-
19
- typedef unsigned char BitSequence;
20
- typedef unsigned long long DataLength;
21
- typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;
22
-
23
- typedef spongeState hashState;
24
-
25
- /**
26
- * Function to initialize the state of the Keccak[r, c] sponge function.
27
- * The rate r and capacity c values are determined from @a hashbitlen.
28
- * @param state Pointer to the state of the sponge function to be initialized.
29
- * @param hashbitlen The desired number of output bits,
30
- * or 0 for Keccak[] with default parameters
31
- * and arbitrarily-long output.
32
- * @pre The value of hashbitlen must be one of 0, 224, 256, 384 and 512.
33
- * @return SUCCESS if successful, BAD_HASHLEN if the value of hashbitlen is incorrect.
34
- */
35
- HashReturn Init(hashState *state, int hashbitlen);
36
- /**
37
- * Function to give input data for the sponge function to absorb.
38
- * @param state Pointer to the state of the sponge function initialized by Init().
39
- * @param data Pointer to the input data.
40
- * When @a databitLen is not a multiple of 8, the last bits of data must be
41
- * in the most significant bits of the last byte.
42
- * @param databitLen The number of input bits provided in the input data.
43
- * @pre In the previous call to Absorb(), databitLen was a multiple of 8.
44
- * @return SUCCESS if successful, FAIL otherwise.
45
- */
46
- HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen);
47
- /**
48
- * Function to squeeze output data from the sponge function.
49
- * If @a hashbitlen was not 0 in the call to Init(), the number of output bits is equal to @a hashbitlen.
50
- * If @a hashbitlen was 0 in the call to Init(), the output bits must be extracted using the Squeeze() function.
51
- * @param state Pointer to the state of the sponge function initialized by Init().
52
- * @param hashval Pointer to the buffer where to store the output data.
53
- * @return SUCCESS if successful, FAIL otherwise.
54
- */
55
- HashReturn Final(hashState *state, BitSequence *hashval);
56
- /**
57
- * Function to compute a hash using the Keccak[r, c] sponge function.
58
- * The rate r and capacity c values are determined from @a hashbitlen.
59
- * @param hashbitlen The desired number of output bits.
60
- * @param data Pointer to the input data.
61
- * When @a databitLen is not a multiple of 8, the last bits of data must be
62
- * in the most significant bits of the last byte.
63
- * @param databitLen The number of input bits provided in the input data.
64
- * @param hashval Pointer to the buffer where to store the output data.
65
- * @pre The value of hashbitlen must be one of 224, 256, 384 and 512.
66
- * @return SUCCESS if successful, BAD_HASHLEN if the value of hashbitlen is incorrect.
67
- */
68
- HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval);
69
-
70
- #endif
@@ -1,309 +0,0 @@
1
- #include "_sha3.h"
2
-
3
- /* Document-module: SHA3
4
- * SHA3
5
- */
6
-
7
- /* Document-class: SHA3::Digest < Digest::Class
8
- * SHA3::Digest allows you to compute message digests
9
- * (interchangeably called "hashes") of arbitrary data that are
10
- * cryptographically secure using SHA3 (Keccak) algorithm.
11
- *
12
- * == Usage
13
- *
14
- * require 'sha3'
15
- *
16
- * === Basics
17
- *
18
- * # Instantiate a new SHA3::Digest class with 256 bit length
19
- * s = SHA3::Digest.new(:sha256)
20
- * # => #<SHA3::Digest: c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470>
21
- *
22
- * # Update hash state, and compute new value
23
- * s.update "Compute Me"
24
- * # => #<SHA3::Digest: c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470>
25
- *
26
- * # << is an .update() alias
27
- * s << "Me too"
28
- * # => #<SHA3::Digest: e26f539eee3a05c52eb1f9439652d23343adea9764f011da232d24cd6d19924a>
29
- *
30
- * # Print digest bytes string
31
- * puts s.digest
32
- *
33
- * # Print digest hex string
34
- * puts s.hexdigest
35
- *
36
- * === Hashing a file
37
- *
38
- * # Compute the hash value for given file, and return the result as hex
39
- * s = SHA3::Digest.new(224).file("my_awesome_file.bin").hexdigest
40
- *
41
- * === Bit operation
42
- *
43
- * # Compute hash of "011"
44
- * SHA3::Digest.compute(:sha224, "\xC0", 3).unpack("H*")
45
- * # => ["2b695a6fd92a2b3f3ce9cfca617d22c9bb52815dd59a9719b01bad25"]
46
- *
47
- * == Notes
48
- *
49
- * ::Digest::Class call sequence ->
50
- * | .alloc() ->
51
- * | .new() ->
52
- * | .update() ->
53
- * | .digest or .hexdigest or .inspect -> (Instance.digest or .hexdigest()) ->
54
- * --| .alloc() ->
55
- * | .copy() ->
56
- * | .finish() ->
57
- *
58
- */
59
-
60
- static int get_hlen(VALUE obj)
61
- {
62
- int hlen;
63
-
64
- if (TYPE(obj) == T_SYMBOL) {
65
- ID symid;
66
-
67
- symid = SYM2ID(obj);
68
-
69
- if (rb_intern("sha224") == symid)
70
- hlen = 224;
71
- else if (rb_intern("sha256") == symid)
72
- hlen = 256;
73
- else if (rb_intern("sha384") == symid)
74
- hlen = 384;
75
- else if (rb_intern("sha512") == symid)
76
- hlen = 512;
77
- else
78
- rb_raise(eDigestError, "invalid hash bit symbol (should be: :sha224, :sha256, :sha384, or :sha512");
79
- }
80
- else if (TYPE(obj) == T_FIXNUM) {
81
- hlen = NUM2INT(obj);
82
-
83
- if ((hlen != 224) && (hlen != 256) && (hlen != 384) && (hlen != 512))
84
- rb_raise(rb_eArgError, "invalid hash bit length (should be: 224, 256, 384, or 512)");
85
- }
86
- else
87
- rb_raise(eDigestError, "unknown type value");
88
-
89
- return hlen;
90
- }
91
-
92
- static void free_allox(MDX *mdx)
93
- {
94
- if (mdx) {
95
- if (mdx->state)
96
- free(mdx->state);
97
-
98
- free(mdx);
99
- }
100
-
101
- return;
102
- }
103
-
104
- static VALUE c_digest_alloc(VALUE klass)
105
- {
106
- MDX *mdx;
107
- VALUE obj;
108
-
109
- mdx = (MDX *) malloc(sizeof(*mdx));
110
- if (!mdx)
111
- rb_raise(eDigestError, "failed to allocate object memory");
112
-
113
- mdx->state = (hashState *) malloc(sizeof(*mdx->state));
114
- if (!mdx->state) {
115
- free_allox(mdx);
116
- rb_raise(eDigestError, "failed to allocate state memory");
117
- }
118
-
119
- obj = Data_Wrap_Struct(klass, 0, free_allox, mdx);
120
-
121
- memset(mdx->state, 0, sizeof(*mdx->state));
122
- mdx->hashbitlen = 0;
123
-
124
- return obj;
125
- }
126
-
127
- static VALUE c_digest_update(VALUE, VALUE);
128
-
129
- // SHA3::Digest.new(type, [data]) -> self
130
- static VALUE c_digest_init(int argc, VALUE *argv, VALUE self)
131
- {
132
- MDX *mdx;
133
- VALUE hlen, data;
134
-
135
- rb_scan_args(argc, argv, "02", &hlen, &data);
136
- GETMDX(self, mdx);
137
-
138
- if (!NIL_P(hlen))
139
- mdx->hashbitlen = get_hlen(hlen);
140
- else
141
- mdx->hashbitlen = 256;
142
-
143
- if (Init(mdx->state, mdx->hashbitlen) != SUCCESS)
144
- rb_raise(eDigestError, "failed to initialize algorithm state");
145
-
146
- if (!NIL_P(data))
147
- return c_digest_update(self, data);
148
-
149
- return self;
150
- }
151
-
152
- // SHA3::Digest.update(data) -> self
153
- static VALUE c_digest_update(VALUE self, VALUE data)
154
- {
155
- MDX *mdx;
156
- DataLength dlen;
157
-
158
- StringValue(data);
159
- GETMDX(self, mdx);
160
-
161
- dlen = (RSTRING_LEN(data) * 8);
162
-
163
- if (Update(mdx->state, RSTRING_PTR(data), dlen) != SUCCESS)
164
- rb_raise(eDigestError, "failed to update hash data");
165
-
166
- return self;
167
- }
168
-
169
- // SHA3::Digest.reset() -> self
170
- static VALUE c_digest_reset(VALUE self)
171
- {
172
- MDX *mdx;
173
-
174
- GETMDX(self, mdx);
175
-
176
- memset(mdx->state, 0, sizeof(*mdx->state));
177
-
178
- if (Init(mdx->state, mdx->hashbitlen) != SUCCESS)
179
- rb_raise(eDigestError, "failed to reset internal state");
180
-
181
- return self;
182
- }
183
-
184
- // SHA3::Digest.copy(obj) -> self
185
- static VALUE c_digest_copy(VALUE self, VALUE obj)
186
- {
187
- MDX *mdx1, *mdx2;
188
-
189
- rb_check_frozen(self);
190
- if (self == obj)
191
- return self;
192
-
193
- GETMDX(self, mdx1);
194
- SAFEGETMDX(obj, mdx2);
195
-
196
- memcpy(mdx1->state, mdx2->state, sizeof(hashState));
197
- mdx1->hashbitlen = mdx2->hashbitlen;
198
-
199
- // Fetch the data again to make sure it was copied
200
- GETMDX(self, mdx1);
201
- SAFEGETMDX(obj, mdx2);
202
- if ((mdx1->state != mdx2->state) && (mdx1->hashbitlen != mdx2->hashbitlen))
203
- rb_raise(eDigestError, "failed to copy state");
204
-
205
- return self;
206
- }
207
-
208
- // SHA3::Digest.digest_length -> Integer
209
- static VALUE c_digest_length(VALUE self)
210
- {
211
- MDX *mdx;
212
- GETMDX(self, mdx);
213
-
214
- return ULL2NUM(mdx->hashbitlen / 8);
215
- }
216
-
217
- // SHA3::Digest.block_length -> Integer
218
- static VALUE c_digest_block_length(VALUE self)
219
- {
220
- MDX *mdx;
221
- GETMDX(self, mdx);
222
-
223
- return ULL2NUM(200 - (2 * (mdx->hashbitlen / 8)));
224
- }
225
-
226
- // SHA3::Digest.name -> String
227
- static VALUE c_digest_name(VALUE self)
228
- {
229
- return rb_str_new2("SHA3");
230
- }
231
-
232
- // SHA3::Digest.finish() -> String
233
- static VALUE c_digest_finish(int argc, VALUE *argv, VALUE self)
234
- {
235
- MDX *mdx;
236
- VALUE str;
237
-
238
- rb_scan_args(argc, argv, "01", &str);
239
- GETMDX(self, mdx);
240
-
241
- if (NIL_P(str)) {
242
- str = rb_str_new(0, mdx->hashbitlen / 8);
243
- }
244
- else {
245
- StringValue(str);
246
- rb_str_resize(str, mdx->hashbitlen / 8);
247
- }
248
-
249
- if (Final(mdx->state, RSTRING_PTR(str)) != SUCCESS)
250
- rb_raise(eDigestError, "failed to finalize digest");
251
-
252
- return str;
253
- }
254
-
255
- // SHA3::Digest.compute(type, data, [datalen]) -> String (bytes)
256
- // TO-DO: styled output (hex)
257
- VALUE c_digest_compute(int argc, VALUE *argv, VALUE self)
258
- {
259
- VALUE hlen, data, dlen, str;
260
- int hashbitlen;
261
- DataLength datalen;
262
-
263
- rb_scan_args(argc, argv, "21", &hlen, &data, &dlen);
264
-
265
- hashbitlen = get_hlen(hlen);
266
-
267
- StringValue(data);
268
-
269
- if (!NIL_P(dlen))
270
- datalen = NUM2ULL(dlen);
271
- else
272
- datalen = (RSTRING_LEN(data) * 8);
273
-
274
- str = rb_str_new(0, hashbitlen / 8);
275
-
276
- if (Hash(hashbitlen, RSTRING_PTR(data), datalen, RSTRING_PTR(str)) != SUCCESS)
277
- rb_raise(eDigestError, "failed to generate hash");
278
-
279
- return str;
280
- }
281
-
282
- void Init_sha3_n()
283
- {
284
- rb_require("digest");
285
-
286
- mSHA3 = rb_define_module("SHA3");
287
- /* SHA3::Digest (class) */
288
- cDigest = rb_define_class_under(mSHA3, "Digest", rb_path2class("Digest::Class"));
289
- /* SHA3::Digest::DigestError (class) */
290
- eDigestError = rb_define_class_under(cDigest, "DigestError", rb_eStandardError);
291
-
292
- // SHA3::Digest (class) methods
293
- rb_define_alloc_func(cDigest, c_digest_alloc);
294
- rb_define_method(cDigest, "initialize", c_digest_init, -1);
295
- rb_define_method(cDigest, "update", c_digest_update, 1);
296
- rb_define_method(cDigest, "reset", c_digest_reset, 0);
297
- rb_define_method(cDigest, "initialize_copy", c_digest_copy, 1);
298
- rb_define_method(cDigest, "digest_length", c_digest_length, 0);
299
- rb_define_method(cDigest, "block_length", c_digest_block_length, 0);
300
- rb_define_method(cDigest, "name", c_digest_name, 0);
301
- rb_define_private_method(cDigest, "finish", c_digest_finish, -1);
302
-
303
- rb_define_alias(cDigest, "<<", "update");
304
-
305
- // SHA3 (module) functions (support bit operations)
306
- rb_define_singleton_method(cDigest, "compute", c_digest_compute, -1);
307
-
308
- return;
309
- }