blake2 0.2.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4250cbe6cd64da1a6b5a975ad4723dc257cb08b3
4
- data.tar.gz: 06f89ae5b24e3a63536cf7718490c12624e0b05b
3
+ metadata.gz: ebb3461da94a57c4dd9ebeaed5555d4b11b0d720
4
+ data.tar.gz: 3784c55e622aa55dd12d07b5e7eb95c3271e73cb
5
5
  SHA512:
6
- metadata.gz: 6c963fd93b7772aa6c39e476b635568b10d521959b67b8d6ee1019707b2a18e3c66c9b09c2047f009785caf47fac9069ce40669fde646909fbceeac3a564e00a
7
- data.tar.gz: 37fefa3f64d105717495ac62c10a12b3f85e551146c8d455be2d0e3577a41e3cc2fdd7d8cc21b27cc80ef8bcd0df71556c2f2a007bb01dc6fa46edf5873adf6b
6
+ metadata.gz: c55e7c376319578fe76410cb85dfa2f0e9c32d071292e27100bf12c4e963928fe8c38dda15375d83e5f4554485e150100fa1bd2ce3a72e2957bccf6b296011c8
7
+ data.tar.gz: 21b309d84d0195f062452c086857fc387be0a928ca5234a029ee083eb8a62c00089e65f98da8516bc06bfd64701f720f380daec4412cd6796398a9f764562359
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,4 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.1.0
4
+ - 2.2.4
5
+ - 2.3.0
6
+ - ruby-head
4
7
  script: rake
@@ -1,6 +1,18 @@
1
1
  # Releases
2
2
 
3
- ## UNRELEASED (0.3.0)
3
+ ## 0.5.0
4
+
5
+ * Locked development dependencies in the gemspec
6
+
7
+ ## 0.4.0 (yanked)
8
+
9
+ * Signed commits and tags
10
+ * I (Franck) changed my PGP key since 0.3.0
11
+
12
+ ## 0.3.0 (yanked)
13
+
14
+ * Another pass at memory leaks
15
+ * Performance non-regression tests
4
16
 
5
17
  ## 0.2.0
6
18
 
data/README.md CHANGED
@@ -1,36 +1,90 @@
1
- # BLAKE2 for Ruby
1
+ # BLAKE2s for Ruby
2
2
 
3
3
  ## SUMMARY
4
4
 
5
- This gem is a C-extension for using BLAKE2 in Ruby.
5
+ This gem is a C-extension for using BLAKE2s in Ruby.
6
6
 
