From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

Crypt::GCM - Galois/Counter Mode (GCM)

SYNOPSIS

my $gcm = Crypt::GCM->new(-key => $key, -cipher => 'Crypt::Rijndael');
my $gcm->set_iv($iv);
my $gcm->aad('');
my $cipher_string = $gcm->encrypt($message);
my $tag = $gcm->tag;

DESCRIPTION

The module implements the Galois/Counter Mode (GCM) for Confidentiality and Authentication. The function of GCM in which the plaintext is encrypted into the ciphertext, and an authentication tag is generated on the AAD and the ciphertext.

new()

my $cipher = Crypt::GCM->new(
-key => pack 'H*', '00000000000000000000000000000000',
-cipher => 'Crypt::Rijndael',
);

The new() method creates an new Crypt::GCM object. It accepts a list of -argument => value pairs selected from the following list:

Argument Description
-------- -----------
-key The encryption/decryption key (required)
-cipher The cipher algorithm (required)

encrypt()

my $ciphertext = $cipher->encrypt($plaintext);

decrypt()

my $plaintext = $cipher->decrypt($ciphertext);

set_iv()

$cipher->set_iv($iv);

This allows you to change the initialization vector. allow 16byte string.

aad()

$cipher->aad($text);
my $text = $cipher->aad();

tag()

$cipher->tag($tag);
my $tag = $cipher->tag();

EXAMPLE

Encrypt

use strict;
my $cipher = Crypt::GCM->new(
-key => pack 'H*', '00000000000000000000000000000000',
-cipher => 'Crypt::Rijndael',
);
$cipher->set_iv(pack 'H*', '000000000000000000000000');
$cipher->aad('');
my $ciphertext = $cipher->encrypt(pack 'H*', '000000000000000000000000000000');
my $tag = $cipher->tag;

Decrypt

use strict;
my $cipher = Crypt::GCM->new(
-key => pack 'H*', '00000000000000000000000000000000',
-cipher => 'Crypt::Rijndael',
);
$cipher->set_iv(pack 'H*', '000000000000000000000000');
$cipher->aad('');
$cipher->tag(pack 'H*', 'ab6e47d42cec13bdf53a67b21257bddf');
my $plaintext = $cipher->decrypt(pack 'H*', '0388dace60b6a392f328c2b971b2fe78');
if (!defined $plaintext) {
die 'cannot decrypt on GCM mode. please check your Authentication Tag';
}

SEE ALSO

NIST Special Publication 800-38D - Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) for Confidentiality and Authenticaton.

http://csrc.nist.gov/publications/drafts/Draft-NIST_SP800-38D_Public_Comment.pdf

AUTHOR

Hiroyuki OYAMA, <oyama@module.jp>

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Hiroyuki OYAMA.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.