digest-blake2b 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/.circleci/config.yml +11 -33
- data/.gitignore +2 -1
- data/README.md +3 -1
- data/Rakefile +3 -2
- data/digest-blake2b.gemspec +2 -1
- data/ext/digest/blake2b/ref/blake2-impl.h +160 -0
- data/ext/digest/blake2b/ref/blake2.h +195 -0
- data/ext/digest/blake2b/ref/blake2b-ref.c +379 -0
- data/ext/digest/blake2b/ref/extconf.rb +7 -0
- data/ext/digest/blake2b/ref/rbext.c +111 -0
- data/ext/digest/blake2b/{ext → sse}/blake2-config.h +0 -0
- data/ext/digest/blake2b/{ext → sse}/blake2-impl.h +0 -0
- data/ext/digest/blake2b/{ext → sse}/blake2.h +0 -0
- data/ext/digest/blake2b/{ext → sse}/blake2b-load-sse2.h +0 -0
- data/ext/digest/blake2b/{ext → sse}/blake2b-load-sse41.h +0 -0
- data/ext/digest/blake2b/{ext → sse}/blake2b-ref.c +0 -0
- data/ext/digest/blake2b/{ext → sse}/blake2b-round.h +0 -0
- data/ext/digest/blake2b/sse/extconf.rb +7 -0
- data/ext/digest/blake2b/{ext → sse}/rbext.c +1 -1
- data/lib/digest/blake2b/impl.rb +11 -0
- data/lib/digest/blake2b/version.rb +1 -1
- data/lib/digest/blake2b.rb +3 -2
- metadata +18 -12
- data/ext/digest/blake2b/ext/extconf.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 664bd156e4290bfe01d6977508732cdaef1b950d0f1a5d632165106c4670f11a
|
4
|
+
data.tar.gz: 392f665ea5dca923c1d05f4f7aa2b2b0fc4e853e115d8cf4a7b884ce335b1f83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8daabe85e4c187b61fc65edb6dffb23ee6c6bbc68199521f8a2b4eafdec7c6d35c090a48941ef17748c1e87481771dbe4124d9c8ea6b5a6213f45da6b2cda481
|
7
|
+
data.tar.gz: 6434d4681b9a4d500ec861315512cec3e1233a5f7afaf376f85a999875d23259b03b1405fd8907b47675bb106ce1499f3a06d70664c13ffb7ba3248d840e684d
|
data/.circleci/config.yml
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
version: 2
|
1
|
+
version: 2.1
|
2
2
|
|
3
3
|
jobs:
|
4
|
-
|
4
|
+
ruby_2_6_x86_64:
|
5
5
|
docker:
|
6
|
-
- image:
|
6
|
+
- image: ruby:2.6
|
7
7
|
environment:
|
8
8
|
STEALTH_ENV: test
|
9
9
|
|
@@ -12,30 +12,19 @@ jobs:
|
|
12
12
|
steps:
|
13
13
|
- checkout
|
14
14
|
|
15
|
-
# Download and cache dependencies
|
16
|
-
- restore_cache:
|
17
|
-
keys:
|
18
|
-
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
19
|
-
# fallback to using the latest cache if no exact match is found
|
20
|
-
- v1-dependencies-
|
21
|
-
|
22
15
|
- run:
|
23
16
|
name: install dependencies
|
24
17
|
command: |
|
18
|
+
gem install bundler
|
25
19
|
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
26
20
|
|
27
|
-
- save_cache:
|
28
|
-
paths:
|
29
|
-
- ./vendor/bundle
|
30
|
-
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
31
|
-
|
32
21
|
# run tests!
|
33
22
|
- run:
|
34
23
|
name: run tests
|
35
24
|
command: |
|
36
25
|
mkdir /tmp/test-results
|
37
26
|
|
38
|
-
bundle exec rake
|
27
|
+
bundle exec rake full
|
39
28
|
|
40
29
|
# collect reports
|
41
30
|
- store_test_results:
|
@@ -43,39 +32,28 @@ jobs:
|
|
43
32
|
- store_artifacts:
|
44
33
|
path: /tmp/test-results
|
45
34
|
destination: test-results
|
46
|
-
|
35
|
+
ruby_2_7_x86_64:
|
47
36
|
docker:
|
48
|
-
- image:
|
37
|
+
- image: ruby:2.7
|
49
38
|
|
50
39
|
working_directory: ~/repo
|
51
40
|
|
52
41
|
steps:
|
53
42
|
- checkout
|
54
43
|
|
55
|
-
# Download and cache dependencies
|
56
|
-
- restore_cache:
|
57
|
-
keys:
|
58
|
-
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
59
|
-
# fallback to using the latest cache if no exact match is found
|
60
|
-
- v1-dependencies-
|
61
|
-
|
62
44
|
- run:
|
63
45
|
name: install dependencies
|
64
46
|
command: |
|
47
|
+
gem install bundler
|
65
48
|
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
66
49
|
|
67
|
-
- save_cache:
|
68
|
-
paths:
|
69
|
-
- ./vendor/bundle
|
70
|
-
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
71
|
-
|
72
50
|
# run tests!
|
73
51
|
- run:
|
74
52
|
name: run tests
|
75
53
|
command: |
|
76
54
|
mkdir /tmp/test-results
|
77
55
|
|
78
|
-
bundle exec rake
|
56
|
+
bundle exec rake full
|
79
57
|
|
80
58
|
# collect reports
|
81
59
|
- store_test_results:
|
@@ -88,5 +66,5 @@ workflows:
|
|
88
66
|
version: 2
|
89
67
|
build:
|
90
68
|
jobs:
|
91
|
-
-
|
92
|
-
-
|
69
|
+
- ruby_2_6_x86_64
|
70
|
+
- ruby_2_7_x86_64
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
Digest::Blake2b
|
2
2
|
===============
|
3
3
|
|
4
|
+
[](https://circleci.com/gh/kotovalexarian/digest-blake2b/tree/master)
|
5
|
+
|
4
6
|
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.
|
5
7
|
|
6
8
|
More info at: [https://blake2.net](https://blake2.net).
|
7
9
|
|
8
10
|
## Summary
|
9
11
|
|
10
|
-
This gem is a C-extension to enable using BLAKE2b in Ruby. This BLAKE2b implementation (or just BLAKE2)
|
12
|
+
This gem is a C-extension to enable using BLAKE2b in Ruby. This reference BLAKE2b implementation (or just BLAKE2). For 64-bit x86 platforms with SSE support optimized implementation is used. It produces digests of any size between 1 and 64 bytes.
|
11
13
|
|
12
14
|
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).
|
13
15
|
|
data/Rakefile
CHANGED
@@ -6,8 +6,9 @@ require 'rake/extensiontask'
|
|
6
6
|
|
7
7
|
spec = Gem::Specification.load('digest-blake2b.gemspec')
|
8
8
|
|
9
|
-
Rake::ExtensionTask.new
|
9
|
+
Rake::ExtensionTask.new "digest/blake2b/#{Digest::Blake2b::IMPL}", spec do |ext|
|
10
10
|
ext.source_pattern = '*.{c,h}'
|
11
|
+
ext.ext_dir = "ext/digest/blake2b/#{Digest::Blake2b::IMPL}"
|
11
12
|
end
|
12
13
|
|
13
14
|
Rake::TestTask.new do |t|
|
@@ -20,7 +21,7 @@ end
|
|
20
21
|
task default: :full
|
21
22
|
|
22
23
|
desc 'clean, compile, and run the full test suite'
|
23
|
-
task full: %
|
24
|
+
task full: %i[clean compile test]
|
24
25
|
|
25
26
|
def gemspec
|
26
27
|
@gemspec ||= begin
|
data/digest-blake2b.gemspec
CHANGED
@@ -4,6 +4,7 @@ lib = File.expand_path('lib', __dir__).freeze
|
|
4
4
|
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
5
5
|
|
6
6
|
require 'digest/blake2b/version'
|
7
|
+
require 'digest/blake2b/impl'
|
7
8
|
|
8
9
|
Gem::Specification.new do |spec|
|
9
10
|
spec.name = 'digest-blake2b'
|
@@ -41,7 +42,7 @@ Gem::Specification.new do |spec|
|
|
41
42
|
|
42
43
|
spec.executables = spec.files.grep %r{^exe/}, &File.method(:basename)
|
43
44
|
|
44
|
-
spec.extensions <<
|
45
|
+
spec.extensions << "ext/digest/blake2b/#{Digest::Blake2b::IMPL}/extconf.rb"
|
45
46
|
|
46
47
|
spec.add_development_dependency 'bundler' , '~> 2.2'
|
47
48
|
spec.add_development_dependency 'minitest' , '~> 5.11'
|
@@ -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
|
@@ -0,0 +1,379 @@
|
|
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
|
+
|
16
|
+
#include <stdint.h>
|
17
|
+
#include <string.h>
|
18
|
+
#include <stdio.h>
|
19
|
+
|
20
|
+
#include "blake2.h"
|
21
|
+
#include "blake2-impl.h"
|
22
|
+
|
23
|
+
static const uint64_t blake2b_IV[8] =
|
24
|
+
{
|
25
|
+
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
|
26
|
+
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
|
27
|
+
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
|
28
|
+
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
|
29
|
+
};
|
30
|
+
|
31
|
+
static const uint8_t blake2b_sigma[12][16] =
|
32
|
+
{
|
33
|
+
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
|
34
|
+
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
|
35
|
+
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
|
36
|
+
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
|
37
|
+
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
|
38
|
+
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
|
39
|
+
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
|
40
|
+
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
|
41
|
+
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
|
42
|
+
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
|
43
|
+
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
|
44
|
+
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
|
45
|
+
};
|
46
|
+
|
47
|
+
|
48
|
+
static void blake2b_set_lastnode( blake2b_state *S )
|
49
|
+
{
|
50
|
+
S->f[1] = (uint64_t)-1;
|
51
|
+
}
|
52
|
+
|
53
|
+
/* Some helper functions, not necessarily useful */
|
54
|
+
static int blake2b_is_lastblock( const blake2b_state *S )
|
55
|
+
{
|
56
|
+
return S->f[0] != 0;
|
57
|
+
}
|
58
|
+
|
59
|
+
static void blake2b_set_lastblock( blake2b_state *S )
|
60
|
+
{
|
61
|
+
if( S->last_node ) blake2b_set_lastnode( S );
|
62
|
+
|
63
|
+
S->f[0] = (uint64_t)-1;
|
64
|
+
}
|
65
|
+
|
66
|
+
static void blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
|
67
|
+
{
|
68
|
+
S->t[0] += inc;
|
69
|
+
S->t[1] += ( S->t[0] < inc );
|
70
|
+
}
|
71
|
+
|
72
|
+
static void blake2b_init0( blake2b_state *S )
|
73
|
+
{
|
74
|
+
size_t i;
|
75
|
+
memset( S, 0, sizeof( blake2b_state ) );
|
76
|
+
|
77
|
+
for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
|
78
|
+
}
|
79
|
+
|
80
|
+
/* init xors IV with input parameter block */
|
81
|
+
int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
|
82
|
+
{
|
83
|
+
const uint8_t *p = ( const uint8_t * )( P );
|
84
|
+
size_t i;
|
85
|
+
|
86
|
+
blake2b_init0( S );
|
87
|
+
|
88
|
+
/* IV XOR ParamBlock */
|
89
|
+
for( i = 0; i < 8; ++i )
|
90
|
+
S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
|
91
|
+
|
92
|
+
S->outlen = P->digest_length;
|
93
|
+
return 0;
|
94
|
+
}
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
int blake2b_init( blake2b_state *S, size_t outlen )
|
99
|
+
{
|
100
|
+
blake2b_param P[1];
|
101
|
+
|
102
|
+
if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
|
103
|
+
|
104
|
+
P->digest_length = (uint8_t)outlen;
|
105
|
+
P->key_length = 0;
|
106
|
+
P->fanout = 1;
|
107
|
+
P->depth = 1;
|
108
|
+
store32( &P->leaf_length, 0 );
|
109
|
+
store32( &P->node_offset, 0 );
|
110
|
+
store32( &P->xof_length, 0 );
|
111
|
+
P->node_depth = 0;
|
112
|
+
P->inner_length = 0;
|
113
|
+
memset( P->reserved, 0, sizeof( P->reserved ) );
|
114
|
+
memset( P->salt, 0, sizeof( P->salt ) );
|
115
|
+
memset( P->personal, 0, sizeof( P->personal ) );
|
116
|
+
return blake2b_init_param( S, P );
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen )
|
121
|
+
{
|
122
|
+
blake2b_param P[1];
|
123
|
+
|
124
|
+
if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
|
125
|
+
|
126
|
+
if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
|
127
|
+
|
128
|
+
P->digest_length = (uint8_t)outlen;
|
129
|
+
P->key_length = (uint8_t)keylen;
|
130
|
+
P->fanout = 1;
|
131
|
+
P->depth = 1;
|
132
|
+
store32( &P->leaf_length, 0 );
|
133
|
+
store32( &P->node_offset, 0 );
|
134
|
+
store32( &P->xof_length, 0 );
|
135
|
+
P->node_depth = 0;
|
136
|
+
P->inner_length = 0;
|
137
|
+
memset( P->reserved, 0, sizeof( P->reserved ) );
|
138
|
+
memset( P->salt, 0, sizeof( P->salt ) );
|
139
|
+
memset( P->personal, 0, sizeof( P->personal ) );
|
140
|
+
|
141
|
+
if( blake2b_init_param( S, P ) < 0 ) return -1;
|
142
|
+
|
143
|
+
{
|
144
|
+
uint8_t block[BLAKE2B_BLOCKBYTES];
|
145
|
+
memset( block, 0, BLAKE2B_BLOCKBYTES );
|
146
|
+
memcpy( block, key, keylen );
|
147
|
+
blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
|
148
|
+
secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
|
149
|
+
}
|
150
|
+
return 0;
|
151
|
+
}
|
152
|
+
|
153
|
+
#define G(r,i,a,b,c,d) \
|
154
|
+
do { \
|
155
|
+
a = a + b + m[blake2b_sigma[r][2*i+0]]; \
|
156
|
+
d = rotr64(d ^ a, 32); \
|
157
|
+
c = c + d; \
|
158
|
+
b = rotr64(b ^ c, 24); \
|
159
|
+
a = a + b + m[blake2b_sigma[r][2*i+1]]; \
|
160
|
+
d = rotr64(d ^ a, 16); \
|
161
|
+
c = c + d; \
|
162
|
+
b = rotr64(b ^ c, 63); \
|
163
|
+
} while(0)
|
164
|
+
|
165
|
+
#define ROUND(r) \
|
166
|
+
do { \
|
167
|
+
G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
|
168
|
+
G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
|
169
|
+
G(r,2,v[ 2],v[ 6],v[10],v[14]); \
|
170
|
+
G(r,3,v[ 3],v[ 7],v[11],v[15]); \
|
171
|
+
G(r,4,v[ 0],v[ 5],v[10],v[15]); \
|
172
|
+
G(r,5,v[ 1],v[ 6],v[11],v[12]); \
|
173
|
+
G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
|
174
|
+
G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
|
175
|
+
} while(0)
|
176
|
+
|
177
|
+
static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
|
178
|
+
{
|
179
|
+
uint64_t m[16];
|
180
|
+
uint64_t v[16];
|
181
|
+
size_t i;
|
182
|
+
|
183
|
+
for( i = 0; i < 16; ++i ) {
|
184
|
+
m[i] = load64( block + i * sizeof( m[i] ) );
|
185
|
+
}
|
186
|
+
|
187
|
+
for( i = 0; i < 8; ++i ) {
|
188
|
+
v[i] = S->h[i];
|
189
|
+
}
|
190
|
+
|
191
|
+
v[ 8] = blake2b_IV[0];
|
192
|
+
v[ 9] = blake2b_IV[1];
|
193
|
+
v[10] = blake2b_IV[2];
|
194
|
+
v[11] = blake2b_IV[3];
|
195
|
+
v[12] = blake2b_IV[4] ^ S->t[0];
|
196
|
+
v[13] = blake2b_IV[5] ^ S->t[1];
|
197
|
+
v[14] = blake2b_IV[6] ^ S->f[0];
|
198
|
+
v[15] = blake2b_IV[7] ^ S->f[1];
|
199
|
+
|
200
|
+
ROUND( 0 );
|
201
|
+
ROUND( 1 );
|
202
|
+
ROUND( 2 );
|
203
|
+
ROUND( 3 );
|
204
|
+
ROUND( 4 );
|
205
|
+
ROUND( 5 );
|
206
|
+
ROUND( 6 );
|
207
|
+
ROUND( 7 );
|
208
|
+
ROUND( 8 );
|
209
|
+
ROUND( 9 );
|
210
|
+
ROUND( 10 );
|
211
|
+
ROUND( 11 );
|
212
|
+
|
213
|
+
for( i = 0; i < 8; ++i ) {
|
214
|
+
S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
#undef G
|
219
|
+
#undef ROUND
|
220
|
+
|
221
|
+
int blake2b_update( blake2b_state *S, const void *pin, size_t inlen )
|
222
|
+
{
|
223
|
+
const unsigned char * in = (const unsigned char *)pin;
|
224
|
+
if( inlen > 0 )
|
225
|
+
{
|
226
|
+
size_t left = S->buflen;
|
227
|
+
size_t fill = BLAKE2B_BLOCKBYTES - left;
|
228
|
+
if( inlen > fill )
|
229
|
+
{
|
230
|
+
S->buflen = 0;
|
231
|
+
memcpy( S->buf + left, in, fill ); /* Fill buffer */
|
232
|
+
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
233
|
+
blake2b_compress( S, S->buf ); /* Compress */
|
234
|
+
in += fill; inlen -= fill;
|
235
|
+
while(inlen > BLAKE2B_BLOCKBYTES) {
|
236
|
+
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
|
237
|
+
blake2b_compress( S, in );
|
238
|
+
in += BLAKE2B_BLOCKBYTES;
|
239
|
+
inlen -= BLAKE2B_BLOCKBYTES;
|
240
|
+
}
|
241
|
+
}
|
242
|
+
memcpy( S->buf + S->buflen, in, inlen );
|
243
|
+
S->buflen += inlen;
|
244
|
+
}
|
245
|
+
return 0;
|
246
|
+
}
|
247
|
+
|
248
|
+
int blake2b_final( blake2b_state *S, void *out, size_t outlen )
|
249
|
+
{
|
250
|
+
uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
|
251
|
+
size_t i;
|
252
|
+
|
253
|
+
if( out == NULL || outlen < S->outlen )
|
254
|
+
return -1;
|
255
|
+
|
256
|
+
if( blake2b_is_lastblock( S ) )
|
257
|
+
return -1;
|
258
|
+
|
259
|
+
blake2b_increment_counter( S, S->buflen );
|
260
|
+
blake2b_set_lastblock( S );
|
261
|
+
memset( S->buf + S->buflen, 0, BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */
|
262
|
+
blake2b_compress( S, S->buf );
|
263
|
+
|
264
|
+
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
|
265
|
+
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
|
266
|
+
|
267
|
+
memcpy( out, buffer, S->outlen );
|
268
|
+
secure_zero_memory(buffer, sizeof(buffer));
|
269
|
+
return 0;
|
270
|
+
}
|
271
|
+
|
272
|
+
/* inlen, at least, should be uint64_t. Others can be size_t. */
|
273
|
+
int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen )
|
274
|
+
{
|
275
|
+
blake2b_state S[1];
|
276
|
+
|
277
|
+
/* Verify parameters */
|
278
|
+
if ( NULL == in && inlen > 0 ) return -1;
|
279
|
+
|
280
|
+
if ( NULL == out ) return -1;
|
281
|
+
|
282
|
+
if( NULL == key && keylen > 0 ) return -1;
|
283
|
+
|
284
|
+
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
|
285
|
+
|
286
|
+
if( keylen > BLAKE2B_KEYBYTES ) return -1;
|
287
|
+
|
288
|
+
if( keylen > 0 )
|
289
|
+
{
|
290
|
+
if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
|
291
|
+
}
|
292
|
+
else
|
293
|
+
{
|
294
|
+
if( blake2b_init( S, outlen ) < 0 ) return -1;
|
295
|
+
}
|
296
|
+
|
297
|
+
blake2b_update( S, ( const uint8_t * )in, inlen );
|
298
|
+
blake2b_final( S, out, outlen );
|
299
|
+
return 0;
|
300
|
+
}
|
301
|
+
|
302
|
+
int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) {
|
303
|
+
return blake2b(out, outlen, in, inlen, key, keylen);
|
304
|
+
}
|
305
|
+
|
306
|
+
#if defined(SUPERCOP)
|
307
|
+
int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
|
308
|
+
{
|
309
|
+
return blake2b( out, BLAKE2B_OUTBYTES, in, inlen, NULL, 0 );
|
310
|
+
}
|
311
|
+
#endif
|
312
|
+
|
313
|
+
#if defined(BLAKE2B_SELFTEST)
|
314
|
+
#include <string.h>
|
315
|
+
#include "blake2-kat.h"
|
316
|
+
int main( void )
|
317
|
+
{
|
318
|
+
uint8_t key[BLAKE2B_KEYBYTES];
|
319
|
+
uint8_t buf[BLAKE2_KAT_LENGTH];
|
320
|
+
size_t i, step;
|
321
|
+
|
322
|
+
for( i = 0; i < BLAKE2B_KEYBYTES; ++i )
|
323
|
+
key[i] = ( uint8_t )i;
|
324
|
+
|
325
|
+
for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
|
326
|
+
buf[i] = ( uint8_t )i;
|
327
|
+
|
328
|
+
/* Test simple API */
|
329
|
+
for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
|
330
|
+
{
|
331
|
+
uint8_t hash[BLAKE2B_OUTBYTES];
|
332
|
+
blake2b( hash, BLAKE2B_OUTBYTES, buf, i, key, BLAKE2B_KEYBYTES );
|
333
|
+
|
334
|
+
if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
|
335
|
+
{
|
336
|
+
goto fail;
|
337
|
+
}
|
338
|
+
}
|
339
|
+
|
340
|
+
/* Test streaming API */
|
341
|
+
for(step = 1; step < BLAKE2B_BLOCKBYTES; ++step) {
|
342
|
+
for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) {
|
343
|
+
uint8_t hash[BLAKE2B_OUTBYTES];
|
344
|
+
blake2b_state S;
|
345
|
+
uint8_t * p = buf;
|
346
|
+
size_t mlen = i;
|
347
|
+
int err = 0;
|
348
|
+
|
349
|
+
if( (err = blake2b_init_key(&S, BLAKE2B_OUTBYTES, key, BLAKE2B_KEYBYTES)) < 0 ) {
|
350
|
+
goto fail;
|
351
|
+
}
|
352
|
+
|
353
|
+
while (mlen >= step) {
|
354
|
+
if ( (err = blake2b_update(&S, p, step)) < 0 ) {
|
355
|
+
goto fail;
|
356
|
+
}
|
357
|
+
mlen -= step;
|
358
|
+
p += step;
|
359
|
+
}
|
360
|
+
if ( (err = blake2b_update(&S, p, mlen)) < 0) {
|
361
|
+
goto fail;
|
362
|
+
}
|
363
|
+
if ( (err = blake2b_final(&S, hash, BLAKE2B_OUTBYTES)) < 0) {
|
364
|
+
goto fail;
|
365
|
+
}
|
366
|
+
|
367
|
+
if (0 != memcmp(hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES)) {
|
368
|
+
goto fail;
|
369
|
+
}
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
puts( "ok" );
|
374
|
+
return 0;
|
375
|
+
fail:
|
376
|
+
puts("error");
|
377
|
+
return -1;
|
378
|
+
}
|
379
|
+
#endif
|
@@ -0,0 +1,111 @@
|
|
1
|
+
#include <ruby/ruby.h>
|
2
|
+
#include <ruby/encoding.h>
|
3
|
+
#include "blake2.h"
|
4
|
+
|
5
|
+
typedef struct {
|
6
|
+
size_t key_length;
|
7
|
+
uint8_t *key_bytes;
|
8
|
+
|
9
|
+
size_t output_length;
|
10
|
+
uint8_t *output;
|
11
|
+
|
12
|
+
VALUE to_hex;
|
13
|
+
VALUE to_bytes;
|
14
|
+
} Blake2;
|
15
|
+
|
16
|
+
VALUE mDigest;
|
17
|
+
VALUE mDigest_cBlake2;
|
18
|
+
|
19
|
+
static void blake2_free(Blake2 *blake2) {
|
20
|
+
free(blake2->key_bytes);
|
21
|
+
free(blake2->output);
|
22
|
+
|
23
|
+
rb_gc_mark(blake2->to_hex);
|
24
|
+
rb_gc_mark(blake2->to_bytes);
|
25
|
+
|
26
|
+
ruby_xfree(blake2);
|
27
|
+
}
|
28
|
+
|
29
|
+
static VALUE blake2_alloc(VALUE klass) {
|
30
|
+
Blake2 *blake2_obj = (Blake2 *)ruby_xmalloc(sizeof(Blake2));
|
31
|
+
|
32
|
+
return Data_Wrap_Struct(klass, NULL, blake2_free, blake2_obj);
|
33
|
+
}
|
34
|
+
|
35
|
+
VALUE m_blake2_initialize(VALUE self, VALUE _len, VALUE _key) {
|
36
|
+
Blake2 *blake2;
|
37
|
+
Data_Get_Struct(self, Blake2, blake2);
|
38
|
+
unsigned int i;
|
39
|
+
|
40
|
+
ID bytes_method = rb_intern("bytes");
|
41
|
+
blake2->to_hex = ID2SYM(rb_intern("to_hex"));
|
42
|
+
blake2->to_bytes = ID2SYM(rb_intern("to_bytes"));
|
43
|
+
|
44
|
+
VALUE key_bytes_ary = rb_funcall(_key, bytes_method, 0);
|
45
|
+
blake2->key_length = RARRAY_LEN(key_bytes_ary);
|
46
|
+
blake2->key_bytes = (uint8_t*)malloc(blake2->key_length * sizeof(uint8_t));
|
47
|
+
|
48
|
+
for(i = 0; i < blake2->key_length; i++) {
|
49
|
+
VALUE byte = rb_ary_entry(key_bytes_ary, i);
|
50
|
+
blake2->key_bytes[i] = NUM2INT(byte);
|
51
|
+
}
|
52
|
+
|
53
|
+
blake2->output_length = NUM2INT(_len);
|
54
|
+
blake2->output = (uint8_t*)malloc(blake2->output_length * sizeof(uint8_t));
|
55
|
+
|
56
|
+
return Qnil;
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
|
61
|
+
Blake2 *blake2;
|
62
|
+
|
63
|
+
char *input = RSTRING_PTR(_input);
|
64
|
+
uint64_t input_length = RSTRING_LEN(_input);
|
65
|
+
unsigned int i;
|
66
|
+
|
67
|
+
Data_Get_Struct(self, Blake2, blake2);
|
68
|
+
|
69
|
+
blake2b(blake2->output, blake2->output_length, input, input_length,
|
70
|
+
blake2->key_bytes, blake2->key_length);
|
71
|
+
|
72
|
+
VALUE result;
|
73
|
+
|
74
|
+
if(_representation == blake2->to_bytes) {
|
75
|
+
result = rb_ary_new2(blake2->output_length);
|
76
|
+
|
77
|
+
for(i = 0; i < blake2->output_length; i++) {
|
78
|
+
rb_ary_push(result, INT2NUM(blake2->output[i]));
|
79
|
+
}
|
80
|
+
} else if(_representation == blake2->to_hex) {
|
81
|
+
unsigned long ary_len = blake2->output_length * (unsigned)sizeof(char) * 2;
|
82
|
+
char *c_str = (char*)malloc(ary_len + 1);
|
83
|
+
|
84
|
+
for(i = 0; i < blake2->output_length; i++) {
|
85
|
+
sprintf(c_str + (i * 2), "%02x", blake2->output[i]);
|
86
|
+
}
|
87
|
+
c_str[ary_len] = 0;
|
88
|
+
|
89
|
+
result = rb_str_new(c_str, ary_len);
|
90
|
+
|
91
|
+
if((unsigned long)RSTRING_LEN(result) != ary_len) {
|
92
|
+
rb_raise(rb_eRuntimeError, "m_blake2_digest: sizes don't match. Ary: %lu != %lu", RSTRING_LEN(result), ary_len);
|
93
|
+
}
|
94
|
+
|
95
|
+
free(c_str);
|
96
|
+
} else {
|
97
|
+
rb_raise(rb_eArgError, "unknown representation :%"PRIsVALUE"", _representation);
|
98
|
+
}
|
99
|
+
|
100
|
+
return result;
|
101
|
+
}
|
102
|
+
|
103
|
+
void Init_ref() {
|
104
|
+
mDigest = rb_define_module("Digest");
|
105
|
+
|
106
|
+
mDigest_cBlake2 = rb_define_class_under(mDigest, "Blake2b", rb_cObject);
|
107
|
+
rb_define_alloc_func(mDigest_cBlake2, blake2_alloc);
|
108
|
+
|
109
|
+
rb_define_private_method(mDigest_cBlake2, "initialize", RUBY_METHOD_FUNC(m_blake2_initialize), 2);
|
110
|
+
rb_define_method(mDigest_cBlake2, "digest", RUBY_METHOD_FUNC(m_blake2_digest), 2);
|
111
|
+
}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -100,7 +100,7 @@ VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
|
|
100
100
|
return result;
|
101
101
|
}
|
102
102
|
|
103
|
-
void
|
103
|
+
void Init_sse() {
|
104
104
|
mDigest = rb_define_module("Digest");
|
105
105
|
|
106
106
|
mDigest_cBlake2 = rb_define_class_under(mDigest, "Blake2b", rb_cObject);
|
data/lib/digest/blake2b.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: digest-blake2b
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Kotov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2021-
|
13
|
+
date: 2021-11-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -74,7 +74,7 @@ email:
|
|
74
74
|
- mauricio@edge14.com
|
75
75
|
executables: []
|
76
76
|
extensions:
|
77
|
-
- ext/digest/blake2b/
|
77
|
+
- ext/digest/blake2b/sse/extconf.rb
|
78
78
|
extra_rdoc_files: []
|
79
79
|
files:
|
80
80
|
- ".circleci/config.yml"
|
@@ -85,16 +85,22 @@ files:
|
|
85
85
|
- Rakefile
|
86
86
|
- checksums/.keep
|
87
87
|
- digest-blake2b.gemspec
|
88
|
-
- ext/digest/blake2b/
|
89
|
-
- ext/digest/blake2b/
|
90
|
-
- ext/digest/blake2b/
|
91
|
-
- ext/digest/blake2b/
|
92
|
-
- ext/digest/blake2b/
|
93
|
-
- ext/digest/blake2b/
|
94
|
-
- ext/digest/blake2b/
|
95
|
-
- ext/digest/blake2b/
|
96
|
-
- ext/digest/blake2b/
|
88
|
+
- ext/digest/blake2b/ref/blake2-impl.h
|
89
|
+
- ext/digest/blake2b/ref/blake2.h
|
90
|
+
- ext/digest/blake2b/ref/blake2b-ref.c
|
91
|
+
- ext/digest/blake2b/ref/extconf.rb
|
92
|
+
- ext/digest/blake2b/ref/rbext.c
|
93
|
+
- ext/digest/blake2b/sse/blake2-config.h
|
94
|
+
- ext/digest/blake2b/sse/blake2-impl.h
|
95
|
+
- ext/digest/blake2b/sse/blake2.h
|
96
|
+
- ext/digest/blake2b/sse/blake2b-load-sse2.h
|
97
|
+
- ext/digest/blake2b/sse/blake2b-load-sse41.h
|
98
|
+
- ext/digest/blake2b/sse/blake2b-ref.c
|
99
|
+
- ext/digest/blake2b/sse/blake2b-round.h
|
100
|
+
- ext/digest/blake2b/sse/extconf.rb
|
101
|
+
- ext/digest/blake2b/sse/rbext.c
|
97
102
|
- lib/digest/blake2b.rb
|
103
|
+
- lib/digest/blake2b/impl.rb
|
98
104
|
- lib/digest/blake2b/key.rb
|
99
105
|
- lib/digest/blake2b/version.rb
|
100
106
|
- performance/performance_suite.rb
|