File Coverage

blib/lib/Crypt/Eksblowfish/Subkeyed.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Crypt::Eksblowfish::Subkeyed - Blowfish/Eksblowfish with access to subkeys
4              
5             =head1 SYNOPSIS
6              
7             use Crypt::Eksblowfish::Subkeyed;
8              
9             $block_size = Crypt::Eksblowfish::Subkeyed->blocksize;
10              
11             $cipher = Crypt::Eksblowfish::Subkeyed
12             ->new_from_subkeys(\@p_array, \@s_boxes);
13             $cipher = Crypt::Eksblowfish::Subkeyed->new_initial;
14              
15             $block_size = $cipher->blocksize;
16             $ciphertext = $cipher->encrypt($plaintext);
17             $plaintext = $cipher->decrypt($ciphertext);
18              
19             $p_array = $cipher->p_array;
20             $s_boxes = $cipher->s_boxes;
21             if($cipher->is_weak) { ...
22              
23             =head1 DESCRIPTION
24              
25             An object of this class encapsulates a keyed instance of the Blowfish
26             or Eksblowfish block cipher, ready to encrypt and decrypt. Normally
27             this class will not be used directly, but through subclasses such as
28             L.
29              
30             Eksblowfish is a variant of the Blowfish cipher with a modified key setup
31             algorithm. This class doesn't implement either form of key setup, but
32             only provides the actual encryption and decryption parts of the ciphers.
33             This part is shared between Blowfish and Eksblowfish, and also any other
34             cipher that uses the core of Blowfish but supplies its own key setup.
35             This class has "Eksblowfish" in its name rather than "Blowfish" merely
36             due to the historical accident that it is derived from the encryption
37             engine that was used to implement Eksblowfish.
38              
39             The key setup phase of a block cipher, also known as the "key
40             schedule", produces a set of "subkeys", which are somewhat like ordinary
41             cryptographic keys (which are the input to the key setup algorithm) but
42             are much larger. In some block ciphers the subkeys also have special
43             interrelationships. In Blowfish the subkeys consist of a "P-array" of 18
44             32-bit entries (one per encryption round plus two more) and four "S-boxes"
45             ("S" is for "substitution") each of which consists of 256 32-bit entries.
46             There is no special relationship between the values of the subkeys.
47              
48             Methods in this class allow a cipher object to be constructed from
49             a full set of subkeys, and for the subkeys to be extracted from a
50             cipher object. Normal users don't need to do either of these things.
51             It's mainly useful when devising a new key schedule to stick onto the
52             Blowfish core, or when performing cryptanalysis of the cipher algorithm.
53              
54             Generating subkeys directly by a strong random process, rather than by
55             expansion of a smaller random key, is an expensive and slightly bizarre
56             way to get greater cryptographic strength from a cipher algorithm.
57             It eliminates attacks on the key schedule, and yields the full strength
58             of the core algorithm. However, this is always a lot less strength than
59             the amount of subkey material, whereas a normal key schedule is designed
60             to yield strength equal to the length of the (much shorter) key. Also,
61             any non-randomness in the source of the subkey material is likely to
62             lead to a cryptographic weakness, whereas a key schedule conceals any
63             non-randomness in the choice of the key.
64              
65             =cut
66              
67             package Crypt::Eksblowfish::Subkeyed;
68              
69 9     9   41817 { use 5.006; }
  9         35  
  9         3196  
70 9     9   64 use warnings;
  9         17  
  9         381  
71 9     9   58 use strict;
  9         18  
  9         360  
72              
73 9     9   54 use XSLoader;
  9         18  
  9         990  
74              
75             our $VERSION = "0.009";
76              
77             XSLoader::load("Crypt::Eksblowfish", $VERSION);
78              
79             =head1 CLASS METHODS
80              
81             =over
82              
83             =item Crypt::Eksblowfish::Subkeyed->blocksize
84              
85             Returns 8, indicating the Eksblowfish block size of 8 octets. This method
86             may be called on either the class or an instance.
87              
88             =back
89              
90             =head1 CONSTRUCTOR
91              
92             =over
93              
94             =item Crypt::Eksblowfish::Subkeyed->new_from_subkeys(ROUND_KEYS, SBOXES)
95              
96             Creates a new Blowfish cipher object encapsulating the supplied subkeys.
97             ROUND_KEYS must be a reference to an array of 18 32-bit integers.
98             SBOXES must be a reference to an array of four references to 256-element
99             arrays of 32-bit integers. These subkeys are used in the standard order
100             for Blowfish.
101              
102             =item Crypt::Eksblowfish::Subkeyed->new_initial
103              
104             The standard Blowfish key schedule is an iterative process, which uses
105             the cipher algorithm to progressively replace subkeys, thus mutating the
106             cipher for subsequent iterations of keying. The Eksblowfish key schedule
107             works similarly, but with a lot more iterations. In both cases, the
108             key setup algorithm begins with a standard set of subkeys, consisting
109             of the initial bits of the fractional part of pi. This constructor
110             creates and returns a Blowfish block cipher object with that standard
111             initial set of subkeys. This is probably useful only to designers of
112             novel key schedules.
113              
114             =back
115              
116             =head1 METHODS
117              
118             =over
119              
120             =item $cipher->blocksize
121              
122             Returns 8, indicating the Eksblowfish block size of 8 octets. This method
123             may be called on either the class or an instance.
124              
125             =item $cipher->encrypt(PLAINTEXT)
126              
127             PLAINTEXT must be exactly eight octets. The block is encrypted, and
128             the ciphertext is returned.
129              
130             =item $cipher->decrypt(CIPHERTEXT)
131              
132             CIPHERTEXT must be exactly eight octets. The block is decrypted, and
133             the plaintext is returned.
134              
135             =item $cipher->p_array
136              
137             Returns a reference to an 18-element array containing the 32-bit round
138             keys used in this cipher object.
139              
140             =item $cipher->s_boxes
141              
142             Returns a reference to a 4-element array containing the S-boxes used in
143             this cipher object. Each S-box is a 256-element array of 32-bit entries.
144              
145             =item $cipher->is_weak
146              
147             Returns a truth value indicating whether this is a weak key. A key is
148             considered weak if any S-box contains a pair of identical entries
149             (in any positions). When Blowfish is used with such an S-box, certain
150             cryptographic attacks are possible that are not possible against most
151             keys. The current (as of 2007) cryptanalytic results on Blowfish do
152             not include an actual break of the algorithm when weak keys are used,
153             but if a break is ever developed then it is likely to be achieved for
154             weak keys before it is achieved for the general case.
155              
156             About one key in every 2^15 is weak (if the keys are randomly selected).
157             Because of the complicated key schedule in standard Blowfish it is not
158             possible to predict which keys will be weak without first performing the
159             full key setup, which is why this is a method on the keyed cipher object.
160             In some uses of Blowfish it may be desired to avoid weak keys; if so,
161             check using this method and generate a new random key when a weak key
162             is detected. Bruce Schneier, the designer of Blowfish, says it is
163             probably not worth avoiding weak keys.
164              
165             =back
166              
167             =head1 SEE ALSO
168              
169             L,
170             L,
171             L
172              
173             =head1 AUTHOR
174              
175             Eksblowfish guts originally by Solar Designer (solar at openwall.com).
176              
177             Modifications and Perl interface by Andrew Main (Zefram)
178             .
179              
180             =head1 COPYRIGHT
181              
182             Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
183             Andrew Main (Zefram)
184              
185             The original Eksblowfish code (in the form of crypt()) from which
186             this module is derived is in the public domain. It may be found at
187             L.
188              
189             =head1 LICENSE
190              
191             This module is free software; you can redistribute it and/or modify it
192             under the same terms as Perl itself.
193              
194             =cut
195              
196             1;