Module: AEZ

Extended by:
FFI::Library
Defined in:
lib/aez.rb,
lib/aez/version.rb

Overview

AEZv5 ruby binding. [AEZv5](web.cs.ucdavis.edu/~rogaway/aez)

Defined Under Namespace

Classes: Error

Constant Summary collapse

MAX_CIPHER_TXT_LENGTH =
2**32 - 1
VERSION =
'0.1.5'

Class Method Summary collapse

Class Method Details

.decrypt(key, ciphertxt, ad, nonce, abyte) ⇒ String

Decrypt a message.

Parameters:

  • key (String)

    key with binary format.

  • ciphertxt (String)

    cipher text with binary format. the ciphertext must not be larger than ‘2^32 - 1`.

  • ad (String)

    ad with binary format.

  • nonce (String)

    nonce with binary format. The nonce length must be ‘1..=16`.

  • abyte (Integer)

    authenticator length which determines how much longer a ciphertext is than its plaintext.

Returns:

  • (String)

    plain text with binary format.

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aez.rb', line 62

def decrypt(key, ciphertxt, ad, nonce, abyte)
  raise Error, 'invalid nonce.' if nonce.empty? || nonce.bytesize > 16
  raise Error, 'ciphertxt length too long.' unless ciphertxt.bytesize < MAX_CIPHER_TXT_LENGTH

  with_context(key) do |context|
    ciphertxt_m = FFI::MemoryPointer.new(:uchar, ciphertxt.bytesize).put_bytes(0, ciphertxt)
    ad_m = ad.empty? ? nil : FFI::MemoryPointer.new(:char, ad.bytesize).put_bytes(0, ad)
    nonce_m = FFI::MemoryPointer.new(:char, nonce.bytesize).put_bytes(0, nonce)
    dest = FFI::MemoryPointer.new(:char, ciphertxt.bytesize - abyte)
    result = aez_decrypt(context, nonce_m, nonce.bytesize, ad_m, ad.bytesize, abyte, ciphertxt_m, ciphertxt.bytesize, dest)
    raise Error, 'decrypt failure.' unless result == 0

    dest.read_string(ciphertxt.bytesize - abyte)
  end
end

.encrypt(key, message, ad, nonce, abyte) ⇒ String

Encrypt a message. these extra bytes add authentication.

Parameters:

  • key (String)

    key with binary format.

  • message (String)

    message with binary format.

  • ad (String)

    ad with binary format.

  • nonce (String)

    nonce with binary format. The nonce length must be ‘1..=16`

  • abyte (Integer)

    authenticator length which determines how much longer a ciphertext is than its plaintext.

Returns:

  • (String)

    cipher text with binary format. The ciphertext may be up to 16 bytes larger than the message,

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aez.rb', line 41

def encrypt(key, message, ad, nonce, abyte)
  raise Error, 'invalid nonce.' if nonce.empty? || nonce.bytesize > 16

  with_context(key) do |context|
    message_m = message.empty? ? nil : FFI::MemoryPointer.new(:uchar, message.bytesize).put_bytes(0, message)
    ad_m = ad.empty? ? nil : FFI::MemoryPointer.new(:char, ad.bytesize).put_bytes(0, ad)
    nonce_m = FFI::MemoryPointer.new(:char, nonce.bytesize).put_bytes(0, nonce)
    dest = FFI::MemoryPointer.new(:char, message.bytesize + abyte)

    aez_encrypt(context, nonce_m, nonce.bytesize, ad_m, ad.bytesize, abyte, message_m, message.bytesize, dest)
    dest.read_string(message.bytesize + abyte)
  end
end

.with_context(key) {|context| ... } ⇒ Object

Yields:

  • (context)


78
79
80
81
82
83
# File 'lib/aez.rb', line 78

def with_context(key)
  context = FFI::MemoryPointer.new(144)
  key_m = FFI::MemoryPointer.new(:uchar, key.bytesize).put_bytes(0, key)
  aez_setup(key_m, key.bytesize, context)
  yield(context) if block_given?
end