7
- For a detailed explanation about BLAKE2, [here's the offical website](https://blake2.net/).
7
+ >BLAKE2 comes in two basic flavors:
8
+ >
9
+ >BLAKE2b (or just BLAKE2) is optimized for 64-bit platforms and
10
+ >produces digests of any size between 1 and 64 bytes.
11
+ >
12
+ >BLAKE2s is optimized for 8- to 32-bit platforms and produces
13
+ >digests of any size between 1 and 32 bytes.
14
+ >
15
+ >Both BLAKE2b and BLAKE2s are believed to be highly secure and perform
16
+ >well on any platform, software, or hardware. BLAKE2 does not require
17
+ >a special "HMAC" (Hashed Message Authentication Code) construction
18
+ >for keyed message authentication as it has a built-in keying
19
+ >mechanism.
20
+ >
21
+ >[https://tools.ietf.org/html/rfc7693#page-3](https://tools.ietf.org/html/rfc7693#page-3)
8
22
 
9
- ## INSTALL
23
+ This implementation supports the BLAKE2s variant with 32 Bytes of output.
24
+
25
+ The C code for this gem is taken from the [official reference C implementation](https://github.com/BLAKE2/BLAKE2)
26
+ as of commit `02bf34f3d49c205812c34dfce9123a7c74509605`.
27
+
28
+ For a detailed explanation about BLAKE2s, [here's the offical website](https://blake2.net/).
10
29
 
11
- gem install blake2
30
+ ## INSTALL
12
31
 
32
+ ```
33
+ gem install blake2
34
+ ```
13
35
 
14
36
  ## USAGE
15
37
 
16
- out_len = 32
17
- input = "hello world"
18
- key = Key.from_string("foo bar baz") # or `Key.none`, or `Key.from_hex("0xDEADBEAF")`
38
+ ``` ruby
39
+ require 'blake2'
40
+
41
+ # The UTF-8 String (Required) that you want to digest.
42
+ input = 'abc'
43
+
44
+ # The main application of keyed BLAKE2 is as a message authentication code (MAC)
45
+ # By default `Blake2::Key.none` is used.
46
+ key = Blake2::Key.none
47
+ # key = Blake2::Key.from_string("foo bar baz")
48
+ # key = Blake2::Key.from_hex('DEADBEAF')
49
+ # key = Blake2::Key.from_bytes([222, 173, 190, 175])
50
+
51
+ # The output length in Bytes of the Hash, Max and Default is 32.
52
+ out_len = 32
53
+
54
+ # HEX OUTPUT
55
+ ############
56
+
57
+ Blake2.hex(input)
58
+ => "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
59
+
60
+ Blake2.hex(input, key)
61
+ => "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
62
+
63
+ Blake2.hex(input, key, out_len)
64
+ => "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
65
+
66
+ # BYTES OUTPUT
67
+ ##############
19
68
 
20
- digestor = Blake2.new(out_len, key)
69
+ Blake2.bytes(input)
70
+ => [80, 140, 94, ...]
21
71
 
22
- digestor.digest(input, :to_hex) # => 9567...b180
23
- digestor.digest(input, :to_bytes) # => [0x95, 0x67, <28 bytes later...>, 0xb1, 0x80]
72
+ Blake2.bytes(input, key)
73
+ => [80, 140, 94, ...]
24
74
 
75
+ Blake2.bytes(input, key, out_len)
76
+ => [80, 140, 94, ...]
25
77
 
26
- ## API
78
+ ```
27
79
 
28
- TODO
80
+ ## DEVELOPMENT
29
81
 
30
- ## TODO
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
83
+ run `rake full` to build and test, or `rake test` to only run the tests.
84
+ You can also run `bin/console` for an interactive prompt that will allow you
85
+ to experiment.
31
86
 
32
- * [ ] Documentation
33
- * [ ] Improve controls/type checks in the `digest` methods
87
+ To install this gem onto your local machine, run `bundle exec rake install`.
34
88
 
35
89
  ## CONTRIBUTE
36
90
 
data/Rakefile CHANGED
@@ -1,18 +1,46 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
1
3
  require 'rake/extensiontask'
4
+
2
5
  spec = Gem::Specification.load('blake2.gemspec')
6
+
3
7
  Rake::ExtensionTask.new('blake2_ext', spec) do |ext|
4
- ext.source_pattern = "*.{c,h}"
8
+ ext.source_pattern = '*.{c,h}'
5
9
  end
6
10
 
7
- require 'rake/testtask'
8
11
  Rake::TestTask.new do |t|
9
- t.libs << "test"
10
- t.pattern = "test/**/*_test.rb"
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
11
14
  t.verbose = true
12
15
  t.warning = true
13
16
  end
14
17
 
15
- task :default => :full
18
+ task default: :full
19
+
20
+ desc 'clean, compile, and run the full test suite'
21
+ task full: %w(clean compile test)
16
22
 
17
- desc "Run the full spec suite"
18
- task :full => ["clean", "compile", "test"]
23
+ def gemspec
24
+ @gemspec ||= begin
25
+ file = File.expand_path('../blake2.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 blake2.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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'blake2'
5
+ require 'pry'
6
+
7
+ Pry.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -1,25 +1,28 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = "blake2"
4
- spec.version = "0.2.0"
4
+ spec.version = "0.5.0"
5
5
  spec.authors = ["Franck Verrot"]
6
6
  spec.email = ["franck@verrot.fr"]
7
7
  spec.homepage = "https://github.com/franckverrot/blake2"
8
- spec.license = "GPLv3"
8
+ spec.license = "GPL-3.0"
9
9
 
10
10
  spec.summary = "BLAKE2 - fast secure hashing - for Ruby"
11
- spec.description = spec.summary
12
- spec.files = `git ls-files -z`.split("\x0")
11
+ spec.required_ruby_version = ">= 2.1.0"
12
+ spec.description = "BLAKE2 is a C-extension for using BLAKE2s in Ruby"
13
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ spec.bindir = "bin"
13
15
 
14
16
  spec.extensions << "ext/blake2_ext/extconf.rb"
15
17
 
16
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
19
  spec.require_paths = ["lib"]
18
20
 
19
- spec.add_development_dependency "rake-compiler"
20
- spec.add_development_dependency "bundler", "~> 1.5"
21
- spec.add_development_dependency "rake"
22
- spec.add_development_dependency "minitest"
21
+ spec.add_development_dependency "rake-compiler", "~> 0.9"
22
+ spec.add_development_dependency "bundler" , "~> 1.5"
23
+ spec.add_development_dependency "rake" , "~> 11.1"
24
+ spec.add_development_dependency "minitest" , "~> 5.8"
25
+ spec.add_development_dependency "pry" , "~> 0.10"
23
26
 
24
27
  spec.cert_chain = ['certs/franckverrot.pem']
25
28
  spec.signing_key = File.expand_path(ENV['RUBYGEMS_CERT_PATH']) if $0 =~ /gem\z/
@@ -0,0 +1 @@
1
+ db9a73c5bb1baea243f4b690023d2a990b2bbb5e5a9ab032b6f844cef9dfe8cb534bf2d121993e986518caeedb7263d67b1c7d9693739b234118f46ba4ae4df1
@@ -1,20 +1,23 @@
1
1
  /*
2
2
  BLAKE2 reference source code package - reference C implementations
3
-
4
- Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
5
-
6
- To the extent possible under law, the author(s) have dedicated all copyright
7
- and related and neighboring rights to this software to the public domain
8
- worldwide. This software is distributed without any warranty.
9
-
10
- You should have received a copy of the CC0 Public Domain Dedication along with
11
- this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
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.
12
14
  */
13
15
  #pragma once
14
16
  #ifndef __BLAKE2_IMPL_H__
15
17
  #define __BLAKE2_IMPL_H__
16
18
 
17
19
  #include <stdint.h>
20
+ #include <string.h>
18
21
 
19
22
  static inline uint32_t load32( const void *src )
20
23
  {
@@ -126,10 +129,10 @@ static inline uint64_t rotr64( const uint64_t w, const unsigned c )
126
129
  }
127
130
 
128
131
  /* prevents compiler optimizing out memset() */
129
- static inline void secure_zero_memory( void *v, size_t n )
132
+ static inline void secure_zero_memory(void *v, size_t n)
130
133
  {
131
- volatile uint8_t *p = ( volatile uint8_t * )v;
132
- while( n-- ) *p++ = 0;
134
+ static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
135
+ memset_v(v, 0, n);
133
136
  }
134
137
 
135
138
  #endif
@@ -1,14 +1,16 @@
1
1
  /*
2
2
  BLAKE2 reference source code package - reference C implementations
3
-
4
- Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
5
-
6
- To the extent possible under law, the author(s) have dedicated all copyright
7
- and related and neighboring rights to this software to the public domain
8
- worldwide. This software is distributed without any warranty.
9
-
10
- You should have received a copy of the CC0 Public Domain Dedication along with
11
- this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
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.
12
14
  */
13
15
  #pragma once
14
16
  #ifndef __BLAKE2_H__
@@ -17,12 +19,6 @@
17
19
  #include <stddef.h>
18
20
  #include <stdint.h>
19
21
 
20
- #if defined(_MSC_VER)
21
- #define ALIGN(x) __declspec(align(x))
22
- #else
23
- #define ALIGN(x) __attribute__((aligned(x)))
24
- #endif
25
-
26
22
  #if defined(__cplusplus)
27
23
  extern "C" {
28
24
  #endif
@@ -45,23 +41,7 @@ extern "C" {
45
41
  BLAKE2B_PERSONALBYTES = 16
46
42
  };
47
43
 
48
- #pragma pack(push, 1)
49
- typedef struct __blake2s_param
50
- {
51
- uint8_t digest_length; // 1
52
- uint8_t key_length; // 2
53
- uint8_t fanout; // 3
54
- uint8_t depth; // 4
55
- uint32_t leaf_length; // 8
56
- uint8_t node_offset[6];// 14
57
- uint8_t node_depth; // 15
58
- uint8_t inner_length; // 16
59
- // uint8_t reserved[0];
60
- uint8_t salt[BLAKE2S_SALTBYTES]; // 24
61
- uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
62
- } blake2s_param;
63
-
64
- ALIGN( 64 ) typedef struct __blake2s_state
44
+ typedef struct __blake2s_state
65
45
  {
66
46
  uint32_t h[8];
67
47
  uint32_t t[2];
@@ -69,24 +49,9 @@ extern "C" {
69
49
  uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
70
50
  size_t buflen;
71
51
  uint8_t last_node;
72
- } blake2s_state ;
73
-
74
- typedef struct __blake2b_param
75
- {
76
- uint8_t digest_length; // 1
77
- uint8_t key_length; // 2
78
- uint8_t fanout; // 3
79
- uint8_t depth; // 4
80
- uint32_t leaf_length; // 8
81
- uint64_t node_offset; // 16
82
- uint8_t node_depth; // 17
83
- uint8_t inner_length; // 18
84
- uint8_t reserved[14]; // 32
85
- uint8_t salt[BLAKE2B_SALTBYTES]; // 48
86
- uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
87
- } blake2b_param;
52
+ } blake2s_state;
88
53
 
89
- ALIGN( 64 ) typedef struct __blake2b_state
54
+ typedef struct __blake2b_state
90
55
  {
91
56
  uint64_t h[8];
92
57
  uint64_t t[2];
@@ -111,6 +76,38 @@ extern "C" {
111
76
  uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
112
77
  size_t buflen;
113
78
  } blake2bp_state;
79
+
80
+
81
+ #pragma pack(push, 1)
82
+ typedef struct __blake2s_param
83
+ {
84
+ uint8_t digest_length; // 1
85
+ uint8_t key_length; // 2
86
+ uint8_t fanout; // 3
87
+ uint8_t depth; // 4
88
+ uint32_t leaf_length; // 8
89
+ uint8_t node_offset[6];// 14
90
+ uint8_t node_depth; // 15
91
+ uint8_t inner_length; // 16
92
+ // uint8_t reserved[0];
93
+ uint8_t salt[BLAKE2S_SALTBYTES]; // 24
94
+ uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
95
+ } blake2s_param;
96
+
97
+ typedef struct __blake2b_param
98
+ {
99
+ uint8_t digest_length; // 1
100
+ uint8_t key_length; // 2
101
+ uint8_t fanout; // 3
102
+ uint8_t depth; // 4
103
+ uint32_t leaf_length; // 8
104
+ uint64_t node_offset; // 16
105
+ uint8_t node_depth; // 17
106
+ uint8_t inner_length; // 18
107
+ uint8_t reserved[14]; // 32
108
+ uint8_t salt[BLAKE2B_SALTBYTES]; // 48
109
+ uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
110
+ } blake2b_param;
114
111
  #pragma pack(pop)
115
112
 
116
113
  // Streaming API
@@ -1,14 +1,16 @@
1
1
  /*
2
2
  BLAKE2 reference source code package - reference C implementations
3
-
4
- Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
5
-
6
- To the extent possible under law, the author(s) have dedicated all copyright
7
- and related and neighboring rights to this software to the public domain
8
- worldwide. This software is distributed without any warranty.
9
-
10
- You should have received a copy of the CC0 Public Domain Dedication along with
11
- this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
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.
12
14
  */
13
15
 
14
16
  #include <stdint.h>
@@ -40,22 +42,27 @@ static const uint8_t blake2s_sigma[10][16] =
40
42
 
41
43
  static inline int blake2s_set_lastnode( blake2s_state *S )
42
44
  {
43
- S->f[1] = ~0U;
45
+ S->f[1] = -1;
44
46
  return 0;
45
47
  }
46
48
 
47
49
  static inline int blake2s_clear_lastnode( blake2s_state *S )
48
50
  {
49
- S->f[1] = 0U;
51
+ S->f[1] = 0;
50
52
  return 0;
51
53
  }
52
54
 
53
55
  /* Some helper functions, not necessarily useful */
56
+ static inline int blake2s_is_lastblock( const blake2s_state *S )
57
+ {
58
+ return S->f[0] != 0;
59
+ }
60
+
54
61
  static inline int blake2s_set_lastblock( blake2s_state *S )
55
62
  {
56
63
  if( S->last_node ) blake2s_set_lastnode( S );
57
64
 
58
- S->f[0] = ~0U;
65
+ S->f[0] = -1;
59
66
  return 0;
60
67
  }
61
68
 
@@ -63,7 +70,7 @@ static inline int blake2s_clear_lastblock( blake2s_state *S )
63
70
  {
64
71
  if( S->last_node ) blake2s_clear_lastnode( S );
65
72
 
66
- S->f[0] = 0U;
73
+ S->f[0] = 0;
67
74
  return 0;
68
75
  }
69
76
 
@@ -301,9 +308,13 @@ int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen )
301
308
  {
302
309
  uint8_t buffer[BLAKE2S_OUTBYTES] = {0};
303
310
 
304
- if( outlen > BLAKE2S_OUTBYTES )
311
+ if( out == NULL || outlen == 0 || outlen > BLAKE2S_OUTBYTES )
312
+ return -1;
313
+
314
+ if( blake2s_is_lastblock( S ) )
305
315
  return -1;
306
316
 
317
+
307
318
  if( S->buflen > BLAKE2S_BLOCKBYTES )
308
319
  {
309
320
  blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
@@ -329,11 +340,15 @@ int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen
329
340
  blake2s_state S[1];
330
341
 
331
342
  /* Verify parameters */
332
- if ( NULL == in ) return -1;
343
+ if ( NULL == in && inlen > 0 ) return -1;
333
344
 
334
345
  if ( NULL == out ) return -1;
335
346
 
336
- if ( NULL == key ) keylen = 0; /* Fail here instead if keylen != 0 and key == NULL? */
347
+ if ( NULL == key && keylen > 0) return -1;
348
+
349
+ if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
350
+
351
+ if( keylen > BLAKE2S_KEYBYTES ) return -1;
337
352
 
338
353
  if( keylen > 0 )
339
354
  {
@@ -349,6 +364,13 @@ int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen
349
364
  return 0;
350
365
  }
351
366
 
367
+ #if defined(SUPERCOP)
368
+ int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
369
+ {
370
+ return blake2s( out, in, NULL, BLAKE2S_OUTBYTES, inlen, 0 );
371
+ }
372
+ #endif
373
+
352
374
  #if defined(BLAKE2S_SELFTEST)
353
375
  #include <string.h>
354
376
  #include "blake2-kat.h"
@@ -18,15 +18,19 @@ VALUE cBlake2;
18
18
  static void blake2_free(Blake2 *blake2) {
19
19
  if(blake2->key_length > 0) {
20
20
  ruby_xfree(blake2->key_bytes);
21
+ blake2->key_length = 0;
21
22
  }
22
23
 
23
24
  if(blake2->output_length > 0) {
24
25
  ruby_xfree(blake2->output);
26
+ blake2->output_length = 0;
25
27
  }
28
+
29
+ free(blake2);
26
30
  }
27
31
 
28
- VALUE blake2_alloc(VALUE klass) {
29
- VALUE blake2_obj = ruby_xmalloc(sizeof(Blake2));
32
+ static VALUE blake2_alloc(VALUE klass) {
33
+ Blake2 *blake2_obj = (Blake2 *)malloc(sizeof(Blake2));
30
34
 
31
35
  return Data_Wrap_Struct(klass, NULL, blake2_free, blake2_obj);
32
36
  }
@@ -59,7 +63,7 @@ VALUE m_blake2_initialize(VALUE self, VALUE _len, VALUE _key) {
59
63
  VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
60
64
  Blake2 *blake2;
61
65
 
62
- char * input = RSTRING_PTR(_input);
66
+ char *input = RSTRING_PTR(_input);
63
67
  int input_length = RSTRING_LEN(_input);
64
68
  int i;
65
69
 
@@ -76,23 +80,27 @@ VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
76
80
  for(i = 0; i < blake2->output_length; i++) {
77
81
  rb_ary_push(result, INT2NUM(blake2->output[i]));
78
82
  }
79
-
80
- return result;
81
83
  } else if(_representation == blake2->to_hex) {
82
- char * c_str = (char*)ruby_xmalloc(blake2->output_length * sizeof(char) * 2);
84
+ int ary_len = blake2->output_length * sizeof(char) * 2;
85
+ char *c_str = (char*)malloc(ary_len + 1);
83
86
 
84
87
  for(i = 0; i < blake2->output_length; i++) {
85
88
  sprintf(c_str + (i * 2), "%02x", blake2->output[i]);
86
89
  }
90
+ c_str[ary_len] = 0;
87
91
 
88
- result = rb_str_new2(c_str);
92
+ result = rb_str_new(c_str, ary_len);
89
93
 
90
- ruby_xfree(c_str);
94
+ if(RSTRING_LEN(result) != ary_len) {
95
+ rb_raise(rb_eRuntimeError, "m_blake2_digest: sizes don't match. Ary: %d != %d", RSTRING_LEN(result), ary_len);
96
+ }
91
97
 
92
- return result;
98
+ free(c_str);
93
99
  } else {
94
- rb_raise(rb_eArgError, "Unknown representation", _representation);
100
+ rb_raise(rb_eArgError, "unknown representation :%"PRIsVALUE"", _representation);
95
101
  }
102
+
103
+ return result;
96
104
  }
97
105
 
98
106
  void Init_blake2_ext() {
@@ -1,2 +1,29 @@
1
1
  require 'blake2_ext'
2
2
  require 'blake2/key'
3
+
4
+ class Blake2
5
+ def self.hex(input, key = Blake2::Key.none, out_len = 32)
6
+ check_if_valid!(input, key, out_len)
7
+ Blake2.new(out_len, key).digest(input, :to_hex)
8
+ end
9
+
10
+ def self.bytes(input, key = Blake2::Key.none, out_len = 32)
11
+ check_if_valid!(input, key, out_len)
12
+ Blake2.new(out_len, key).digest(input, :to_bytes)
13
+ end
14
+
15
+ def self.check_if_valid!(input, key, out_len)
16
+ unless input.is_a?(String)
17
+ raise ArgumentError, 'input arg must be a String'
18
+ end
19
+
20
+ unless key.is_a?(Blake2::Key)
21
+ raise ArgumentError, 'key arg must be a Blake2::Key'
22
+ end
23
+
24
+ unless out_len.is_a?(Integer) && out_len.between?(1, 32)
25
+ raise ArgumentError, 'out_len arg must be an Integer between 1 and 32 inclusive'
26
+ end
27
+ end
28
+ private_class_method :check_if_valid!
29
+ end
@@ -1,23 +1,55 @@
1
1
  class Blake2
2
+ # Validate and normalize an HMAC key, provided in different formats,
3
+ # into an Array of Integer Bytes.
2
4
  class Key
3
- def bytes
4
- @bytes
5
- end
5
+ attr_reader :bytes
6
6
 
7
7
  def initialize(bytes)
8
8
  @bytes = bytes
9
9
  end
10
10
 
11
+ # Create a blank Key
12
+ #
13
+ # @return [Blake2::Key] a Blake2::Key object with a `bytes` attr
14
+ def self.none
15
+ new([])
16
+ end
17
+
18
+ # Create a key from an ASCII String
19
+ #
20
+ # @param str [String] an ASCII String key
21
+ # @return [Blake2::Key] a Blake2::Key object with a `bytes` attr
11
22
  def self.from_string(str)
12
- new(str.bytes)
23
+ if str.is_a?(String) && str.ascii_only?
24
+ new(str.bytes)
25
+ else
26
+ raise ArgumentError, 'key must be an ASCII String'
27
+ end
13
28
  end
14
29
 
15
- def self.from_hex(hex_str)
16
- new([hex_str].pack("H*").bytes)
30
+ # Create a key from a Hex String [a-fA-F0-9]
31
+ #
32
+ # @param str [String] a Hex String key
33
+ # @return [Blake2::Key] a Blake2::Key object with a `bytes` attr
34
+ def self.from_hex(str)
35
+ if str.is_a?(String) && str.match(/^[a-fA-F0-9]+$/)
36
+ new([str].pack('H*').bytes)
37
+ else
38
+ raise ArgumentError, 'key must be a Hex String [a-fA-F0-9]'
39
+ end
17
40
  end
18
41
 
19
- def self.none
20
- new([])
42
+ # Create a key from Array of Integer (0-255) Bytes.
43
+ # This simply validates and passes through the Array.
44
+ #
45
+ # @param str [Array] an Array of Integer (0-255) Bytes
46
+ # @return [Blake2::Key] a Blake2::Key object with a `bytes` attr
47
+ def self.from_bytes(bytes)
48
+ if bytes.all? { |b| b.is_a?(Integer) && b.between?(0, 255) }
49
+ new(bytes)
50
+ else
51
+ raise ArgumentError, 'key must be a Byte Array of Integers (0-255)'
52
+ end
21
53
  end
22
54
  end
23
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blake2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franck Verrot
@@ -30,22 +30,22 @@ cert_chain:
30
30
  Z7BoH/Mfxw3P4yyir173p9JWvVqLiQE13XBeD13NPA8neoWpLJxm6k03PcTElT/E
31
31
  QrOSgKrHAb/fFJma
32
32
  -----END CERTIFICATE-----
33
- date: 2016-05-13 00:00:00.000000000 Z
33
+ date: 2016-05-15 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rake-compiler
37
37
  requirement: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '0.9'
42
42
  type: :development
43
43
  prerelease: false
44
44
  version_requirements: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '0.9'
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: bundler
51
51
  requirement: !ruby/object:Gem::Requirement
@@ -64,31 +64,45 @@ dependencies:
64
64
  name: rake
65
65
  requirement: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '0'
69
+ version: '11.1'
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">="
74
+ - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: '0'
76
+ version: '11.1'
77
77
  - !ruby/object:Gem::Dependency
78
78
  name: minitest
79
79
  requirement: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ">="
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '5.8'
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '5.8'
91
+ - !ruby/object:Gem::Dependency
92
+ name: pry
93
+ requirement: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
82
96
  - !ruby/object:Gem::Version
83
- version: '0'
97
+ version: '0.10'
84
98
  type: :development
85
99
  prerelease: false
86
100
  version_requirements: !ruby/object:Gem::Requirement
87
101
  requirements:
88
- - - ">="
102
+ - - "~>"
89
103
  - !ruby/object:Gem::Version
90
- version: '0'
91
- description: BLAKE2 - fast secure hashing - for Ruby
104
+ version: '0.10'
105
+ description: BLAKE2 is a C-extension for using BLAKE2s in Ruby
92
106
  email:
93
107
  - franck@verrot.fr
94
108
  executables: []
@@ -103,9 +117,12 @@ files:
103
117
  - LICENSE.txt
104
118
  - README.md
105
119
  - Rakefile
120
+ - bin/console
121
+ - bin/setup
106
122
  - blake2.gemspec
107
123
  - certs/franckverrot.pem
108
124
  - checksums/0.1.0.sha512
125
+ - checksums/0.3.0.sha512
109
126
  - ext/blake2_ext/blake2-impl.h
110
127
  - ext/blake2_ext/blake2.h
111
128
  - ext/blake2_ext/blake2s-ref.c
@@ -113,12 +130,9 @@ files:
113
130
  - ext/blake2_ext/rbext.c
114
131
  - lib/blake2.rb
115
132
  - lib/blake2/key.rb
116
- - test/blake2/key_test.rb
117
- - test/blake2_test.rb
118
- - test/test_helper.rb
119
133
  homepage: https://github.com/franckverrot/blake2
120
134
  licenses:
121
- - GPLv3
135
+ - GPL-3.0
122
136
  metadata: {}
123
137
  post_install_message:
124
138
  rdoc_options: []
@@ -128,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
142
  requirements:
129
143
  - - ">="
130
144
  - !ruby/object:Gem::Version
131
- version: '0'
145
+ version: 2.1.0
132
146
  required_rubygems_version: !ruby/object:Gem::Requirement
133
147
  requirements:
134
148
  - - ">="
@@ -140,7 +154,4 @@ rubygems_version: 2.6.4
140
154
  signing_key:
141
155
  specification_version: 4
142
156
  summary: BLAKE2 - fast secure hashing - for Ruby
143
- test_files:
144
- - test/blake2/key_test.rb
145
- - test/blake2_test.rb
146
- - test/test_helper.rb
157
+ test_files: []
metadata.gz.sig CHANGED
Binary file
@@ -1,18 +0,0 @@
1
- require 'test_helper'
2
-
3
- class KeyTest < MiniTest::Test
4
- def test_from_string
5
- key = Blake2::Key.from_string("foo bar baz")
6
- assert_equal [102, 111, 111, 32, 98, 97, 114, 32, 98, 97, 122], key.bytes
7
- end
8
-
9
- def test_none
10
- key = Blake2::Key.none
11
- assert_equal [], key.bytes
12
- end
13
-
14
- def test_from_hex
15
- key = Blake2::Key.from_hex("DEADBEAF")
16
- assert_equal [0XDE, 0xAD, 0xBE, 0xAF], key.bytes
17
- end
18
- end
@@ -1,25 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Blake2Test < MiniTest::Test
4
- def setup
5
- out_len = 32
6
- key = Blake2::Key.from_string("foo bar baz") # or `Key.none`, or `Key.from_hex("0xDEADBEAF")`
7
-
8
- @digestor = Blake2.new(out_len, key)
9
-
10
- @input = "hello world"
11
- @expected = "95670379036532875f58bf23fbcb549675b656bd639a6124a614ccd8a980b180"
12
- end
13
-
14
- def test_to_hex
15
- res = @digestor.digest(@input, :to_hex) # => 9567...b180
16
- assert_kind_of String, res
17
- assert_equal @expected, res
18
- end
19
-
20
- def to_bytes
21
- res = @digestor.digest(@input, :to_bytes) # => [0x95, 0x67, <28 bytes later...>, 0xb1, 0x80]
22
- assert_kind_of Array, res
23
- assert_equal @expected.pack("H*").bytes, res
24
- end
25
- end
@@ -1,11 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'minitest/assertions'
3
- require 'pp'
4
-
5
- class Array
6
- def hd
7
- self.each.map { |b| sprintf("%X", b) }.join
8
- end
9
- end
10
-
11
- require 'blake2'