1Crypt::AuthEnc::GCM(3)User Contributed Perl DocumentationCrypt::AuthEnc::GCM(3)
2
3
4

NAME

6       Crypt::AuthEnc::GCM - Authenticated encryption in GCM mode
7

SYNOPSIS

9        ### OO interface
10        use Crypt::AuthEnc::GCM;
11
12        # encrypt and authenticate
13        my $ae = Crypt::AuthEnc::GCM->new("AES", $key, $iv);
14        $ae->adata_add('additional_authenticated_data1');
15        $ae->adata_add('additional_authenticated_data2');
16        my $ct = $ae->encrypt_add('data1');
17        $ct .= $ae->encrypt_add('data2');
18        $ct .= $ae->encrypt_add('data3');
19        my $tag = $ae->encrypt_done();
20
21        # decrypt and verify
22        my $ae = Crypt::AuthEnc::GCM->new("AES", $key, $iv);
23        $ae->adata_add('additional_authenticated_data1');
24        $ae->adata_add('additional_authenticated_data2');
25        my $pt = $ae->decrypt_add('ciphertext1');
26        $pt .= $ae->decrypt_add('ciphertext2');
27        $pt .= $ae->decrypt_add('ciphertext3');
28        my $tag = $ae->decrypt_done();
29        die "decrypt failed" unless $tag eq $expected_tag;
30
31        #or
32        my $result = $ae->decrypt_done($expected_tag); # 0 or 1
33
34        ### functional interface
35        use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify);
36
37        my ($ciphertext, $tag) = gcm_encrypt_authenticate('AES', $key, $iv, $adata, $plaintext);
38        my $plaintext = gcm_decrypt_verify('AES', $key, $iv, $adata, $ciphertext, $tag);
39

DESCRIPTION

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

EXPORT

44       Nothing is exported by default.
45
46       You can export selected functions:
47
48         use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify);
49

FUNCTIONS

51   gcm_encrypt_authenticate
52        my ($ciphertext, $tag) = gcm_encrypt_authenticate($cipher, $key, $iv, $adata, $plaintext);
53
54        # $cipher .. 'AES' or name of any other cipher with 16-byte block len
55        # $key ..... AES key of proper length (128/192/256bits)
56        # $iv ...... initialization vector
57        # $adata ... additional authenticated data
58
59   gcm_decrypt_verify
60        my $plaintext = gcm_decrypt_verify($cipher, $key, $iv, $adata, $ciphertext, $tag);
61        # on error returns undef
62

METHODS

64   new
65        my $ae = Crypt::AuthEnc::GCM->new($cipher, $key);
66        #or
67        my $ae = Crypt::AuthEnc::GCM->new($cipher, $key, $iv);
68
69        # $cipher .. 'AES' or name of any other cipher
70        # $key ..... encryption key of proper length
71        # $iv ...... initialization vector (optional, you can set it later via iv_add method)
72
73   iv_add
74       Set initialization vector (IV).
75
76        $ae->iv_add($iv_data);                        #can be called multiple times
77
78   adata_add
79       Add additional authenticated data.  Can be called after all "iv_add"
80       calls but before the first "encrypt_add" or "decrypt_add".
81
82        $ae->adata_add($aad_data);                    # can be called multiple times
83
84   encrypt_add
85        $ciphertext = $ae->encrypt_add($data);        # can be called multiple times
86
87   encrypt_done
88        $tag = $ae->encrypt_done();                   # returns $tag value
89
90   decrypt_add
91        $plaintext = $ae->decrypt_add($ciphertext);   # can be called multiple times
92
93   decrypt_done
94        my $tag = $ae->decrypt_done;           # returns $tag value
95        #or
96        my $result = $ae->decrypt_done($tag);  # returns 1 (success) or 0 (failure)
97
98   reset
99        $ae->reset;
100
101   clone
102        my $ae_new = $ae->clone;
103

SEE ALSO

105       •   CryptX, Crypt::AuthEnc::CCM, Crypt::AuthEnc::EAX,
106           Crypt::AuthEnc::OCB
107
108       •   <https://en.wikipedia.org/wiki/Galois/Counter_Mode>
109
110
111
112perl v5.38.0                      2023-10-04            Crypt::AuthEnc::GCM(3)
Impressum