Skip to content

Commit

Permalink
Add Documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
didactic-drunk committed Aug 6, 2019
1 parent fde955c commit 31c3ead
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions benchmarks/crypto_box.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ ebufs1 = sizes.map { |size| Bytes.new(size + Sodium::CryptoBox::MAC_SIZE) }.to_a
dbufs2 = sizes.map { |size| Bytes.new(size) }.to_a
ebufs2 = sizes.map { |size| Bytes.new(size + Sodium::CryptoBox::PublicKey::SEAL_SIZE) }.to_a

Benchmark.ips do |bm|
Benchmark.ips warmup: 0.5 do |bm|
sizes.each_with_index do |size, i|
dbuf = dbufs1[i]
ebuf = ebufs1[i]

bm.report "box encrypt #{size}" do
to_alice.encrypt_easy dbuf, ebuf, nonce: nonce
to_alice.encrypt dbuf, ebuf, nonce: nonce
end

bm.report "box decrypt #{size}" do
from_bob.decrypt_easy ebuf, dbuf, nonce: nonce
from_bob.decrypt ebuf, dbuf, nonce: nonce
end
end

Expand Down
11 changes: 10 additions & 1 deletion src/sodium/crypto_box.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ module Sodium
# TODO: precompute using crypto_box_beforenm
end

# Encrypts data and returns {ciphertext, nonce}
def encrypt(src)
encrypt src.to_slice
end

def encrypt(src : Bytes, dst = Bytes.new(src.bytesize + MAC_SIZE), nonce = Nonce.new)
# Encrypts data and returns {ciphertext, nonce}
#
# Optionally supply a destination buffer.
def encrypt(src : Bytes, dst = Bytes.new(src.bytesize + MAC_SIZE), nonce = Nonce.new) : {Bytes, Nonce}
if LibSodium.crypto_box_easy(dst, src, src.bytesize, nonce.to_slice, @public_key.to_slice, @secret_key.to_slice) != 0
raise Error.new("crypto_box_easy")
end
{dst, nonce}
end

# Returns decrypted message.
#
def decrypt(src)
decrypt src.to_slice
end

# Returns decrypted message.
#
# Optionally supply a destination buffer.
def decrypt(src : Bytes, dst = Bytes.new(src.bytesize - MAC_SIZE), nonce = Nonce.new) : Bytes
if LibSodium.crypto_box_open_easy(dst, src, src.bytesize, nonce.to_slice, @public_key.to_slice, @secret_key.to_slice) != 0
raise Error::DecryptionFailed.new("crypto_box_open_easy")
Expand Down
1 change: 1 addition & 0 deletions src/sodium/crypto_box/public_key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Sodium::CryptoBox
KEY_SIZE = LibSodium.crypto_box_publickeybytes.to_i
SEAL_SIZE = LibSodium.crypto_box_sealbytes

# Returns key
delegate to_slice, to: @bytes

# :nodoc:
Expand Down
1 change: 1 addition & 0 deletions src/sodium/crypto_box/secret_key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Sodium::CryptoBox

getter public_key : PublicKey

# Returns key
delegate to_slice, to: @sbuf

@seed : SecureBuffer?
Expand Down
2 changes: 2 additions & 0 deletions src/sodium/lib_sodium.cr
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ module Sodium
end

module Sodium
# Constant time memory compare.
def self.memcmp(a : Bytes, b : Bytes) : Bool
if a.bytesize != b.bytesize
false
Expand All @@ -293,6 +294,7 @@ module Sodium
end
end

# Constant time memory compare.
# Raises unless comparison succeeds.
def self.memcmp!(a, b)
raise Error::MemcmpFailed.new unless memcmp(a, b)
Expand Down
11 changes: 10 additions & 1 deletion src/sodium/secret_box.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Sodium
NONCE_SIZE = LibSodium.crypto_secretbox_noncebytes.to_i
MAC_SIZE = LibSodium.crypto_secretbox_macbytes.to_i

# Returns key
delegate to_slice, to: @buf

# Generate a new random key held in a SecureBuffer.
Expand All @@ -37,18 +38,22 @@ module Sodium

# Copy bytes to a new SecureBuffer
#
# Optionally erases bytes after copying if erase is set
# Optionally erases bytes after copying if erase is set.
def initialize(bytes : Bytes, erase = false)
if bytes.bytesize != KEY_SIZE
raise ArgumentError.new("Secret key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}")
end
@buf = SecureBuffer.new bytes, erase: erase
end

# Encrypts data and returns {ciphertext, nonce}
def encrypt(data)
encrypt data.to_slice
end

# Encrypts data and returns {ciphertext, nonce}
#
# Optionally supply a destination buffer.
def encrypt(src : Bytes, dst : Bytes = Bytes.new(src.bytesize + MAC_SIZE), nonce : Nonce = Nonce.new) : {Bytes, Nonce}
if dst.bytesize != (src.bytesize + MAC_SIZE)
raise ArgumentError.new("dst.bytesize must be src.bytesize + MAC_SIZE, got #{dst.bytesize}")
Expand All @@ -59,13 +64,17 @@ module Sodium
{dst, nonce}
end

# Returns decrypted message.
def decrypt(src : Bytes, nonce : Nonce) : Bytes
dst_size = src.bytesize - MAC_SIZE
raise Sodium::Error::DecryptionFailed.new("encrypted data too small #{src.bytesize}") if dst_size <= 0
dst = Bytes.new dst_size
decrypt(src, dst, nonce)
end

# Returns decrypted message.
#
# Optionally supply a destination buffer.
def decrypt(src : Bytes, dst : Bytes, nonce : Nonce) : Bytes
if dst.bytesize != (src.bytesize - MAC_SIZE)
raise ArgumentError.new("dst.bytesize must be src.bytesize - MAC_SIZE, got #{dst.bytesize}")
Expand Down
1 change: 1 addition & 0 deletions src/sodium/sign/public_key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Sodium
KEY_SIZE = LibSodium.crypto_sign_publickeybytes.to_i
SIG_SIZE = LibSodium.crypto_sign_bytes.to_i

# Returns key
delegate to_slice, to: @bytes

# :nodoc:
Expand Down
1 change: 1 addition & 0 deletions src/sodium/sign/secret_key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Sodium

getter public_key : PublicKey

# Returns key
delegate to_slice, to: @sbuf

@seed : SecureBuffer?
Expand Down

0 comments on commit 31c3ead

Please sign in to comment.