File Coverage

blib/lib/Crypt/Eksblowfish/Family.pm
Criterion Covered Total %
statement 53 53 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 20 20 100.0
pod 8 8 100.0
total 85 86 98.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Crypt::Eksblowfish::Family - Eksblowfish cipher family
4              
5             =head1 SYNOPSIS
6              
7             use Crypt::Eksblowfish::Family;
8              
9             $family = Crypt::Eksblowfish::Family->new_family(8, $salt);
10              
11             $cost = $family->cost;
12             $salt = $family->salt;
13             $block_size = $family->blocksize;
14             $key_size = $family->keysize;
15             $cipher = $family->new($key);
16              
17             =head1 DESCRIPTION
18              
19             An object of this class represents an Eksblowfish cipher family.
20             It contains the family parameters (cost and salt), and if combined
21             with a key it yields an encryption function. See L
22             for discussion of the Eksblowfish algorithm.
23              
24             It is intended that an object of this class can be used in situations
25             such as the "-cipher" parameter to C. Normally that parameter
26             is the name of a class, such as "Crypt::Rijndael", where the class
27             implements a block cipher algorithm. The class provides a C
28             constructor that accepts a key. In the case of Eksblowfish, the key
29             alone is not sufficient. An Eksblowfish family fills the role of block
30             cipher algorithm. Therefore a family object is used in place of a class
31             name, and it is the family object the provides the C constructor.
32              
33             =head2 Crypt::CBC
34              
35             C itself has a problem, with the result that this class can
36             no longer be used with it in the manner originally intended.
37              
38             When this class was originally designed, it worked with C
39             as described above: an object of this class would be accepted by
40             C as a cipher algorithm, and C would happily
41             supply it with a key and encrypt using the resulting cipher object.
42             C didn't realise it was dealing with a family object, however,
43             and there was some risk that a future version might accidentally squash
44             the object into a string, which would be no use. In the course of
45             discussion about regularising the use of cipher family objects, the
46             author of C got hold of the wrong end of the stick, and
47             ended up changing C in a way that totally breaks this usage,
48             rather than putting it on a secure footing.
49              
50             The present behaviour of C is that if an object (rather
51             than a class name) is supplied as the "-cipher" parameter then it has
52             a completely different meaning from usual. In this case, the object
53             supplied is used as the keyed cipher, rather than as a cipher algorithm
54             which must be given a key. This bypasses all of C's usual
55             keying logic, which can hash and salt a passphrase to generate the key.
56             It is arguably a useful feature, but it's a gross abuse of the "-cipher"
57             parameter and a severe impediment to the use of family-keyed cipher
58             algorithms.
59              
60             This class now provides a workaround. For the benefit of C,
61             and any other crypto plumbing that requires a keyable cipher algorithm
62             to look like a Perl class (rather than an object), a family object
63             of this class can in fact be reified as a class of its own. See the
64             method L.
65              
66             =cut
67              
68             package Crypt::Eksblowfish::Family;
69              
70 1     1   49483 { use 5.006; }
  1         4  
  1         35  
71 1     1   6 use warnings;
  1         11  
  1         37  
72 1     1   5 use strict;
  1         2  
  1         36  
73              
74 1     1   4 use Carp qw(croak);
  1         2  
  1         85  
75 1     1   529 use Crypt::Eksblowfish 0.005;
  1         27  
  1         35  
76 1     1   1344 use Class::Mix 0.001 qw(genpkg);
  1         8083  
  1         362  
