Passphrase/Crypt16.pm

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