rbnacl 3.1.2 → 3.2.0
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/.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
@@ -14,19 +14,19 @@ module RbNaCl
|
|
14
14
|
STANDARD_GROUP_ELEMENT = ["0900000000000000000000000000000000000000000000000000000000000000"].pack("H*").freeze
|
15
15
|
|
16
16
|
# Order of the standard group
|
17
|
-
STANDARD_GROUP_ORDER = 2**252 +
|
17
|
+
STANDARD_GROUP_ORDER = 2**252 + 27_742_317_777_372_353_535_851_937_790_883_648_493
|
18
18
|
|
19
19
|
include KeyComparator
|
20
20
|
include Serializable
|
21
21
|
|
22
22
|
extend Sodium
|
23
23
|
|
24
|
-
sodium_type
|
24
|
+
sodium_type :scalarmult
|
25
25
|
sodium_primitive :curve25519
|
26
26
|
|
27
|
-
sodium_function
|
28
|
-
|
29
|
-
|
27
|
+
sodium_function :scalarmult_curve25519,
|
28
|
+
:crypto_scalarmult_curve25519,
|
29
|
+
[:pointer, :pointer, :pointer]
|
30
30
|
|
31
31
|
# Number of bytes in a scalar on this curve
|
32
32
|
SCALARBYTES = 32
|
@@ -67,15 +67,21 @@ module RbNaCl
|
|
67
67
|
# Return the point serialized as bytes
|
68
68
|
#
|
69
69
|
# @return [String] 32-byte string representing this point
|
70
|
-
def to_bytes
|
70
|
+
def to_bytes
|
71
|
+
@point
|
72
|
+
end
|
71
73
|
|
72
74
|
@base_point = new(STANDARD_GROUP_ELEMENT)
|
73
75
|
|
74
76
|
# NaCl's standard base point for all Curve25519 public keys
|
75
77
|
#
|
76
78
|
# @return [RbNaCl::Point] standard base point (a.k.a. standard group element)
|
77
|
-
def self.base
|
78
|
-
|
79
|
+
def self.base
|
80
|
+
@base_point
|
81
|
+
end
|
82
|
+
class << self
|
83
|
+
attr_reader :base_point
|
84
|
+
end
|
79
85
|
end
|
80
86
|
end
|
81
87
|
end
|
data/lib/rbnacl/hash.rb
CHANGED
@@ -25,7 +25,7 @@ module RbNaCl
|
|
25
25
|
def self.sha256(data)
|
26
26
|
data = data.to_str
|
27
27
|
digest = Util.zeros(SHA256::BYTES)
|
28
|
-
SHA256.hash_sha256(digest, data, data.bytesize) ||
|
28
|
+
SHA256.hash_sha256(digest, data, data.bytesize) || fail(CryptoError, "Hashing failed!")
|
29
29
|
digest
|
30
30
|
end
|
31
31
|
|
@@ -40,7 +40,7 @@ module RbNaCl
|
|
40
40
|
# @return [String] The SHA-512 hash as raw bytes (Or encoded as per the second argument)
|
41
41
|
def self.sha512(data)
|
42
42
|
digest = Util.zeros(SHA512::BYTES)
|
43
|
-
SHA512.hash_sha512(digest, data, data.bytesize) ||
|
43
|
+
SHA512.hash_sha512(digest, data, data.bytesize) || fail(CryptoError, "Hashing failed!")
|
44
44
|
digest
|
45
45
|
end
|
46
46
|
|
data/lib/rbnacl/hash/blake2b.rb
CHANGED
@@ -13,16 +13,16 @@ module RbNaCl
|
|
13
13
|
class Blake2b
|
14
14
|
extend Sodium
|
15
15
|
|
16
|
-
sodium_type
|
16
|
+
sodium_type :generichash
|
17
17
|
sodium_primitive :blake2b
|
18
|
-
sodium_constant
|
19
|
-
sodium_constant
|
20
|
-
sodium_constant
|
21
|
-
sodium_constant
|
18
|
+
sodium_constant :BYTES_MIN
|
19
|
+
sodium_constant :BYTES_MAX
|
20
|
+
sodium_constant :KEYBYTES_MIN
|
21
|
+
sodium_constant :KEYBYTES_MAX
|
22
22
|
|
23
|
-
sodium_function
|
24
|
-
|
25
|
-
|
23
|
+
sodium_function :generichash_blake2b,
|
24
|
+
:crypto_generichash_blake2b,
|
25
|
+
[:pointer, :size_t, :pointer, :ulong_long, :pointer, :size_t]
|
26
26
|
|
27
27
|
# Create a new Blake2b hash object
|
28
28
|
#
|
@@ -38,15 +38,15 @@ module RbNaCl
|
|
38
38
|
|
39
39
|
if @key
|
40
40
|
@key_size = @key.bytesize
|
41
|
-
|
42
|
-
|
41
|
+
fail LengthError, "key too short" if @key_size < KEYBYTES_MIN
|
42
|
+
fail LengthError, "key too long" if @key_size > KEYBYTES_MAX
|
43
43
|
else
|
44
44
|
@key_size = 0
|
45
45
|
end
|
46
46
|
|
47
47
|
@digest_size = opts.fetch(:digest_size, BYTES_MAX)
|
48
|
-
|
49
|
-
|
48
|
+
fail LengthError, "digest size too short" if @digest_size < BYTES_MIN
|
49
|
+
fail LengthError, "digest size too long" if @digest_size > BYTES_MAX
|
50
50
|
end
|
51
51
|
|
52
52
|
# Calculate a Blake2b digest
|
@@ -56,7 +56,7 @@ module RbNaCl
|
|
56
56
|
# @return [String] Blake2b digest of the string as raw bytes
|
57
57
|
def digest(message)
|
58
58
|
digest = Util.zeros(@digest_size)
|
59
|
-
self.class.generichash_blake2b(digest, @digest_size, message, message.bytesize, @key, @key_size) ||
|
59
|
+
self.class.generichash_blake2b(digest, @digest_size, message, message.bytesize, @key, @key_size) || fail(CryptoError, "Hashing failed!")
|
60
60
|
digest
|
61
61
|
end
|
62
62
|
end
|
data/lib/rbnacl/hash/sha256.rb
CHANGED
@@ -4,12 +4,12 @@ module RbNaCl
|
|
4
4
|
# Provides a binding for the SHA256 function in libsodium
|
5
5
|
module SHA256
|
6
6
|
extend Sodium
|
7
|
-
sodium_type
|
7
|
+
sodium_type :hash
|
8
8
|
sodium_primitive :sha256
|
9
|
-
sodium_constant
|
10
|
-
sodium_function
|
11
|
-
|
12
|
-
|
9
|
+
sodium_constant :BYTES
|
10
|
+
sodium_function :hash_sha256,
|
11
|
+
:crypto_hash_sha256,
|
12
|
+
[:pointer, :pointer, :ulong_long]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/rbnacl/hash/sha512.rb
CHANGED
@@ -4,12 +4,12 @@ module RbNaCl
|
|
4
4
|
# Provides the binding for the SHA512 hash function
|
5
5
|
module SHA512
|
6
6
|
extend Sodium
|
7
|
-
sodium_type
|
7
|
+
sodium_type :hash
|
8
8
|
sodium_primitive :sha512
|
9
|
-
sodium_constant
|
10
|
-
sodium_function
|
11
|
-
|
12
|
-
|
9
|
+
sodium_constant :BYTES
|
10
|
+
sodium_function :hash_sha512,
|
11
|
+
:crypto_hash_sha512,
|
12
|
+
[:pointer, :pointer, :ulong_long]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/rbnacl/hmac/sha256.rb
CHANGED
@@ -15,20 +15,21 @@ module RbNaCl
|
|
15
15
|
class SHA256 < Auth
|
16
16
|
extend Sodium
|
17
17
|
|
18
|
-
sodium_type
|
18
|
+
sodium_type :auth
|
19
19
|
sodium_primitive :hmacsha256
|
20
|
-
sodium_constant
|
21
|
-
sodium_constant
|
20
|
+
sodium_constant :BYTES
|
21
|
+
sodium_constant :KEYBYTES
|
22
|
+
|
23
|
+
sodium_function :auth_hmacsha256,
|
24
|
+
:crypto_auth_hmacsha256,
|
25
|
+
[:pointer, :pointer, :ulong_long, :pointer]
|
26
|
+
|
27
|
+
sodium_function :auth_hmacsha256_verify,
|
28
|
+
:crypto_auth_hmacsha256_verify,
|
29
|
+
[:pointer, :pointer, :ulong_long, :pointer]
|
22
30
|
|
23
|
-
sodium_function :auth_hmacsha256,
|
24
|
-
:crypto_auth_hmacsha256,
|
25
|
-
[:pointer, :pointer, :ulong_long, :pointer]
|
26
|
-
|
27
|
-
sodium_function :auth_hmacsha256_verify,
|
28
|
-
:crypto_auth_hmacsha256_verify,
|
29
|
-
[:pointer, :pointer, :ulong_long, :pointer]
|
30
|
-
|
31
31
|
private
|
32
|
+
|
32
33
|
def compute_authenticator(authenticator, message)
|
33
34
|
self.class.auth_hmacsha256(authenticator, message, message.bytesize, key)
|
34
35
|
end
|
@@ -15,20 +15,21 @@ module RbNaCl
|
|
15
15
|
class SHA512256 < Auth
|
16
16
|
extend Sodium
|
17
17
|
|
18
|
-
sodium_type
|
18
|
+
sodium_type :auth
|
19
19
|
sodium_primitive :hmacsha512256
|
20
|
-
sodium_constant
|
21
|
-
sodium_constant
|
20
|
+
sodium_constant :BYTES
|
21
|
+
sodium_constant :KEYBYTES
|
22
22
|
|
23
|
-
sodium_function
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
sodium_function
|
28
|
-
|
29
|
-
|
23
|
+
sodium_function :auth_hmacsha512256,
|
24
|
+
:crypto_auth_hmacsha512256,
|
25
|
+
[:pointer, :pointer, :ulong_long, :pointer]
|
26
|
+
|
27
|
+
sodium_function :auth_hmacsha512256_verify,
|
28
|
+
:crypto_auth_hmacsha512256_verify,
|
29
|
+
[:pointer, :pointer, :ulong_long, :pointer]
|
30
30
|
|
31
31
|
private
|
32
|
+
|
32
33
|
def compute_authenticator(authenticator, message)
|
33
34
|
self.class.auth_hmacsha512256(authenticator, message, message.bytesize, key)
|
34
35
|
end
|
data/lib/rbnacl/init.rb
CHANGED
@@ -23,9 +23,9 @@ module RbNaCl
|
|
23
23
|
return nil
|
24
24
|
end
|
25
25
|
|
26
|
-
if Util.verify32(
|
26
|
+
if Util.verify32(to_bytes, other)
|
27
27
|
return 0
|
28
|
-
elsif
|
28
|
+
elsif to_bytes > other
|
29
29
|
return 1
|
30
30
|
else
|
31
31
|
return -1
|
@@ -53,7 +53,7 @@ module RbNaCl
|
|
53
53
|
else
|
54
54
|
return false
|
55
55
|
end
|
56
|
-
Util.verify32(
|
56
|
+
Util.verify32(to_bytes, other)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -20,10 +20,10 @@ module RbNaCl
|
|
20
20
|
class Poly1305 < Auth
|
21
21
|
extend Sodium
|
22
22
|
|
23
|
-
sodium_type
|
23
|
+
sodium_type :onetimeauth
|
24
24
|
sodium_primitive :poly1305
|
25
|
-
sodium_constant
|
26
|
-
sodium_constant
|
25
|
+
sodium_constant :BYTES
|
26
|
+
sodium_constant :KEYBYTES
|
27
27
|
|
28
28
|
sodium_function :onetimeauth_poly1305,
|
29
29
|
:crypto_onetimeauth_poly1305,
|
@@ -34,6 +34,7 @@ module RbNaCl
|
|
34
34
|
[:pointer, :pointer, :ulong_long, :pointer]
|
35
35
|
|
36
36
|
private
|
37
|
+
|
37
38
|
def compute_authenticator(authenticator, message)
|
38
39
|
self.class.onetimeauth_poly1305(authenticator, message, message.bytesize, key)
|
39
40
|
end
|
@@ -41,7 +42,6 @@ module RbNaCl
|
|
41
42
|
def verify_message(authenticator, message)
|
42
43
|
self.class.onetimeauth_poly1305_verify(authenticator, message, message.bytesize, key)
|
43
44
|
end
|
44
|
-
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
@@ -19,16 +19,14 @@ module RbNaCl
|
|
19
19
|
extend Sodium
|
20
20
|
|
21
21
|
begin
|
22
|
-
sodium_type
|
22
|
+
sodium_type :pwhash
|
23
23
|
sodium_primitive :scryptsalsa208sha256
|
24
24
|
|
25
25
|
sodium_constant :SALTBYTES
|
26
26
|
|
27
|
-
sodium_function
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
sodium_function :scrypt,
|
28
|
+
:crypto_pwhash_scryptsalsa208sha256,
|
29
|
+
[:pointer, :ulong_long, :pointer, :ulong_long, :pointer, :ulong_long, :size_t]
|
32
30
|
|
33
31
|
# Create a new SCrypt password hash object
|
34
32
|
#
|
@@ -38,11 +36,12 @@ module RbNaCl
|
|
38
36
|
# @return [RbNaCl::PasswordHash::SCrypt] An SCrypt password hasher object
|
39
37
|
def initialize(opslimit, memlimit, digest_size = 64)
|
40
38
|
# TODO: sanity check these parameters
|
41
|
-
@opslimit
|
39
|
+
@opslimit = opslimit
|
40
|
+
@memlimit = memlimit
|
42
41
|
|
43
42
|
# TODO: check digest size validity
|
44
|
-
#raise LengthError, "digest size too short" if @digest_size < BYTES_MIN
|
45
|
-
#raise LengthError, "digest size too long" if @digest_size > BYTES_MAX
|
43
|
+
# raise LengthError, "digest size too short" if @digest_size < BYTES_MIN
|
44
|
+
# raise LengthError, "digest size too long" if @digest_size > BYTES_MAX
|
46
45
|
|
47
46
|
@digest_size = digest_size
|
48
47
|
end
|
@@ -57,11 +56,11 @@ module RbNaCl
|
|
57
56
|
digest = Util.zeros(@digest_size)
|
58
57
|
salt = Util.check_string(salt, SALTBYTES, "salt")
|
59
58
|
|
60
|
-
self.class.scrypt(digest, @digest_size, password, password.bytesize, salt, @opslimit, @memlimit) ||
|
59
|
+
self.class.scrypt(digest, @digest_size, password, password.bytesize, salt, @opslimit, @memlimit) || fail(CryptoError, "scrypt failed!")
|
61
60
|
digest
|
62
61
|
end
|
63
62
|
rescue FFI::NotFoundError
|
64
|
-
def initialize(
|
63
|
+
def initialize(_opslimit, _memlimit, _digest_size = 64)
|
65
64
|
raise NotImplementedError, "scrypt not implemented in this version of libsodium"
|
66
65
|
end
|
67
66
|
end
|
data/lib/rbnacl/random.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "thread"
|
2
2
|
|
3
3
|
# encoding: binary
|
4
4
|
module RbNaCl
|
@@ -19,7 +19,7 @@ module RbNaCl
|
|
19
19
|
# @param [Integer] n number of random bytes desired
|
20
20
|
#
|
21
21
|
# @return [String] random bytes.
|
22
|
-
def self.random_bytes(n=32)
|
22
|
+
def self.random_bytes(n = 32)
|
23
23
|
buf = RbNaCl::Util.zeros(n)
|
24
24
|
@mutex.synchronize { c_random_bytes(buf, n) }
|
25
25
|
buf
|
@@ -20,12 +20,12 @@ module RbNaCl
|
|
20
20
|
class XSalsa20Poly1305
|
21
21
|
extend Sodium
|
22
22
|
|
23
|
-
sodium_type
|
23
|
+
sodium_type :secretbox
|
24
24
|
sodium_primitive :xsalsa20poly1305
|
25
|
-
sodium_constant
|
26
|
-
sodium_constant
|
27
|
-
sodium_constant
|
28
|
-
sodium_constant
|
25
|
+
sodium_constant :KEYBYTES
|
26
|
+
sodium_constant :NONCEBYTES
|
27
|
+
sodium_constant :ZEROBYTES
|
28
|
+
sodium_constant :BOXZEROBYTES
|
29
29
|
|
30
30
|
sodium_function :secretbox_xsalsa20poly1305,
|
31
31
|
:crypto_secretbox_xsalsa20poly1305,
|
@@ -67,10 +67,12 @@ module RbNaCl
|
|
67
67
|
msg = Util.prepend_zeros(ZEROBYTES, message)
|
68
68
|
ct = Util.zeros(msg.bytesize)
|
69
69
|
|
70
|
-
self.class.secretbox_xsalsa20poly1305(ct, msg, msg.bytesize, nonce, @key)
|
70
|
+
success = self.class.secretbox_xsalsa20poly1305(ct, msg, msg.bytesize, nonce, @key)
|
71
|
+
fail CryptoError, "Encryption failed" unless success
|
72
|
+
|
71
73
|
Util.remove_zeros(BOXZEROBYTES, ct)
|
72
74
|
end
|
73
|
-
|
75
|
+
alias_method :encrypt, :box
|
74
76
|
|
75
77
|
# Decrypts a ciphertext
|
76
78
|
#
|
@@ -91,35 +93,47 @@ module RbNaCl
|
|
91
93
|
ct = Util.prepend_zeros(BOXZEROBYTES, ciphertext)
|
92
94
|
message = Util.zeros(ct.bytesize)
|
93
95
|
|
94
|
-
self.class.secretbox_xsalsa20poly1305_open(message, ct, ct.bytesize, nonce, @key)
|
96
|
+
success = self.class.secretbox_xsalsa20poly1305_open(message, ct, ct.bytesize, nonce, @key)
|
97
|
+
fail CryptoError, "Decryption failed. Ciphertext failed verification." unless success
|
98
|
+
|
95
99
|
Util.remove_zeros(ZEROBYTES, message)
|
96
100
|
end
|
97
|
-
|
101
|
+
alias_method :decrypt, :open
|
98
102
|
|
99
103
|
# The crypto primitive for the SecretBox instance
|
100
104
|
#
|
101
105
|
# @return [Symbol] The primitive used
|
102
|
-
def primitive
|
106
|
+
def primitive
|
107
|
+
self.class.primitive
|
108
|
+
end
|
103
109
|
|
104
110
|
# The nonce bytes for the SecretBox class
|
105
111
|
#
|
106
112
|
# @return [Integer] The number of bytes in a valid nonce
|
107
|
-
def self.nonce_bytes
|
113
|
+
def self.nonce_bytes
|
114
|
+
NONCEBYTES
|
115
|
+
end
|
108
116
|
|
109
117
|
# The nonce bytes for the SecretBox instance
|
110
118
|
#
|
111
119
|
# @return [Integer] The number of bytes in a valid nonce
|
112
|
-
def nonce_bytes
|
120
|
+
def nonce_bytes
|
121
|
+
NONCEBYTES
|
122
|
+
end
|
113
123
|
|
114
124
|
# The key bytes for the SecretBox class
|
115
125
|
#
|
116
126
|
# @return [Integer] The number of bytes in a valid key
|
117
|
-
def self.key_bytes
|
127
|
+
def self.key_bytes
|
128
|
+
KEYBYTES
|
129
|
+
end
|
118
130
|
|
119
131
|
# The key bytes for the SecretBox instance
|
120
132
|
#
|
121
133
|
# @return [Integer] The number of bytes in a valid key
|
122
|
-
def key_bytes
|
134
|
+
def key_bytes
|
135
|
+
KEYBYTES
|
136
|
+
end
|
123
137
|
end
|
124
138
|
end
|
125
139
|
end
|
data/lib/rbnacl/self_test.rb
CHANGED
@@ -2,14 +2,16 @@
|
|
2
2
|
|
3
3
|
start = Time.now if $DEBUG
|
4
4
|
|
5
|
+
# NaCl/libsodium for Ruby
|
5
6
|
module RbNaCl
|
6
7
|
class SelfTestFailure < RbNaCl::CryptoError; end
|
7
8
|
|
9
|
+
# Self-test performed at startup
|
8
10
|
module SelfTest
|
9
11
|
module_function
|
10
12
|
|
11
13
|
def vector(name)
|
12
|
-
[
|
14
|
+
[TEST_VECTORS[name]].pack("H*")
|
13
15
|
end
|
14
16
|
|
15
17
|
def box_test
|
@@ -30,27 +32,18 @@ module RbNaCl
|
|
30
32
|
message = vector :box_message
|
31
33
|
ciphertext = vector :box_ciphertext
|
32
34
|
|
33
|
-
unless box.encrypt(nonce, message) == ciphertext
|
34
|
-
|
35
|
-
raise SelfTestFailure, "failed to generate correct ciphertext"
|
36
|
-
#:nocov:
|
37
|
-
end
|
38
|
-
|
39
|
-
unless box.decrypt(nonce, ciphertext) == message
|
40
|
-
#:nocov:
|
41
|
-
raise SelfTestFailure, "failed to decrypt ciphertext correctly"
|
42
|
-
#:nocov:
|
43
|
-
end
|
35
|
+
fail SelfTestFailure, "failed to generate correct ciphertext" unless box.encrypt(nonce, message) == ciphertext
|
36
|
+
fail SelfTestFailure, "failed to decrypt ciphertext correctly" unless box.decrypt(nonce, ciphertext) == message
|
44
37
|
|
45
38
|
begin
|
46
39
|
passed = false
|
47
40
|
corrupt_ct = ciphertext.dup
|
48
|
-
corrupt_ct[23] =
|
41
|
+
corrupt_ct[23] = " "
|
49
42
|
box.decrypt(nonce, corrupt_ct)
|
50
43
|
rescue CryptoError
|
51
44
|
passed = true
|
52
45
|
ensure
|
53
|
-
passed
|
46
|
+
passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext")
|
54
47
|
end
|
55
48
|
end
|
56
49
|
|
@@ -60,7 +53,7 @@ module RbNaCl
|
|
60
53
|
|
61
54
|
unless verify_key.to_s == vector(:sign_public)
|
62
55
|
#:nocov:
|
63
|
-
|
56
|
+
fail SelfTestFailure, "failed to generate verify key correctly"
|
64
57
|
#:nocov:
|
65
58
|
end
|
66
59
|
|
@@ -69,24 +62,24 @@ module RbNaCl
|
|
69
62
|
|
70
63
|
unless signature == vector(:sign_signature)
|
71
64
|
#:nocov:
|
72
|
-
|
65
|
+
fail SelfTestFailure, "failed to generate correct signature"
|
73
66
|
#:nocov:
|
74
67
|
end
|
75
68
|
|
76
69
|
unless verify_key.verify(signature, message)
|
77
70
|
#:nocov:
|
78
|
-
|
71
|
+
fail SelfTestFailure, "failed to verify a valid signature"
|
79
72
|
#:nocov:
|
80
73
|
end
|
81
74
|
|
82
75
|
begin
|
83
76
|
passed = false
|
84
|
-
bad_signature = signature[0,63] +
|
77
|
+
bad_signature = signature[0, 63] + "0"
|
85
78
|
verify_key.verify(bad_signature, message)
|
86
79
|
rescue CryptoError
|
87
80
|
passed = true
|
88
81
|
ensure
|
89
|
-
passed
|
82
|
+
passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext")
|
90
83
|
end
|
91
84
|
end
|
92
85
|
|
@@ -94,11 +87,7 @@ module RbNaCl
|
|
94
87
|
message = vector :sha256_message
|
95
88
|
digest = vector :sha256_digest
|
96
89
|
|
97
|
-
unless RbNaCl::Hash.sha256(message) == digest
|
98
|
-
#:nocov:
|
99
|
-
raise SelfTestFailure, "failed to generate a correct SHA256 digest"
|
100
|
-
#:nocov:
|
101
|
-
end
|
90
|
+
fail SelfTestFailure, "failed to generate a correct SHA256 digest" unless RbNaCl::Hash.sha256(message) == digest
|
102
91
|
end
|
103
92
|
|
104
93
|
def hmac_test(klass, tag)
|
@@ -106,25 +95,16 @@ module RbNaCl
|
|
106
95
|
|
107
96
|
message = vector :auth_message
|
108
97
|
|
109
|
-
unless authenticator.auth(message) == vector(tag)
|
110
|
-
|
111
|
-
raise SelfTestFailure, "#{klass} failed to generate correct authentication tag"
|
112
|
-
#:nocov:
|
113
|
-
end
|
114
|
-
|
115
|
-
unless authenticator.verify(vector(tag), message)
|
116
|
-
#:nocov:
|
117
|
-
raise SelfTestFailure, "#{klass} failed to verify correct authentication tag"
|
118
|
-
#:nocov:
|
119
|
-
end
|
98
|
+
fail SelfTestFailure, "#{klass} failed to generate correct authentication tag" unless authenticator.auth(message) == vector(tag)
|
99
|
+
fail SelfTestFailure, "#{klass} failed to verify correct authentication tag" unless authenticator.verify(vector(tag), message)
|
120
100
|
|
121
101
|
begin
|
122
102
|
passed = false
|
123
|
-
authenticator.verify(vector(tag), message +
|
103
|
+
authenticator.verify(vector(tag), message + " ")
|
124
104
|
rescue CryptoError
|
125
105
|
passed = true
|
126
106
|
ensure
|
127
|
-
passed
|
107
|
+
passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext")
|
128
108
|
end
|
129
109
|
end
|
130
110
|
end
|