77              
78             our $VERSION = "0.009";
79              
80             =head1 CONSTRUCTOR
81              
82             =over
83              
84             =item Crypt::Eksblowfish::Family->new_family(COST, SALT)
85              
86             Creates and returns an object representing the Eksblowfish cipher
87             family specified by the parameters. The SALT is a family key, and must
88             be exactly 16 octets. COST is an integer parameter controlling the
89             expense of keying: the number of operations in key setup is proportional
90             to 2^COST.
91              
92             =cut
93              
94             sub new_family {
95 10     10 1 5320 my($class, $cost, $salt) = @_;
96 10         73 return bless({ cost => $cost, salt => $salt }, $class);
97             }
98              
99             =back
100              
101             =head1 METHODS
102              
103             =over
104              
105             =item $family->cost
106              
107             Extracts and returns the cost parameter.
108              
109             =cut
110              
111 20     20 1 10694 sub cost { $_[0]->{cost} }
112              
113             =item $family->salt
114              
115             Extracts and returns the salt parameter.
116              
117             =cut
118              
119 20     20 1 97 sub salt { $_[0]->{salt} }
120              
121             =item $family->blocksize
122              
123             Returns 8, indicating the Eksblowfish block size of 8 octets.
124              
125             =cut
126              
127 20     20 1 71 sub blocksize { 8 }
128              
129             =item $family->keysize
130              
131             Returns 0, indicating that the key size is variable. This situation is
132             handled specially by C.
133              
134             =cut
135              
136 20     20 1 64 sub keysize { 0 }
137              
138             =item $family->new(KEY)
139              
140             Performs key setup on a new instance of the Eksblowfish algorithm,
141             returning the keyed state. The KEY may be any length from 1 octet to 72
142             octets inclusive. The object returned is of class C;
143             see L for the encryption and decryption methods.
144              
145             Note that this method is called on a family object, not on the class
146             C.
147              
148             =cut
149              
150             sub new {
151 21     21 1 5356 my($self, $key) = @_;
152 21 100       310 croak "Crypt::Eksblowfish::Family::new is not a class method ".
153             "(perhaps you want new_family instead)"
154             if ref($self) eq "";
155 20         292770 return Crypt::Eksblowfish->new($self->{cost}, $self->{salt}, $key);
156             }
157              
158             =item $family->encrypt
159              
160             This method nominally exists, to satisfy C. It can't really
161             be used: it doesn't make any sense.
162              
163             =cut
164              
165 10     10 1 1337 sub encrypt { croak "Crypt::Eksblowfish::Family::encrypt called" }
166              
167             =item $family->as_class
168              
169             Generates and returns (the name of) a Perl class that behaves as a
170             keyable cipher algorithm identical to this Eksblowfish cipher family.
171             The same methods that can be called as instance methods on $family can
172             be called as class methods on the generated class.
173              
174             You should prefer to use the family object directly wherever you can.
175             Aside from being a silly indirection, the classes generated by this
176             method cannot be garbage-collected. This method exists only to cater to
177             C, which requires a keyable cipher algorithm to look like a
178             Perl class, and won't operate correctly on one that looks like an object.
179              
180             =cut
181              
182             sub as_class {
183 20     20 1 26228 my($self) = @_;
184 20   66     118 return $self->{as_class} ||= do {
185 10         58 my $pkg = genpkg(__PACKAGE__."::");
186 1     1   11 no strict "refs";
  1         2  
  1         256  
187 10         391 @{"${pkg}::ISA"} = (ref($self));
  10         217  
188 10         62 *{"${pkg}::new_family"} =
189 10     10   52 sub { croak $_[0]."->new_family called" };
  10         2188  
190 10     10   36 *{"${pkg}::cost"} = sub { $self->cost };
  10         52  
  10         14132  
191 10     10   33 *{"${pkg}::salt"} = sub { $self->salt };
  10         63  
  10         37  
192 10     10   38 *{"${pkg}::new"} = sub { shift; $self->new(@_) };
  10         60  
  10         14  
  10         39  
193 10     10   35 *{"${pkg}::as_class"} = sub { $pkg };
  10         47  
  10         19564  
194 10         61 $pkg;
195             };
196             }
197              
198             =back
199              
200             =head1 SEE ALSO
201              
202             L,
203             L
204              
205             =head1 AUTHOR
206              
207             Andrew Main (Zefram)
208              
209             =head1 COPYRIGHT
210              
211             Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
212             Andrew Main (Zefram)
213              
214             =head1 LICENSE
215              
216             This module is free software; you can redistribute it and/or modify it
217             under the same terms as Perl itself.
218              
219             =cut
220              
221             1;