blake2b 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ # BLAKE2b for Ruby
2
+
3
+ BLAKE2 is a cryptographic hash function faster than MD5, SHA-1, SHA-2, and SHA-3, yet is at least as secure as the latest standard SHA-3. BLAKE2 has been adopted by many projects due to its high speed, security, and simplicity.
4
+
5
+ More info at: [https://blake2.net](https://blake2.net).
6
+
7
+ ## SUMMARY
8
+
9
+ This gem is a C-extension for using BLAKE2b in Ruby. BLAKE2b (or just BLAKE2) is optimized for 64-bit platforms—including NEON-enabled ARMs—and produces digests of any size between 1 and 64 bytes.
10
+
11
+ The C code for this gem is taken from the [official reference C implementation](https://github.com/BLAKE2/BLAKE2) as of commit [ca4c89314abff54e3806b44e4a08164f8204f09a](https://github.com/BLAKE2/BLAKE2/tree/ca4c89314abff54e3806b44e4a08164f8204f09a).
12
+
13
+ ## INSTALL
14
+
15
+ ```
16
+ gem install blake2b
17
+ ```
18
+
19
+ ## USAGE
20
+
21
+ ``` ruby
22
+ require 'blake2b'
23
+
24
+ # The UTF-8 String (Required) that you want to digest.
25
+ input = 'abc'
26
+
27
+ # The main application of keyed BLAKE2 is as a message authentication code (MAC)
28
+ # By default `Blake2b::Key.none` is used.
29
+ key = Blake2b::Key.none
30
+ # key = Blake2b::Key.from_string("foo bar baz")
31
+ # key = Blake2b::Key.from_hex('DEADBEAF')
32
+ # key = Blake2b::Key.from_bytes([222, 173, 190, 175])
33
+
34
+ # The output length in Bytes of the Hash, Max and Default is 32.
35
+ out_len = 32
36
+
37
+ # HEX OUTPUT
38
+ ############
39
+
40
+ Blake2b.hex(input)
41
+ => "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
42
+
43
+ Blake2b.hex(input, key)
44
+ => "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
45
+
46
+ Blake2b.hex(input, key, out_len)
47
+ => "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
48
+
49
+ # BYTES OUTPUT
50
+ ##############
51
+
52
+ Blake2b.bytes(input)
53
+ => [80, 140, 94, ...]
54
+
55
+ Blake2b.bytes(input, key)
56
+ => [80, 140, 94, ...]
57
+
58
+ Blake2b.bytes(input, key, out_len)
59
+ => [80, 140, 94, ...]
60
+
61
+ ```
62
+
63
+ ## DEVELOPMENT
64
+
65
+ After checking out the repo, run `bundle` to install dependencies. Then,
66
+ run `rake full` to build and test, or `rake test` to only run the tests.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`.
69
+
70
+ ## Future
71
+
72
+ Hopefully this gem will not be required once Ruby [issue #12802](https://bugs.ruby-lang.org/issues/12802) is resolved. Blake2 will either be included natively into MRI or available through the OpenSSL library.
73
+
74
+ ## License
75
+
76
+ Blake2b is based heavily on [Blake2](https://github.com/franckverrot/blake2) by Franck Verrot, Copyright 2014.
77
+
78
+ Blake2b is copyright 2018, Mauricio Gomes.
79
+
80
+ The original work (Blake2) and the modified work (Blake2b) are licensed GPL v3.0. See LICENSE.txt for details.
@@ -0,0 +1,46 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rake/extensiontask'
4
+
5
+ spec = Gem::Specification.load('blake2b.gemspec')
6
+
7
+ Rake::ExtensionTask.new('blake2b_ext', spec) do |ext|
8
+ ext.source_pattern = '*.{c,h}'
9
+ end
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ t.warning = true
16
+ end
17
+
18
+ task default: :full
19
+
20
+ desc 'clean, compile, and run the full test suite'
21
+ task full: %w(clean compile test)
22
+
23
+ def gemspec
24
+ @gemspec ||= begin
25
+ file = File.expand_path('../blake2b.gemspec', __FILE__)
26
+ eval(File.read(file), binding, file)
27
+ end
28
+ end
29
+
30
+ desc "Validate the gemspec"
31
+ task :gemspec do
32
+ gemspec.validate
33
+ end
34
+
35
+ desc "Build the gem"
36
+ task :gem => [:gemspec, :build] do
37
+ mkdir_p "pkg"
38
+ sh "gem build blake2b.gemspec"
39
+ mv "#{gemspec.full_name}.gem", "pkg"
40
+
41
+ require 'digest/sha2'
42
+ built_gem_path = "pkg/#{gemspec.full_name}.gem"
43
+ checksum = Digest::SHA512.new.hexdigest(File.read(built_gem_path))
44
+ checksum_path = "checksums/#{gemspec.version}.sha512"
45
+ File.open(checksum_path, 'w' ) {|f| f.write(checksum) }
46
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "blake2b"
4
+ spec.version = "0.9.0"
5
+ spec.authors = ["Franck Verrot", "Mauricio Gomes"]
6
+ spec.email = ["mauricio@edge14.com"]
7
+ spec.homepage = "https://github.com/mgomes/blake2b"
8
+ spec.license = "GPL-3.0"
9
+
10
+ spec.summary = "A cryptographic hash function faster than MD5, SHA-1, SHA-2, and SHA-3 for 64-bit systems."
11
+ spec.required_ruby_version = ">= 2.1.0"
12
+ spec.description = "A cryptographic hash function faster than MD5, SHA-1, SHA-2, and SHA-3 for 64-bit systems."
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+
15
+ spec.extensions << "ext/blake2b_ext/extconf.rb"
16
+
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "rake-compiler", "~> 0.9"
21
+ spec.add_development_dependency "bundler" , "~> 1.5"
22
+ spec.add_development_dependency "rake" , "~> 11.1"
23
+ spec.add_development_dependency "minitest" , "~> 5.11"
24
+ end
@@ -0,0 +1,160 @@
1
+ /*
2
+ BLAKE2 reference source code package - reference C implementations
3
+
4
+ Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5
+ terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6
+ your option. The terms of these licenses can be found at:
7
+
8
+ - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9
+ - OpenSSL license : https://www.openssl.org/source/license.html
10
+ - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ More information about the BLAKE2 hash function can be found at
13
+ https://blake2.net.
14
+ */
15
+ #ifndef BLAKE2_IMPL_H
16
+ #define BLAKE2_IMPL_H
17
+
18
+ #include <stdint.h>
19
+ #include <string.h>
20
+
21
+ #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
22
+ #if defined(_MSC_VER)
23
+ #define BLAKE2_INLINE __inline
24
+ #elif defined(__GNUC__)
25
+ #define BLAKE2_INLINE __inline__
26
+ #else
27
+ #define BLAKE2_INLINE
28
+ #endif
29
+ #else
30
+ #define BLAKE2_INLINE inline
31
+ #endif
32
+
33
+ static BLAKE2_INLINE uint32_t load32( const void *src )
34
+ {
35
+ #if defined(NATIVE_LITTLE_ENDIAN)
36
+ uint32_t w;
37
+ memcpy(&w, src, sizeof w);
38
+ return w;
39
+ #else
40
+ const uint8_t *p = ( const uint8_t * )src;
41
+ return (( uint32_t )( p[0] ) << 0) |
42
+ (( uint32_t )( p[1] ) << 8) |
43
+ (( uint32_t )( p[2] ) << 16) |
44
+ (( uint32_t )( p[3] ) << 24) ;
45
+ #endif
46
+ }
47
+
48
+ static BLAKE2_INLINE uint64_t load64( const void *src )
49
+ {
50
+ #if defined(NATIVE_LITTLE_ENDIAN)
51
+ uint64_t w;
52
+ memcpy(&w, src, sizeof w);
53
+ return w;
54
+ #else
55
+ const uint8_t *p = ( const uint8_t * )src;
56
+ return (( uint64_t )( p[0] ) << 0) |
57
+ (( uint64_t )( p[1] ) << 8) |
58
+ (( uint64_t )( p[2] ) << 16) |
59
+ (( uint64_t )( p[3] ) << 24) |
60
+ (( uint64_t )( p[4] ) << 32) |
61
+ (( uint64_t )( p[5] ) << 40) |
62
+ (( uint64_t )( p[6] ) << 48) |
63
+ (( uint64_t )( p[7] ) << 56) ;
64
+ #endif
65
+ }
66
+
67
+ static BLAKE2_INLINE uint16_t load16( const void *src )
68
+ {
69
+ #if defined(NATIVE_LITTLE_ENDIAN)
70
+ uint16_t w;
71
+ memcpy(&w, src, sizeof w);
72
+ return w;
73
+ #else
74
+ const uint8_t *p = ( const uint8_t * )src;
75
+ return (( uint16_t )( p[0] ) << 0) |
76
+ (( uint16_t )( p[1] ) << 8) ;
77
+ #endif
78
+ }
79
+
80
+ static BLAKE2_INLINE void store16( void *dst, uint16_t w )
81
+ {
82
+ #if defined(NATIVE_LITTLE_ENDIAN)
83
+ memcpy(dst, &w, sizeof w);
84
+ #else
85
+ uint8_t *p = ( uint8_t * )dst;
86
+ *p++ = ( uint8_t )w; w >>= 8;
87
+ *p++ = ( uint8_t )w;
88
+ #endif
89
+ }
90
+
91
+ static BLAKE2_INLINE void store32( void *dst, uint32_t w )
92
+ {
93
+ #if defined(NATIVE_LITTLE_ENDIAN)
94
+ memcpy(dst, &w, sizeof w);
95
+ #else
96
+ uint8_t *p = ( uint8_t * )dst;
97
+ p[0] = (uint8_t)(w >> 0);
98
+ p[1] = (uint8_t)(w >> 8);
99
+ p[2] = (uint8_t)(w >> 16);
100
+ p[3] = (uint8_t)(w >> 24);
101
+ #endif
102
+ }
103
+
104
+ static BLAKE2_INLINE void store64( void *dst, uint64_t w )
105
+ {
106
+ #if defined(NATIVE_LITTLE_ENDIAN)
107
+ memcpy(dst, &w, sizeof w);
108
+ #else
109
+ uint8_t *p = ( uint8_t * )dst;
110
+ p[0] = (uint8_t)(w >> 0);
111
+ p[1] = (uint8_t)(w >> 8);
112
+ p[2] = (uint8_t)(w >> 16);
113
+ p[3] = (uint8_t)(w >> 24);
114
+ p[4] = (uint8_t)(w >> 32);
115
+ p[5] = (uint8_t)(w >> 40);
116
+ p[6] = (uint8_t)(w >> 48);
117
+ p[7] = (uint8_t)(w >> 56);
118
+ #endif
119
+ }
120
+
121
+ static BLAKE2_INLINE uint64_t load48( const void *src )
122
+ {
123
+ const uint8_t *p = ( const uint8_t * )src;
124
+ return (( uint64_t )( p[0] ) << 0) |
125
+ (( uint64_t )( p[1] ) << 8) |
126
+ (( uint64_t )( p[2] ) << 16) |
127
+ (( uint64_t )( p[3] ) << 24) |
128
+ (( uint64_t )( p[4] ) << 32) |
129
+ (( uint64_t )( p[5] ) << 40) ;
130
+ }
131
+
132
+ static BLAKE2_INLINE void store48( void *dst, uint64_t w )
133
+ {
134
+ uint8_t *p = ( uint8_t * )dst;
135
+ p[0] = (uint8_t)(w >> 0);
136
+ p[1] = (uint8_t)(w >> 8);
137
+ p[2] = (uint8_t)(w >> 16);
138
+ p[3] = (uint8_t)(w >> 24);
139
+ p[4] = (uint8_t)(w >> 32);
140
+ p[5] = (uint8_t)(w >> 40);
141
+ }
142
+
143
+ static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c )
144
+ {
145
+ return ( w >> c ) | ( w << ( 32 - c ) );
146
+ }
147
+
148
+ static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c )
149
+ {
150
+ return ( w >> c ) | ( w << ( 64 - c ) );
151
+ }
152
+
153
+ /* prevents compiler optimizing out memset() */
154
+ static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n)
155
+ {
156
+ static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
157
+ memset_v(v, 0, n);
158
+ }
159
+
160
+ #endif
@@ -0,0 +1,195 @@
1
+ /*
2
+ BLAKE2 reference source code package - reference C implementations
3
+
4
+ Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5
+ terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6
+ your option. The terms of these licenses can be found at:
7
+
8
+ - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9
+ - OpenSSL license : https://www.openssl.org/source/license.html
10
+ - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ More information about the BLAKE2 hash function can be found at
13
+ https://blake2.net.
14
+ */
15
+ #ifndef BLAKE2_H
16
+ #define BLAKE2_H
17
+
18
+ #include <stddef.h>
19
+ #include <stdint.h>
20
+
21
+ #if defined(_MSC_VER)
22
+ #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
23
+ #else
24
+ #define BLAKE2_PACKED(x) x __attribute__((packed))
25
+ #endif
26
+
27
+ #if defined(__cplusplus)
28
+ extern "C" {
29
+ #endif
30
+
31
+ enum blake2s_constant
32
+ {
33
+ BLAKE2S_BLOCKBYTES = 64,
34
+ BLAKE2S_OUTBYTES = 32,
35
+ BLAKE2S_KEYBYTES = 32,
36
+ BLAKE2S_SALTBYTES = 8,
37
+ BLAKE2S_PERSONALBYTES = 8
38
+ };
39
+
40
+ enum blake2b_constant
41
+ {
42
+ BLAKE2B_BLOCKBYTES = 128,
43
+ BLAKE2B_OUTBYTES = 64,
44
+ BLAKE2B_KEYBYTES = 64,
45
+ BLAKE2B_SALTBYTES = 16,
46
+ BLAKE2B_PERSONALBYTES = 16
47
+ };
48
+
49
+ typedef struct blake2s_state__
50
+ {
51
+ uint32_t h[8];
52
+ uint32_t t[2];
53
+ uint32_t f[2];
54
+ uint8_t buf[BLAKE2S_BLOCKBYTES];
55
+ size_t buflen;
56
+ size_t outlen;
57
+ uint8_t last_node;
58
+ } blake2s_state;
59
+
60
+ typedef struct blake2b_state__
61
+ {
62
+ uint64_t h[8];
63
+ uint64_t t[2];
64
+ uint64_t f[2];
65
+ uint8_t buf[BLAKE2B_BLOCKBYTES];
66
+ size_t buflen;
67
+ size_t outlen;
68
+ uint8_t last_node;
69
+ } blake2b_state;
70
+
71
+ typedef struct blake2sp_state__
72
+ {
73
+ blake2s_state S[8][1];
74
+ blake2s_state R[1];
75
+ uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
76
+ size_t buflen;
77
+ size_t outlen;
78
+ } blake2sp_state;
79
+
80
+ typedef struct blake2bp_state__
81
+ {
82
+ blake2b_state S[4][1];
83
+ blake2b_state R[1];
84
+ uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
85
+ size_t buflen;
86
+ size_t outlen;
87
+ } blake2bp_state;
88
+
89
+
90
+ BLAKE2_PACKED(struct blake2s_param__
91
+ {
92
+ uint8_t digest_length; /* 1 */
93
+ uint8_t key_length; /* 2 */
94
+ uint8_t fanout; /* 3 */
95
+ uint8_t depth; /* 4 */
96
+ uint32_t leaf_length; /* 8 */
97
+ uint32_t node_offset; /* 12 */
98
+ uint16_t xof_length; /* 14 */
99
+ uint8_t node_depth; /* 15 */
100
+ uint8_t inner_length; /* 16 */
101
+ /* uint8_t reserved[0]; */
102
+ uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */
103
+ uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
104
+ });
105
+
106
+ typedef struct blake2s_param__ blake2s_param;
107
+
108
+ BLAKE2_PACKED(struct blake2b_param__
109
+ {
110
+ uint8_t digest_length; /* 1 */
111
+ uint8_t key_length; /* 2 */
112
+ uint8_t fanout; /* 3 */
113
+ uint8_t depth; /* 4 */
114
+ uint32_t leaf_length; /* 8 */
115
+ uint32_t node_offset; /* 12 */
116
+ uint32_t xof_length; /* 16 */
117
+ uint8_t node_depth; /* 17 */
118
+ uint8_t inner_length; /* 18 */
119
+ uint8_t reserved[14]; /* 32 */
120
+ uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
121
+ uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
122
+ });
123
+
124
+ typedef struct blake2b_param__ blake2b_param;
125
+
126
+ typedef struct blake2xs_state__
127
+ {
128
+ blake2s_state S[1];
129
+ blake2s_param P[1];
130
+ } blake2xs_state;
131
+
132
+ typedef struct blake2xb_state__
133
+ {
134
+ blake2b_state S[1];
135
+ blake2b_param P[1];
136
+ } blake2xb_state;
137
+
138
+ /* Padded structs result in a compile-time error */
139
+ enum {
140
+ BLAKE2_DUMMY_1 = 1/(sizeof(blake2s_param) == BLAKE2S_OUTBYTES),
141
+ BLAKE2_DUMMY_2 = 1/(sizeof(blake2b_param) == BLAKE2B_OUTBYTES)
142
+ };
143
+
144
+ /* Streaming API */
145
+ int blake2s_init( blake2s_state *S, size_t outlen );
146
+ int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
147
+ int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
148
+ int blake2s_update( blake2s_state *S, const void *in, size_t inlen );
149
+ int blake2s_final( blake2s_state *S, void *out, size_t outlen );
150
+
151
+ int blake2b_init( blake2b_state *S, size_t outlen );
152
+ int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
153
+ int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
154
+ int blake2b_update( blake2b_state *S, const void *in, size_t inlen );
155
+ int blake2b_final( blake2b_state *S, void *out, size_t outlen );
156
+
157
+ int blake2sp_init( blake2sp_state *S, size_t outlen );
158
+ int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
159
+ int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen );
160
+ int blake2sp_final( blake2sp_state *S, void *out, size_t outlen );
161
+
162
+ int blake2bp_init( blake2bp_state *S, size_t outlen );
163
+ int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
164
+ int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen );
165
+ int blake2bp_final( blake2bp_state *S, void *out, size_t outlen );
166
+
167
+ /* Variable output length API */
168
+ int blake2xs_init( blake2xs_state *S, const size_t outlen );
169
+ int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen );
170
+ int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen );
171
+ int blake2xs_final(blake2xs_state *S, void *out, size_t outlen);
172
+
173
+ int blake2xb_init( blake2xb_state *S, const size_t outlen );
174
+ int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen );
175
+ int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen );
176
+ int blake2xb_final(blake2xb_state *S, void *out, size_t outlen);
177
+
178
+ /* Simple API */
179
+ int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
180
+ int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
181
+
182
+ int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
183
+ int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
184
+
185
+ int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
186
+ int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
187
+
188
+ /* This is simply an alias for blake2b */
189
+ int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
190
+
191
+ #if defined(__cplusplus)
192
+ }
193
+ #endif
194
+
195
+ #endif