File Coverage

blib/lib/Crypt/Cipher.pm
Criterion Covered Total %
statement 13 14 92.8
branch n/a
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 19 21 90.4


line stmt bran cond sub pod time code
1             package Crypt::Cipher;
2              
3 62     62   2312939 use strict;
  62         98  
  62         1840  
4 62     62   348 use warnings;
  62         82  
  62         3323  
5             our $VERSION = '0.088_004';
6              
7 62     62   267 use Carp;
  62         108  
  62         4169  
8             $Carp::Internal{(__PACKAGE__)}++;
9 62     62   16458 use CryptX;
  62         143  
  62         5847  
10              
11             ### the following methods/functions are implemented in XS:
12             # - new
13             # - DESTROY
14             # - blocksize
15             # - decrypt
16             # - default_rounds
17             # - encrypt
18             # - max_keysize
19             # - min_keysize
20              
21 167     167 1 1199 sub keysize { goto \&max_keysize; } # for Crypt::CBC compatibility
22              
23 0     0     sub CLONE_SKIP { 1 } # prevent cloning
24              
25             1;
26              
27             =pod
28              
29             =head1 NAME
30              
31             Crypt::Cipher - Generic interface to cipher functions
32              
33             =head1 SYNOPSIS
34              
35             #### example 1 (encrypting single block)
36             use Crypt::Cipher;
37              
38             my $key = '1234567890abcdef'; # 16 bytes, valid for AES-128
39             my $plaintext_block = '1234567890123456'; # one AES block (16 bytes)
40             my $c = Crypt::Cipher->new('AES', $key);
41             my $blocksize = $c->blocksize;
42             my $ciphertext = $c->encrypt($plaintext_block); # encrypt 1 block
43             my $plaintext = $c->decrypt($ciphertext); #decrypt 1 block
44              
45             ### example 2 (using CBC mode)
46             use Crypt::Mode::CBC;
47              
48             my $cbc_key = '1234567890abcdef'; # 16 bytes, valid for AES-128
49             my $iv = 'fedcba0987654321'; # 16 bytes
50             my $cbc = Crypt::Mode::CBC->new('AES');
51             my $cbc_ciphertext = $cbc->encrypt("secret data", $cbc_key, $iv);
52              
53             #### example 3 (compatibility with Crypt::CBC)
54             use Crypt::CBC;
55             use Crypt::Cipher;
56              
57             my $compat_key = '1234567890abcdef'; # 16 bytes, valid for AES-128
58             my $compat_iv = 'fedcba0987654321'; # 16 bytes
59             my $cipher = Crypt::Cipher->new('AES', $compat_key);
60             my $compat_cbc = Crypt::CBC->new( -cipher=>$cipher, -iv=>$compat_iv );
61             my $compat_ciphertext = $compat_cbc->encrypt("secret data");
62              
63             =head1 DESCRIPTION
64              
65             Provides an interface to various symmetric cipher algorithms.
66              
67             B This module implements just elementary "one-block-(en|de)cryption" operation - if you want to
68             encrypt/decrypt generic data you have to use some of the cipher block modes - check for example
69             L, L or L (which will be slower).
70              
71             =head1 METHODS
72              
73             Unless noted otherwise, assume C<$c> is an existing cipher object created via
74             C, for example:
75              
76             my $c = Crypt::Cipher->new('AES', '1234567890abcdef');
77              
78             =head2 new
79              
80             Constructor, returns a reference to the cipher object.
81              
82             ## basic scenario
83             my $c = Crypt::Cipher->new($name, $key);
84             # $name = one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
85             # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
86             # 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64',
87             # 'SEED', 'SM4', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent'
88             # simply any for which there exists Crypt::Cipher::
89             # $key = binary key (keysize should comply with selected cipher requirements)
90              
91             ## some of the ciphers (e.g. MULTI2, RC5, SAFER) allow one to set number of rounds
92             my $c = Crypt::Cipher->new('MULTI2', $key, $rounds);
93             # $rounds = positive integer (should comply with selected cipher requirements)
94              
95             =head2 encrypt
96              
97             Encrypts C<$plaintext> and returns C<$ciphertext>. An empty string is
98             accepted and returned unchanged; otherwise C<$plaintext> must be exactly
99             B bytes long.
100              
101             my $ciphertext = $c->encrypt($plaintext);
102              
103             =head2 decrypt
104              
105             Decrypts C<$ciphertext> and returns C<$plaintext>. An empty string is
106             accepted and returned unchanged; otherwise C<$ciphertext> must be exactly
107             B bytes long.
108              
109             my $plaintext = $c->decrypt($ciphertext);
110              
111             =head2 keysize
112              
113             Just an alias for B (needed for L compatibility).
114              
115             =head2 max_keysize
116              
117             Returns the maximal allowed key size (in bytes) for given cipher.
118              
119             $c->max_keysize;
120             #or
121             Crypt::Cipher->max_keysize('AES');
122             #or
123             Crypt::Cipher::max_keysize('AES');
124              
125             =head2 min_keysize
126              
127             Returns the minimal allowed key size (in bytes) for given cipher.
128              
129             $c->min_keysize;
130             #or
131             Crypt::Cipher->min_keysize('AES');
132             #or
133             Crypt::Cipher::min_keysize('AES');
134              
135             =head2 blocksize
136              
137             Returns block size (in bytes) for given cipher.
138              
139             $c->blocksize;
140             #or
141             Crypt::Cipher->blocksize('AES');
142             #or
143             Crypt::Cipher::blocksize('AES');
144              
145             =head2 default_rounds
146              
147             Returns default number of rounds for given cipher. NOTE: only some ciphers (e.g. MULTI2, RC5, SAFER) allow one to set number of rounds via new().
148              
149             $c->default_rounds;
150             #or
151             Crypt::Cipher->default_rounds('AES');
152             #or
153             Crypt::Cipher::default_rounds('AES');
154              
155             =head1 SEE ALSO
156              
157             =over
158              
159             =item * L
160              
161             =item * Check subclasses like L, L, ...
162              
163             =back
164              
165             =cut