NAME
Crypt::AuthEnc::ChaCha20Poly1305 - Authenticated encryption in ChaCha20-Poly1305 mode
SYNOPSIS
### OO interfaceuse Crypt::AuthEnc::ChaCha20Poly1305;my $key = '...';my $nonce = '...';my $expected_tag = '...';# encrypt and authenticatemy $ae_enc = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $nonce);$ae_enc->adata_add('additional_authenticated_data1');$ae_enc->adata_add('additional_authenticated_data2');my $ct = $ae_enc->encrypt_add('data1');$ct .= $ae_enc->encrypt_add('data2');$ct .= $ae_enc->encrypt_add('data3');my $tag = $ae_enc->encrypt_done();# decrypt and verifymy $ae_dec = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $nonce);$ae_dec->adata_add('additional_authenticated_data1');$ae_dec->adata_add('additional_authenticated_data2');my $pt = $ae_dec->decrypt_add('ciphertext1');$pt .= $ae_dec->decrypt_add('ciphertext2');$pt .= $ae_dec->decrypt_add('ciphertext3');$ae_dec->decrypt_done($expected_tag) or die "decrypt failed"; # constant-time tag check#or, if you need the computed tag, compare it with slow_eq from Crypt::Misc#(Perl's 'eq' is not constant-time and leaks timing information)my $computed_tag = $ae_dec->decrypt_done();die "decrypt failed" unless Crypt::Misc::slow_eq($computed_tag, $expected_tag);### functional interfaceuse Crypt::AuthEnc::ChaCha20Poly1305 qw(chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify);my $key = '...';my $nonce = '...';my $adata = '...';my $plaintext = '...';my ($ciphertext, $tag) = chacha20poly1305_encrypt_authenticate($key, $nonce, $adata, $plaintext);my $decrypted = chacha20poly1305_decrypt_verify($key, $nonce, $adata, $ciphertext, $tag);
DESCRIPTION
Provides authenticated encryption with ChaCha20-Poly1305 as defined in RFC 7539 <https://www.rfc-editor.org/rfc/rfc7539>.
This is a stateful API. Build one message by calling, in order: "new" or "set_iv", optional "adata_add", zero or more "encrypt_add" or "decrypt_add" calls, then "encrypt_done" or "decrypt_done".
Use a fresh object per message. If you construct with new($key) you must call set_iv($iv) before adding AAD or processing plaintext/ciphertext. When verifying, decrypt_done($expected_tag) is the safer one-step form; decrypt_done() without arguments only returns the calculated tag. The first "encrypt_done" / "decrypt_done" call finalizes the object. After that, further "set_iv", "set_iv_rfc7905", "adata_add", "encrypt_add", "decrypt_add", "encrypt_done", and "decrypt_done" calls croak.
EXPORT
Nothing is exported by default.
You can export selected functions:
use Crypt::AuthEnc::ChaCha20Poly1305 qw(chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify);
FUNCTIONS
chacha20poly1305_encrypt_authenticate
my ($ciphertext, $tag) = chacha20poly1305_encrypt_authenticate($key, $nonce, $adata, $plaintext);# $key ..... [binary string] key of proper length (128 or 256 bits / 16 or 32 bytes)# $nonce ... [binary string] nonce (64 or 96 bits / 8 or 12 bytes)# $adata ... [binary string] additional authenticated data (optional)
chacha20poly1305_decrypt_verify
my $plaintext = chacha20poly1305_decrypt_verify($key, $nonce, $adata, $ciphertext, $tag);# on error returns undef
METHODS
Unless noted otherwise, assume $ae is an existing AEAD object created via "new", for example:
my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $nonce);
new
my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $nonce);#ormy $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key);# $key ..... [binary string] encryption key of proper length (128 or 256 bits / 16 or 32 bytes)# $nonce ... [binary string] nonce (64 or 96 bits / 8 or 12 bytes)
adata_add
Add additional authenticated data. Can be called only before the first "encrypt_add" or "decrypt_add". Returns the object itself (for chaining).
$ae->adata_add($aad_data); # can be called multiple times
encrypt_add
Returns a binary string of ciphertext (raw bytes).
my $ciphertext = $ae->encrypt_add($data); # can be called multiple times
encrypt_done
Returns the authentication tag as a binary string (raw bytes). This call finalizes the current message.
my $tag = $ae->encrypt_done(); # returns $tag value
decrypt_add
Returns a binary string of plaintext (raw bytes).
my $plaintext = $ae->decrypt_add($ciphertext); # can be called multiple times
decrypt_done
Without argument returns the computed tag as a binary string. With $tag argument returns 1 (success) or 0 (failure). This call finalizes the current message.
my $tag = $ae->decrypt_done; # returns $tag value#ormy $result = $ae->decrypt_done($tag); # returns 1 (success) or 0 (failure)
set_iv
my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key)->set_iv($nonce);# $nonce ... [binary string] nonce (64 or 96 bits / 8 or 12 bytes)
Call "set_iv" before the first "adata_add", "encrypt_add", or "decrypt_add" for a message.
set_iv_rfc7905
See RFC 7905 <https://www.rfc-editor.org/rfc/rfc7905>.
my $ae = Crypt::AuthEnc::ChaCha20Poly1305->new($key)->set_iv_rfc7905($nonce, $seqnum);# $nonce ... [binary string] nonce (96 bits / 12 bytes)# $seqnum .. [integer] 64-bit integer (sequence number)
clone
Returns a copy of the AEAD object in its current state.
my $ae_new = $ae->clone;
SEE ALSO
- •
-
CryptX, Crypt::AuthEnc::XChaCha20Poly1305, Crypt::AuthEnc::GCM, Crypt::AuthEnc::CCM, Crypt::AuthEnc::EAX, Crypt::AuthEnc::OCB
- •
-
<https://www.rfc-editor.org/rfc/rfc7539>