| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Crypt::Eksblowfish::Uklblowfish - Blowfish cipher with unrestricted key length |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Crypt::Eksblowfish::Uklblowfish; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$block_size = Crypt::Eksblowfish::Uklblowfish->blocksize; |
|
10
|
|
|
|
|
|
|
$key_size = Crypt::Eksblowfish::Uklblowfish->keysize; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$cipher = Crypt::Eksblowfish::Uklblowfish->new($key); |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$block_size = $cipher->blocksize; |
|
15
|
|
|
|
|
|
|
$ciphertext = $cipher->encrypt($plaintext); |
|
16
|
|
|
|
|
|
|
$plaintext = $cipher->decrypt($ciphertext); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$p_array = $cipher->p_array; |
|
19
|
|
|
|
|
|
|
$s_boxes = $cipher->s_boxes; |
|
20
|
|
|
|
|
|
|
if($cipher->is_weak) { ... |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
An object of this type encapsulates a keyed instance of the Blowfish |
|
25
|
|
|
|
|
|
|
block cipher, ready to encrypt and decrypt. However, if you're |
|
26
|
|
|
|
|
|
|
looking for an implementation of Blowfish you most likely want |
|
27
|
|
|
|
|
|
|
L. This class differs from the standard |
|
28
|
|
|
|
|
|
|
Blowfish in that it accepts some keys that Blowfish officially does |
|
29
|
|
|
|
|
|
|
not permit. |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Blowfish is a symmetric cipher algorithm designed by Bruce Schneier in |
|
32
|
|
|
|
|
|
|
1993. It operates on 64-bit blocks, and takes a variable-length key. |
|
33
|
|
|
|
|
|
|
Officially the key can vary from 32 bits (4 octets) to 448 bits (56 |
|
34
|
|
|
|
|
|
|
octets) in increments of 8 bits (1 octet). In fact the algorithm can |
|
35
|
|
|
|
|
|
|
easily operate on a key of any number of octets from 1 (8 bits) to 72 |
|
36
|
|
|
|
|
|
|
(576 bits). Some implementations don't enforce the official key length |
|
37
|
|
|
|
|
|
|
limits, and so for compatibility it is sometimes necessary to handle a |
|
38
|
|
|
|
|
|
|
Blowfish key of a prohibited length. That is what this class is for. |
|
39
|
|
|
|
|
|
|
The "Ukl" in the name stands for "unrestricted key length". |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Using a very short key is generally a bad idea because there aren't |
|
42
|
|
|
|
|
|
|
very many keys of that length and so it's easy for an attacker to try |
|
43
|
|
|
|
|
|
|
them all. The official 32-bit minimum for Blowfish was already far |
|
44
|
|
|
|
|
|
|
too short for serious security at the time that Blowfish was designed. |
|
45
|
|
|
|
|
|
|
(A machine to crack 56-bit DES keys by brute force in a few days each |
|
46
|
|
|
|
|
|
|
was publicly built only five years later.) Do not base your security |
|
47
|
|
|
|
|
|
|
on the secrecy of a short key. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Using overlong keys has more interesting effects, which depend on internal |
|
50
|
|
|
|
|
|
|
features of Blowfish. When the key exceeds 64 octets (512 bits), varying |
|
51
|
|
|
|
|
|
|
key bits past that length results in subkeys which have predictable |
|
52
|
|
|
|
|
|
|
relationships. There is also some possibility of equivalent keys when |
|
53
|
|
|
|
|
|
|
the keys exceed 64 octets and differ only in the first 8 octets (64 bits). |
|
54
|
|
|
|
|
|
|
These phenomena have not been extensively studied in the open literature, |
|
55
|
|
|
|
|
|
|
so it is difficult to judge the degree of cryptographic weakness that |
|
56
|
|
|
|
|
|
|
results from them. It is clear that beyond some length Blowfish keys |
|
57
|
|
|
|
|
|
|
do not have as much strength as their length would suggest, and it is |
|
58
|
|
|
|
|
|
|
possible that overlong keys have specific weaknesses that render them |
|
59
|
|
|
|
|
|
|
weaker than shorter keys. If choosing a key for security, it is advised |
|
60
|
|
|
|
|
|
|
to stay within the official length limit of 56 octets. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
In summary: using Blowfish keys of officially-unsupported lengths |
|
63
|
|
|
|
|
|
|
causes security problems. If you are using Blowfish for security, |
|
64
|
|
|
|
|
|
|
and have the choice, use a key of an officially-supported length (and |
|
65
|
|
|
|
|
|
|
a standard implementation such as L). |
|
66
|
|
|
|
|
|
|
Use out-of-range key lengths (and this class) only for compatibility or |
|
67
|
|
|
|
|
|
|
cryptanalytic reasons. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
package Crypt::Eksblowfish::Uklblowfish; |
|
72
|
|
|
|
|
|
|
|
|
73
|
1
|
|
|
1
|
|
96480
|
{ use 5.006; } |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
466
|
|
|
74
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
48
|
|
|
75
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
156
|
|
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
our $VERSION = "0.009"; |
|
78
|
|
|
|
|
|
|
|
|
79
|
1
|
|
|
1
|
|
1430
|
use parent "Crypt::Eksblowfish::Subkeyed"; |
|
|
1
|
|
|
|
|
836
|
|
|
|
1
|
|
|
|
|
7
|
|
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
die "mismatched versions of Crypt::Eksblowfish modules" |
|
82
|
|
|
|
|
|
|
unless $Crypt::Eksblowfish::Subkeyed::VERSION eq $VERSION; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 CLASS METHODS |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=over |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item Crypt::Eksblowfish::Uklblowfish->blocksize |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Returns 8, indicating the Blowfish block size of 8 octets. This method |
|
91
|
|
|
|
|
|
|
may be called on either the class or an instance. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item Crypt::Eksblowfish::Uklblowfish->keysize |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns 0, indicating that the key size is variable. This situation is |
|
96
|
|
|
|
|
|
|
handled specially by C. |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=back |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
|
101
|
|
|
|
|
|
|
|
|
102
|
1
|
|
|
1
|
1
|
981
|
sub keysize { 0 } |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=over |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item Crypt::Eksblowfish::Uklblowfish->new(KEY) |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Performs key setup on a new instance of the Blowfish algorithm, returning |
|
111
|
|
|
|
|
|
|
the keyed state. The KEY may be any length from 1 octet to 72 octets |
|
112
|
|
|
|
|
|
|
inclusive. |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=back |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 METHODS |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=over |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item $cipher->blocksize |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Returns 8, indicating the Blowfish 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
|
|
|
|
|
|
|
=item $cipher->s_boxes |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
These methods extract the subkeys from the keyed cipher. |
|
140
|
|
|
|
|
|
|
This is not required in ordinary operation. See the superclass |
|
141
|
|
|
|
|
|
|
L for details. |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item $cipher->is_weak |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This method checks whether the cipher has been keyed with a weak key. |
|
146
|
|
|
|
|
|
|
It may be desired to avoid using weak keys. See the superclass |
|
147
|
|
|
|
|
|
|
L for details. |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This method does not detect any cryptographic weaknesses that might result |
|
150
|
|
|
|
|
|
|
from the related-key properties and other features of overlong keys. |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=back |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 AUTHOR |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Eksblowfish guts originally by Solar Designer (solar at openwall.com). |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Modifications and Perl interface by Andrew Main (Zefram) |
|
163
|
|
|
|
|
|
|
. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 |
|
168
|
|
|
|
|
|
|
Andrew Main (Zefram) |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The original Eksblowfish code (in the form of crypt()) from which |
|
171
|
|
|
|
|
|
|
this module is derived is in the public domain. It may be found at |
|
172
|
|
|
|
|
|
|
L. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 LICENSE |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
|
177
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
1; |