Passphrase/BigCrypt.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 74.1% 43 / 58 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 81.0% 154 / 190 CC 36 SCAR 34.7
CC
36
Cov
83%
new
49.9
match
5.0
BEGIN
1.0
CRAP 32.1
« Passphrase/VMSPurdy.pm ↑ Summary Passphrase/BlowfishCrypt.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::BigCrypt - passphrases using bigcrypt algorithm
4  
5 =head1 SYNOPSIS
6  
7 use Authen::Passphrase::BigCrypt;
8  
9 $ppr = Authen::Passphrase::BigCrypt->new(
10 salt_base64 => "qi",
11 hash_base64 => "yh4XPJGsOZ2MEAyLkfWqeQ");
12  
13 $ppr = Authen::Passphrase::BigCrypt->new(
14 salt_random => 12,
15 passphrase => "passphrase");
16  
17 $salt = $ppr->salt;
18 $salt_base64 = $ppr->salt_base64_2;
19 $hash = $ppr->hash;
20 $hash_base64 = $ppr->hash_base64;
21  
22 $pprs = $ppr->sections;
23  
24 if($ppr->match($passphrase)) { ...
25  
26 =head1 DESCRIPTION
27  
28 An object of this class encapsulates a passphrase hashed using the
29 "bigcrypt" hash function found in HP-UX, Digital Unix, OSF/1, and some
30 other flavours of Unix. Do not confuse this with the "crypt16" found
31 on Ultrix and Tru64 (for which see L<Authen::Passphrase::Crypt16>).
32 This is a subclass of L<Authen::Passphrase>, and this document assumes
33 that the reader is familiar with the documentation for that class.
34  
35 This is a derivation of the original DES-based crypt function found on all
36 Unices (see L<Authen::Passphrase::DESCrypt>). The first eight bytes of
37 the passphrase are used as a DES key to encrypt the all-bits-zero block
38 through 25 rounds of (12-bit) salted DES, just like the original crypt.
39 Then, if the passphrase is longer than eight bytes, the next eight bytes
40 are used as a DES key to encrypt the all-bits-zero block through 25
41 rounds of salted DES, using as salt the first 12 bits of the hash of the
42 first section. Then, if the passphrase is longer than sixteen bytes,
43 the next eight bytes are used, with salt consisting of the first 12
44 bits of the hash of the second section. This repeats until the entire
45 passphrase has been used. The hashes of all the sections are concatenated
46 to form the final hash.
47  
48 A password hash of this scheme is conventionally represented in ASCII
49 using the base 64 encoding of the underlying DES-based crypt function.
50 The first two characters give the salt for the first section, the next
51 eleven give the hash of the first section, the next eleven give the hash
52 of the second section, and so on. A hash thus encoded is used as a crypt
53 string, on those systems where the bigcrypt algorithm is part of crypt(),
54 but the syntax clashes with that of crypt16. This module does not treat
55 it as a crypt string syntax.
56  
57 Because the sections of the passphrase are hashed separately, it is
58 possible to manipulate (e.g., crack) a section hash in isolation.
59 See L<Authen::Passphrase::DESCrypt> for handling of a single section.
60  
61 I<Warning:> This is a fatally flawed design, often providing I<less>
62 security than the plain DES scheme alone. Do not use seriously.
63  
64 =cut
65  
66 package Authen::Passphrase::BigCrypt;
67  
68 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use Authen::Passphrase::DESCrypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 2 ▶use Crypt::UnixCrypt_XS 0.08 qw(base64_to_block base64_to_int12);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use Crypt::SysRandom 'random_bytes';
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 2 our $VERSION = "0.009";
79  
80 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 =head1 CONSTRUCTOR
83  
84 =over
85  
86 =item Authen::Passphrase::BigCrypt->new(ATTR => VALUE, ...)
87  
88 Generates a new passphrase recogniser object using the bigcrypt hash
89 algorithm. The following attributes may be given:
90  
91 =over
92  
93 =item B<salt>
94  
95 The salt for the first section, as an integer in the range [0, 4096).
96  
97 =item B<salt_base64>
98  
99 The salt for the first section, as a string of two base 64 digits.
100  
101 =item B<salt_random>
102  
103 Causes salt for the first section to be generated randomly. The value
104 given for this attribute must be 12, indicating generation of 12 bits
105 of salt.
106  
107 =item B<hash>
108  
109 The hash, as a string of bytes.
110  
111 =item B<hash_base64>
112  
113 The hash, as a string of base 64 digits.
114  
115 =item B<passphrase>
116  
117 A passphrase that will be accepted.
118  
119 =back
120  
121 The salt for the first section must be given, and either the hash or
122 the passphrase.
123  
124 =cut
125  
126 14 ▶sub new {
Subroutine new: called CC=32 SCAR=39.1
Pod documented
127 14 my $class = shift;
128 14 my $salt;
129 my @hashes;
130 14 my $passphrase;
131 14 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 28 14 42
132 28 my $attr = shift;
133 28 my $value = shift;
134 28 ▶ if($attr eq "salt") {
Branch: 6 branches
BranchTrueFalseTotal
if ($attr eq 'salt') { } 4 24 28
elsif ($attr eq 'salt_base64') { } 9 15 24
elsif ($attr eq 'salt_random') { } 1 14 15
elsif ($attr eq 'hash') { } 1 13 14
elsif ($attr eq 'hash_base64') { } 9 4 13
elsif ($attr eq 'passphrase') { } 4 0 4
135 4 ▶ croak "salt specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $salt 0 4 4
136 if defined $salt;
137 4 ▶ croak "\"$value\" is not a valid salt"
Branch: 1 branch
BranchTrueFalseTotal
unless $value == int $value and $value >= 0 and $value < 4096 0 4 4
Condition33%2/6
$value == int $value and $value >= 0 and $value < 4096
!ll&&!rl&&r
004
$value == int $value and $value >= 0
!ll&&!rl&&r
004
MC/DC0%0/3
$value == int $value and $value >= 0 and $value < 4096
$value == int $value $value >= 0 $value < 4096
Truth table25%1/4
A and B and C
A= $value == int $valueB= $value >= 0C= $value < 4096
ABCresult
0XX0
10X0
1100
1111
138 unless $value == int($value) &&
139 $value >= 0 && $value < 4096;
140 4 $salt = $value;
141 } elsif($attr eq "salt_base64") {
142 9 ▶ croak "salt specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $salt 0 9 9
143 if defined $salt;
144 9 ▶ $value =~ m#\A[./0-9A-Za-z]{2}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ m[\A[./0-9A-Za-z]{2}\z] or croak(qq["$value" is not a valid salt]) 9 0 9
145 or croak "\"$value\" is not a valid salt";
146 9 $salt = base64_to_int12($value);
147 } elsif($attr eq "salt_random") {
148 1 ▶ croak "salt specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if defined $salt 0 1 1
149 if defined $salt;
150 1 ▶ croak "\"$value\" is not a valid salt size"
Branch: 1 branch
BranchTrueFalseTotal
unless $value == 12 0 1 1
151 unless $value == 12;
152 1 $salt = unpack("S", random_bytes(2)) % 4096;
153 } elsif($attr eq "hash") {
154 1 ▶ croak "hash specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if @hashes or defined $passphrase 0 1 1
Condition33%1/3
@hashes or defined $passphrase
l!l&&r!l&&!r
001
MC/DC0%0/2
@hashes or defined $passphrase
@hashes defined $passphrase
Truth table33%1/3
A or B
A= @hashesB= defined $passphrase
ABresult
1X1
011
000
155 if @hashes || defined($passphrase);
156 1 ▶ $value =~ m#\A(?:[\x00-\xff]{8})+\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A(?:[\x00-\xff]{8})+\z/ or croak("not a valid bigcrypt hash") 1 0 1
157 or croak "not a valid bigcrypt hash";
158 1 ▶ push @hashes, $1 while $value =~ /(.{8})/sg;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /(.{8})/gs and do { push @hashes, $1 } 2 1 3
159 } elsif($attr eq "hash_base64") {
160 9 ▶ croak "hash specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if @hashes or defined $passphrase 0 9 9
Condition33%1/3
@hashes or defined $passphrase
l!l&&r!l&&!r
009
MC/DC0%0/2
@hashes or defined $passphrase
@hashes defined $passphrase
Truth table33%1/3
A or B
A= @hashesB= defined $passphrase
ABresult
1X1
011
000
161 if @hashes || defined($passphrase);
162 9 ▶ $value =~ m#\A(?:[./0-9A-Za-z]{10}[.26AEIMQUYcgkosw])
Branch: 1 branch
BranchTrueFalseTotal
$value =~ m[\A(?:[./0-9A-Za-z]{10}[.26AEIMQUYcgkosw]) +\z]x or croak(qq["$value" is not a valid ] . 'encoded hash') 9 0 9
163 +\z#x
164 or croak "\"$value\" is not a valid ".
165 "encoded hash";
166 9 ▶ while($value =~ /(.{11})/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(.{11})/gs) 16 9 25
167 16 my $b64 = $1;
168 16 push @hashes, base64_to_block($b64);
169 }
170 } elsif($attr eq "passphrase") {
171 4 ▶ croak "passphrase specified redundantly"
Branch: 1 branch
BranchTrueFalseTotal
if @hashes or defined $passphrase 0 4 4
Condition33%1/3
@hashes or defined $passphrase
l!l&&r!l&&!r
004
MC/DC0%0/2
@hashes or defined $passphrase
@hashes defined $passphrase
Truth table33%1/3
A or B
A= @hashesB= defined $passphrase
ABresult
1X1
011
000
172 if @hashes || defined($passphrase);
173 4 $passphrase = $value;
174 } else {
175 0 croak "unrecognised attribute `$attr'";
176 }
177 }
178 14 ▶ croak "salt not specified" unless defined $salt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $salt 0 14 14
179 14 my @sections;
180 14 ▶ if(defined $passphrase) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $passphrase) { } 4 10 14
elsif (@hashes) { } 10 0 10
181 4 ▶ my $nsegs = $passphrase eq "" ? 1 :
Branch: 1 branch
BranchTrueFalseTotal
$passphrase eq '' ? : 0 4 4
182 ((length($passphrase) + 7) >> 3);
183 4 ▶ for(my $i = 0; $i != $nsegs; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $nsegs) 8 4 12
184 8 push @sections,
185 Authen::Passphrase::DESCrypt
186 ->new(salt => $salt,
187 passphrase =>
188 substr($passphrase, $i << 3, 8));
189 8 $salt = base64_to_int12(
190 substr($sections[-1]->hash_base64, 0, 2));
191 }
192 } elsif(@hashes) {
193 10 foreach my $hash (@hashes) {
194 18 push @sections, Authen::Passphrase::DESCrypt
195 ->new(salt => $salt, hash => $hash);
196 18 $salt = base64_to_int12(
197 substr($sections[-1]->hash_base64, 0, 2));
198 }
199 } else {
200 0 croak "hash not specified";
201 }
202 14 return bless(\@sections, $class);
203 }
204  
205 =back
206  
207 =head1 METHODS
208  
209 =over
210  
211 =item $ppr->salt
212  
213 Returns the salt for the first section, as a Perl integer.
214  
215 =cut
216  
217 4 ▶sub salt { $_[0]->[0]->salt }
Subroutine salt: called CC=1 SCAR=0.0
Pod documented
218  
219 =item $ppr->salt_base64_2
220  
221 Returns the salt for the first section, as a string of two base 64 digits.
222  
223 =cut
224  
225 11 ▶sub salt_base64_2 { $_[0]->[0]->salt_base64_2 }
Subroutine salt_base64_2: called CC=1 SCAR=0.0
Pod documented
226  
227 =item $ppr->hash
228  
229 Returns the hash value, as a string of bytes.
230  
231 =cut
232  
233 2 ▶sub hash { join("", map { $_->hash } @{$_[0]}) }
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
234  
235 =item $ppr->hash_base64
236  
237 Returns the hash value, as a string of base 64 digits. This is the
238 concatenation of the base 64 encodings of the section hashes, rather
239 than a base64 encoding of the combined hash.
240  
241 =cut
242  
243 13 ▶sub hash_base64 { join("", map { $_->hash_base64 } @{$_[0]}) }
Subroutine hash_base64: called CC=1 SCAR=0.0
Pod documented
244  
245 =item $ppr->sections
246  
247 Returns a reference to an array of L<Authen::Passphrase::DESCrypt>
248 passphrase recognisers for the sections of the passphrase.
249  
250 =cut
251  
252 11 ▶sub sections { [ @{$_[0]} ] }
Subroutine sections: called CC=1 SCAR=0.0
Pod documented
253  
254 =item $ppr->match(PASSPHRASE)
255  
256 This method is part of the standard L<Authen::Passphrase> interface.
257  
258 =cut
259  
260 82 ▶sub match {
Subroutine match: called CC=5 SCAR=16.1
Pod documented
261 82 my Authen::Passphrase::BigCrypt $self = shift;
262 82 my($passphrase) = @_;
263 82 ▶ my $nsegs = $passphrase eq "" ? 1 : ((length($passphrase) + 7) >> 3);
Branch: 1 branch
BranchTrueFalseTotal
$passphrase eq '' ? : 9 73 82
264 82 ▶ return 0 unless $nsegs == @$self;
Branch: 1 branch
BranchTrueFalseTotal
unless $nsegs == @$self 46 36 82
265 36 ▶ for(my $i = $nsegs; $i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 46 10 56
266 46 ▶ return 0 unless $self->[$i]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[$i]->match(substr($passphrase, $i << 3, 8)) 26 20 46
267 ->match(substr($passphrase, $i << 3, 8));
268 }
269 10 return 1;
270 }
271  
272 =back
273  
274 =head1 SEE ALSO
275  
276 L<Authen::Passphrase>,
277 L<Authen::Passphrase::DESCrypt>
278  
279 =head1 AUTHOR
280  
281 Andrew Main (Zefram) <zefram@fysh.org>
282  
283 =head1 COPYRIGHT
284  
285 Copyright (C) 2006, 2007, 2009, 2010, 2012
286 Andrew Main (Zefram) <zefram@fysh.org>
287  
288 =head1 LICENSE
289  
290 This module is free software; you can redistribute it and/or modify it
291 under the same terms as Perl itself.
292  
293 =cut
294  
295 2 1;
« Passphrase/VMSPurdy.pm ↑ Summary Passphrase/BlowfishCrypt.pm »