Skip to content

Commit

Permalink
Merge pull request #131 from gssbzn/rubocop-improvements
Browse files Browse the repository at this point in the history
Improve rubocop settings
  • Loading branch information
tarcieri committed Apr 20, 2016
2 parents 7947c71 + 432cf8a commit 4411b66
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 105 deletions.
11 changes: 8 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LineLength:
Style/StringLiterals:
EnforcedStyle: double_quotes

Style/SingleSpaceBeforeFirstArg:
Style/SpaceBeforeFirstArg:
Enabled: false

Style/GlobalVars:
Expand All @@ -20,8 +20,13 @@ Style/GlobalVars:
#

Metrics/MethodLength:
CountComments: false
Max: 25
Max: 22

Metrics/AbcSize:
Max: 20

AllCops:
Include:
- '**/Rakefile'
Exclude:
- 'spec/**/*'
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ To use RbNaCl, you will need to install libsodium:

https://github.com/jedisct1/libsodium

At least version `1.0.0` is recommended.

For OS X users, libsodium is available via homebrew and can be installed with:

brew install libsodium
Expand Down
6 changes: 3 additions & 3 deletions lib/rbnacl/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def auth(message)
def verify(authenticator, message)
auth = authenticator.to_s
Util.check_length(auth, tag_bytes, "Provided authenticator")
verify_message(auth, message) || fail(BadAuthenticatorError, "Invalid authenticator provided, message is corrupt")
verify_message(auth, message) || raise(BadAuthenticatorError, "Invalid authenticator provided, message is corrupt")
end

# The crypto primitive for this authenticator instance
Expand Down Expand Up @@ -111,11 +111,11 @@ def tag_bytes
private

def compute_authenticator(_authenticator, _message)
fail NotImplementedError
raise NotImplementedError
end

def verify_message(_authenticator, _message)
fail NotImplementedError
raise NotImplementedError
end
end
end
12 changes: 6 additions & 6 deletions lib/rbnacl/boxes/curve25519xsalsa20poly1305.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Curve25519XSalsa20Poly1305
def initialize(public_key, private_key)
@public_key = public_key.is_a?(PublicKey) ? public_key : PublicKey.new(public_key)
@private_key = private_key.is_a?(PrivateKey) ? private_key : PrivateKey.new(private_key)
fail IncorrectPrimitiveError unless @public_key.primitive == primitive && @private_key.primitive == primitive
raise IncorrectPrimitiveError unless @public_key.primitive == primitive && @private_key.primitive == primitive
end

# Encrypts a message
Expand All @@ -121,10 +121,10 @@ def box(nonce, message)
msg = Util.prepend_zeros(ZEROBYTES, message)
ct = Util.zeros(msg.bytesize)

self.class.box_curve25519xsalsa20poly1305_afternm(ct, msg, msg.bytesize, nonce, beforenm) || fail(CryptoError, "Encryption failed")
self.class.box_curve25519xsalsa20poly1305_afternm(ct, msg, msg.bytesize, nonce, beforenm) || raise(CryptoError, "Encryption failed")
Util.remove_zeros(BOXZEROBYTES, ct)
end
alias_method :encrypt, :box
alias encrypt box

# Decrypts a ciphertext
#
Expand All @@ -146,11 +146,11 @@ def open(nonce, ciphertext)
message = Util.zeros(ct.bytesize)

success = self.class.box_curve25519xsalsa20poly1305_open_afternm(message, ct, ct.bytesize, nonce, beforenm)
fail CryptoError, "Decryption failed. Ciphertext failed verification." unless success
raise CryptoError, "Decryption failed. Ciphertext failed verification." unless success

Util.remove_zeros(ZEROBYTES, message)
end
alias_method :decrypt, :open
alias decrypt open

# The crypto primitive for the box class
#
Expand Down Expand Up @@ -179,7 +179,7 @@ def beforenm
@_key ||= begin
key = Util.zeros(BEFORENMBYTES)
success = self.class.box_curve25519xsalsa20poly1305_beforenm(key, @public_key.to_s, @private_key.to_s)
fail CryptoError, "Failed to derive shared key" unless success
raise CryptoError, "Failed to derive shared key" unless success
key
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialize(private_key)
def self.generate
pk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES)
sk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES)
box_curve25519xsalsa20poly1305_keypair(pk, sk) || fail(CryptoError, "Failed to generate a key pair")
box_curve25519xsalsa20poly1305_keypair(pk, sk) || raise(CryptoError, "Failed to generate a key pair")
new(sk)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rbnacl/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Hash
def self.sha256(data)
data = data.to_str
digest = Util.zeros(SHA256::BYTES)
SHA256.hash_sha256(digest, data, data.bytesize) || fail(CryptoError, "Hashing failed!")
SHA256.hash_sha256(digest, data, data.bytesize) || raise(CryptoError, "Hashing failed!")
digest
end

