package Crypt::AuthEnc::GCM; use strict; use warnings; our $VERSION = '0.091'; require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import'; our %EXPORT_TAGS = ( all => [qw( gcm_encrypt_authenticate gcm_decrypt_verify )] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw(); use Carp; $Carp::Internal{(__PACKAGE__)}++; use CryptX; sub CLONE_SKIP { 1 } # prevent cloning 1; =pod =head1 NAME Crypt::AuthEnc::GCM - Authenticated encryption in GCM mode =head1 SYNOPSIS ### OO interface use Crypt::AuthEnc::GCM; my $key = '...'; my $iv = '...'; my $expected_tag = '...'; # encrypt and authenticate my $ae_enc = Crypt::AuthEnc::GCM->new("AES", $key, $iv); $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 verify my $ae_dec = Crypt::AuthEnc::GCM->new("AES", $key, $iv); $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 interface use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify); my $key = '...'; my $iv = '...'; my $adata = '...'; my $plaintext = '...'; my ($ciphertext, $tag) = gcm_encrypt_authenticate('AES', $key, $iv, $adata, $plaintext); my $decrypted = gcm_decrypt_verify('AES', $key, $iv, $adata, $ciphertext, $tag); =head1 DESCRIPTION Galois/Counter Mode (GCM) - provides encryption and authentication. Use a fresh object per message unless you intentionally reuse the same key via C. The normal call order is: C, one or more C calls, optional C calls, zero or more C / C calls, then C / C. The first C / C call finalizes the object. After that, further C, C, C, C, C, and C calls croak until you call C. If you construct with C, you must provide the IV via C before adding authenticated data or payload data. After C, start a new message with the same key by supplying the IV again, and re-add AAD if needed. When verifying, C is the safer form. The no-argument form of C only returns the computed tag. =head1 EXPORT Nothing is exported by default. You can export selected functions: use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify); =head1 FUNCTIONS =head2 gcm_encrypt_authenticate my ($ciphertext, $tag) = gcm_encrypt_authenticate($cipher, $key, $iv, $adata, $plaintext); # $cipher .. [string] 'AES' or name of any other cipher with 16-byte block len # $key ..... [binary string] AES key of proper length (128/192/256 bits) # $iv ...... [binary string] initialization vector # $adata ... [binary string] additional authenticated data =head2 gcm_decrypt_verify my $plaintext = gcm_decrypt_verify($cipher, $key, $iv, $adata, $ciphertext, $tag); # on error returns undef =head1 METHODS Unless noted otherwise, assume C<$ae> is an existing AEAD object created via C, for example: my $ae = Crypt::AuthEnc::GCM->new($cipher, $key, $iv); =head2 new my $ae = Crypt::AuthEnc::GCM->new($cipher, $key); #or my $ae = Crypt::AuthEnc::GCM->new($cipher, $key, $iv); # $cipher .. [string] 'AES' or name of any other cipher # $key ..... [binary string] encryption key of proper length # $iv ...... [binary string] initialization vector (optional, you can set it later via iv_add method) =head2 iv_add Set initialization vector (IV). Multiple calls are concatenated to form the full IV (the data is appended, not replaced). Returns the object itself. $ae->iv_add($iv_data); # can be called multiple times before AAD/payload Call C before the first C, C, or C. If you reuse the object via C, provide the IV again for the new message. =head2 adata_add Add B. Can be called B all C calls but before the first C or C. Returns the object itself (for chaining). $ae->adata_add($aad_data); # can be called multiple times =head2 encrypt_add Returns a binary string of ciphertext (raw bytes). my $ciphertext = $ae->encrypt_add($data); # can be called multiple times =head2 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 =head2 decrypt_add Returns a binary string of plaintext (raw bytes). my $plaintext = $ae->decrypt_add($ciphertext); # can be called multiple times =head2 decrypt_done Without argument returns the computed tag as a binary string. With C<$tag> argument returns C<1> (success) or C<0> (failure). This call finalizes the current message. my $tag = $ae->decrypt_done; # returns $tag value #or my $result = $ae->decrypt_done($tag); # returns 1 (success) or 0 (failure) Use the C form for authentication checks. The no-argument form only returns the computed tag. =head2 reset $ae->reset; Start a new message with the same key. After C, call C again, then C if needed, before processing payload data. C also clears the finalized state set by C / C. =head2 clone Returns a copy of the AEAD object in its current state. my $ae_new = $ae->clone; =head1 SEE ALSO =over =item * L, L, L, L =item * L =item * L =back =cut