Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ext/mri/bcrypt_pbkdf_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ static VALUE bc_crypt_pbkdf(VALUE self, VALUE pass, VALUE salt, VALUE keylen, VA
StringValue(salt);

size_t okeylen = NUM2ULONG(keylen);
if (okeylen == 0 || okeylen > 1024)
LONG_LONG rounds_value = NUM2LL(rounds);
if (rounds_value > UINT_MAX)
rb_raise(rb_eRangeError, "rounds %lld too big to convert to 'unsigned int'", rounds_value);
if (rounds_value < 1 || okeylen == 0 || okeylen > 1024)
return Qnil;
u_int8_t* okey = xmalloc(okeylen);
VALUE out;
Expand All @@ -20,7 +23,7 @@ static VALUE bc_crypt_pbkdf(VALUE self, VALUE pass, VALUE salt, VALUE keylen, VA
RSTRING_PTR(pass), RSTRING_LEN(pass),
(const u_int8_t*)RSTRING_PTR(salt), RSTRING_LEN(salt),
okey, okeylen,
NUM2ULONG(rounds));
(unsigned int)rounds_value);
if (ret < 0) {
xfree(okey);
return Qnil;
Expand Down
14 changes: 14 additions & 0 deletions test/bcrypt_pnkdf/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def test_invalid_pbkdf_arguments_return_nil
assert_nil BCryptPbkdf::Engine.__bc_crypt_pbkdf('pass', 'salt', 1025, 1)
end

def test_negative_rounds_return_nil
assert_nil BCryptPbkdf::Engine.__bc_crypt_pbkdf('pass', 'salt', 32, -1)
assert_nil BCryptPbkdf::Engine.__bc_crypt_pbkdf('pass', 'salt', 32, -(1 << 31))
end

def test_rounds_must_fit_the_native_range
assert_raises(RangeError) do
BCryptPbkdf::Engine.__bc_crypt_pbkdf('pass', 'salt', 32, (1 << 32) + 1)
end
assert_raises(TypeError) do
BCryptPbkdf::Engine.__bc_crypt_pbkdf('pass', 'salt', 32, Object.new)
end
end

def test_hash_argument_lengths
sha2pass = OpenSSL::Digest.digest('SHA512', 'pass')
sha2salt = OpenSSL::Digest.digest('SHA512', 'salt')
Expand Down
Loading