class Crypto::AeadChacha20Poly1305

Overview

AEAD_CHACHA20_POLY1305 is an authenticated encryption with additional data algorithm.

Defined in:

crypto/aead.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(key : Bytes, nonce : Bytes, io : IO) #

The inputs to AEAD_CHACHA20_POLY1305 are:

  • key: A 256-bit key
  • nonce: A 96-bit nonce -- different for each invocation with the same key
  • io: the buffer to write the authenticated plain and ciphertext to

[View source]

Class Method Detail

def self.decrypt(key : Bytes, nonce : Bytes, aad : Bytes, ciphertext : Bytes, tag : Bytes) : Bytes #

The inverse of .encrypt. Verifies tag against #aad and ciphertext before decrypting, raising TagException when it does not match.


[View source]
def self.encrypt(key : Bytes, nonce : Bytes, aad : Bytes, plaintext : Bytes) : Tuple(Bytes, Bytes) #

One-shot form of AEAD_CHACHA20_POLY1305 as specified in RFC 8439 section 2.8, as an alternative to the streaming/IO based instance interface above. Useful whenever a protocol needs a plain encrypt(plaintext, aad) -> {ciphertext, tag} call per message, such as protecting a single TLS 1.3 record (RFC 8446 section 5.2).

This method does not construct the per-record nonce for you: per RFC 8446 section 5.3 it is the connection's fixed IV XORed with the big-endian, zero left-padded 64-bit record sequence number.

Returns the ciphertext (same length as plaintext) and the 16-byte authentication tag.


[View source]

Instance Method Detail

def aad(data : Bytes) #

Arbitrary length additional authenticated data (AAD). Needs to be written before the plaintext. Can be called multiple times, but only the last block can be less then 16 bytes.


[View source]
def decrypt(data : Bytes, tag : Bytes) : Bytes #

decrpyt parses the data and verifies the data with the tag returns the additional authenticated data. The plaintext is written to the provided mem, in case the tag is not validating the data an exception is raised.


[View source]
def final : Bytes #

write final footer


[View source]
def update(data : Bytes) #

An arbitrary length plaintext, has to be multiples of 16 bytes last call might be with less then 16 bytes


[View source]