Passphrase/BlowfishCrypt.pm

filter: Statementstmt 97.7% 85 / 87 Branchbran 67.7% 42 / 62 Conditioncond 46.6% 7 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 81.2% 165 / 203 CC 37 SCAR 34.2
CC
37
Cov
82%
new
50.0
from_crypt
3.2
as_crypt
3.0
CRAP 30.6
« Passphrase/BigCrypt.pm ↑ Summary Passphrase/PHPass.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::BlowfishCrypt - passphrases using the Blowfish-based
4 Unix crypt()
5  
6 =head1 SYNOPSIS
7  
8 use Authen::Passphrase::BlowfishCrypt;
9  
10 $ppr = Authen::Passphrase::BlowfishCrypt->new(
11 cost => 14,
12 salt => "sodium__chloride",
13 hash_base64 => "BPZijhMHLvPeNMHd6XwZyNamOXVBTPi");
14  
15 $ppr = Authen::Passphrase::BlowfishCrypt->new(
16 cost => 14, salt_random => 1,
17 passphrase => "passphrase");
18  
19 $ppr = Authen::Passphrase::BlowfishCrypt->from_crypt(
20 '$2a$08$a07iYVTrVz7hYEvtakjiXOB'.
21 'PZijhMHLvPeNMHd6XwZyNamOXVBTPi');
22  
23 $ppr = Authen::Passphrase::BlowfishCrypt->from_rfc2307(
24 '{CRYPT}$2a$08$a07iYVTrVz7hYEvtakjiXOB'.
25 'PZijhMHLvPeNMHd6XwZyNamOXVBTPi');
26  
27 $key_nul = $ppr->key_nul;
28 $cost = $ppr->cost;
29 $cost = $ppr->keying_nrounds_log2;
30 $salt = $ppr->salt;
31 $salt_base64 = $ppr->salt_base64;
32 $hash = $ppr->hash;
33 $hash_base64 = $ppr->hash_base64;
34  
35 if($ppr->match($passphrase)) { ...
36  
37 $passwd = $ppr->as_crypt;
38 $userPassword = $ppr->as_rfc2307;
39  
40 =head1 DESCRIPTION
41  
42 An object of this class encapsulates a passphrase hashed using the
43 Blowfish-based Unix crypt() hash function, known as "bcrypt". This is
44 a subclass of L<Authen::Passphrase>, and this document assumes that the
45 reader is familiar with the documentation for that class.
46  
47 The crypt() function in a modern Unix actually supports several different
48 passphrase schemes. This class is concerned only with one particular
49 scheme, a Blowfish-based algorithm designed by Niels Provos and David
50 Mazieres for OpenBSD. To handle the whole range of passphrase schemes
51 supported by the modern crypt(), see the
52 L<from_crypt|Authen::Passphrase/from_crypt> constructor and the
53 L<as_crypt|Authen::Passphrase/as_crypt> method in L<Authen::Passphrase>.
54  
55 The Blowfish-based crypt() scheme uses a variant of Blowfish called
56 "Eksblowfish", for "expensive key schedule Blowfish". It has the
57 cryptographic strength of Blowfish, and a very slow key setup phase
58 to resist brute-force attacks. There is a "cost" parameter to the
59 scheme: the length of key setup is proportional to 2^cost. There is
60 a 128-bit salt. Up to 72 characters of the passphrase will be used;
61 any more will be ignored.
62  
63 The cost, salt, and passphrase are all used to (very
64 slowly) key Eksblowfish. Once key setup is done, the string
65 "OrpheanBeholderScryDoubt" (three Blowfish blocks long) is encrypted 64
66 times in ECB mode. The final byte of the ciphertext is then dropped,
67 yielding a 23-byte hash.
68  
69 In the crypt() function the salt and hash are represented in ASCII
70 using a base 64 encoding. The base 64 digits are "B<.>", "B</>",
71 "B<A>" to "B<Z>", "B<a>" to "B<z>", "B<0>" to "B<9>" (in that order).
72 The 16-byte salt is represented as 22 base 64 digits, and the 23-byte
73 hash as 31 base 64 digits.
74  
75 This algorithm is intended for situations where the efficiency of
76 a brute force attack is a concern. It is suitable for use in new
77 applications where this requirement exists. If that is not a concern,
78 and it suffices to merely make brute force the most efficient attack, see
79 L<Authen::Passphrase::SaltedDigest> for more efficient hash algorithms.
80  
81 Choice of the cost parameter is critical, due to the need to trade off
82 expense of brute-force attack against speed of legitimate passphrase
83 verification. A traditional target is that verification should take
84 about one second on widely-available hardware. (Algorithms that are
85 concerned about brute force speed but lack a cost parameter have often
86 aimed for this, with respect to hardware available at the time of the
87 algorithm's introduction.) As of 2011, this is achieved with a cost
88 parameter around 14.
89  
90 =cut
91  
92 package Authen::Passphrase::BlowfishCrypt;
93  
94 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
100 2 ▶use Crypt::Eksblowfish::Bcrypt 0.008 qw(bcrypt_hash en_base64 de_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use Crypt::SysRandom 'random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 2 our $VERSION = "0.009";
104  
105 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 =head1 CONSTRUCTORS
108  
109 =over
110  
111 =item Authen::Passphrase::BlowfishCrypt->new(ATTR => VALUE, ...)
112  
113 Generates a new passphrase recogniser object using the Blowfish-based
114 crypt() algorithm. The following attributes may be given:
115  
116 =over
117  
118 =item B<key_nul>
119  
120 Truth value indicating whether to append a NUL to the passphrase before using
121 it as a key. The algorithm as originally devised does not do this,
122 but it was later modified to do it. The version that does append NUL
123 is to be preferred. Default true.
124  
125 =item B<cost>
126  
127 Base-two logarithm of the number of keying rounds to perform.
128  
129 =item B<keying_nrounds_log2>
130  
131 Synonym for B<cost>.
132  
133 =item B<salt>
134  
135 The salt, as a 16-byte string.
136  
137 =item B<salt_base64>
138  
139 The salt, as a string of 22 base 64 digits.
140  
141 =item B<salt_random>
142  
143 Causes salt to be generated randomly. The value given for this attribute
144 is ignored.
145  
146 =item B<hash>
147  
148 The hash, as a 23-byte string.
149  
150 =item B<hash_base64>
151  
152 The hash, as a string of 31 base 64 digits.
153  
154 =item B<passphrase>
155  
156 A passphrase that will be accepted.
157  
158 =back
159  
160 The cost and salt must be given, and either the hash or the passphrase.
161  
162 =cut
163  
164 11 ▶sub new {
Subroutine new: called CC=33 SCAR=39.1
Pod documented
165 11 my $class = shift;
166 11 my $self = bless({}, $class);
167 11 my $passphrase;
168 11 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 40 11 51
169 40 my $attr = shift;
170 40 my $value = shift;
171 40 ▶ if($attr eq "key_nul") {
Branch: 8 branches
BranchTrueFalseTotal
if ($attr eq 'key_nul') { } 7 33 40
elsif ($attr eq 'cost' or $attr eq 'keying_nrounds_log2') { } 11 22 33
elsif ($attr eq 'salt') { } 1 21 22
elsif ($attr eq 'salt_base64') { } 9 12 21
elsif ($attr eq 'salt_random') { } 1 11 12
elsif ($attr eq 'hash') { } 1 10 11
elsif ($attr eq 'hash_base64') { } 8 2 10
elsif ($attr eq 'passphrase') { } 2 0 2
172 croak "foldness specified redundantly"
173 7 ▶ if exists $self->{fold};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"fold"} 0 7 7
174 7 $self->{key_nul} = !!$value;
175 ▶ } elsif($attr eq "cost" || $attr eq "keying_nrounds_log2") {
Condition100%3/3
$attr eq "cost" or $attr eq "keying_nrounds_log2"
l!l&&r!l&&!r
10122
MC/DC100%2/2
$attr eq "cost" or $attr eq "keying_nrounds_log2"
$attr eq "cost" $attr eq "keying_nrounds_log2"
Truth table100%3/3
A or B
A= $attr eq "cost"B= $attr eq "keying_nrounds_log2"
ABresult
1X1
011
000
176 croak "cost specified redundantly"
177 11 ▶ if exists $self->{cost};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cost"} 0 11 11
178 11 ▶ croak "\"$value\" is not a valid cost parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless $value == int $value and $value >= 0 0 11 11
Condition33%1/3
$value == int $value and $value >= 0
!ll&&!rl&&r
0011
MC/DC0%0/2
$value == int $value and $value >= 0
$value == int $value $value >= 0
Truth table33%1/3
A and B
A= $value == int $valueB= $value >= 0
ABresult
0X0
100
111
179 unless $value == int($value) && $value >= 0;
180 11 $self->{cost} = 0+$value;
181 } elsif($attr eq "salt") {
182 croak "salt specified redundantly"
183 1 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 1 1
184 1 ▶ $value =~ m#\A[\x00-\xff]{16}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{16}\z/ or croak(qq["$value" is not a valid raw salt]) 1 0 1
185 or croak "\"$value\" is not a valid raw salt";
186 1 $self->{salt} = "$value";
187 } elsif($attr eq "salt_base64") {
188 croak "salt specified redundantly"
189 9 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 9 9
190 9 ▶ croak "\"$value\" is not a valid salt"
Branch: 1 branch
BranchTrueFalseTotal
unless length $value == 22 0 9 9
191 unless length($value) == 22;
192 9 $self->{salt} = de_base64($value);
193 } elsif($attr eq "salt_random") {
194 croak "salt specified redundantly"
195 1 ▶ if exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"salt"} 0 1 1
196 1 $self->{salt} = random_bytes(16);
197 } elsif($attr eq "hash") {
198 croak "hash specified redundantly"
199 1 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 1 1
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
001
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
200 defined($passphrase);
201 1 ▶ $value =~ m#\A[\x00-\xff]{23}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{23}\z/ or croak("not a valid raw hash") 1 0 1
202 or croak "not a valid raw hash";
203 1 $self->{hash} = "$value";
204 } elsif($attr eq "hash_base64") {
205 croak "hash specified redundantly"
206 8 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 8 8
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
008
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
207 defined($passphrase);
208 8 ▶ croak "\"$value\" is not a valid hash"
Branch: 1 branch
BranchTrueFalseTotal
unless length $value == 31 0 8 8
209 unless length($value) == 31;
210 8 $self->{hash} = de_base64($value);
211 } elsif($attr eq "passphrase") {
212 croak "passphrase specified redundantly"
213 2 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 2 2
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
002
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
214 defined($passphrase);
215 2 $passphrase = $value;
216 } else {
217 0 croak "unrecognised attribute `$attr'";
218 }
219 }
220 11 ▶ $self->{key_nul} = !!1 unless exists $self->{key_nul};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"key_nul"} 4 7 11
221 11 ▶ croak "cost not specified" unless exists $self->{cost};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"cost"} 0 11 11
222 11 ▶ croak "salt not specified" unless exists $self->{salt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"salt"} 0 11 11
223 11 ▶ $self->{hash} = $self->_hash_of($passphrase) if defined $passphrase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 2 9 11
224 11 ▶ croak "hash not specified" unless exists $self->{hash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"} 0 11 11
225 11 return $self;
226 }
227  
228 =item Authen::Passphrase::BlowfishCrypt->from_crypt(PASSWD)
229  
230 Generates a new passphrase recogniser object using the Blowfish-based
231 crypt() algorithm, from a crypt string. The crypt string must start with
232 "B<$2$>" for the version that does not append NUL to the key, or "B<$2a$>"
233 for the version that does. The next two characters must be decimal digits
234 giving the cost parameter. This must be followed by "B<$>", 22 base 64
235 digits giving the salt, and finally 31 base 64 digits giving the hash.
236  
237 =cut
238  
239 2 ▶sub from_crypt {
Subroutine from_crypt: called CC=3 SCAR=11.8
Pod documented
240 2 my($class, $passwd) = @_;
241 2 ▶ if($passwd =~ /\A(\$2a?\$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passwd =~ /\A(\$2a?\$)/) 2 0 2
242 2 ▶ $passwd =~ m#\A\$2(a?)\$([0-9]{2})\$
Branch: 1 branch
BranchTrueFalseTotal
$passwd =~ m[\A\$2(a?)\$([0-9]{2})\$ ([./A-Za-z0-9]{22})([./A-Za-z0-9]{31})\z]x or croak("malformed $1 data") 2 0 2
243 ([./A-Za-z0-9]{22})([./A-Za-z0-9]{31})\z#x
244 or croak "malformed $1 data";
245 2 my($kn, $cost, $salt, $hash) = ($1, $2, $3, $4);
246 2 return $class->new(key_nul => $kn, cost => $cost,
247 salt_base64 => $salt, hash_base64 => $hash);
248 }
249 0 return $class->SUPER::from_crypt($passwd);
250 }
251  
252 =item Authen::Passphrase::BlowfishCrypt->from_rfc2307(USERPASSWORD)
253  
254 Generates a new passphrase recogniser object using the Blowfish-based
255 crypt() algorithm, from an RFC 2307 string. The string must consist of
256 "B<{CRYPT}>" (case insensitive) followed by an acceptable crypt string.
257  
258 =back
259  
260 =head1 METHODS
261  
262 =over
263  
264 =item $ppr->key_nul
265  
266 Returns a truth value indicating whether a NUL will be appended to the
267 passphrase before using it as a key.
268  
269 =cut
270  
271 21 ▶sub key_nul {
Subroutine key_nul: called CC=1 SCAR=0.0
Pod documented
272 21 my($self) = @_;
273 21 return $self->{key_nul};
274 }
275  
276 =item $ppr->cost
277  
278 Returns the base-two logarithm of the number of keying rounds that will
279 be performed.
280  
281 =cut
282  
283 23 ▶sub cost {
Subroutine cost: called CC=1 SCAR=0.0
Pod documented
284 23 my($self) = @_;
285 23 return $self->{cost};
286 }
287  
288 =item $ppr->keying_nrounds_log2
289  
290 Synonym for L</cost>.
291  
292 =cut
293  
294 2 *keying_nrounds_log2 = \&cost;
295  
296 =item $ppr->salt
297  
298 Returns the salt, as a string of sixteen bytes.
299  
300 =cut
301  
302 3 ▶sub salt {
Subroutine salt: called CC=1 SCAR=0.0
Pod documented
303 3 my($self) = @_;
304 3 return $self->{salt};
305 }
306  
307 =item $ppr->salt_base64
308  
309 Returns the salt, as a string of 22 base 64 digits.
310  
311 =cut
312  
313 20 ▶sub salt_base64 {
Subroutine salt_base64: called CC=1 SCAR=0.0
Pod documented
314 20 my($self) = @_;
315 20 return en_base64($self->{salt});
316 }
317  
318 =item $ppr->hash
319  
320 Returns the hash value, as a string of 23 bytes.
321  
322 =cut
323  
324 3 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
325 3 my($self) = @_;
326 3 return $self->{hash};
327 }
328  
329 =item $ppr->hash_base64
330  
331 Returns the hash value, as a string of 31 base 64 digits.
332  
333 =cut
334  
335 20 ▶sub hash_base64 {
Subroutine hash_base64: called CC=1 SCAR=0.0
Pod documented
336 20 my($self) = @_;
337 20 return en_base64($self->{hash});
338 }
339  
340 =item $ppr->match(PASSPHRASE)
341  
342 =item $ppr->as_crypt
343  
344 =item $ppr->as_rfc2307
345  
346 These methods are part of the standard L<Authen::Passphrase> interface.
347  
348 =cut
349  
350 28 ▶sub _hash_of {
Subroutine _hash_of: called CC=1 SCAR=0.0
351 28 my($self, $passphrase) = @_;
352 return bcrypt_hash({
353 key_nul => $self->{key_nul},
354 cost => $self->{cost},
355 salt => $self->{salt},
356 28 }, $passphrase);
357 }
358  
359 26 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
360 26 my($self, $passphrase) = @_;
361 26 return $self->_hash_of($passphrase) eq $self->{hash};
362 }
363  
364 10 ▶sub as_crypt {
Subroutine as_crypt: called CC=3 SCAR=11.1
Pod documented
365 10 my($self) = @_;
366 croak "passphrase can't be expressed as a crypt string"
367 10 ▶ if $self->{cost} > 99;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cost"} > 99 0 10 10
368 10 ▶ return sprintf("\$2%s\$%02d\$%s%s", $self->key_nul ? "a" : "",
Branch: 1 branch
BranchTrueFalseTotal
$self->key_nul ? : 2 8 10
369 $self->cost, $self->salt_base64, $self->hash_base64);
370 }
371  
372 =back
373  
374 =head1 SEE ALSO
375  
376 L<Authen::Passphrase>,
377 L<Crypt::Eksblowfish::Bcrypt>
378  
379 =head1 AUTHOR
380  
381 Andrew Main (Zefram) <zefram@fysh.org>
382  
383 =head1 COPYRIGHT
384  
385 Copyright (C) 2006, 2007, 2009, 2010, 2012
386 Andrew Main (Zefram) <zefram@fysh.org>
387  
388 =head1 LICENSE
389  
390 This module is free software; you can redistribute it and/or modify it
391 under the same terms as Perl itself.
392  
393 =cut
394  
395 2 1;
« Passphrase/BigCrypt.pm ↑ Summary Passphrase/PHPass.pm »