File Coverage

blib/lib/Crypt/Camellia.pm
Criterion Covered Total %
statement 29 35 82.8
branch 6 12 50.0
condition 5 6 83.3
subroutine 9 10 90.0
pod 4 6 66.6
total 53 69 76.8


line stmt bran cond sub pod time code
1             package Crypt::Camellia;
2              
3 4     4   108901 use 5.008001;
  4         15  
  4         149  
4 4     4   20 use strict;
  4         11  
  4         147  
5 4     4   20 use warnings;
  4         14  
  4         106  
6 4     4   21 use Carp;
  4         6  
  4         2224  
7              
8             our $VERSION = '2.02';
9              
10             require XSLoader;
11             XSLoader::load('Crypt::Camellia', $VERSION);
12              
13             # Preloaded methods go here.
14              
15             sub usage {
16 0     0 0 0 my ($package, $filename, $line, $subr) = caller(1);
17 0         0 $Carp::CarpLevel = 2;
18 0         0 croak "Usage: $subr(@_)";
19             }
20              
21 7855     7855 1 17897 sub blocksize { 16 }
22 1     1 0 26 sub keysize { 32 }
23              
24             sub new{
25 3908 50   3908 1 1559638 usage("new Camellia key") unless @_ == 2;
26 3908         6219 my ($type, $key) = @_;
27 3908         11750 my $self = { keysize => length($key) };
28 3908         8289 bless $self, $type;
29 3908 50 100     20220 if ($self->{keysize} != 16 && $self->{keysize} != 24 && $self->{keysize} != 32) {
      66        
30 0         0 croak "wrong key length: key must be 128, 192 or 256 bits long";
31             }
32 3908         22391 $self->{keytable} = Crypt::Camellia::keygen($key, $self->{keysize}*8);
33 3908         11255 return $self;
34             }
35              
36             sub encrypt{
37 3928 50   3928 1 22412 usage("encrypt data[16 bytes]") unless @_ == 2;
38 3928         5453 my ($self,$data) = @_;
39 3928 50       7975 if (length $data != blocksize()) {
40 0         0 croak "datasize not multiple of blocksize (16 bytes)";
41             }
42 3928         26838 return Crypt::Camellia::crypt($data, $self->{keytable}, $self->{keysize}*8, 1);
43             }
44              
45             sub decrypt {
46 3926 50   3926 1 18387 usage("decrypt data[16 bytes]") unless @_ == 2;
47 3926         5531 my ($self,$data) = @_;
48 3926 50       6843 if (length $data != blocksize()) {
49 0         0 croak "datasize not multiple of blocksize (16 bytes)";
50             }
51 3926         24603 return Crypt::Camellia::crypt($data, $self->{keytable}, $self->{keysize}*8, 0);
52             }
53              
54              
55             1;
56             __END__
57             # Below is stub documentation for your module. You'd better edit it!
58              
59             =head1 NAME
60              
61             Crypt::Camellia - Perl Camellia encryption module
62              
63             =head1 SYNOPSIS
64              
65             use Crypt::Camellia;
66             my $cipher = Crypt::Camellia->new($key);
67             my $ciphertext = $cipher->encrypt($plaintext);
68             my $plaintext = $cipher->decrypt($ciphertext);
69              
70             # more likely
71             use Crypt::CBC;
72             $cipher = Crypt::CBC->new( -key => 'my secret key',
73             -cipher => 'Camellia'
74             );
75              
76             =head1 DESCRIPTION
77              
78             This module implements Camelia 128-bit Block Cipher for perl.
79             For Camellia. Check L<https://info.isl.ntt.co.jp/crypt/eng/camellia/index_s.html>. Here is an exerpt of that page.
80              
81             =over 2
82              
83             Camellia is a symmetric key block cipher developed jointly in 2000 by
84             world top class encryption researchers at NTT and Mitsubishi Electric
85             Corporation. Technologically speaking, Camellia naturally has not only
86             a high level of security, but also excellent efficiency and practical
87             characteristics. It can be implemented at high performance by software
88             on various platforms. In regard to hardware implementation, compact
89             and low-power consumption type implementation as well as high-speed
90             implementation is possible.
91              
92             Based on these technological advantages, Camellia has been
93             internationally recognized, for example the selection project on the
94             European recommendation of strong cryptographic primitives, NESSIE,
95             evaluated Camellia to have "many similarities to the AES, so much of
96             the analysis for the AES is also applicable to Camellia." Currently,
97             Camellia is the only cipher internationally recognized which has the
98             same level of security and performance as AES, and is selected for
99             many international standard/recommended ciphers. In particular, as
100             Japanese domestic ciphers, this is the first case to be approved as
101             IETF standard ciphers (Proposed Standard RFC) .
102              
103             =back
104              
105             Crypt::Camellia has the following methods:
106              
107             =over 2
108              
109             blocksize()
110             keysize()
111             encrypt()
112             decrypt()
113              
114             =back
115              
116             Like many Crypt:: modules like L<Crypt::Blowfish> and L<Crypt::DES>,
117             this module works as a backend of L<Crypt::CBC>.
118              
119             =head1 FUNCTIONS
120              
121             =over 2
122              
123             =item blocksize
124              
125             Returns the size (in bytes) of the block cipher. Currently this is a
126             fixed value of 16.
127              
128             =item new
129              
130             my $cipher = Crypt::Camellia-E<gt>new($key);
131              
132             This creates a new Crypt::Camellia BlockCipher object, using $key,
133             where $key is a key of C<keysize()> bytes (minimum of eight bytes).
134              
135             =item encrypt
136              
137             my $ciphertext = $cipher-E<gt>encrypt($plaintext);
138              
139             This function encrypts $plaintext and returns the $ciphertext
140             where $plaintext and $ciphertext must be of C<blocksize()> bytes.
141             (hint: Camellia is an 16 byte block cipher)
142              
143             =item decrypt
144              
145             my $plaintext = $cipher-E<gt>decrypt($ciphertext);
146              
147             This function decrypts $ciphertext and returns the $plaintext
148             where $plaintext and $ciphertext must be of C<blocksize()> bytes.
149             (hint: see previous hint)
150              
151             =back
152              
153             =head1 EXAMPLE
154              
155             my $key = pack("H16", "0123456789ABCDEF"); # min. 8 bytes
156             my $cipher = Crypt::Camellia->new($key);
157             my $ciphertext = $cipher->encrypt("plaintex");# SEE NOTES
158             print unpack("H16", $ciphertext), "\n";
159              
160             =head1 NOTES
161              
162             For practical uses use this module via L<Crypt::CBC> rather than directly.
163              
164             =head1 SEE ALSO
165              
166             L<Crypt::CBC>,
167             L<Crypt::Rijndael>,
168             L<Crypt::DES>,
169             L<Crypt::IDEA>
170              
171             =head1 AUTHOR
172              
173             Dan Kogai, E<lt>dankogai@dan.co.jpE<gt>
174              
175             Current maintainer is Hiroyuki OYAMA E<lt>oyama@module.jpE<gt>.
176              
177             And
178              
179             NTT L<http://info.isl.ntt.co.jp/crypt/camellia/index.html>
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             Copyright (C) 2006 by Dan Kogai
184              
185             This library is free software; you can redistribute it and/or modify
186             it under the same terms as Perl itself, either Perl version 5.8.8 or,
187             at your option, any later version of Perl 5 you may have available.
188              
189             Except for anything under camellia/ directory which is licensed as follows:
190              
191             Copyright (c) 2006
192             NTT (Nippon Telegraph and Telephone Corporation) . All rights reserved.
193              
194             Redistribution and use in source and binary forms, with or without
195             modification, are permitted provided that the following conditions
196             are met:
197             1. Redistributions of source code must retain the above copyright
198             notice, this list of conditions and the following disclaimer as
199             the first lines of this file unmodified.
200             2. Redistributions in binary form must reproduce the above copyright
201             notice, this list of conditions and the following disclaimer in the
202             documentation and/or other materials provided with the distribution.
203              
204             THIS SOFTWARE IS PROVIDED BY NTT ``AS IS'' AND ANY EXPRESS OR IMPLIED
205             WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
206             MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207             IN NO EVENT SHALL NTT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
208             SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
209             LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
210             DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211             THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
212             (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
213             OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
214              
215             =cut