DeoxysII (DeoxysII v1.0.2)

View Source

This work is derived from: https://github.com/oasisprotocol/deoxysii

Copyright (c) 2019 Oasis Labs Inc. info@oasislabs.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Summary

Functions

Decrypts a ciphertext using Deoxys-II authenticated decryption.

Encrypts a message using Deoxys-II authenticated encryption.

Functions

decrypt(self, nonce, ad, ciphertext)

Decrypts a ciphertext using Deoxys-II authenticated decryption.

Parameters

  • self: A DeoxysII struct containing the derived key.
  • nonce: A binary nonce (must be 15 bytes).
  • ad: Associated data that will be authenticated.
  • ciphertext: The ciphertext to decrypt (with authentication tag appended).

Returns

The decrypted message if the authentication tag is valid, nil otherwise.

Examples

iex> key = <<0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
...>         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f>>
iex> nonce = <<0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e>>
iex> deoxys = DeoxysII.new(key)
iex> ciphertext = <<0x2b, 0x97, 0xbd, 0x77, 0x71, 0x2f, 0x0c, 0xde, 0x97, 0x53, 0x09, 0x95, 0x9d, 0xfe, 0x1d, 0x7c>>
iex> DeoxysII.decrypt(deoxys, nonce, <<>>, ciphertext)
<<>>

# Encrypt and then decrypt a message with associated data
iex> key = <<0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
...>         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f>>
iex> nonce = DeoxysII.random_nonce()
iex> ad = <<0x00, 0x01, 0x02>>
iex> msg = <<0x10, 0x11, 0x12>>
iex> deoxys = DeoxysII.new(key)
iex> ct = DeoxysII.encrypt(deoxys, nonce, ad, msg)
iex> DeoxysII.decrypt(deoxys, nonce, ad, ct)
<<0x10, 0x11, 0x12>>

encrypt(self, nonce, ad, msg)

Encrypts a message using Deoxys-II authenticated encryption.

Parameters

  • self: A DeoxysII struct containing the derived key.
  • nonce: A binary nonce (must be 15 bytes).
  • ad: Associated data that will be authenticated but not encrypted.
  • msg: The message to encrypt.

Returns

The encrypted message with the authentication tag appended.

Example

# Create a new DeoxysII instance with a 32-byte key
iex> key = Base.decode16!("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", case: :lower)
iex> nonce = DeoxysII.random_nonce()
iex> ad = Base.decode16!("000102030405060708090a0b0c0d0e0f", case: :lower)
iex> msg = Base.decode16!("000102030405060708090a0b0c0d0e0f", case: :lower)
iex> x = DeoxysII.new(key)
iex> ciphertext = DeoxysII.encrypt(x, nonce, ad, msg)
iex> byte_size(ciphertext) == byte_size(msg) + DeoxysII.tag_size()
true

new(key)

nonce_size()

random_nonce()

tag_size()