Crypt::AuthEnc::GCM(3pm)

NAME
Crypt::AuthEnc::GCM - Authenticated encryption in GCM mode

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);

DESCRIPTION
Galois/Counter Mode (GCM) - provides encryption and authentication.

Use a fresh object per message unless you intentionally reuse the same key via "reset". The normal call order is: "new", one or more "iv_add" calls, optional "adata_add" calls, zero or more "encrypt_add" / "decrypt_add" calls, then "encrypt_done" / "decrypt_done". The first "encrypt_done" / "decrypt_done" call finalizes the object. After that, further "iv_add", "adata_add", "encrypt_add", "decrypt_add", "encrypt_done", and "decrypt_done" calls croak until you call "reset".

If you construct with "new($cipher, $key)", you must provide the IV via "iv_add" before adding authenticated data or payload data. After "reset", start a new message with the same key by supplying the IV again, and re-add AAD if needed.

When verifying, decrypt_done($expected_tag) is the safer form. The no-argument form of "decrypt_done" only returns the computed tag.

EXPORT
Nothing is exported by default.

You can export selected functions:

  use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify);

FUNCTIONS

  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

  gcm_decrypt_verify
 my $plaintext = gcm_decrypt_verify($cipher, $key, $iv, $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::GCM->new($cipher, $key, $iv);

  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)

  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 "iv_add" before the first "adata_add", "encrypt_add", or "decrypt_add". If you reuse the object via "reset", provide the IV again for the new message.

  adata_add
Add additional authenticated data. Can be called after all "iv_add" calls but 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
 #or
 my $result = $ae->decrypt_done($tag);  # returns 1 (success) or 0 (failure)

Use the decrypt_done($tag) form for authentication checks. The no-argument form only returns the computed tag.

  reset
 $ae->reset;

Start a new message with the same key. After "reset", call "iv_add" again, then "adata_add" if needed, before processing payload data. "reset" also clears the finalized state set by "encrypt_done" / "decrypt_done".

  clone
Returns a copy of the AEAD object in its current state.

 my $ae_new = $ae->clone;

SEE ALSO

•   CryptX, Crypt::AuthEnc::CCM, Crypt::AuthEnc::EAX, Crypt::AuthEnc::OCB

•   <https://en.wikipedia.org/wiki/Galois/Counter_Mode>

•   <https://csrc.nist.gov/pubs/sp/800/38/d/final>