ffi-libsodium 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3e68050c2be22e883d5a4b8d33e506716eb4832
4
- data.tar.gz: 1b0db0b2e8deba32fe9d50b2541ec33dc4555d47
3
+ metadata.gz: 193aa2e92e5fc462d8d7f3c68a2ab71c9fb855a0
4
+ data.tar.gz: abc95a71587cea89a4d119ed6a1625b4337532cd
5
5
  SHA512:
6
- metadata.gz: 665970302f6c60c038d6c101976ac89609fb18c34c2c8527f18a7450f36c6368b31568aa526add077c02fb489025c8c0a665288e784593a6099ada95eb8f4a37
7
- data.tar.gz: 3f952d79deca01a35f021c311d9c148192c058d486e5cd2dc7c4aa98e174831d1130028cec4610beebf33a2db2e17450864d95f5a0010849bd9aa5b9ad73d494
6
+ metadata.gz: 19ab15b9ced7dbbc0da8b58bfbe2717c55dad11731ecd09c0c7c774a42a3b58260467ee6116fd17a65b01d8d020679fba5bb9a770d1c972007d2b20a20d2bbb7
7
+ data.tar.gz: e757e619ae02c46e91c336aebc1b632c0a652eec8e71453bf9efc3870e2be4448fd0fc22bef42e456889865fdb7c63a04242a99ad7cf138db6c278245491f5c0
@@ -29,8 +29,8 @@ module Crypto
29
29
  NPUBBYTES = npubbytes.freeze
30
30
  ABYTES = abytes.freeze
31
31
 
32
- attach_function :crypto_aead_chacha20poly1305_encrypt, [:buffer_out, :buffer_out, :buffer_in, :ulong_long, :buffer_in, :ulong_long, :pointer, :buffer_in, :buffer_in], :int, blocking: true
33
- attach_function :crypto_aead_chacha20poly1305_decrypt, [:buffer_out, :buffer_out, :pointer, :buffer_in, :ulong_long, :buffer_in, :ulong_long, :buffer_in, :buffer_in], :int, blocking: true
32
+ attach_function :crypto_aead_chacha20poly1305_encrypt, [:buffer_out, :pointer, :buffer_in, :ulong_long, :buffer_in, :ulong_long, :pointer, :buffer_in, :buffer_in], :int, blocking: true
33
+ attach_function :crypto_aead_chacha20poly1305_decrypt, [:buffer_out, :pointer, :pointer, :buffer_in, :ulong_long, :buffer_in, :ulong_long, :buffer_in, :buffer_in], :int, blocking: true
34
34
 
35
35
  module_function
36
36
 
@@ -45,30 +45,30 @@ module Crypto
45
45
  check_length(key, KEYBYTES, :SecretKey)
46
46
 
47
47
  ciphertext = FFI::MemoryPointer.new(:uchar, message_len + ABYTES)
48
- ciphertext_len = FFI::MemoryPointer.new(:ulong_long)
49
48
  key.readonly if key.is_a?(Sodium::SecretBuffer)
50
- crypto_aead_chacha20poly1305_encrypt(ciphertext, ciphertext_len, message, message_len, additional_data, additional_data_len, nil, nonce, key)
49
+ crypto_aead_chacha20poly1305_encrypt(ciphertext, nil, message, message_len, additional_data, additional_data_len, nil, nonce, key)
51
50
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
52
51
 
53
- [ciphertext, ciphertext_len.read_ulong_long]
52
+ ciphertext
54
53
  end
55
54
 
56
- def decrypt(ciphertext, clen, additional_data, nonce, key)
57
- ciphertext_len = get_int(clen)
55
+ def decrypt(ciphertext, additional_data, nonce, key)
56
+ unless ((ciphertext_len = get_size(ciphertext)) - ABYTES) > 0
57
+ fail Sodium::LengthError, "Ciphertext is too short", caller
58
+ end
58
59
  additional_data_len = get_size(additional_data)
