blib/lib/Crypt/AuthEnc/OCB.pm

filter: Statementstmt 87.5% 21 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 50.0% 1 / 2 totaltotal 78.7% 26 / 33 CC 1 SCAR 3.6
CC
1
Cov
88%
aad_add
2.0
blocksize
2.0
CLONE_SKIP
2.0
CRAP 1.4
« blib/lib/Crypt/PK.pm ↑ Summary blib/lib/Crypt/PRNG/ChaCha20.pm »
1 package Crypt::AuthEnc::OCB;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our $VERSION = '0.091';
6  
7 5 require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
8 5 our %EXPORT_TAGS = ( all => [qw( ocb_encrypt_authenticate ocb_decrypt_verify )] );
9 5 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
10 5 our @EXPORT = qw();
11  
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 $Carp::Internal{(__PACKAGE__)}++;
14 5 ▶use CryptX;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # obsolete, only for backwards compatibility
17 0 ▶sub aad_add { goto &adata_add }
Subroutine aad_add: not called CC=1 SCAR=6.9
Pod undocumented
18 0 ▶sub blocksize { return 16 }
Subroutine blocksize: not called CC=1 SCAR=6.9
Pod documented
19  
20 0 ▶sub CLONE_SKIP { 1 } # prevent cloning
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
21  
22 5 1;
23  
24 =pod
25  
26 =head1 NAME
27  
28 Crypt::AuthEnc::OCB - Authenticated encryption in OCBv3 mode
29  
30 =head1 SYNOPSIS
31  
32 ### OO interface
33 use Crypt::AuthEnc::OCB;
34  
35 my $key = '...';
36 my $nonce = '...';
37 my $tag_len = 16;
38 my $expected_tag = '...';
39  
40 # encrypt and authenticate
41 my $ae_enc = Crypt::AuthEnc::OCB->new("AES", $key, $nonce, $tag_len);
42 $ae_enc->adata_add('additional_authenticated_data1');
43 $ae_enc->adata_add('additional_authenticated_data2');
44 my $ct = $ae_enc->encrypt_add('data1');
45 $ct .= $ae_enc->encrypt_add('data2');
46 $ct .= $ae_enc->encrypt_add('data3');
47 $ct .= $ae_enc->encrypt_last('rest of data');
48 my $tag = $ae_enc->encrypt_done();
49  
50 # decrypt and verify
51 my $ae_dec = Crypt::AuthEnc::OCB->new("AES", $key, $nonce, $tag_len);
52 $ae_dec->adata_add('additional_authenticated_data1');
53 $ae_dec->adata_add('additional_authenticated_data2');
54 my $pt = $ae_dec->decrypt_add('ciphertext1');
55 $pt .= $ae_dec->decrypt_add('ciphertext2');
56 $pt .= $ae_dec->decrypt_add('ciphertext3');
57 $pt .= $ae_dec->decrypt_last('rest of data');
58 $ae_dec->decrypt_done($expected_tag) or die "decrypt failed"; # constant-time tag check
59  
60 #or, if you need the computed tag, compare it with slow_eq from Crypt::Misc
61 #(Perl's 'eq' is not constant-time and leaks timing information)
62 my $computed_tag = $ae_dec->decrypt_done();
63 die "decrypt failed" unless Crypt::Misc::slow_eq($computed_tag, $expected_tag);
64  
65 ### functional interface
66 use Crypt::AuthEnc::OCB qw(ocb_encrypt_authenticate ocb_decrypt_verify);
67  
68 my $key = '...';
69 my $nonce = '...';
70 my $adata = '...';
71 my $plaintext = '...';
72 my $tag_len = 16;
73  
74 my ($ciphertext, $tag) = ocb_encrypt_authenticate('AES', $key, $nonce, $adata, $tag_len, $plaintext);
75 my $decrypted = ocb_decrypt_verify('AES', $key, $nonce, $adata, $ciphertext, $tag);
76  
77 =head1 DESCRIPTION
78  
79 This module implements OCB v3 according to
80 L<https://www.rfc-editor.org/rfc/rfc7253>.
81  
82 This is a stateful API. Build one message by calling, in order:
83 C<new>, optional C<adata_add>, zero or more C<encrypt_add> or C<decrypt_add>
84 calls for full blocks, one optional C<encrypt_last> or C<decrypt_last> for the
85 final partial block, then C<encrypt_done> or C<decrypt_done>.
86  
87 Use a fresh object per message. When verifying, C<decrypt_done($expected_tag)>
88 is the safer one-step form; C<decrypt_done()> without arguments only returns
89 the calculated tag.
90 The first C<encrypt_done> / C<decrypt_done> call finalizes the object. After that,
91 further C<adata_add>, C<encrypt_add>, C<encrypt_last>, C<decrypt_add>,
92 C<decrypt_last>, C<encrypt_done>, and C<decrypt_done> calls croak.
93  
94 =head1 EXPORT
95  
96 Nothing is exported by default.
97  
98 You can export selected functions:
99  
100 use Crypt::AuthEnc::OCB qw(ocb_encrypt_authenticate ocb_decrypt_verify);
101  
102 =head1 FUNCTIONS
103  
104 =head2 ocb_encrypt_authenticate
105  
106 my ($ciphertext, $tag) = ocb_encrypt_authenticate($cipher, $key, $nonce, $adata, $tag_len, $plaintext);
107  
108 # $cipher .. [string] 'AES' or name of any other cipher with 16-byte block len
109 # $key ..... [binary string] AES key of proper length (128/192/256 bits)
110 # $nonce ... [binary string] unique nonce/salt (no need to keep it secret)
111 # $adata ... [binary string] additional authenticated data
112 # $tag_len . [integer] required length of output tag
113  
114 Use tag lengths from 4 to 16 bytes. Out-of-range values passed to this
115 functional helper are normalized to 16.
116  
117 =head2 ocb_decrypt_verify
118  
119 my $plaintext = ocb_decrypt_verify($cipher, $key, $nonce, $adata, $ciphertext, $tag);
120 # on error returns undef
121  
122 =head1 METHODS
123  
124 Unless noted otherwise, assume C<$ae> is an existing AEAD object created via
125 C<new>, for example:
126  
127 my $ae = Crypt::AuthEnc::OCB->new($cipher, $key, $nonce);
128  
129 =head2 new
130  
131 my $ae = Crypt::AuthEnc::OCB->new($cipher, $key, $nonce, $tag_len);
132  
133 # $cipher .. [string] 'AES' or name of any other cipher with 16-byte block len
134 # $key ..... [binary string] AES key of proper length (128/192/256 bits)
135 # $nonce ... [binary string] unique nonce/salt (no need to keep it secret)
136 # $tag_len . [integer] required length of output tag
137  
138 B<IMPORTANT:> The length of the tag is a security parameter and a zero-length
139 tag is legal (it simply provides no authenticity), which is the caller's choice.
140  
141 =head2 adata_add
142  
143 Can be called only before the first C<encrypt_add>, C<encrypt_last>,
144 C<decrypt_add>, or C<decrypt_last>.
145 Returns the object itself (for chaining).
146  
147 $ae->adata_add($adata); #can be called multiple times
148  
149 =head2 encrypt_add
150  
151 Returns a binary string of ciphertext (raw bytes).
152  
153 my $ciphertext = $ae->encrypt_add($data); # can be called multiple times
154  
155 # note: \ must be a multiple of the block length (16 for AES)
156  
157 =head2 encrypt_last
158  
159 my $ciphertext = $ae->encrypt_last($data);
160  
161 =head2 encrypt_done
162  
163 Returns the authentication tag as a binary string (raw bytes).
164 This call finalizes the current message.
165  
166 my $tag = $ae->encrypt_done(); # returns $tag value
167  
168 =head2 decrypt_add
169  
170 Returns a binary string of plaintext (raw bytes).
171  
172 my $plaintext = $ae->decrypt_add($ciphertext); # can be called multiple times
173  
174 # note: \ must be a multiple of the block length (16 for AES)
175  
176 =head2 decrypt_last
177  
178 my $plaintext = $ae->decrypt_last($data);
179  
180 =head2 decrypt_done
181  
182 Without argument returns the computed tag as a binary string. With C<$tag> argument returns C<1> (success) or C<0> (failure).
183 This call finalizes the current message.
184  
185 my $tag = $ae->decrypt_done; # returns $tag value
186 #or
187 my $result = $ae->decrypt_done($tag); # returns 1 (success) or 0 (failure)
188  
189 =head2 blocksize
190  
191 my $bs = $ae->blocksize; # always returns 16
192  
193 Returns the block size of the underlying cipher (always 16, since OCB requires
194 a 128-bit block cipher such as AES).
195  
196 =head2 clone
197  
198 Returns a copy of the AEAD object in its current state.
199  
200 my $ae_new = $ae->clone;
201  
202 =head1 SEE ALSO
203  
204 =over
205  
206 =item * L<CryptX>, L<Crypt::AuthEnc::CCM>, L<Crypt::AuthEnc::GCM>, L<Crypt::AuthEnc::EAX>
207  
208 =item * L<https://en.wikipedia.org/wiki/OCB_mode>
209  
210 =item * L<https://www.rfc-editor.org/rfc/rfc7253>
211  
212 =back
213  
214 =cut
« blib/lib/Crypt/PK.pm ↑ Summary blib/lib/Crypt/PRNG/ChaCha20.pm »