File Coverage

blib/lib/Crypt/Mode/CTR.pm
Criterion Covered Total %
statement 15 16 93.7
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Crypt::Mode::CTR;
2              
3             ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY!
4              
5 3     3   128522 use strict;
  3         7  
  3         141  
6 3     3   24 use warnings;
  3         6  
  3         195  
7             our $VERSION = '0.089';
8              
9 3     3   530 use Crypt::Cipher;
  3         7  
  3         709  
10              
11             sub encrypt {
12 14     14 1 255087 my ($self, $pt) = (shift, shift);
13 14         65 local $SIG{__DIE__} = \&CryptX::_croak;
14 14         192 $self->start_encrypt(@_)->add($pt);
15             }
16              
17             sub decrypt {
18 14     14 1 4897 my ($self, $ct) = (shift, shift);
19 14         49 local $SIG{__DIE__} = \&CryptX::_croak;
20 14         109 $self->start_decrypt(@_)->add($ct);
21             }
22              
23 0     0     sub CLONE_SKIP { 1 } # prevent cloning
24              
25             1;
26              
27             =pod
28              
29             =head1 NAME
30              
31             Crypt::Mode::CTR - Block cipher mode CTR [Counter mode]
32              
33             =head1 SYNOPSIS
34              
35             use Crypt::Mode::CTR;
36             my $m = Crypt::Mode::CTR->new('AES');
37             my $key = '1234567890123456';
38             my $iv = '1234567890123456';
39             my $plaintext = 'example plaintext';
40             my $chunk1 = 'example ';
41             my $chunk2 = 'plaintext';
42              
43             # encrypt or decrypt in one call
44             my $single_ciphertext = $m->encrypt($plaintext, $key, $iv);
45             my $single_plaintext = $m->decrypt($single_ciphertext, $key, $iv);
46              
47             # encrypt more chunks
48             $m->start_encrypt($key, $iv);
49             my $chunked_ciphertext = '';
50             $chunked_ciphertext .= $m->add($chunk1);
51             $chunked_ciphertext .= $m->add($chunk2);
52              
53             # decrypt more chunks
54             $m->start_decrypt($key, $iv);
55             my $chunked_plaintext = '';
56             $chunked_plaintext .= $m->add($chunked_ciphertext);
57              
58             =head1 DESCRIPTION
59              
60             This module implements CTR cipher mode. B It works only with ciphers from L (Crypt::Cipher::NNNN).
61              
62             =head1 METHODS
63              
64             Unless noted otherwise, assume C<$m> is an existing mode object created via
65             C, for example:
66              
67             my $m = Crypt::Mode::CTR->new('AES');
68              
69             =head2 new
70              
71             my $m = Crypt::Mode::CTR->new($cipher_name);
72             #or
73             my $m = Crypt::Mode::CTR->new($cipher_name, $ctr_mode, $ctr_width);
74             #or
75             my $m = Crypt::Mode::CTR->new($cipher_name, $ctr_mode, $ctr_width, $cipher_rounds);
76              
77             # $cipher_name .. [string] one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
78             # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
79             # 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64',
80             # 'SEED', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent'
81             # or any for which there is a Crypt::Cipher:: module
82             # $ctr_mode ..... [integer] CTR_COUNTER_LITTLE_ENDIAN (0) - little-endian counter (DEFAULT)
83             # CTR_COUNTER_BIG_ENDIAN (1) - big-endian counter
84             # CTR_COUNTER_LITTLE_ENDIAN | 2 (2) - little-endian + RFC 3686 initial-counter-incrementing
85             # CTR_COUNTER_BIG_ENDIAN | 2 (3) - big-endian + RFC 3686 initial-counter-incrementing
86             # $ctr_width .... [integer] counter width in bytes (DEFAULT = full block width, e.g. 16 for AES)
87             # $cipher_rounds ... [integer] optional, number of rounds for given cipher
88              
89             =head2 encrypt
90              
91             Encrypts the plaintext in a single call. Returns the ciphertext as a binary string.
92             The plaintext scalar is converted to bytes using Perl's usual scalar
93             stringification. Defined scalars, including numbers and string-overloaded
94             objects, are accepted. C is treated as an empty string and may emit
95             Perl's usual "uninitialized value" warning.
96              
97             my $ciphertext = $m->encrypt($plaintext, $key, $iv);
98              
99             =head2 decrypt
100              
101             Decrypts the ciphertext in a single call. Returns the plaintext as a binary string.
102             The ciphertext scalar is converted to bytes using Perl's usual scalar
103             stringification. Defined scalars, including numbers and string-overloaded
104             objects, are accepted. C is treated as an empty string and may emit
105             Perl's usual "uninitialized value" warning.
106              
107             my $plaintext = $m->decrypt($ciphertext, $key, $iv);
108              
109             =head2 start_encrypt
110              
111             Initializes encryption mode. Returns the object itself.
112              
113             $m->start_encrypt($key, $iv);
114              
115             =head2 start_decrypt
116              
117             Initializes decryption mode. Returns the object itself.
118              
119             $m->start_decrypt($key, $iv);
120              
121             =head2 add
122              
123             Feeds data to the encryption or decryption stream. Returns a binary string.
124              
125             Each argument is converted to bytes using Perl's usual scalar stringification.
126             Defined scalars, including numbers and string-overloaded objects, are
127             accepted. C is treated as an empty string and may emit Perl's usual
128             "uninitialized value" warning.
129              
130             # in encrypt mode
131             my $ciphertext = $m->add($plaintext);
132              
133             # in decrypt mode
134             my $plaintext = $m->add($ciphertext);
135              
136             =head2 finish
137              
138             CTR is a streaming mode and does not use padding, so C returns an empty
139             string. It exists for API consistency with L and
140             L and may be safely called or omitted.
141              
142             $m->start_encrypt($key, $iv);
143             my $ciphertext = '';
144             $ciphertext .= $m->add($chunk1);
145             $ciphertext .= $m->add($chunk2);
146             $ciphertext .= $m->finish; # returns ''
147              
148             =head1 SEE ALSO
149              
150             =over
151              
152             =item * L, L
153              
154             =item * L, L, ...
155              
156             =item * L
157              
158             =back
159              
160             =cut