ffi-libsodium 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/crypto/aead/chacha20_poly1305.rb +2 -3
- data/lib/crypto/auth.rb +1 -2
- data/lib/crypto/box.rb +8 -9
- data/lib/crypto/generic_hash.rb +3 -4
- data/lib/crypto/one_time_auth.rb +2 -3
- data/lib/crypto/pw_hash/scrypt_salsa208_sha256.rb +2 -2
- data/lib/crypto/scalar_mult.rb +1 -2
- data/lib/crypto/secret_box.rb +2 -3
- data/lib/crypto/short_hash.rb +1 -2
- data/lib/crypto/sign.rb +8 -9
- data/lib/crypto/sign/ed25519.rb +1 -2
- data/lib/libsodium.rb +0 -1
- data/lib/random_bytes.rb +3 -2
- data/lib/sodium.rb +3 -4
- data/lib/sodium/version.rb +1 -1
- metadata +2 -3
- data/lib/sodium/buffer.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74a7166738e41a800aaf12b433c2f05f32ea032d
|
4
|
+
data.tar.gz: 7a0988313333e6174878ef6f9b39584dc4503139
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e131be5dcf2e7963ca0795773fdb823bab55ba89fc60475dc11637ef7ef1bb86dfaceac5213f0c02c7f317d997f3372c4c734f70cc00c1e213c76b011cd65969
|
7
|
+
data.tar.gz: 89a7331b233909cb01f4de635b91c34e253a4efb0958a03821ec3b97dd609cfe8f9a8588e0dca6ada99e79e77260e244f47ccd996cd1de04a8cab02e6e05fdb9
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../../sodium/utils'
|
3
3
|
require_relative '../../random_bytes'
|
4
|
-
require_relative '../../sodium/buffer'
|
5
4
|
require_relative '../../sodium/secret_buffer'
|
6
5
|
require_relative '../../sodium/errors'
|
7
6
|
|
@@ -45,7 +44,7 @@ module Crypto
|
|
45
44
|
check_length(nonce, NPUBBYTES, :Nonce)
|
46
45
|
check_length(key, KEYBYTES, :SecretKey)
|
47
46
|
|
48
|
-
ciphertext =
|
47
|
+
ciphertext = zeros(message_len + ABYTES)
|
49
48
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
50
49
|
crypto_aead_chacha20poly1305_encrypt(ciphertext, nil, message, message_len, additional_data, get_size(additional_data), nil, nonce, key)
|
51
50
|
|
@@ -60,7 +59,7 @@ module Crypto
|
|
60
59
|
check_length(nonce, NPUBBYTES, :Nonce)
|
61
60
|
check_length(key, KEYBYTES, :SecretKey)
|
62
61
|
|
63
|
-
decrypted =
|
62
|
+
decrypted = zeros(decrypted_len)
|
64
63
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
65
64
|
if crypto_aead_chacha20poly1305_decrypt(decrypted, nil, nil, ciphertext, ciphertext_len, additional_data, get_size(additional_data), nonce, key) == 0
|
66
65
|
decrypted
|
data/lib/crypto/auth.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
|
-
require_relative '../sodium/buffer'
|
4
3
|
require_relative '../sodium/secret_buffer'
|
5
4
|
|
6
5
|
module Crypto
|
@@ -26,7 +25,7 @@ module Crypto
|
|
26
25
|
def auth(message, key)
|
27
26
|
check_length(key, KEYBYTES, :SecretKey)
|
28
27
|
|
29
|
-
mac =
|
28
|
+
mac = zeros(BYTES)
|
30
29
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
31
30
|
crypto_auth(mac, message, get_size(message), key)
|
32
31
|
|
data/lib/crypto/box.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
3
|
require_relative '../random_bytes'
|
4
|
-
require_relative '../sodium/buffer'
|
5
4
|
require_relative '../sodium/secret_buffer'
|
6
5
|
require_relative '../sodium/errors'
|
7
6
|
|
@@ -39,8 +38,8 @@ module Crypto
|
|
39
38
|
end
|
40
39
|
|
41
40
|
def keypair
|
42
|
-
public_key =
|
43
|
-
secret_key =
|
41
|
+
public_key = zeros(PUBLICKEYBYTES)
|
42
|
+
secret_key = zeros(SECRETKEYBYTES)
|
44
43
|
crypto_box_keypair(public_key, secret_key)
|
45
44
|
|
46
45
|
[public_key, secret_key]
|
@@ -49,8 +48,8 @@ module Crypto
|
|
49
48
|
def seed_keypair(seed)
|
50
49
|
check_length(seed, SEEDBYTES, :Seed)
|
51
50
|
|
52
|
-
public_key =
|
53
|
-
secret_key =
|
51
|
+
public_key = zeros(PUBLICKEYBYTES)
|
52
|
+
secret_key = zeros(SECRETKEYBYTES)
|
54
53
|
seed.readonly if seed.is_a?(Sodium::SecretBuffer)
|
55
54
|
crypto_box_seed_keypair(public_key, secret_key, seed)
|
56
55
|
|
@@ -60,7 +59,7 @@ module Crypto
|
|
60
59
|
end
|
61
60
|
|
62
61
|
def memory_locked_keypair
|
63
|
-
public_key =
|
62
|
+
public_key = zeros(PUBLICKEYBYTES)
|
64
63
|
secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES)
|
65
64
|
crypto_box_keypair(public_key, secret_key)
|
66
65
|
secret_key.noaccess
|
@@ -71,7 +70,7 @@ module Crypto
|
|
71
70
|
def memory_locked_seed_keypair(seed)
|
72
71
|
check_length(seed, SEEDBYTES, :Seed)
|
73
72
|
|
74
|
-
public_key =
|
73
|
+
public_key = zeros(PUBLICKEYBYTES)
|
75
74
|
secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES)
|
76
75
|
seed.readonly if seed.is_a?(Sodium::SecretBuffer)
|
77
76
|
crypto_box_seed_keypair(public_key, secret_key, seed)
|
@@ -88,7 +87,7 @@ module Crypto
|
|
88
87
|
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
89
88
|
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
90
89
|
|
91
|
-
ciphertext =
|
90
|
+
ciphertext = zeros(message_len + MACBYTES)
|
92
91
|
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
93
92
|
crypto_box_easy(ciphertext, message, message_len, nonce, public_key, secret_key)
|
94
93
|
|
@@ -103,7 +102,7 @@ module Crypto
|
|
103
102
|
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
104
103
|
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
105
104
|
|
106
|
-
decrypted =
|
105
|
+
decrypted = zeros(ciphertext_len - MACBYTES)
|
107
106
|
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
108
107
|
if crypto_box_open_easy(decrypted, ciphertext, ciphertext_len, nonce, public_key, secret_key) == 0
|
109
108
|
decrypted
|
data/lib/crypto/generic_hash.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
|
-
require_relative '../sodium/buffer'
|
4
3
|
require_relative '../sodium/secret_buffer'
|
5
4
|
require_relative '../sodium/errors'
|
6
5
|
|
@@ -52,7 +51,7 @@ module Crypto
|
|
52
51
|
key_len = 0
|
53
52
|
end
|
54
53
|
|
55
|
-
blake2b =
|
54
|
+
blake2b = zeros(hash_size)
|
56
55
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
57
56
|
if crypto_generichash(blake2b, hash_size, message, get_size(message), key, key_len) == 0
|
58
57
|
blake2b
|
@@ -73,7 +72,7 @@ module Crypto
|
|
73
72
|
state = State.new
|
74
73
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
75
74
|
if crypto_generichash_init(state, key, key_len, hash_size) == 0
|
76
|
-
[state,
|
75
|
+
[state, zeros(hash_size)]
|
77
76
|
else
|
78
77
|
raise Sodium::CryptoError
|
79
78
|
end
|
@@ -86,7 +85,7 @@ module Crypto
|
|
86
85
|
end
|
87
86
|
|
88
87
|
def final(state, blake2b)
|
89
|
-
if crypto_generichash_final(state, blake2b, blake2b.
|
88
|
+
if crypto_generichash_final(state, blake2b, blake2b.bytesize) == 0
|
90
89
|
blake2b
|
91
90
|
else
|
92
91
|
raise Sodium::CryptoError
|
data/lib/crypto/one_time_auth.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
|
-
require_relative '../sodium/buffer'
|
4
3
|
require_relative '../sodium/secret_buffer'
|
5
4
|
|
6
5
|
module Crypto
|
@@ -35,7 +34,7 @@ module Crypto
|
|
35
34
|
def onetimeauth(message, key)
|
36
35
|
check_length(key, KEYBYTES, :SecretKey)
|
37
36
|
|
38
|
-
out =
|
37
|
+
out = zeros(BYTES)
|
39
38
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
40
39
|
crypto_onetimeauth(out, message, get_size(message), key)
|
41
40
|
|
@@ -71,7 +70,7 @@ module Crypto
|
|
71
70
|
end
|
72
71
|
|
73
72
|
def final(state)
|
74
|
-
out =
|
73
|
+
out = zeros(BYTES)
|
75
74
|
crypto_onetimeauth_final(state, out)
|
76
75
|
out
|
77
76
|
end
|
@@ -61,9 +61,9 @@ module Crypto
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def str(passwd, opslimit = OPSLIMIT_INTERACTIVE, memlimit = MEMLIMIT_INTERACTIVE)
|
64
|
-
hashed_password =
|
64
|
+
hashed_password = zeros(STRBYTES - 1)
|
65
65
|
if crypto_pwhash_scryptsalsa208sha256_str(hashed_password, passwd, passwd.bytesize, opslimit, memlimit) == 0
|
66
|
-
hashed_password
|
66
|
+
hashed_password
|
67
67
|
else
|
68
68
|
raise NoMemoryError, "Failed to allocate memory max size=#{memlimit} bytes", caller
|
69
69
|
end
|
data/lib/crypto/scalar_mult.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
|
-
require_relative '../sodium/buffer'
|
4
3
|
require_relative '../sodium/secret_buffer'
|
5
4
|
|
6
5
|
module Crypto
|
@@ -26,7 +25,7 @@ module Crypto
|
|
26
25
|
def base(secret_key)
|
27
26
|
check_length(secret_key, SCALARBYTES, :SecretKey)
|
28
27
|
|
29
|
-
public_key =
|
28
|
+
public_key = zeros(BYTES)
|
30
29
|
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
31
30
|
crypto_scalarmult_base(public_key, secret_key)
|
32
31
|
|
data/lib/crypto/secret_box.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
3
|
require_relative '../random_bytes'
|
4
|
-
require_relative '../sodium/buffer'
|
5
4
|
require_relative '../sodium/secret_buffer'
|
6
5
|
require_relative '../sodium'
|
7
6
|
|
@@ -36,7 +35,7 @@ module Crypto
|
|
36
35
|
check_length(nonce, NONCEBYTES, :Nonce)
|
37
36
|
check_length(key, KEYBYTES, :SecretKey)
|
38
37
|
|
39
|
-
ciphertext =
|
38
|
+
ciphertext = zeros(message_len + MACBYTES)
|
40
39
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
41
40
|
crypto_secretbox_easy(ciphertext, message, message_len, nonce, key)
|
42
41
|
|
@@ -50,7 +49,7 @@ module Crypto
|
|
50
49
|
check_length(nonce, NONCEBYTES, :Nonce)
|
51
50
|
check_length(key, KEYBYTES, :SecretKey)
|
52
51
|
|
53
|
-
decrypted =
|
52
|
+
decrypted = zeros(ciphertext_len - MACBYTES)
|
54
53
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
55
54
|
if crypto_secretbox_open_easy(decrypted, ciphertext, ciphertext_len, nonce, key) == 0
|
56
55
|
decrypted
|
data/lib/crypto/short_hash.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
|
-
require_relative '../sodium/buffer'
|
4
3
|
require_relative '../sodium/secret_buffer'
|
5
4
|
|
6
5
|
module Crypto
|
@@ -25,7 +24,7 @@ module Crypto
|
|
25
24
|
def shorthash(short_data, key)
|
26
25
|
check_length(key, KEYBYTES, :SecretKey)
|
27
26
|
|
28
|
-
siphash =
|
27
|
+
siphash = zeros(BYTES)
|
29
28
|
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
30
29
|
crypto_shorthash(siphash, short_data, get_size(short_data), key)
|
31
30
|
|
data/lib/crypto/sign.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../sodium/utils'
|
3
|
-
require_relative '../sodium/buffer'
|
4
3
|
require_relative '../sodium/secret_buffer'
|
5
4
|
require_relative '../sodium'
|
6
5
|
|
@@ -32,8 +31,8 @@ module Crypto
|
|
32
31
|
module_function
|
33
32
|
|
34
33
|
def keypair
|
35
|
-
public_key =
|
36
|
-
secret_key =
|
34
|
+
public_key = zeros(PUBLICKEYBYTES)
|
35
|
+
secret_key = zeros(SECRETKEYBYTES)
|
37
36
|
crypto_sign_keypair(public_key, secret_key)
|
38
37
|
|
39
38
|
[public_key, secret_key]
|
@@ -42,8 +41,8 @@ module Crypto
|
|
42
41
|
def seed_keypair(seed)
|
43
42
|
check_length(seed, SEEDBYTES, :Seed)
|
44
43
|
|
45
|
-
public_key =
|
46
|
-
secret_key =
|
44
|
+
public_key = zeros(PUBLICKEYBYTES)
|
45
|
+
secret_key = zeros(SECRETKEYBYTES)
|
47
46
|
seed.readonly if seed.is_a?(Sodium::SecretBuffer)
|
48
47
|
crypto_sign_seed_keypair(public_key, secret_key, seed)
|
49
48
|
|
@@ -53,7 +52,7 @@ module Crypto
|
|
53
52
|
end
|
54
53
|
|
55
54
|
def memory_locked_keypair
|
56
|
-
public_key =
|
55
|
+
public_key = zeros(PUBLICKEYBYTES)
|
57
56
|
secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES)
|
58
57
|
crypto_sign_keypair(public_key, secret_key)
|
59
58
|
secret_key.noaccess
|
@@ -64,7 +63,7 @@ module Crypto
|
|
64
63
|
def memory_locked_seed_keypair(seed)
|
65
64
|
check_length(seed, SEEDBYTES, :Seed)
|
66
65
|
|
67
|
-
public_key =
|
66
|
+
public_key = zeros(PUBLICKEYBYTES)
|
68
67
|
secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES)
|
69
68
|
seed.readonly if seed.is_a?(Sodium::SecretBuffer)
|
70
69
|
crypto_sign_seed_keypair(public_key, secret_key, seed)
|
@@ -79,7 +78,7 @@ module Crypto
|
|
79
78
|
message_len = get_size(message)
|
80
79
|
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
81
80
|
|
82
|
-
sealed_message =
|
81
|
+
sealed_message = zeros(message_len + BYTES)
|
83
82
|
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
84
83
|
crypto_sign(sealed_message, nil, message, message_len, secret_key)
|
85
84
|
|
@@ -92,7 +91,7 @@ module Crypto
|
|
92
91
|
sealed_message_len = get_size(sealed_message)
|
93
92
|
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
94
93
|
|
95
|
-
unsealed_message =
|
94
|
+
unsealed_message = zeros(sealed_message_len - BYTES)
|
96
95
|
unsealed_message_len = FFI::MemoryPointer.new(:ulong_long)
|
97
96
|
if crypto_sign_open(unsealed_message, unsealed_message_len, sealed_message, sealed_message_len, public_key) == 0
|
98
97
|
unsealed_message
|
data/lib/crypto/sign/ed25519.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative '../../sodium/utils'
|
3
3
|
require_relative '../scalar_mult'
|
4
|
-
require_relative '../../sodium/buffer'
|
5
4
|
require_relative '../../sodium/secret_buffer'
|
6
5
|
|
7
6
|
module Crypto
|
@@ -35,7 +34,7 @@ module Crypto
|
|
35
34
|
def pk_to_curve25519(public_key)
|
36
35
|
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
37
36
|
|
38
|
-
curve25519_pk =
|
37
|
+
curve25519_pk = zeros(ScalarMult::BYTES)
|
39
38
|
crypto_sign_ed25519_pk_to_curve25519(curve25519_pk, public_key)
|
40
39
|
|
41
40
|
curve25519_pk
|
data/lib/libsodium.rb
CHANGED
data/lib/random_bytes.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'ffi'
|
2
|
-
require_relative 'sodium/
|
2
|
+
require_relative 'sodium/utils'
|
3
3
|
|
4
4
|
module RandomBytes
|
5
5
|
extend FFI::Library
|
6
|
+
extend Sodium::Utils
|
6
7
|
ffi_lib :libsodium
|
7
8
|
|
8
9
|
attach_function :randombytes_buf, [:buffer_out, :size_t], :void
|
@@ -15,7 +16,7 @@ module RandomBytes
|
|
15
16
|
module_function
|
16
17
|
|
17
18
|
def buf(size)
|
18
|
-
buf =
|
19
|
+
buf = zeros(size)
|
19
20
|
randombytes_buf(buf, size)
|
20
21
|
buf
|
21
22
|
end
|
data/lib/sodium.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative 'sodium/errors'
|
3
3
|
require_relative 'sodium/utils'
|
4
|
-
require_relative 'sodium/buffer'
|
5
4
|
|
6
5
|
module Sodium
|
7
6
|
extend FFI::Library
|
@@ -57,12 +56,12 @@ module Sodium
|
|
57
56
|
|
58
57
|
def bin2hex(bin)
|
59
58
|
bin_len = get_size(bin)
|
60
|
-
hex =
|
61
|
-
sodium_bin2hex(hex, hex.
|
59
|
+
hex = zeros(bin_len * 2)
|
60
|
+
sodium_bin2hex(hex, hex.bytesize, bin, bin_len)
|
62
61
|
end
|
63
62
|
|
64
63
|
def hex2bin(hex, bin_maxlen, ignore = nil)
|
65
|
-
bin =
|
64
|
+
bin = zeros(bin_maxlen)
|
66
65
|
bin_len = FFI::MemoryPointer.new(:size_t)
|
67
66
|
if sodium_hex2bin(bin, bin_maxlen, hex, hex.bytesize, ignore, bin_len, nil) == 0
|
68
67
|
size = bin_len.size == 8 ? bin_len.get_uint64(0) : bin_len.get_uint32(0)
|
data/lib/sodium/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ffi-libsodium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hendrik Beskow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|
@@ -60,7 +60,6 @@ files:
|
|
60
60
|
- lib/libsodium.rb
|
61
61
|
- lib/random_bytes.rb
|
62
62
|
- lib/sodium.rb
|
63
|
-
- lib/sodium/buffer.rb
|
64
63
|
- lib/sodium/core_ext.rb
|
65
64
|
- lib/sodium/errors.rb
|
66
65
|
- lib/sodium/mprotect.rb
|