digest-blake2b 0.0.1
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 +7 -0
- data/.circleci/config.yml +92 -0
- data/.gitignore +45 -0
- data/Gemfile +2 -0
- data/LICENSE +674 -0
- data/README.md +115 -0
- data/Rakefile +46 -0
- data/checksums/.keep +0 -0
- data/digest-blake2b.gemspec +49 -0
- data/ext/digest/blake2b_ext/blake2-config.h +72 -0
- data/ext/digest/blake2b_ext/blake2-impl.h +160 -0
- data/ext/digest/blake2b_ext/blake2.h +195 -0
- data/ext/digest/blake2b_ext/blake2b-load-sse2.h +68 -0
- data/ext/digest/blake2b_ext/blake2b-load-sse41.h +402 -0
- data/ext/digest/blake2b_ext/blake2b-ref.c +373 -0
- data/ext/digest/blake2b_ext/blake2b-round.h +157 -0
- data/ext/digest/blake2b_ext/extconf.rb +3 -0
- data/ext/digest/blake2b_ext/rbext.c +111 -0
- data/lib/digest/blake2b/key.rb +59 -0
- data/lib/digest/blake2b/version.rb +7 -0
- data/lib/digest/blake2b.rb +36 -0
- data/performance/performance_suite.rb +61 -0
- metadata +126 -0
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
Digest::Blake2b
|
2
|
+
===============
|
3
|
+
|
4
|
+
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
|
+
|
6
|
+
More info at: [https://blake2.net](https://blake2.net).
|
7
|
+
|
8
|
+
## Summary
|
9
|
+
|
10
|
+
This gem is a C-extension to enable using BLAKE2b in Ruby. This BLAKE2b implementation (or just BLAKE2) is optimized for 64-bit platforms with SSE support (excluding NEON-enabled ARMs). It produces digests of any size between 1 and 64 bytes.
|
11
|
+
|
12
|
+
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
|
+
|
14
|
+
## Install
|
15
|
+
|
16
|
+
```
|
17
|
+
gem install blake2b
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
``` ruby
|
23
|
+
require 'blake2b'
|
24
|
+
|
25
|
+
# The UTF-8 String (Required) that you want to digest.
|
26
|
+
input = 'abc'
|
27
|
+
|
28
|
+
# The main application of keyed BLAKE2 is as a message authentication code (MAC)
|
29
|
+
# By default `Blake2b::Key.none` is used.
|
30
|
+
key = Blake2b::Key.none
|
31
|
+
# key = Blake2b::Key.from_string("foo bar baz")
|
32
|
+
# key = Blake2b::Key.from_hex('DEADBEAF')
|
33
|
+
# key = Blake2b::Key.from_bytes([222, 173, 190, 175])
|
34
|
+
|
35
|
+
# The output length in Bytes of the Hash, Max and Default is 32.
|
36
|
+
out_len = 32
|
37
|
+
|
38
|
+
# HEX OUTPUT
|
39
|
+
############
|
40
|
+
|
41
|
+
Blake2b.hex(input)
|
42
|
+
=> "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
|
43
|
+
|
44
|
+
Blake2b.hex(input, key)
|
45
|
+
=> "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
|
46
|
+
|
47
|
+
Blake2b.hex(input, key, out_len)
|
48
|
+
=> "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
|
49
|
+
|
50
|
+
# BYTES OUTPUT
|
51
|
+
##############
|
52
|
+
|
53
|
+
Blake2b.bytes(input)
|
54
|
+
=> [80, 140, 94, ...]
|
55
|
+
|
56
|
+
Blake2b.bytes(input, key)
|
57
|
+
=> [80, 140, 94, ...]
|
58
|
+
|
59
|
+
Blake2b.bytes(input, key, out_len)
|
60
|
+
=> [80, 140, 94, ...]
|
61
|
+
|
62
|
+
```
|
63
|
+
|
64
|
+
## Performance
|
65
|
+
|
66
|
+
`Blake2b` really shines on larger inputs. Here are some benchmarks on various input sizes. You can find the performance suite used for these benchmarks at `performance/performance_suite.rb`. All tests were run on an iMac 27" Late 2014, 4GHz Core i7 CPU (4790K) w/ SSE4.1 + SSE4.2, 32GB DDR3 RAM.
|
67
|
+
|
68
|
+
### 1KB (1M digests)
|
69
|
+
|
70
|
+
```
|
71
|
+
MD5 result: 2.694545999998809 seconds.
|
72
|
+
SHA2 result: 4.037195000011707 seconds.
|
73
|
+
SHA512 result: 3.213850000000093 seconds.
|
74
|
+
BLAKE2s result: 5.6867979999951785 seconds.
|
75
|
+
BLAKE2b result: 4.375018999999156 seconds.
|
76
|
+
```
|
77
|
+
|
78
|
+
### 50KB (500k digests)
|
79
|
+
|
80
|
+
```
|
81
|
+
MD5 result: 34.33997299999464 seconds.
|
82
|
+
SHA2 result: 50.161426999999094 seconds.
|
83
|
+
SHA512 result: 35.24845699999423 seconds.
|
84
|
+
BLAKE2s result: 64.8592859999917 seconds.
|
85
|
+
BLAKE2b result: 30.783814999987953 seconds.
|
86
|
+
```
|
87
|
+
|
88
|
+
### 250KB (500k digests)
|
89
|
+
|
90
|
+
```
|
91
|
+
MD5 result: 67.89016799999808 seconds.
|
92
|
+
SHA2 result: 103.09026799999992 seconds.
|
93
|
+
SHA512 result: 72.46762200001103 seconds.
|
94
|
+
BLAKE2s result: 133.5229810000019 seconds.
|
95
|
+
BLAKE2b result: 64.30263599999307 seconds.
|
96
|
+
```
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
After checking out the repo, run `bundle` to install dependencies. Then,
|
101
|
+
run `rake full` to build and test, or `rake test` to only run the tests.
|
102
|
+
|
103
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
104
|
+
|
105
|
+
## Future
|
106
|
+
|
107
|
+
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.
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
Blake2b is based heavily on [Blake2](https://github.com/franckverrot/blake2) by Franck Verrot, Copyright 2014.
|
112
|
+
|
113
|
+
Blake2b is copyright 2018, Mauricio Gomes.
|
114
|
+
|
115
|
+
The original work (Blake2) and the modified work (Blake2b) are licensed GPL v3.0. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/extensiontask'
|
4
|
+
|
5
|
+
spec = Gem::Specification.load('digest-blake2b.gemspec')
|
6
|
+
|
7
|
+
Rake::ExtensionTask.new('digest/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('digest-blake2b.gemspec', __dir__)
|
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 digest-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
|
data/checksums/.keep
ADDED
File without changes
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__).freeze
|
4
|
+
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
5
|
+
|
6
|
+
require 'digest/blake2b/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'digest-blake2b'
|
10
|
+
spec.version = Digest::Blake2b::VERSION
|
11
|
+
spec.license = 'GPL-3.0'
|
12
|
+
spec.homepage = 'https://github.com/kotovalexarian/digest-blake2b.rb'
|
13
|
+
spec.summary = 'The BLAKE2b cryptographic hash function.'
|
14
|
+
|
15
|
+
spec.required_ruby_version = '~> 2.1'
|
16
|
+
|
17
|
+
spec.authors = ['Franck Verrot', 'Mauricio Gomes']
|
18
|
+
spec.email = %w[mauricio@edge14.com]
|
19
|
+
|
20
|
+
spec.description = <<-DESCRIPTION.split.join ' '
|
21
|
+
BLAKE2b is a cryptographic hash function
|
22
|
+
faster than MD5, SHA-1, SHA-2, and SHA-3 for 64-bit systems.
|
23
|
+
DESCRIPTION
|
24
|
+
|
25
|
+
spec.metadata = {
|
26
|
+
'homepage_uri' => 'https://github.com/kotovalexarian/digest-blake2b.rb',
|
27
|
+
'source_code_uri' => 'https://github.com/kotovalexarian/digest-blake2b.rb',
|
28
|
+
'bug_tracker_uri' =>
|
29
|
+
'https://github.com/kotovalexarian/digest-blake2b.rb/issues',
|
30
|
+
}.freeze
|
31
|
+
|
32
|
+
spec.bindir = 'exe'
|
33
|
+
spec.require_paths = ['lib']
|
34
|
+
|
35
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
36
|
+
f.match %r{^(test|spec|features)/}
|
37
|
+
end
|
38
|
+
|
39
|
+
spec.test_files = spec.files.grep %r{^(test|spec|features)/}
|
40
|
+
|
41
|
+
spec.executables = spec.files.grep %r{^exe/}, &File.method(:basename)
|
42
|
+
|
43
|
+
spec.extensions << 'ext/digest/blake2b_ext/extconf.rb'
|
44
|
+
|
45
|
+
spec.add_development_dependency 'rake-compiler', '~> 0.9'
|
46
|
+
spec.add_development_dependency 'bundler' , '~> 1.5'
|
47
|
+
spec.add_development_dependency 'rake' , '~> 11.1'
|
48
|
+
spec.add_development_dependency 'minitest' , '~> 5.11'
|
49
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
/*
|
2
|
+
BLAKE2 reference source code package - optimized 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_CONFIG_H
|
16
|
+
#define BLAKE2_CONFIG_H
|
17
|
+
|
18
|
+
/* These don't work everywhere */
|
19
|
+
#if defined(__SSE2__) || defined(__x86_64__) || defined(__amd64__)
|
20
|
+
#define HAVE_SSE2
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#if defined(__SSSE3__)
|
24
|
+
#define HAVE_SSSE3
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#if defined(__SSE4_1__)
|
28
|
+
#define HAVE_SSE41
|
29
|
+
#endif
|
30
|
+
|
31
|
+
#if defined(__AVX__)
|
32
|
+
#define HAVE_AVX
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#if defined(__XOP__)
|
36
|
+
#define HAVE_XOP
|
37
|
+
#endif
|
38
|
+
|
39
|
+
|
40
|
+
#ifdef HAVE_AVX2
|
41
|
+
#ifndef HAVE_AVX
|
42
|
+
#define HAVE_AVX
|
43
|
+
#endif
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#ifdef HAVE_XOP
|
47
|
+
#ifndef HAVE_AVX
|
48
|
+
#define HAVE_AVX
|
49
|
+
#endif
|
50
|
+
#endif
|
51
|
+
|
52
|
+
#ifdef HAVE_AVX
|
53
|
+
#ifndef HAVE_SSE41
|
54
|
+
#define HAVE_SSE41
|
55
|
+
#endif
|
56
|
+
#endif
|
57
|
+
|
58
|
+
#ifdef HAVE_SSE41
|
59
|
+
#ifndef HAVE_SSSE3
|
60
|
+
#define HAVE_SSSE3
|
61
|
+
#endif
|
62
|
+
#endif
|
63
|
+
|
64
|
+
#ifdef HAVE_SSSE3
|
65
|
+
#define HAVE_SSE2
|
66
|
+
#endif
|
67
|
+
|
68
|
+
#if !defined(HAVE_SSE2)
|
69
|
+
#error "This code requires at least SSE2."
|
70
|
+
#endif
|
71
|
+
|
72
|
+
#endif
|
@@ -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
|