59
60
  check_length(nonce, NPUBBYTES, :Nonce)
60
61
  check_length(key, KEYBYTES, :SecretKey)
61
62
 
62
- decrypted = FFI::MemoryPointer.new(:uchar, ciphertext_len)
63
- decrypted_len = FFI::MemoryPointer.new(:ulong_long)
63
+ decrypted = FFI::MemoryPointer.new(:uchar, ciphertext_len - ABYTES)
64
64
  key.readonly if key.is_a?(Sodium::SecretBuffer)
65
- rc = crypto_aead_chacha20poly1305_decrypt(decrypted, decrypted_len, nil, ciphertext, ciphertext_len, additional_data, additional_data_len, nonce, key)
65
+ rc = crypto_aead_chacha20poly1305_decrypt(decrypted, nil, nil, ciphertext, ciphertext_len, additional_data, additional_data_len, nonce, key)
66
66
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
67
67
  if rc == -1
68
68
  raise Sodium::CryptoError, "Message forged", caller
69
69
  end
70
70
 
71
- [decrypted, decrypted_len.read_ulong_long]
71
+ decrypted
72
72
  end
73
73
  end
74
74
  end
data/lib/crypto/auth.rb CHANGED
@@ -48,7 +48,9 @@ module Crypto
48
48
  end
49
49
  end
50
50
 
51
- def self.auth(*args)
51
+ module_function
52
+
53
+ def auth(*args)
52
54
  Auth.auth(*args)
53
55
  end
54
56
  end
data/lib/crypto/box.rb CHANGED
@@ -55,8 +55,10 @@ module Crypto
55
55
 
56
56
  public_key = Sodium::Buffer.new(:uchar, PUBLICKEYBYTES)
57
57
  secret_key = Sodium::Buffer.new(:uchar, SECRETKEYBYTES)
58
+ seed.readonly if seed.is_a?(Sodium::SecretBuffer)
58
59
  crypto_box_seed_keypair(public_key, secret_key, seed)
59
-
60
+ seed.noaccess if seed.is_a?(Sodium::SecretBuffer)
61
+
60
62
  [public_key, secret_key]
61
63
  end
62
64
 
@@ -73,8 +75,10 @@ module Crypto
73
75
  check_length(seed, SEEDBYTES, :Seed)
74
76
 
75
77
  public_key = Sodium::Buffer.new(:uchar, PUBLICKEYBYTES)
76
- secret_key = Sodium::SecretBuffer.new(:uchar, SECRETKEYBYTES)
78
+ secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES)
79
+ seed.readonly if seed.is_a?(Sodium::SecretBuffer)
77
80
  crypto_box_seed_keypair(public_key, secret_key, seed)
81
+ seed.noaccess if seed.is_a?(Sodium::SecretBuffer)
78
82
  secret_key.noaccess
79
83
 
80
84
  [public_key, secret_key]
@@ -86,7 +90,7 @@ module Crypto
86
90
  check_length(public_key, PUBLICKEYBYTES, :PublicKey)
87
91
  check_length(secret_key, SECRETKEYBYTES, :SecretKey)
88
92
 
89
- ciphertext = Sodium::Buffer.new(:uchar, MACBYTES + message_len)
93
+ ciphertext = Sodium::Buffer.new(:uchar, message_len + MACBYTES)
90
94
  secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
91
95
  crypto_box_easy(ciphertext, message, message_len, nonce, public_key, secret_key)
92
96
  secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer)
@@ -49,7 +49,7 @@ module Crypto
49
49
  RandomBytes.buf(SALTBYTES)
50
50
  end
51
51
 
52
- def scryptsalsa208sha256(passwd, outlen, salt, opslimit = OPSLIMIT_INTERACTIVE, memlimit = MEMLIMIT_INTERACTIVE)
52
+ def scryptsalsa208sha256(outlen, passwd, salt, opslimit = OPSLIMIT_INTERACTIVE, memlimit = MEMLIMIT_INTERACTIVE)
53
53
  passwd_len = get_size(passwd)
