Skip to content

Commit

Permalink
Merge pull request #189 from valthon/master
Browse files Browse the repository at this point in the history
add fallback constants for Argon2
  • Loading branch information
tarcieri committed Jan 14, 2019
2 parents f72fdca + 0242903 commit 2084365
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/rbnacl/boxes/curve25519xsalsa20poly1305.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Curve25519XSalsa20Poly1305
sodium_constant :BOXZEROBYTES
sodium_constant :BEFORENMBYTES
sodium_constant :PUBLICKEYBYTES
sodium_constant :SECRETKEYBYTES, :PRIVATEKEYBYTES
sodium_constant :SECRETKEYBYTES, name: :PRIVATEKEYBYTES

sodium_function :box_curve25519xsalsa20poly1305_beforenm,
:crypto_box_curve25519xsalsa20poly1305_beforenm,
Expand Down
24 changes: 12 additions & 12 deletions lib/rbnacl/password_hash/argon2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class Argon2
sodium_constant :ALG_ARGON2I13
sodium_constant :ALG_ARGON2ID13 if Sodium::Version::ARGON2ID_SUPPORTED

sodium_constant :SALTBYTES # 16
sodium_constant :STRBYTES # 128
sodium_constant :OPSLIMIT_INTERACTIVE # 4
sodium_constant :MEMLIMIT_INTERACTIVE # 2 ** 25 (32mb)
sodium_constant :OPSLIMIT_MODERATE # 6
sodium_constant :MEMLIMIT_MODERATE # 2 ** 27 (128mb)
sodium_constant :OPSLIMIT_SENSITIVE # 8
sodium_constant :MEMLIMIT_SENSITIVE # 2 ** 29 (512mb)
sodium_constant :MEMLIMIT_MIN # 8192
sodium_constant :MEMLIMIT_MAX # 4_294_967_296
sodium_constant :OPSLIMIT_MIN # 3
sodium_constant :OPSLIMIT_MAX # 10
sodium_constant :SALTBYTES, fallback: 16
sodium_constant :STRBYTES, fallback: 128
sodium_constant :OPSLIMIT_INTERACTIVE, fallback: 4
sodium_constant :MEMLIMIT_INTERACTIVE, fallback: 2**25 # (32mb)
sodium_constant :OPSLIMIT_MODERATE, fallback: 6
sodium_constant :MEMLIMIT_MODERATE, fallback: 2**27 # (128mb)
sodium_constant :OPSLIMIT_SENSITIVE, fallback: 8
sodium_constant :MEMLIMIT_SENSITIVE, fallback: 2**29 # (512mb)
sodium_constant :MEMLIMIT_MIN, fallback: 8192
sodium_constant :MEMLIMIT_MAX, fallback: 4_294_967_296
sodium_constant :OPSLIMIT_MIN, fallback: 3
sodium_constant :OPSLIMIT_MAX, fallback: 10

ARGON2_MIN_OUTLEN = 16
ARGON2_MAX_OUTLEN = 0xFFFFFFFF
Expand Down
6 changes: 3 additions & 3 deletions lib/rbnacl/signatures/ed25519.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module Ed25519
sodium_type :sign
sodium_primitive :ed25519
sodium_constant :SEEDBYTES
sodium_constant :PUBLICKEYBYTES, :VERIFYKEYBYTES
sodium_constant :SECRETKEYBYTES, :SIGNINGKEYBYTES
sodium_constant :BYTES, :SIGNATUREBYTES
sodium_constant :PUBLICKEYBYTES, name: :VERIFYKEYBYTES
sodium_constant :SECRETKEYBYTES, name: :SIGNINGKEYBYTES
sodium_constant :BYTES, name: :SIGNATUREBYTES
end
end
end
21 changes: 13 additions & 8 deletions lib/rbnacl/sodium.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ def primitive
sodium_primitive
end

def sodium_constant(constant, name = constant)
fn = if sodium_primitive
"crypto_#{sodium_type}_#{sodium_primitive}_#{constant.to_s.downcase}"
else
"crypto_#{sodium_type}_#{constant.to_s.downcase}"
end
attach_function fn, [], :size_t
const_set(name, public_send(fn))
def sodium_constant(constant, name: constant, fallback: nil)
fn_name_components = ["crypto", sodium_type, sodium_primitive, constant.to_s.downcase]
fn_name = fn_name_components.compact.join("_")

begin
attach_function fn_name, [], :size_t
rescue FFI::NotFoundError
raise if fallback.nil?

define_singleton_method fn_name, -> { fallback }
end

const_set(name, public_send(fn_name))
end

def sodium_function(name, function, arguments)
Expand Down
51 changes: 51 additions & 0 deletions spec/rbnacl/sodium_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
RSpec.describe RbNaCl::Sodium do
subject(:sodium_class) do
class SodiumExtendedClass
extend RbNaCl::Sodium

sodium_type :auth
sodium_primitive :hmacsha512
# sodium_constant :BYTES
# sodium_constant :KEYBYTES
end
SodiumExtendedClass
end

context ".sodium_constant" do
it "retrieves the libsodium constant" do
sodium_class.sodium_constant :BYTES
expect(sodium_class::BYTES).to eq(64)
end

context "with alternate constant name" do
it "sets the alternate constant name" do
sodium_class.sodium_constant :BYTES, name: :COOL_BYTES
expect(sodium_class::COOL_BYTES).to eq(64)
end
end

context "when libsodium does not define the constant" do
it "raises an exception" do
expect do
sodium_class.sodium_constant :MIN_DANCING_PARTNERS
end.to raise_error(FFI::NotFoundError)
end
end

context "with fallback" do
context "when libsodium defines the constant" do
it "return the libsodium value" do
sodium_class.sodium_constant :BYTES, fallback: 888
expect(sodium_class::BYTES).to eq(64)
end
end

context "when libsodium does not define the constant" do
it "uses the fallback" do
sodium_class.sodium_constant :MAX_PANDAS, fallback: 24
expect(sodium_class::MAX_PANDAS).to eq(24)
end
end
end
end
end

0 comments on commit 2084365

Please sign in to comment.