Expand All @@ -40,7 +40,7 @@ def self.sha256(data)
# @return [String] The SHA-512 hash digest as raw bytes
def self.sha512(data)
digest = Util.zeros(SHA512::BYTES)
SHA512.hash_sha512(digest, data, data.bytesize) || fail(CryptoError, "Hashing failed!")
SHA512.hash_sha512(digest, data, data.bytesize) || raise(CryptoError, "Hashing failed!")
digest
end

Expand Down
10 changes: 5 additions & 5 deletions lib/rbnacl/hash/blake2b.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def initialize(opts = {})

if @key
@key_size = @key.bytesize
fail LengthError, "key too short" if @key_size < KEYBYTES_MIN
fail LengthError, "key too long" if @key_size > KEYBYTES_MAX
raise LengthError, "key too short" if @key_size < KEYBYTES_MIN
raise LengthError, "key too long" if @key_size > KEYBYTES_MAX
else
@key_size = 0
end

@digest_size = opts.fetch(:digest_size, BYTES_MAX)
fail LengthError, "digest size too short" if @digest_size < BYTES_MIN
fail LengthError, "digest size too long" if @digest_size > BYTES_MAX
raise LengthError, "digest size too short" if @digest_size < BYTES_MIN
raise LengthError, "digest size too long" if @digest_size > BYTES_MAX

@personal = opts.fetch(:personal, EMPTY_PERSONAL)
@personal = Util.zero_pad(PERSONALBYTES, @personal)
Expand All @@ -72,7 +72,7 @@ def initialize(opts = {})
def digest(message)
digest = Util.zeros(@digest_size)
self.class.generichash_blake2b(digest, @digest_size, message, message.bytesize, @key, @key_size, @salt, @personal) ||
fail(CryptoError, "Hashing failed!")
raise(CryptoError, "Hashing failed!")
digest
end
end
Expand Down
21 changes: 13 additions & 8 deletions lib/rbnacl/key_comparator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ def <=>(other)
else
return nil
end

if Util.verify32(to_bytes, other)
return 0
elsif to_bytes > other
return 1
else
return -1
end
compare32(other)
end

# equality operator
Expand All @@ -55,5 +48,17 @@ def ==(other)
end
Util.verify32(to_bytes, other)
end

private

def compare32(other)
if Util.verify32(to_bytes, other)
0
elsif to_bytes > other
1
else
-1
end
end
end
end
83 changes: 38 additions & 45 deletions lib/rbnacl/password_hash/scrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,45 @@ module PasswordHash
# on GPUs or FPGAs) with additional computation.
class SCrypt
extend Sodium
sodium_type :pwhash
sodium_primitive :scryptsalsa208sha256

sodium_constant :SALTBYTES

sodium_function :scrypt,
:crypto_pwhash_scryptsalsa208sha256,
[:pointer, :ulong_long, :pointer, :ulong_long, :pointer, :ulong_long, :size_t]

# Create a new SCrypt password hash object
#
# @param [Integer] opslimit the CPU cost (e.g. 2**20)
# @param [Integer] memlimit the memory cost (e.g. 2**24)
#
# @return [RbNaCl::PasswordHash::SCrypt] An SCrypt password hasher object
def initialize(opslimit, memlimit, digest_size = 64)
# TODO: sanity check these parameters
@opslimit = opslimit
@memlimit = memlimit

# TODO: check digest size validity
# raise LengthError, "digest size too short" if @digest_size < BYTES_MIN
# raise LengthError, "digest size too long" if @digest_size > BYTES_MAX

@digest_size = digest_size
end

begin
sodium_type :pwhash
sodium_primitive :scryptsalsa208sha256

sodium_constant :SALTBYTES

sodium_function :scrypt,
:crypto_pwhash_scryptsalsa208sha256,
[:pointer, :ulong_long, :pointer, :ulong_long, :pointer, :ulong_long, :size_t]

# Create a new SCrypt password hash object
#
# @param [Integer] opslimit the CPU cost (e.g. 2**20)
# @param [Integer] memlimit the memory cost (e.g. 2**24)
#
# @return [RbNaCl::PasswordHash::SCrypt] An SCrypt password hasher object
def initialize(opslimit, memlimit, digest_size = 64)
# TODO: sanity check these parameters
@opslimit = opslimit
@memlimit = memlimit

# TODO: check digest size validity
# raise LengthError, "digest size too short" if @digest_size < BYTES_MIN
# raise LengthError, "digest size too long" if @digest_size > BYTES_MAX

@digest_size = digest_size
end

# Calculate an scrypt digest for a given password and salt
#
# @param [String] password to be hashed
# @param [String] salt to make the digest unique
#
# @return [String] scrypt digest of the string as raw bytes
def digest(password, salt)
digest = Util.zeros(@digest_size)
salt = Util.check_string(salt, SALTBYTES, "salt")

self.class.scrypt(digest, @digest_size, password, password.bytesize, salt, @opslimit, @memlimit) || fail(CryptoError, "scrypt failed!")
digest
end
rescue FFI::NotFoundError
def initialize(_opslimit, _memlimit, _digest_size = 64)
raise NotImplementedError, "scrypt not implemented in this version of libsodium"
end
# Calculate an scrypt digest for a given password and salt
#
# @param [String] password to be hashed
# @param [String] salt to make the digest unique
#
# @return [String] scrypt digest of the string as raw bytes
def digest(password, salt)
digest = Util.zeros(@digest_size)
salt = Util.check_string(salt, SALTBYTES, "salt")

self.class.scrypt(digest, @digest_size, password, password.bytesize, salt, @opslimit, @memlimit) || raise(CryptoError, "scrypt failed!")
digest
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rbnacl/secret_boxes/xsalsa20poly1305.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def box(nonce, message)
ct = Util.zeros(msg.bytesize)

success = self.class.secretbox_xsalsa20poly1305(ct, msg, msg.bytesize, nonce, @key)
fail CryptoError, "Encryption failed" unless success
raise CryptoError, "Encryption failed" unless success

Util.remove_zeros(BOXZEROBYTES, ct)
end
alias_method :encrypt, :box
alias encrypt box

# Decrypts a ciphertext
#
Expand All @@ -94,11 +94,11 @@ def open(nonce, ciphertext)
message = Util.zeros(ct.bytesize)

success = self.class.secretbox_xsalsa20poly1305_open(message, ct, ct.bytesize, nonce, @key)
fail CryptoError, "Decryption failed. Ciphertext failed verification." unless success
raise CryptoError, "Decryption failed. Ciphertext failed verification." unless success

Util.remove_zeros(ZEROBYTES, message)
end
alias_method :decrypt, :open
alias decrypt open

# The crypto primitive for the SecretBox instance
#
Expand Down
22 changes: 11 additions & 11 deletions lib/rbnacl/self_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def box_common_test(box)
message = vector :box_message
ciphertext = vector :box_ciphertext

fail SelfTestFailure, "failed to generate correct ciphertext" unless box.encrypt(nonce, message) == ciphertext
fail SelfTestFailure, "failed to decrypt ciphertext correctly" unless box.decrypt(nonce, ciphertext) == message
raise SelfTestFailure, "failed to generate correct ciphertext" unless box.encrypt(nonce, message) == ciphertext
raise SelfTestFailure, "failed to decrypt ciphertext correctly" unless box.decrypt(nonce, ciphertext) == message

begin
passed = false
Expand All @@ -43,7 +43,7 @@ def box_common_test(box)
rescue CryptoError
passed = true
ensure
passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext")
passed || raise(SelfTestFailure, "failed to detect corrupt ciphertext")
end
end

Expand All @@ -53,7 +53,7 @@ def digital_signature_test

unless verify_key.to_s == vector(:sign_public)
#:nocov:
fail SelfTestFailure, "failed to generate verify key correctly"
raise SelfTestFailure, "failed to generate verify key correctly"
#:nocov:
end

Expand All @@ -62,13 +62,13 @@ def digital_signature_test

unless signature == vector(:sign_signature)
#:nocov:
fail SelfTestFailure, "failed to generate correct signature"
raise SelfTestFailure, "failed to generate correct signature"
#:nocov:
end

unless verify_key.verify(signature, message)
#:nocov:
fail SelfTestFailure, "failed to verify a valid signature"
raise SelfTestFailure, "failed to verify a valid signature"
#:nocov:
end

Expand All @@ -79,32 +79,32 @@ def digital_signature_test
rescue CryptoError
passed = true
ensure
passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext")
passed || raise(SelfTestFailure, "failed to detect corrupt ciphertext")
end
end

def sha256_test
message = vector :sha256_message
digest = vector :sha256_digest

fail SelfTestFailure, "failed to generate a correct SHA256 digest" unless RbNaCl::Hash.sha256(message) == digest
raise SelfTestFailure, "failed to generate a correct SHA256 digest" unless RbNaCl::Hash.sha256(message) == digest
end

def hmac_test(klass, tag)
authenticator = klass.new(vector(:auth_key))

message = vector :auth_message

fail SelfTestFailure, "#{klass} failed to generate correct authentication tag" unless authenticator.auth(message) == vector(tag)
fail SelfTestFailure, "#{klass} failed to verify correct authentication tag" unless authenticator.verify(vector(tag), message)
raise SelfTestFailure, "#{klass} failed to generate correct authentication tag" unless authenticator.auth(message) == vector(tag)
raise SelfTestFailure, "#{klass} failed to verify correct authentication tag" unless authenticator.verify(vector(tag), message)

begin
passed = false
authenticator.verify(vector(tag), message + " ")
rescue CryptoError
passed = true
ensure
passed || fail(SelfTestFailure, "failed to detect corrupt ciphertext")
passed || raise(SelfTestFailure, "failed to detect corrupt ciphertext")
end
end
end
Expand Down
Loading

0 comments on commit 4411b66

Please sign in to comment.