54
54
  check_length(salt, SALTBYTES, :Salt)
55
55
  if opslimit < OPSLIMIT_INTERACTIVE
@@ -37,7 +37,7 @@ module Crypto
37
37
 
38
38
  def scalarmut(secret_key, public_key)
39
39
  check_length(secret_key, SCALARBYTES, :SecretKey)
40
- check_length(public_key, SCALARBYTES, :PublicKey)
40
+ check_length(public_key, BYTES, :PublicKey)
41
41
 
42
42
  shared_secret = Sodium::SecretBuffer.new(BYTES)
43
43
  secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
@@ -35,7 +35,7 @@ module Crypto
35
35
  check_length(nonce, NONCEBYTES, :Nonce)
36
36
  check_length(key, KEYBYTES, :SecretKey)
37
37
 
38
- ciphertext = Sodium::Buffer.new(:uchar, MACBYTES + message_len)
38
+ ciphertext = Sodium::Buffer.new(:uchar, message_len + MACBYTES)
39
39
  key.readonly if key.is_a?(Sodium::SecretBuffer)
40
40
  crypto_secretbox_easy(ciphertext, message, message_len, nonce, key)
41
41
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
data/lib/crypto/sign.rb CHANGED
@@ -75,24 +75,25 @@ module Crypto
75
75
  message_len = get_size(message)
76
76
  check_length(secret_key, SECRETKEYBYTES, :SecretKey)
77
77
 
78
- sealed_message = FFI::MemoryPointer.new(:uchar, BYTES + message_len)
79
- sealed_message_len = FFI::MemoryPointer.new(:ulong_long)
78
+ sealed_message = Sodium::Buffer.new(:uchar, message_len + BYTES)
80
79
  secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
81
- crypto_sign(sealed_message, sealed_message_len, message, message_len, secret_key)
80
+ crypto_sign(sealed_message, nil, message, message_len, secret_key)
82
81
  secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer)
83
82
 
84
- [sealed_message, sealed_message_len.read_ulong_long]
83
+ sealed_message
85
84
  end
86
85
 
87
- def open(sealed_message, smlen, public_key)
88
- sealed_message_len = get_int(smlen)
86
+ def open(sealed_message, public_key)
87
+ sealed_message_len = get_size(sealed_message)
89
88
  check_length(public_key, PUBLICKEYBYTES, :PublicKey)
90
89
 
91
- unsealed_message = FFI::MemoryPointer.new(:uchar, sealed_message_len)
90
+ unsealed_message = Sodium::Buffer.new(:uchar, sealed_message_len - BYTES)
92
91
  unsealed_message_len = FFI::MemoryPointer.new(:ulong_long)
93
- crypto_sign_open(unsealed_message, unsealed_message_len, sealed_message, sealed_message_len, public_key)
92
+ if crypto_sign_open(unsealed_message, unsealed_message_len, sealed_message, sealed_message_len, public_key) == -1
93
+ raise Sodium::CryptoError, "Incorrect signature", caller
94
+ end
94
95
 
95
- [unsealed_message, unsealed_message_len.read_ulong_long]
96
+ unsealed_message
96
97
  end
97
98
  end
98
99
 
data/lib/sodium/utils.rb CHANGED
@@ -57,6 +57,8 @@ module Sodium
57
57
  data.bytesize
58
58
  elsif data.is_a?(FFI::Pointer) ||data.respond_to?(:size)
59
59
  data.size
60
+ elsif data.nil?
61
+ 0
60
62
  else
61
63
  fail ArgumentError, "#{data.class} doesn't respond to :bytesize or :size", caller
62
64
  end
@@ -1,3 +1,3 @@
1
1
  module Sodium
2
- VERSION = '0.0.5'.freeze
2
+ VERSION = '0.0.6'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-libsodium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hendrik Beskow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-05 00:00:00.000000000 Z
11
+ date: 2014-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi