| 1 | package Crypt::Mode::CTR; | ||
| 2 | |||
| 3 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | ||
| 4 | |||
| 5 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 4 | our $VERSION = '0.091'; | |
| 8 | |||
| 9 | 4 | ▶ | use Crypt::Cipher; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 14 | ▶ | sub encrypt { |
|
Subroutine
encrypt: called CC=1 SCAR=0.0
Pod
documented
| |||
| 12 | 14 | my ($self, $pt) = (shift, shift); | |
| 13 | 14 | local $SIG{__DIE__} = \&CryptX::_croak; | |
| 14 | 14 | $self->start_encrypt(@_)->add($pt); | |
| 15 | } | ||
| 16 | |||
| 17 | 14 | ▶ | sub decrypt { |
|
Subroutine
decrypt: called CC=1 SCAR=0.0
Pod
documented
| |||
| 18 | 14 | my ($self, $ct) = (shift, shift); | |
| 19 | 14 | local $SIG{__DIE__} = \&CryptX::_croak; | |
| 20 | 14 | $self->start_decrypt(@_)->add($ct); | |
| 21 | } | ||
| 22 | |||
| 23 | 0 | ▶ | sub CLONE_SKIP { 1 } # prevent cloning |
|
Subroutine
CLONE_SKIP: not called CC=1 SCAR=6.9
| |||
| 24 | |||
| 25 | 4 | 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<Note:> It works only with ciphers from L<CryptX> (Crypt::Cipher::NNNN). | ||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | Unless noted otherwise, assume C<$m> is an existing mode object created via | ||
| 65 | C<new>, 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 <NAME> for which there is a Crypt::Cipher::<NAME> 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<undef> 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<undef> 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<undef> 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<finish> returns an empty | ||
| 139 | string. It exists for API consistency with L<Crypt::Mode::CBC> and | ||
| 140 | L<Crypt::Mode::ECB> 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<CryptX>, L<Crypt::Cipher> | ||
| 153 | |||
| 154 | =item * L<Crypt::Cipher::AES>, L<Crypt::Cipher::Blowfish>, ... | ||
| 155 | |||
| 156 | =item * L<https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29> | ||
| 157 | |||
| 158 | =back | ||
| 159 | |||
| 160 | =cut | ||