sha3 0.2.5 → 1.0.3
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.
- checksums.yaml +5 -5
- data/.gitignore +232 -20
- data/.travis.yml +18 -12
- data/Gemfile +1 -1
- data/Gemfile.ci +5 -5
- data/LICENSE.txt +1 -1
- data/README.md +120 -0
- data/Rakefile +15 -18
- data/ext/sha3/KeccakF-1600-interface.h +28 -34
- data/ext/sha3/KeccakHash.c +80 -0
- data/ext/sha3/KeccakHash.h +110 -0
- data/ext/sha3/KeccakSponge.c +127 -201
- data/ext/sha3/KeccakSponge.h +74 -37
- data/ext/sha3/Optimized64/KeccakF-1600-64.macros +2199 -0
- data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +3 -0
- data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +508 -0
- data/ext/sha3/{KeccakF-1600-unrolling.macros → Optimized64/KeccakF-1600-unrolling.macros} +16 -14
- data/ext/sha3/Optimized64/SnP-interface.h +47 -0
- data/ext/sha3/Reference/KeccakF-1600-reference.c +311 -0
- data/ext/sha3/Reference/KeccakF-reference.h +26 -0
- data/ext/sha3/Reference/SnP-FBWL-default.c +96 -0
- data/ext/sha3/Reference/SnP-FBWL-default.h +26 -0
- data/ext/sha3/Reference/SnP-interface.h +42 -0
- data/ext/sha3/{displayIntermediateValues.c → Reference/displayIntermediateValues.c} +52 -11
- data/ext/sha3/{displayIntermediateValues.h → Reference/displayIntermediateValues.h} +11 -6
- data/ext/sha3/SnP-Relaned.h +249 -0
- data/ext/sha3/brg_endian.h +0 -0
- data/ext/sha3/digest.c +182 -167
- data/ext/sha3/digest.h +37 -29
- data/ext/sha3/extconf.rb +13 -13
- data/ext/sha3/sha3.c +46 -30
- data/ext/sha3/sha3.h +10 -9
- data/lib/sha3/doc.rb +26 -39
- data/lib/sha3/version.rb +2 -3
- data/sha3.gemspec +13 -15
- data/spec/generate_tests.rb +22 -57
- data/spec/sha3_core_spec.rb +111 -133
- data/spec/spec_helper.rb +2 -2
- data/tests.sh +11 -9
- metadata +46 -51
- data/README.rdoc +0 -132
- data/ext/sha3/KeccakF-1600-32-rvk.macros +0 -555
- data/ext/sha3/KeccakF-1600-32-s1.macros +0 -1187
- data/ext/sha3/KeccakF-1600-32-s2.macros +0 -1187
- data/ext/sha3/KeccakF-1600-32.macros +0 -26
- data/ext/sha3/KeccakF-1600-64.macros +0 -728
- data/ext/sha3/KeccakF-1600-int-set.h +0 -6
- data/ext/sha3/KeccakF-1600-opt.c +0 -504
- data/ext/sha3/KeccakF-1600-opt32-settings.h +0 -4
- data/ext/sha3/KeccakF-1600-opt32.c-arch +0 -524
- data/ext/sha3/KeccakF-1600-opt64-settings.h +0 -7
- data/ext/sha3/KeccakF-1600-opt64.c-arch +0 -504
- data/ext/sha3/KeccakF-1600-reference.c-arch +0 -300
- data/ext/sha3/KeccakF-1600-x86-64-gas.s +0 -766
- data/ext/sha3/KeccakF-1600-x86-64-shld-gas.s +0 -766
- data/ext/sha3/KeccakNISTInterface.c +0 -81
- data/ext/sha3/KeccakNISTInterface.h +0 -70
data/ext/sha3/digest.c
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
#include "sha3.h"
|
4
4
|
|
5
|
-
VALUE
|
6
|
-
VALUE
|
5
|
+
VALUE cSHA3Digest;
|
6
|
+
VALUE eSHA3DigestError;
|
7
7
|
|
8
8
|
/*
|
9
9
|
* == Notes
|
10
|
-
*
|
10
|
+
*
|
11
11
|
* ::Digest::Class call sequence ->
|
12
12
|
* | .alloc() ->
|
13
13
|
* | .new() ->
|
@@ -16,240 +16,255 @@ VALUE eDigestError;
|
|
16
16
|
* --| .alloc() ->
|
17
17
|
* | .copy() ->
|
18
18
|
* | .finish() ->
|
19
|
-
*
|
19
|
+
*
|
20
20
|
*/
|
21
21
|
|
22
|
-
static void free_allox(MDX *mdx)
|
22
|
+
static void free_allox(MDX *mdx)
|
23
23
|
{
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
if (mdx)
|
25
|
+
{
|
26
|
+
if (mdx->state)
|
27
|
+
{
|
28
|
+
free(mdx->state);
|
29
|
+
}
|
27
30
|
|
28
|
-
|
29
|
-
|
31
|
+
free(mdx);
|
32
|
+
}
|
30
33
|
|
31
|
-
|
34
|
+
return;
|
32
35
|
}
|
33
36
|
|
34
|
-
static VALUE c_digest_alloc(VALUE klass)
|
35
|
-
{
|
36
|
-
|
37
|
-
|
37
|
+
static VALUE c_digest_alloc(VALUE klass)
|
38
|
+
{
|
39
|
+
MDX *mdx;
|
40
|
+
VALUE obj;
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
mdx = (MDX *)malloc(sizeof(MDX));
|
43
|
+
if (!mdx)
|
44
|
+
{
|
45
|
+
rb_raise(eSHA3DigestError, "failed to allocate object memory");
|
46
|
+
}
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
mdx->state = (Keccak_HashInstance *)malloc(sizeof(Keccak_HashInstance));
|
49
|
+
if (!mdx->state)
|
50
|
+
{
|
51
|
+
free_allox(mdx);
|
52
|
+
rb_raise(eSHA3DigestError, "failed to allocate state memory");
|
53
|
+
}
|
48
54
|
|
49
|
-
|
55
|
+
obj = Data_Wrap_Struct(klass, 0, free_allox, mdx);
|
50
56
|
|
51
|
-
|
52
|
-
|
57
|
+
memset(mdx->state, 0, sizeof(Keccak_HashInstance));
|
58
|
+
mdx->hashbitlen = 0;
|
53
59
|
|
54
|
-
|
60
|
+
return obj;
|
55
61
|
}
|
56
62
|
|
57
63
|
static VALUE c_digest_update(VALUE, VALUE);
|
58
64
|
|
65
|
+
HashReturn c_keccak_hash_initialize(MDX *mdx)
|
66
|
+
{
|
67
|
+
HashReturn r = FAIL;
|
68
|
+
|
69
|
+
switch (mdx->hashbitlen)
|
70
|
+
{
|
71
|
+
case 224:
|
72
|
+
r = Keccak_HashInitialize_SHA3_224(mdx->state);
|
73
|
+
break;
|
74
|
+
case 256:
|
75
|
+
r = Keccak_HashInitialize_SHA3_256(mdx->state);
|
76
|
+
break;
|
77
|
+
case 384:
|
78
|
+
r = Keccak_HashInitialize_SHA3_384(mdx->state);
|
79
|
+
break;
|
80
|
+
case 512:
|
81
|
+
r = Keccak_HashInitialize_SHA3_512(mdx->state);
|
82
|
+
break;
|
83
|
+
}
|
84
|
+
|
85
|
+
return r;
|
86
|
+
}
|
87
|
+
|
59
88
|
// SHA3::Digest.new(type, [data]) -> self
|
60
89
|
static VALUE c_digest_init(int argc, VALUE *argv, VALUE self)
|
61
|
-
{
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
90
|
+
{
|
91
|
+
MDX *mdx;
|
92
|
+
VALUE hlen, data;
|
93
|
+
|
94
|
+
rb_scan_args(argc, argv, "02", &hlen, &data);
|
95
|
+
GETMDX(self, mdx);
|
96
|
+
|
97
|
+
if (!NIL_P(hlen))
|
98
|
+
{
|
99
|
+
mdx->hashbitlen = get_hlen(hlen);
|
100
|
+
}
|
101
|
+
else
|
102
|
+
{
|
103
|
+
mdx->hashbitlen = 256;
|
104
|
+
}
|
105
|
+
|
106
|
+
if (c_keccak_hash_initialize(mdx) != SUCCESS)
|
107
|
+
{
|
108
|
+
rb_raise(eSHA3DigestError, "failed to initialize algorithm state");
|
109
|
+
}
|
110
|
+
|
111
|
+
if (!NIL_P(data))
|
112
|
+
{
|
113
|
+
return c_digest_update(self, data);
|
114
|
+
}
|
78
115
|
|
79
|
-
|
116
|
+
return self;
|
80
117
|
}
|
81
118
|
|
82
119
|
// SHA3::Digest.update(data) -> self
|
83
|
-
static VALUE c_digest_update(VALUE self, VALUE data)
|
120
|
+
static VALUE c_digest_update(VALUE self, VALUE data)
|
84
121
|
{
|
85
|
-
|
86
|
-
|
122
|
+
MDX *mdx;
|
123
|
+
DataLength dlen;
|
87
124
|
|
88
|
-
|
89
|
-
|
125
|
+
StringValue(data);
|
126
|
+
GETMDX(self, mdx);
|
90
127
|
|
91
|
-
|
128
|
+
dlen = (RSTRING_LEN(data) * 8);
|
92
129
|
|
93
|
-
|
94
|
-
|
130
|
+
if (Keccak_HashUpdate(mdx->state, (BitSequence *)RSTRING_PTR(data), dlen) != SUCCESS)
|
131
|
+
{
|
132
|
+
rb_raise(eSHA3DigestError, "failed to update hash data");
|
133
|
+
}
|
95
134
|
|
96
|
-
|
135
|
+
return self;
|
97
136
|
}
|
98
137
|
|
99
138
|
// SHA3::Digest.reset() -> self
|
100
|
-
static VALUE c_digest_reset(VALUE self)
|
139
|
+
static VALUE c_digest_reset(VALUE self)
|
101
140
|
{
|
102
|
-
|
141
|
+
MDX *mdx;
|
103
142
|
|
104
|
-
|
143
|
+
GETMDX(self, mdx);
|
105
144
|
|
106
|
-
|
145
|
+
memset(mdx->state, 0, sizeof(Keccak_HashInstance));
|
107
146
|
|
108
|
-
|
109
|
-
|
147
|
+
if (c_keccak_hash_initialize(mdx) != SUCCESS)
|
148
|
+
{
|
149
|
+
rb_raise(eSHA3DigestError, "failed to reset internal state");
|
150
|
+
}
|
110
151
|
|
111
|
-
|
152
|
+
return self;
|
112
153
|
}
|
113
154
|
|
114
|
-
// Fix: And, permanent reminder of a rookie mistake in c_digest_copy, comparing structs with ==/!= op
|
115
|
-
// Fix: Woke-up after 2-hours of sleep, and for good reason. Fixed string comparison. Need to re-read K&R!
|
116
155
|
static int cmp_states(MDX *mdx1, MDX *mdx2)
|
117
156
|
{
|
118
157
|
return (
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
(mdx1->state->squeezing == mdx2->state->squeezing) &&
|
127
|
-
(mdx1->state->bitsAvailableForSqueezing == mdx2->state->bitsAvailableForSqueezing)
|
128
|
-
);
|
158
|
+
(mdx1->hashbitlen == mdx2->hashbitlen) &&
|
159
|
+
(strcmp((const char *)mdx1->state->sponge.state, (const char *)mdx2->state->sponge.state) == 0) &&
|
160
|
+
(mdx1->state->sponge.rate == mdx2->state->sponge.rate) &&
|
161
|
+
(mdx1->state->sponge.byteIOIndex == mdx2->state->sponge.byteIOIndex) &&
|
162
|
+
(mdx1->state->sponge.squeezing == mdx2->state->sponge.squeezing) &&
|
163
|
+
(mdx1->state->fixedOutputLength == mdx2->state->fixedOutputLength) &&
|
164
|
+
(mdx1->state->delimitedSuffix == mdx2->state->delimitedSuffix));
|
129
165
|
}
|
130
166
|
|
131
167
|
// SHA3::Digest.copy(obj) -> self
|
132
168
|
static VALUE c_digest_copy(VALUE self, VALUE obj)
|
133
169
|
{
|
134
|
-
|
170
|
+
MDX *mdx1, *mdx2;
|
135
171
|
|
136
|
-
|
137
|
-
|
138
|
-
|
172
|
+
rb_check_frozen(self);
|
173
|
+
if (self == obj)
|
174
|
+
{
|
175
|
+
return self;
|
176
|
+
}
|
177
|
+
|
178
|
+
GETMDX(self, mdx1);
|
179
|
+
SAFEGETMDX(obj, mdx2);
|
139
180
|
|
140
|
-
|
141
|
-
|
181
|
+
memcpy(mdx1->state, mdx2->state, sizeof(Keccak_HashInstance));
|
182
|
+
mdx1->hashbitlen = mdx2->hashbitlen;
|
142
183
|
|
143
|
-
|
144
|
-
|
184
|
+
// Fetch the data again to make sure it was copied
|
185
|
+
GETMDX(self, mdx1);
|
186
|
+
SAFEGETMDX(obj, mdx2);
|
145
187
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
rb_raise(eDigestError, "failed to copy state");
|
188
|
+
if (!cmp_states(mdx1, mdx2))
|
189
|
+
{
|
190
|
+
rb_raise(eSHA3DigestError, "failed to copy state");
|
191
|
+
}
|
151
192
|
|
152
|
-
|
193
|
+
return self;
|
153
194
|
}
|
154
195
|
|
155
196
|
// SHA3::Digest.digest_length -> Integer
|
156
197
|
static VALUE c_digest_length(VALUE self)
|
157
198
|
{
|
158
|
-
|
159
|
-
|
199
|
+
MDX *mdx;
|
200
|
+
GETMDX(self, mdx);
|
160
201
|
|
161
|
-
|
202
|
+
return ULL2NUM(mdx->hashbitlen / 8);
|
162
203
|
}
|
163
204
|
|
164
205
|
// SHA3::Digest.block_length -> Integer
|
165
|
-
static VALUE c_digest_block_length(VALUE self)
|
206
|
+
static VALUE c_digest_block_length(VALUE self)
|
166
207
|
{
|
167
|
-
|
168
|
-
|
208
|
+
MDX *mdx;
|
209
|
+
GETMDX(self, mdx);
|
169
210
|
|
170
|
-
|
211
|
+
return ULL2NUM(200 - (2 * (mdx->hashbitlen / 8)));
|
171
212
|
}
|
172
213
|
|
173
214
|
// SHA3::Digest.name -> String
|
174
|
-
static VALUE c_digest_name(VALUE self)
|
215
|
+
static VALUE c_digest_name(VALUE self)
|
175
216
|
{
|
176
|
-
|
217
|
+
return rb_str_new2("SHA3");
|
177
218
|
}
|
178
219
|
|
179
220
|
// SHA3::Digest.finish() -> String
|
180
|
-
static VALUE c_digest_finish(int argc, VALUE *argv, VALUE self)
|
221
|
+
static VALUE c_digest_finish(int argc, VALUE *argv, VALUE self)
|
181
222
|
{
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
static VALUE c_digest_compute(int argc, VALUE *argv, VALUE self)
|
205
|
-
{
|
206
|
-
VALUE hlen, data, dlen, str;
|
207
|
-
int hashbitlen;
|
208
|
-
DataLength datalen;
|
209
|
-
|
210
|
-
rb_scan_args(argc, argv, "21", &hlen, &data, &dlen);
|
211
|
-
|
212
|
-
hashbitlen = get_hlen(hlen);
|
213
|
-
|
214
|
-
StringValue(data);
|
215
|
-
|
216
|
-
if (!NIL_P(dlen))
|
217
|
-
datalen = NUM2ULL(dlen);
|
218
|
-
else
|
219
|
-
datalen = (RSTRING_LEN(data) * 8);
|
220
|
-
|
221
|
-
str = rb_str_new(0, hashbitlen / 8);
|
222
|
-
|
223
|
-
if (Hash(hashbitlen, RSTRING_PTR(data), datalen, RSTRING_PTR(str)) != SUCCESS)
|
224
|
-
rb_raise(eDigestError, "failed to generate hash");
|
225
|
-
|
226
|
-
return str;
|
223
|
+
MDX *mdx;
|
224
|
+
VALUE str;
|
225
|
+
|
226
|
+
rb_scan_args(argc, argv, "01", &str);
|
227
|
+
GETMDX(self, mdx);
|
228
|
+
|
229
|
+
if (NIL_P(str))
|
230
|
+
{
|
231
|
+
str = rb_str_new(0, mdx->hashbitlen / 8);
|
232
|
+
}
|
233
|
+
else
|
234
|
+
{
|
235
|
+
StringValue(str);
|
236
|
+
rb_str_resize(str, mdx->hashbitlen / 8);
|
237
|
+
}
|
238
|
+
|
239
|
+
if (Keccak_HashFinal(mdx->state, (BitSequence *)RSTRING_PTR(str)) != SUCCESS)
|
240
|
+
{
|
241
|
+
rb_raise(eSHA3DigestError, "failed to finalize digest");
|
242
|
+
}
|
243
|
+
|
244
|
+
return str;
|
227
245
|
}
|
228
246
|
|
229
247
|
void Init_sha3_n_digest()
|
230
248
|
{
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
return;
|
255
|
-
}
|
249
|
+
rb_require("digest");
|
250
|
+
|
251
|
+
/* SHA3::Digest (class) */
|
252
|
+
cSHA3Digest = rb_define_class_under(mSHA3, "Digest", rb_path2class("Digest::Class"));
|
253
|
+
/* SHA3::Digest::DigestError (class) */
|
254
|
+
eSHA3DigestError = rb_define_class_under(cSHA3Digest, "DigestError", rb_eStandardError);
|
255
|
+
|
256
|
+
// SHA3::Digest (class) methods
|
257
|
+
rb_define_alloc_func(cSHA3Digest, c_digest_alloc);
|
258
|
+
rb_define_method(cSHA3Digest, "initialize", c_digest_init, -1);
|
259
|
+
rb_define_method(cSHA3Digest, "update", c_digest_update, 1);
|
260
|
+
rb_define_method(cSHA3Digest, "reset", c_digest_reset, 0);
|
261
|
+
rb_define_method(cSHA3Digest, "initialize_copy", c_digest_copy, 1);
|
262
|
+
rb_define_method(cSHA3Digest, "digest_length", c_digest_length, 0);
|
263
|
+
rb_define_method(cSHA3Digest, "block_length", c_digest_block_length, 0);
|
264
|
+
rb_define_method(cSHA3Digest, "name", c_digest_name, 0);
|
265
|
+
rb_define_private_method(cSHA3Digest, "finish", c_digest_finish, -1);
|
266
|
+
|
267
|
+
rb_define_alias(cSHA3Digest, "<<", "update");
|
268
|
+
|
269
|
+
return;
|
270
|
+
}
|
data/ext/sha3/digest.h
CHANGED
@@ -3,38 +3,46 @@
|
|
3
3
|
#ifndef _DIGEST_H_
|
4
4
|
#define _DIGEST_H_
|
5
5
|
|
6
|
-
#ifdef
|
7
|
-
extern "C"
|
6
|
+
#ifdef __cplusplus
|
7
|
+
extern "C"
|
8
|
+
{
|
8
9
|
#endif
|
9
10
|
|
10
11
|
// From ruby/ext/openssl/ossl_digest.c
|
11
|
-
#define GETMDX(obj, mdx)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
12
|
+
#define GETMDX(obj, mdx) \
|
13
|
+
do \
|
14
|
+
{ \
|
15
|
+
Data_Get_Struct((obj), MDX, (mdx)); \
|
16
|
+
if (!(mdx)) \
|
17
|
+
{ \
|
18
|
+
rb_raise(rb_eRuntimeError, "Digest data not initialized!"); \
|
19
|
+
} \
|
20
|
+
} while (0)
|
21
|
+
|
22
|
+
#define SAFEGETMDX(obj, mdx) \
|
23
|
+
do \
|
24
|
+
{ \
|
25
|
+
if (!rb_obj_is_kind_of(obj, cSHA3Digest)) \
|
26
|
+
{ \
|
27
|
+
rb_raise(rb_eTypeError, "wrong argument (%s)! (expected %s)", \
|
28
|
+
rb_obj_classname(obj), rb_class2name(cSHA3Digest)); \
|
29
|
+
} \
|
30
|
+
GETMDX(obj, mdx); \
|
31
|
+
} while (0)
|
32
|
+
|
33
|
+
extern VALUE cSHA3Digest;
|
34
|
+
extern VALUE eSHA3DigestError;
|
35
|
+
|
36
|
+
typedef struct
|
37
|
+
{
|
38
|
+
Keccak_HashInstance *state;
|
39
|
+
int hashbitlen;
|
40
|
+
} MDX;
|
41
|
+
|
42
|
+
void Init_sha3_n_digest(void);
|
43
|
+
|
44
|
+
#ifdef __cplusplus
|
37
45
|
}
|
38
46
|
#endif
|
39
47
|
|
40
|
-
#endif
|
48
|
+
#endif
|
data/ext/sha3/extconf.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
require 'mkmf'
|
2
2
|
require 'rbconfig'
|
3
3
|
|
4
|
-
FileUtils.rm "#{$srcdir}/KeccakF-1600-opt.c", :force => true
|
5
|
-
|
6
4
|
target_cpu = RbConfig::CONFIG['target_cpu']
|
7
5
|
|
8
6
|
if 1.size == 4 and target_cpu =~ /i386|x86_32/ # x86 32bit optimized code
|
9
|
-
Logging::message "=== Using
|
10
|
-
FileUtils.cp "#{$srcdir}/
|
7
|
+
Logging::message "=== Using reference ===\n"
|
8
|
+
FileUtils.cp Dir["#{$srcdir}/Reference/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
|
11
9
|
elsif 1.size == 8 and target_cpu =~ /i686|x86_64/
|
12
|
-
Logging::message "=== Using
|
13
|
-
FileUtils.cp "#{$srcdir}/
|
14
|
-
else
|
15
|
-
Logging::message "=== Using reference
|
16
|
-
FileUtils.cp "#{$srcdir}/
|
10
|
+
Logging::message "=== Using optimized (64-bit) ===\n"
|
11
|
+
FileUtils.cp Dir["#{$srcdir}/Optimized64/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
|
12
|
+
else
|
13
|
+
Logging::message "=== Using reference ===\n"
|
14
|
+
FileUtils.cp Dir["#{$srcdir}/Reference/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
|
17
15
|
end
|
18
16
|
|
19
|
-
find_header("KeccakF-1600-interface.h")
|
20
|
-
find_header("KeccakSponge.h")
|
21
|
-
find_header("KeccakNISTInterface.h")
|
22
17
|
find_header("sha3.h")
|
23
18
|
find_header("digest.h")
|
24
19
|
|
25
|
-
$CFLAGS = ' -fomit-frame-pointer -O3 -g0
|
20
|
+
$CFLAGS = ' -fomit-frame-pointer -O3 -g0'
|
21
|
+
|
22
|
+
if enable_config('march-tune-native', false)
|
23
|
+
$CFLAGS += ' -march=native'
|
24
|
+
end
|
25
|
+
|
26
26
|
create_makefile 'sha3_n'
|
data/ext/sha3/sha3.c
CHANGED
@@ -7,40 +7,56 @@ VALUE eSHA3Error;
|
|
7
7
|
|
8
8
|
int get_hlen(VALUE obj)
|
9
9
|
{
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
10
|
+
int hlen;
|
11
|
+
|
12
|
+
if (TYPE(obj) == T_SYMBOL)
|
13
|
+
{
|
14
|
+
ID symid;
|
15
|
+
|
16
|
+
symid = SYM2ID(obj);
|
17
|
+
|
18
|
+
if (rb_intern("sha224") == symid)
|
19
|
+
{
|
20
|
+
hlen = 224;
|
21
|
+
}
|
22
|
+
else if (rb_intern("sha256") == symid)
|
23
|
+
{
|
24
|
+
hlen = 256;
|
25
|
+
}
|
26
|
+
else if (rb_intern("sha384") == symid)
|
27
|
+
{
|
28
|
+
hlen = 384;
|
29
|
+
}
|
30
|
+
else if (rb_intern("sha512") == symid)
|
31
|
+
{
|
32
|
+
hlen = 512;
|
33
|
+
}
|
34
|
+
else
|
35
|
+
{
|
36
|
+
rb_raise(eSHA3Error, "invalid hash bit symbol (should be: :sha224, :sha256, :sha384, or :sha512");
|
37
|
+
}
|
38
|
+
}
|
39
|
+
else if (TYPE(obj) == T_FIXNUM)
|
40
|
+
{
|
41
|
+
hlen = NUM2INT(obj);
|
42
|
+
|
43
|
+
if ((hlen != 224) && (hlen != 256) && (hlen != 384) && (hlen != 512))
|
44
|
+
{
|
45
|
+
rb_raise(rb_eArgError, "invalid hash bit length (should be: 224, 256, 384, or 512)");
|
46
|
+
}
|
47
|
+
}
|
25
48
|
else
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
if ((hlen != 224) && (hlen != 256) && (hlen != 384) && (hlen != 512))
|
32
|
-
rb_raise(rb_eArgError, "invalid hash bit length (should be: 224, 256, 384, or 512)");
|
33
|
-
}
|
34
|
-
else
|
35
|
-
rb_raise(eSHA3Error, "unknown type value");
|
36
|
-
|
37
|
-
return hlen;
|
49
|
+
{
|
50
|
+
rb_raise(eSHA3Error, "unknown type value");
|
51
|
+
}
|
52
|
+
|
53
|
+
return hlen;
|
38
54
|
}
|
39
55
|
|
40
56
|
void Init_sha3_n()
|
41
57
|
{
|
42
|
-
|
43
|
-
|
58
|
+
mSHA3 = rb_define_module("SHA3");
|
59
|
+
eSHA3Error = rb_define_class_under(mSHA3, "SHA3Error", rb_eStandardError);
|
44
60
|
|
45
|
-
|
61
|
+
Init_sha3_n_digest();
|
46
62
|
}
|
data/ext/sha3/sha3.h
CHANGED
@@ -5,21 +5,22 @@
|
|
5
5
|
|
6
6
|
#include <ruby.h>
|
7
7
|
|
8
|
-
#include "
|
8
|
+
#include "KeccakHash.h"
|
9
9
|
#include "digest.h"
|
10
10
|
|
11
|
-
#ifdef
|
12
|
-
extern "C"
|
11
|
+
#ifdef __cplusplus
|
12
|
+
extern "C"
|
13
|
+
{
|
13
14
|
#endif
|
14
15
|
|
15
|
-
extern VALUE mSHA3;
|
16
|
-
extern VALUE eSHA3Error;
|
16
|
+
extern VALUE mSHA3;
|
17
|
+
extern VALUE eSHA3Error;
|
17
18
|
|
18
|
-
int get_hlen(VALUE);
|
19
|
-
void Init_sha3_n(void);
|
19
|
+
int get_hlen(VALUE);
|
20
|
+
void Init_sha3_n(void);
|
20
21
|
|
21
|
-
#ifdef
|
22
|
+
#ifdef __cplusplus
|
22
23
|
}
|
23
24
|
#endif
|
24
25
|
|
25
|
-
#endif
|
26
|
+
#endif
|