keccak 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,117 @@
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 "displayIntermediateValues.h"
16
+ #include "KeccakNISTInterface.h"
17
+
18
+ FILE *intermediateValueFile = 0;
19
+ int displayLevel = 0;
20
+
21
+ void displaySetIntermediateValueFile(FILE *f)
22
+ {
23
+ intermediateValueFile = f;
24
+ }
25
+
26
+ void displaySetLevel(int level)
27
+ {
28
+ displayLevel = level;
29
+ }
30
+
31
+ void displayBytes(int level, const char *text, const unsigned char *bytes, unsigned int size)
32
+ {
33
+ unsigned int i;
34
+
35
+ if ((intermediateValueFile) && (level <= displayLevel)) {
36
+ fprintf(intermediateValueFile, "%s:\n", text);
37
+ for(i=0; i<size; i++)
38
+ fprintf(intermediateValueFile, "%02X ", bytes[i]);
39
+ fprintf(intermediateValueFile, "\n");
40
+ fprintf(intermediateValueFile, "\n");
41
+ }
42
+ }
43
+
44
+ void displayBits(int level, const char *text, const unsigned char *data, unsigned int size, int MSBfirst)
45
+ {
46
+ unsigned int i, iByte, iBit;
47
+
48
+ if ((intermediateValueFile) && (level <= displayLevel)) {
49
+ fprintf(intermediateValueFile, "%s:\n", text);
50
+ for(i=0; i<size; i++) {
51
+ iByte = i/8;
52
+ iBit = i%8;
53
+ if (MSBfirst)
54
+ fprintf(intermediateValueFile, "%d ", ((data[iByte] << iBit) & 0x80) != 0);
55
+ else
56
+ fprintf(intermediateValueFile, "%d ", ((data[iByte] >> iBit) & 0x01) != 0);
57
+ }
58
+ fprintf(intermediateValueFile, "\n");
59
+ fprintf(intermediateValueFile, "\n");
60
+ }
61
+ }
62
+
63
+ void displayStateAsBytes(int level, const char *text, const unsigned char *state)
64
+ {
65
+ displayBytes(level, text, state, KeccakPermutationSizeInBytes);
66
+ }
67
+
68
+ void displayStateAs32bitWords(int level, const char *text, const unsigned int *state)
69
+ {
70
+ unsigned int i;
71
+
72
+ if ((intermediateValueFile) && (level <= displayLevel)) {
73
+ fprintf(intermediateValueFile, "%s:\n", text);
74
+ for(i=0; i<KeccakPermutationSize/64; i++) {
75
+ fprintf(intermediateValueFile, "%08X:%08X", (unsigned int)state[2*i+0], (unsigned int)state[2*i+1]);
76
+ if ((i%5) == 4)
77
+ fprintf(intermediateValueFile, "\n");
78
+ else
79
+ fprintf(intermediateValueFile, " ");
80
+ }
81
+ }
82
+ }
83
+
84
+ void displayStateAs64bitWords(int level, const char *text, const unsigned long long int *state)
85
+ {
86
+ unsigned int i;
87
+
88
+ if ((intermediateValueFile) && (level <= displayLevel)) {
89
+ fprintf(intermediateValueFile, "%s:\n", text);
90
+ for(i=0; i<KeccakPermutationSize/64; i++) {
91
+ fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] >> 32));
92
+ fprintf(intermediateValueFile, "%08X", (unsigned int)(state[i] & 0xFFFFFFFFULL));
93
+ if ((i%5) == 4)
94
+ fprintf(intermediateValueFile, "\n");
95
+ else
96
+ fprintf(intermediateValueFile, " ");
97
+ }
98
+ }
99
+ }
100
+
101
+ void displayRoundNumber(int level, unsigned int i)
102
+ {
103
+ if ((intermediateValueFile) && (level <= displayLevel)) {
104
+ fprintf(intermediateValueFile, "\n");
105
+ fprintf(intermediateValueFile, "--- Round %d ---\n", i);
106
+ fprintf(intermediateValueFile, "\n");
107
+ }
108
+ }
109
+
110
+ void displayText(int level, const char *text)
111
+ {
112
+ if ((intermediateValueFile) && (level <= displayLevel)) {
113
+ fprintf(intermediateValueFile, "%s", text);
114
+ fprintf(intermediateValueFile, "\n");
115
+ fprintf(intermediateValueFile, "\n");
116
+ }
117
+ }
@@ -0,0 +1,29 @@
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 _displayIntermediateValues_h_
15
+ #define _displayIntermediateValues_h_
16
+
17
+ #include <stdio.h>
18
+
19
+ void displaySetIntermediateValueFile(FILE *f);
20
+ void displaySetLevel(int level);
21
+ void displayBytes(int level, const char *text, const unsigned char *bytes, unsigned int size);
22
+ void displayBits(int level, const char *text, const unsigned char *data, unsigned int size, int MSBfirst);
23
+ void displayStateAsBytes(int level, const char *text, const unsigned char *state);
24
+ void displayStateAs32bitWords(int level, const char *text, const unsigned int *state);
25
+ void displayStateAs64bitWords(int level, const char *text, const unsigned long long int *state);
26
+ void displayRoundNumber(int level, unsigned int i);
27
+ void displayText(int level, const char *text);
28
+
29
+ #endif
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ have_header('ruby/digest.h')
4
+ have_func('rb_str_set_len')
5
+
6
+ $CFLAGS << " -fvisibility=hidden"
7
+ create_makefile('digest/sha3')
data/ext/digest/sha3.c ADDED
@@ -0,0 +1,153 @@
1
+ #include "ruby.h"
2
+ #ifdef HAVE_RUBY_DIGEST_H
3
+ #include "ruby/digest.h"
4
+ #else
5
+ #include "digest.h"
6
+ #endif
7
+ #include "KeccakNISTInterface.h"
8
+
9
+ #define MAX_DIGEST_SIZE 64
10
+ #define DEFAULT_DIGEST_LEN 512
11
+
12
+ static int sha3_init_func(hashState *ctx);
13
+ static void sha3_update_func(hashState *ctx, unsigned char *str, size_t len);
14
+ static int sha3_finish_func(hashState *ctx, unsigned char *digest);
15
+
16
+ /*
17
+ Metadata definition for the SHA3 algorithm.
18
+ Defines the Version, sizes for block and digest as well as
19
+ the entry points for the algorithms
20
+ */
21
+ static rb_digest_metadata_t sha3 = {
22
+ RUBY_DIGEST_API_VERSION,
23
+ DEFAULT_DIGEST_LEN,
24
+ KeccakPermutationSize - (2 * DEFAULT_DIGEST_LEN), //size of blocks
25
+ sizeof(hashState), //size of context for the object we'll be passed in below functions.
26
+ (rb_digest_hash_init_func_t)sha3_init_func,
27
+ (rb_digest_hash_update_func_t)sha3_update_func,
28
+ (rb_digest_hash_finish_func_t)sha3_finish_func,
29
+ };
30
+
31
+ /* Initialization function for the algorithm,
32
+ gets called during allocation of the digest object.
33
+ we override initialize to do custom hash size, so we don't care too much here.
34
+ */
35
+ static int
36
+ sha3_init_func(hashState *ctx) {
37
+ // Just return a 1 ' successful' we override the init function
38
+ // so this is not necessary
39
+ // the base class alloc calls this to initialize the algorithm
40
+ return 1;
41
+ }
42
+
43
+ /* Update function, take the current context and add str to it */
44
+ static void
45
+ sha3_update_func(hashState *ctx, unsigned char *str, size_t len) {
46
+ Update(ctx, str, len * 8);
47
+ }
48
+
49
+ /* Finish the hash calculation and return the finished string */
50
+ static int
51
+ sha3_finish_func(hashState *ctx, unsigned char *digest) {
52
+ Final(ctx, digest);
53
+ return 1;
54
+ }
55
+
56
+ /* Ruby method. Digest::SHA3#finish()
57
+ * No Arguments
58
+ * @returns [String] Encoded Digest String
59
+ */
60
+ static VALUE
61
+ rb_sha3_finish(VALUE self) {
62
+ hashState *ctx;
63
+ VALUE digest;
64
+
65
+ ctx = (hashState *)RTYPEDDATA_DATA(self);
66
+ digest = rb_str_new(0, ctx->capacity / 2 / 8);
67
+ sha3_finish_func(ctx, (unsigned char *)RSTRING_PTR(digest));
68
+
69
+ return digest;
70
+ }
71
+
72
+ /* :nodoc: private method
73
+ * initialize the ctx with the bitlength
74
+ */
75
+ static void
76
+ sha3_init(hashState *ctx, size_t bitlen) {
77
+ switch (Init(ctx, bitlen)) {
78
+ case SUCCESS:
79
+ return;
80
+ case FAIL:
81
+ rb_raise(rb_eRuntimeError, "Unknown error");
82
+ case BAD_HASHLEN:
83
+ rb_raise(rb_eArgError, "Bad hash length (must be 0, 224, 256, 384 or 512)");
84
+ default:
85
+ rb_raise(rb_eRuntimeError, "Unknown error code");
86
+ }
87
+ }
88
+
89
+ /* Ruby method. Digest::SHA3.new(hashlen)
90
+ * @param hashlen The length of hash, only supports 224, 256, 384 or 512
91
+ * @returns [Digest::SHA3] new object.
92
+ */
93
+ static VALUE
94
+ rb_sha3_initialize(int argc, VALUE *argv, VALUE self) {
95
+ hashState *ctx;
96
+ VALUE hashlen;
97
+ int i_hashlen;
98
+
99
+ if (rb_scan_args(argc, argv, "01", &hashlen) == 0) {
100
+ i_hashlen = DEFAULT_DIGEST_LEN;
101
+ } else {
102
+ i_hashlen = NUM2INT(hashlen);
103
+ }
104
+ if ( i_hashlen == 0) {
105
+ rb_raise(rb_eArgError, "Unsupported hash length");
106
+ }
107
+
108
+ ctx = (hashState *)RTYPEDDATA_DATA(self);
109
+ sha3_init(ctx, i_hashlen);
110
+
111
+ return rb_call_super(0, NULL);
112
+ }
113
+
114
+ /* Ruby method. Digest::SHA3#digest_length
115
+ * @returns [Numeric] Length of the digest.
116
+ */
117
+ static VALUE
118
+ rb_sha3_digest_length(VALUE self) {
119
+ hashState *ctx;
120
+
121
+ ctx = (hashState *)RTYPEDDATA_DATA(self);
122
+ return INT2FIX(ctx->capacity / 2 / 8);
123
+ }
124
+
125
+ /* Ruby method. Digest::SHA3#block_length
126
+ * @returns [Numeric] Length of blocks in this digest.
127
+ */
128
+ static VALUE
129
+ rb_sha3_block_length(VALUE self) {
130
+ hashState *ctx;
131
+
132
+ ctx = (hashState *)RTYPEDDATA_DATA(self);
133
+ return INT2FIX(ctx->rate / 8);
134
+ }
135
+
136
+ void __attribute__((visibility("default")))
137
+ Init_sha3() {
138
+ VALUE mDigest, cDigest_Base, cSHA3;
139
+
140
+ rb_require("digest");
141
+
142
+ mDigest = rb_path2class("Digest");
143
+ cDigest_Base = rb_path2class("Digest::Base");
144
+
145
+ cSHA3 = rb_define_class_under(mDigest, "SHA3", cDigest_Base);
146
+
147
+ rb_iv_set(cSHA3, "metadata", Data_Wrap_Struct(0, 0, 0, (void *)&sha3));
148
+
149
+ rb_define_method(cSHA3, "initialize", rb_sha3_initialize, -1);
150
+ rb_define_method(cSHA3, "digest_length", rb_sha3_digest_length, 0);
151
+ rb_define_method(cSHA3, "block_length", rb_sha3_block_length, 0);
152
+ rb_define_method(cSHA3, "finish", rb_sha3_finish, 0);
153
+ }
data/keccak.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ require File.expand_path('lib/digest/sha3/version')
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "keccak"
5
+ s.version = Digest::SHA3::Version::STRING
6
+ s.summary = "The Keccak (SHA-3) hash used by Ethereum."
7
+ s.email = "ruby@q9f.cc"
8
+ s.homepage = "https://github.com/q9f/keccak.rb"
9
+ s.description = "The Keccak (SHA-3) hash use by Ethereum. This does not implement the final FIPS202 standard, today known as SHA3 but rather an early version, commonly referred to as Keccak."
10
+ s.authors = ["Afri Schoedon", "Chris Metcalfe", "Hongli Lai (Phusion)", "Keccak authors"]
11
+ s.extensions << "ext/digest/extconf.rb"
12
+ s.required_ruby_version = ">= 2.2"
13
+ s.license = "Apache-2.0"
14
+
15
+ s.files = Dir[
16
+ "README.md",
17
+ "COPYRIGHT",
18
+ "LICENSE",
19
+ "Makefile",
20
+ "keccak.gemspec",
21
+ "ext/**/*.{c,h,rb}",
22
+ "lib/**/*"
23
+ ]
24
+ end
@@ -0,0 +1,7 @@
1
+ module Digest
2
+ class SHA3
3
+ module Version
4
+ STRING = "1.2.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keccak
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Afri Schoedon
8
+ - Chris Metcalfe
9
+ - Hongli Lai (Phusion)
10
+ - Keccak authors
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2021-09-30 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: The Keccak (SHA-3) hash use by Ethereum. This does not implement the
17
+ final FIPS202 standard, today known as SHA3 but rather an early version, commonly
18
+ referred to as Keccak.
19
+ email: ruby@q9f.cc
20
+ executables: []
21
+ extensions:
22
+ - ext/digest/extconf.rb
23
+ extra_rdoc_files: []
24
+ files:
25
+ - COPYRIGHT
26
+ - LICENSE
27
+ - Makefile
28
+ - README.md
29
+ - ext/digest/KeccakF-1600-int-set.h
30
+ - ext/digest/KeccakF-1600-interface.h
31
+ - ext/digest/KeccakF-1600-reference.c
32
+ - ext/digest/KeccakNISTInterface.c
33
+ - ext/digest/KeccakNISTInterface.h
34
+ - ext/digest/KeccakSponge.c
35
+ - ext/digest/KeccakSponge.h
36
+ - ext/digest/brg_endian.h
37
+ - ext/digest/displayIntermediateValues.c
38
+ - ext/digest/displayIntermediateValues.h
39
+ - ext/digest/extconf.rb
40
+ - ext/digest/sha3.c
41
+ - keccak.gemspec
42
+ - lib/digest/sha3/version.rb
43
+ homepage: https://github.com/q9f/keccak.rb
44
+ licenses:
45
+ - Apache-2.0
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '2.2'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.2.27
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: The Keccak (SHA-3) hash used by Ethereum.
66
+ test_files: []