rbnacl 3.1.2 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +27 -0
- data/.travis.yml +4 -3
- data/CHANGES.md +5 -0
- data/Gemfile +8 -3
- data/Guardfile +1 -1
- data/README.md +5 -6
- data/Rakefile +3 -2
- data/lib/rbnacl.rb +1 -1
- data/lib/rbnacl/auth.rb +21 -8
- data/lib/rbnacl/boxes/curve25519xsalsa20poly1305.rb +17 -13
- data/lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb +68 -65
- data/lib/rbnacl/boxes/curve25519xsalsa20poly1305/public_key.rb +49 -47
- data/lib/rbnacl/group_elements/curve25519.rb +14 -8
- data/lib/rbnacl/hash.rb +2 -2
- data/lib/rbnacl/hash/blake2b.rb +13 -13
- data/lib/rbnacl/hash/sha256.rb +5 -5
- data/lib/rbnacl/hash/sha512.rb +5 -5
- data/lib/rbnacl/hmac/sha256.rb +12 -11
- data/lib/rbnacl/hmac/sha512256.rb +11 -10
- data/lib/rbnacl/init.rb +1 -1
- data/lib/rbnacl/key_comparator.rb +3 -3
- data/lib/rbnacl/one_time_auths/poly1305.rb +4 -4
- data/lib/rbnacl/password_hash/scrypt.rb +10 -11
- data/lib/rbnacl/random.rb +2 -2
- data/lib/rbnacl/secret_boxes/xsalsa20poly1305.rb +28 -14
- data/lib/rbnacl/self_test.rb +17 -37
- data/lib/rbnacl/serializable.rb +9 -4
- data/lib/rbnacl/signatures/ed25519.rb +1 -0
- data/lib/rbnacl/signatures/ed25519/signing_key.rb +17 -9
- data/lib/rbnacl/signatures/ed25519/verify_key.rb +17 -6
- data/lib/rbnacl/simple_box.rb +6 -3
- data/lib/rbnacl/sodium.rb +6 -7
- data/lib/rbnacl/sodium/version.rb +3 -2
- data/lib/rbnacl/test_vectors.rb +57 -55
- data/lib/rbnacl/util.rb +12 -11
- data/lib/rbnacl/version.rb +3 -1
- data/rbnacl.gemspec +6 -8
- data/spec/rbnacl/authenticators/poly1305_spec.rb +1 -1
- data/spec/rbnacl/boxes/curve25519xsalsa20poly1305/private_key_spec.rb +1 -1
- data/spec/rbnacl/boxes/curve25519xsalsa20poly1305/public_key_spec.rb +1 -1
- data/spec/rbnacl/boxes/curve25519xsalsa20poly1305_spec.rb +2 -2
- data/spec/rbnacl/group_element_spec.rb +1 -1
- data/spec/rbnacl/hash/blake2b_spec.rb +1 -1
- data/spec/rbnacl/hash_spec.rb +1 -1
- data/spec/rbnacl/hmac/sha256_spec.rb +1 -1
- data/spec/rbnacl/hmac/sha512256_spec.rb +1 -1
- data/spec/rbnacl/password_hash/scrypt_spec.rb +3 -3
- data/spec/rbnacl/secret_box_spec.rb +3 -3
- data/spec/rbnacl/signatures/ed25519/signing_key_spec.rb +2 -2
- data/spec/rbnacl/signatures/ed25519/verify_key_spec.rb +12 -7
- data/spec/rbnacl/simple_box_spec.rb +2 -2
- data/spec/rbnacl/util_spec.rb +28 -29
- data/spec/shared/authenticator.rb +12 -12
- data/spec/shared/box.rb +2 -4
- data/spec/spec_helper.rb +11 -18
- data/tasks/rspec.rake +2 -2
- data/tasks/rubocop.rake +1 -1
- metadata +4 -5
- data/lib/rbnacl/rake_tasks.rb +0 -57
- data/tasks/ci.rake +0 -11
data/lib/rbnacl/serializable.rb
CHANGED
@@ -2,14 +2,19 @@
|
|
2
2
|
module RbNaCl
|
3
3
|
# Serialization features shared across all "key-like" classes
|
4
4
|
module Serializable
|
5
|
-
def to_s
|
6
|
-
|
5
|
+
def to_s
|
6
|
+
to_bytes
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_str
|
10
|
+
to_bytes
|
11
|
+
end
|
7
12
|
|
8
13
|
# Inspect this key
|
9
14
|
#
|
10
15
|
# @return [String] a string representing this key
|
11
16
|
def inspect
|
12
|
-
"#<#{self.class}:#{Util.bin2hex(to_bytes)[0,8]}>"
|
17
|
+
"#<#{self.class}:#{Util.bin2hex(to_bytes)[0, 8]}>"
|
13
18
|
end
|
14
19
|
end
|
15
|
-
end
|
20
|
+
end
|
@@ -23,7 +23,7 @@ module RbNaCl
|
|
23
23
|
include KeyComparator
|
24
24
|
include Serializable
|
25
25
|
|
26
|
-
extend
|
26
|
+
extend Sodium
|
27
27
|
|
28
28
|
sodium_type :sign
|
29
29
|
sodium_primitive :ed25519
|
@@ -58,10 +58,11 @@ module RbNaCl
|
|
58
58
|
pk = Util.zeros(Ed25519::VERIFYKEYBYTES)
|
59
59
|
sk = Util.zeros(Ed25519::SIGNINGKEYBYTES)
|
60
60
|
|
61
|
-
self.class.sign_ed25519_seed_keypair(pk, sk, seed) ||
|
61
|
+
self.class.sign_ed25519_seed_keypair(pk, sk, seed) || fail(CryptoError, "Failed to generate a key pair")
|
62
62
|
|
63
|
-
@seed
|
64
|
-
@
|
63
|
+
@seed = seed
|
64
|
+
@signing_key = sk
|
65
|
+
@verify_key = VerifyKey.new(pk)
|
65
66
|
end
|
66
67
|
|
67
68
|
# Sign a message using this key
|
@@ -81,23 +82,30 @@ module RbNaCl
|
|
81
82
|
# Return the raw seed value of this key
|
82
83
|
#
|
83
84
|
# @return [String] seed used to create this key
|
84
|
-
def to_bytes
|
85
|
+
def to_bytes
|
86
|
+
@seed
|
87
|
+
end
|
85
88
|
|
86
89
|
# The crypto primitive this SigningKey class uses for signatures
|
87
90
|
#
|
88
91
|
# @return [Symbol] The primitive
|
89
|
-
def primitive
|
92
|
+
def primitive
|
93
|
+
self.class.primitive
|
94
|
+
end
|
90
95
|
|
91
96
|
# The size of signatures generated by the SigningKey class
|
92
97
|
#
|
93
98
|
# @return [Integer] The number of bytes in a signature
|
94
|
-
def self.signature_bytes
|
99
|
+
def self.signature_bytes
|
100
|
+
Ed25519::SIGNATUREBYTES
|
101
|
+
end
|
95
102
|
|
96
103
|
# The size of signatures generated by the SigningKey instance
|
97
104
|
#
|
98
105
|
# @return [Integer] The number of bytes in a signature
|
99
|
-
def signature_bytes
|
100
|
-
|
106
|
+
def signature_bytes
|
107
|
+
Ed25519::SIGNATUREBYTES
|
108
|
+
end
|
101
109
|
end
|
102
110
|
end
|
103
111
|
end
|
@@ -12,7 +12,7 @@ module RbNaCl
|
|
12
12
|
include KeyComparator
|
13
13
|
include Serializable
|
14
14
|
|
15
|
-
extend
|
15
|
+
extend Sodium
|
16
16
|
|
17
17
|
sodium_type :sign
|
18
18
|
sodium_primitive :ed25519
|
@@ -50,28 +50,39 @@ module RbNaCl
|
|
50
50
|
buffer = Util.zeros(sig_and_msg.bytesize)
|
51
51
|
buffer_len = Util.zeros(FFI::Type::LONG_LONG.size)
|
52
52
|
|
53
|
-
self.class.sign_ed25519_open(buffer, buffer_len, sig_and_msg, sig_and_msg.bytesize, @key)
|
53
|
+
success = self.class.sign_ed25519_open(buffer, buffer_len, sig_and_msg, sig_and_msg.bytesize, @key)
|
54
|
+
fail(BadSignatureError, "signature was forged/corrupt") unless success
|
55
|
+
|
56
|
+
true
|
54
57
|
end
|
55
58
|
|
56
59
|
# Return the raw key in byte format
|
57
60
|
#
|
58
61
|
# @return [String] raw key as bytes
|
59
|
-
def to_bytes
|
62
|
+
def to_bytes
|
63
|
+
@key
|
64
|
+
end
|
60
65
|
|
61
66
|
# The crypto primitive this VerifyKey class uses for signatures
|
62
67
|
#
|
63
68
|
# @return [Symbol] The primitive
|
64
|
-
def primitive
|
69
|
+
def primitive
|
70
|
+
self.class.primitive
|
71
|
+
end
|
65
72
|
|
66
73
|
# The size of signatures verified by the VerifyKey class
|
67
74
|
#
|
68
75
|
# @return [Integer] The number of bytes in a signature
|
69
|
-
def self.signature_bytes
|
76
|
+
def self.signature_bytes
|
77
|
+
Ed25519::SIGNATUREBYTES
|
78
|
+
end
|
70
79
|
|
71
80
|
# The size of signatures verified by the VerifyKey instance
|
72
81
|
#
|
73
82
|
# @return [Integer] The number of bytes in a signature
|
74
|
-
def signature_bytes
|
83
|
+
def signature_bytes
|
84
|
+
Ed25519::SIGNATUREBYTES
|
85
|
+
end
|
75
86
|
end
|
76
87
|
end
|
77
88
|
end
|
data/lib/rbnacl/simple_box.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: binary
|
2
|
-
require
|
2
|
+
require "forwardable"
|
3
|
+
|
4
|
+
# NaCl/libsodium for Ruby
|
3
5
|
module RbNaCl
|
4
6
|
# The simplest nonce strategy that could possibly work
|
5
7
|
#
|
@@ -78,7 +80,7 @@ module RbNaCl
|
|
78
80
|
cipher_text = @box.box(nonce, message)
|
79
81
|
nonce + cipher_text
|
80
82
|
end
|
81
|
-
|
83
|
+
alias_method :encrypt, :box
|
82
84
|
|
83
85
|
# Decrypts the ciphertext with a random nonce
|
84
86
|
#
|
@@ -94,9 +96,10 @@ module RbNaCl
|
|
94
96
|
nonce, ciphertext = extract_nonce(enciphered_message.to_s)
|
95
97
|
@box.open(nonce, ciphertext)
|
96
98
|
end
|
97
|
-
|
99
|
+
alias_method :decrypt, :open
|
98
100
|
|
99
101
|
private
|
102
|
+
|
100
103
|
def generate_nonce
|
101
104
|
Random.random_bytes(nonce_bytes)
|
102
105
|
end
|
data/lib/rbnacl/sodium.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: binary
|
2
|
-
require
|
2
|
+
require "ffi"
|
3
3
|
|
4
4
|
module RbNaCl
|
5
5
|
# Provides helpers for defining the libsodium bindings
|
@@ -9,11 +9,10 @@ module RbNaCl
|
|
9
9
|
if defined?(RBNACL_LIBSODIUM_GEM_LIB_PATH)
|
10
10
|
klass.ffi_lib RBNACL_LIBSODIUM_GEM_LIB_PATH
|
11
11
|
else
|
12
|
-
klass.ffi_lib
|
12
|
+
klass.ffi_lib "sodium"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
16
|
def sodium_type(type = nil)
|
18
17
|
return @type if type.nil?
|
19
18
|
@type = type
|
@@ -28,14 +27,14 @@ module RbNaCl
|
|
28
27
|
sodium_primitive
|
29
28
|
end
|
30
29
|
|
31
|
-
def sodium_constant(constant, name=constant)
|
30
|
+
def sodium_constant(constant, name = constant)
|
32
31
|
fn = "crypto_#{sodium_type}_#{sodium_primitive}_#{constant.to_s.downcase}"
|
33
|
-
attach_function fn, [], :
|
34
|
-
|
32
|
+
attach_function fn, [], :size_t
|
33
|
+
const_set(name, public_send(fn))
|
35
34
|
end
|
36
35
|
|
37
36
|
def sodium_function(name, function, arguments)
|
38
|
-
|
37
|
+
module_eval <<-eos, __FILE__, __LINE__ + 1
|
39
38
|
attach_function #{function.inspect}, #{arguments.inspect}, :int
|
40
39
|
def self.#{name}(*args)
|
41
40
|
ret = #{function}(*args)
|
@@ -1,7 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require "rbnacl/sodium"
|
2
2
|
|
3
3
|
module RbNaCl
|
4
4
|
module Sodium
|
5
|
+
# libsodium version API
|
5
6
|
module Version
|
6
7
|
MINIMUM_LIBSODIUM_VERSION = "0.4.3"
|
7
8
|
|
@@ -16,7 +17,7 @@ module RbNaCl
|
|
16
17
|
|
17
18
|
case installed_version <=> minimum_version
|
18
19
|
when -1
|
19
|
-
|
20
|
+
fail "Sorry, you need to install libsodium #{MINIMUM_LIBSODIUM_VERSION}+. You have #{Version::STRING} installed"
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
data/lib/rbnacl/test_vectors.rb
CHANGED
@@ -1,113 +1,115 @@
|
|
1
1
|
# encoding: binary
|
2
|
+
|
3
|
+
# NaCl/libsodium for Ruby
|
2
4
|
module RbNaCl
|
3
5
|
# Reference library of test vectors used to verify the software is correct
|
4
|
-
|
6
|
+
TEST_VECTORS = {
|
5
7
|
#
|
6
8
|
# Curve25519 test vectors
|
7
9
|
# Taken from the NaCl distribution
|
8
10
|
#
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
11
|
+
alice_private: "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a",
|
12
|
+
alice_public: "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a",
|
13
|
+
bob_private: "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb",
|
14
|
+
bob_public: "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f",
|
15
|
+
alice_mult_bob: "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742",
|
14
16
|
|
15
17
|
#
|
16
18
|
# Box test vectors
|
17
19
|
# Taken from the NaCl distribution
|
18
20
|
#
|
19
|
-
:
|
20
|
-
:
|
21
|
-
:
|
22
|
-
"e5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb31"
|
23
|
-
"0e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde"
|
24
|
-
"048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f93776384864"
|
21
|
+
secret_key: "1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389",
|
22
|
+
box_nonce: "69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37",
|
23
|
+
box_message: "be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffc" \
|
24
|
+
"e5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb31" \
|
25
|
+
"0e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde" \
|
26
|
+
"048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f93776384864" \
|
25
27
|
"5e0705",
|
26
28
|
|
27
|
-
:
|
28
|
-
"48332ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c972"
|
29
|
-
"71d2c20f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae"
|
30
|
-
"90224368517acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b3"
|
29
|
+
box_ciphertext: "f3ffc7703f9400e52a7dfb4b3d3305d98e993b9f48681273c29650ba32fc76ce" \
|
30
|
+
"48332ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c972" \
|
31
|
+
"71d2c20f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae" \
|
32
|
+
"90224368517acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b3" \
|
31
33
|
"7973f622a43d14a6599b1f654cb45a74e355a5",
|
32
34
|
|
33
35
|
#
|
34
36
|
# Ed25519 test vectors
|
35
37
|
# Taken from the Python test vectors: http://ed25519.cr.yp.to/python/sign.input
|
36
38
|
#
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:
|
40
|
-
"ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01"
|
41
|
-
"dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313"
|
42
|
-
"c6b1f4556ea26ba49d46e8b561e0fc76633ac9766e68e21fba7edca93c4c7460"
|
39
|
+
sign_private: "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd",
|
40
|
+
sign_public: "77f48b59caeda77751ed138b0ec667ff50f8768c25d48309a8f386a2bad187fb",
|
41
|
+
sign_message: "916c7d1d268fc0e77c1bef238432573c39be577bbea0998936add2b50a653171" \
|
42
|
+
"ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01" \
|
43
|
+
"dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313" \
|
44
|
+
"c6b1f4556ea26ba49d46e8b561e0fc76633ac9766e68e21fba7edca93c4c7460" \
|
43
45
|
"376d7f3ac22ff372c18f613f2ae2e856af40",
|
44
|
-
:
|
46
|
+
sign_signature: "6bd710a368c1249923fc7a1610747403040f0cc30815a00f9ff548a896bbda0b" \
|
45
47
|
"4eb2ca19ebcf917f0f34200a9edbad3901b64ab09cc5ef7b9bcc3c40c0ff7509",
|
46
48
|
|
47
49
|
#
|
48
50
|
# SHA256 test vectors
|
49
51
|
# Taken from the NSRL test vectors: http://www.nsrl.nist.gov/testdata/
|
50
|
-
:
|
52
|
+
sha256_message: "6162636462636465636465666465666765666768666768696768696a68696a6b" \
|
51
53
|
"696a6b6c6a6b6c6d6b6c6d6e6c6d6e6f6d6e6f706e6f7071",
|
52
|
-
:
|
53
|
-
:
|
54
|
+
sha256_digest: "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
|
55
|
+
sha256_empty: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
54
56
|
|
55
57
|
#
|
56
58
|
# SHA512 test vectors
|
57
59
|
# self-created (FIXME: find standard test vectors)
|
58
|
-
:
|
60
|
+
sha512_message: "54686520717569636b2062726f776e20666f78206a756d7073206f7665722074" \
|
59
61
|
"6865206c617a7920646f672e",
|
60
|
-
:
|
62
|
+
sha512_digest: "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bb" \
|
61
63
|
"c6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed",
|
62
|
-
:
|
64
|
+
sha512_empty: "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce" \
|
63
65
|
"47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
|
64
66
|
|
65
67
|
# Blake2b test vectors
|
66
68
|
# self-created? (TODO: double check, fix)
|
67
|
-
:
|
69
|
+
blake2b_message: "54686520717569636b2062726f776e20666f78206a756d7073206f7665722074" \
|
68
70
|
"6865206c617a7920646f67",
|
69
|
-
:
|
71
|
+
blake2b_digest: "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673" \
|
70
72
|
"f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918",
|
71
|
-
:
|
73
|
+
blake2b_empty: "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419" \
|
72
74
|
"d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce",
|
73
75
|
|
74
76
|
# from the Blake2 paper(?) (TODO: double check)
|
75
|
-
:
|
76
|
-
"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
|
77
|
-
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"
|
78
|
-
"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
|
79
|
-
"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
|
80
|
-
"a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
|
81
|
-
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
|
77
|
+
blake2b_keyed_message: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" \
|
78
|
+
"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f" \
|
79
|
+
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f" \
|
80
|
+
"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f" \
|
81
|
+
"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f" \
|
82
|
+
"a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf" \
|
83
|
+
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf" \
|
82
84
|
"e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe",
|
83
|
-
:
|
85
|
+
blake2b_key: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" \
|
84
86
|
"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
|
85
|
-
:
|
87
|
+
blake2b_keyed_digest: "142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e9248" \
|
86
88
|
"4be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461",
|
87
89
|
|
88
90
|
# scrypt test vectors
|
89
91
|
# Taken from http://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01#page-14
|
90
|
-
:
|
91
|
-
"82ad86b83c8f20a23dbb74f6da60b0b6ecffd67134d45946ac8ebfb3064294bc"
|
92
|
+
scrypt_password: "4a857e2ee8aa9b6056f2424e84d24a72473378906ee04a46cb05311502d5250b" \
|
93
|
+
"82ad86b83c8f20a23dbb74f6da60b0b6ecffd67134d45946ac8ebfb3064294bc" \
|
92
94
|
"097d43ced68642bfb8bbbdd0f50b30118f5e",
|
93
|
-
:
|
94
|
-
:
|
95
|
-
:
|
96
|
-
:
|
95
|
+
scrypt_salt: "39d82eef32010b8b79cc5ba88ed539fbaba741100f2edbeca7cc171ffeabf258",
|
96
|
+
scrypt_opslimit: 758_010,
|
97
|
+
scrypt_memlimit: 5_432_947,
|
98
|
+
scrypt_digest: "bcc5c2fd785e4781d1201ed43d84925537e2a540d3de55f5812f29e9dd0a4a00" \
|
97
99
|
"451a5c8ddbb4862c03d45c75bf91b7fb49265feb667ad5c899fdbf2ca19eac67",
|
98
100
|
|
99
101
|
# Auth test vectors
|
100
102
|
# Taken from NaCl distribution
|
101
103
|
#
|
102
|
-
:
|
103
|
-
:
|
104
|
-
"c0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738"
|
105
|
-
"b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da"
|
106
|
-
"99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74"
|
104
|
+
auth_key: "eea6a7251c1e72916d11c2cb214d3c252539121d8e234e652d651fa4c8cff880",
|
105
|
+
auth_message: "8e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186a" \
|
106
|
+
"c0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738" \
|
107
|
+
"b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da" \
|
108
|
+
"99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74" \
|
107
109
|
"e355a5",
|
108
|
-
:
|
110
|
+
auth_onetime: "f3ffc7703f9400e52a7dfb4b3d3305d9",
|
109
111
|
# self-created (FIXME: find standard test vectors)
|
110
|
-
:
|
111
|
-
:
|
112
|
+
auth_hmacsha256: "7f7b9b707e8790ca8620ff94df5e6533ddc8e994060ce310c9d7de04d44aabc3",
|
113
|
+
auth_hmacsha512256: "b2a31b8d4e01afcab2ee545b5caf4e3d212a99d7b3a116a97cec8e83c32e107d"
|
112
114
|
}
|
113
115
|
end
|
data/lib/rbnacl/util.rb
CHANGED
@@ -6,7 +6,9 @@ module RbNaCl
|
|
6
6
|
|
7
7
|
sodium_function :c_verify16, :crypto_verify_16, [:pointer, :pointer]
|
8
8
|
sodium_function :c_verify32, :crypto_verify_32, [:pointer, :pointer]
|
9
|
+
|
9
10
|
module_function
|
11
|
+
|
10
12
|
# Returns a string of n zeros
|
11
13
|
#
|
12
14
|
# Lots of the functions require us to create strings to pass into functions of a specified size.
|
@@ -14,11 +16,11 @@ module RbNaCl
|
|
14
16
|
# @param [Integer] n the size of the string to make
|
15
17
|
#
|
16
18
|
# @return [String] A nice collection of zeros
|
17
|
-
def zeros(n=32)
|
19
|
+
def zeros(n = 32)
|
18
20
|
zeros = "\0" * n
|
19
21
|
# make sure they're 8-bit zeros, not 7-bit zeros. Otherwise we might get
|
20
22
|
# encoding errors later
|
21
|
-
zeros.respond_to?(:force_encoding) ? zeros.force_encoding(
|
23
|
+
zeros.respond_to?(:force_encoding) ? zeros.force_encoding("ASCII-8BIT") : zeros
|
22
24
|
end
|
23
25
|
|
24
26
|
# Prepends a message with zeros
|
@@ -57,15 +59,15 @@ module RbNaCl
|
|
57
59
|
# @param description [String] Description of the string (used in the error)
|
58
60
|
def check_length(string, length, description)
|
59
61
|
if string.nil?
|
60
|
-
|
61
|
-
|
62
|
-
|
62
|
+
fail LengthError,
|
63
|
+
"#{description} was nil (Expected #{length.to_int})",
|
64
|
+
caller
|
63
65
|
end
|
64
66
|
|
65
67
|
if string.bytesize != length.to_int
|
66
|
-
|
67
|
-
|
68
|
-
|
68
|
+
fail LengthError,
|
69
|
+
"#{description} was #{string.bytesize} bytes (Expected #{length.to_int})",
|
70
|
+
caller
|
69
71
|
end
|
70
72
|
true
|
71
73
|
end
|
@@ -83,12 +85,12 @@ module RbNaCl
|
|
83
85
|
# @param description [String] Description of the string (used in the error)
|
84
86
|
def check_string(string, length, description)
|
85
87
|
unless string.respond_to? :to_str
|
86
|
-
|
88
|
+
fail TypeError, "can't convert #{string.class} into String with #to_str"
|
87
89
|
end
|
88
90
|
|
89
91
|
string = string.to_str
|
90
92
|
unless string.encoding == Encoding::BINARY
|
91
|
-
|
93
|
+
fail EncodingError, "strings must use BINARY encoding (got #{string.encoding})"
|
92
94
|
end
|
93
95
|
check_length(string, length, description)
|
94
96
|
|
@@ -180,4 +182,3 @@ module RbNaCl
|
|
180
182
|
end
|
181
183
|
end
|
182
184
|
end
|
183
|
-
|