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 65     65   3254942 use strict;
  65         127  
  65         2714  
4 65     65   491 use warnings;
  65         111  
  65         4935  
5             our $VERSION = '0.089';
6              
7 65     65   405 use Carp;
  65         148  
  65         5487  
8             $Carp::Internal{(__PACKAGE__)}++;
9 65     65   24331 use CryptX;
  65         223  
  65         8097  
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 1519 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 only implements single-block encryption and decryption.
68             For general data, use a block mode such as
69             L, L, or L (which is 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             # or any for which there is a Crypt::Cipher:: module
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 maximum allowed key size in bytes for the 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 minimum allowed key size in bytes for the 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 the block size in bytes for the 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 the default number of rounds for the given cipher. Only some ciphers,
148             such as MULTI2, RC5, and SAFER, let you set the number of rounds via C.
149              
150             $c->default_rounds;
151             #or
152             Crypt::Cipher->default_rounds('AES');
153             #or
154             Crypt::Cipher::default_rounds('AES');
155              
156             =head1 SEE ALSO
157              
158             =over
159              
160             =item * L
161              
162             =item * See subclasses such as L, L, ...
163              
164             =back
165              
166             =cut