| 1 | package Database::BI::Password; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | our $VERSION = '0.023.1'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Digest::SHA qw(hmac_sha256); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Database::BI::I18N (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | our @EXPORT_OK = qw( hash_password verify_password is_password_hash ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # PBKDF2-HMAC-SHA256 (RFC 8018), written out because no password-hashing | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # module is a prerequisite and Digest::SHA is core. The work is in | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # hmac_sha256, which is XS: 200_000 rounds take about 0.4s here, which a reader | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # logging in does not notice and a guesser with a copy of the hash does. The | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # rounds are stored in the hash, so a later default does not lock anyone out. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | Readonly my $DEFAULT_ROUNDS => 200_000; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | Readonly my $MIN_ROUNDS => 10_000; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | Readonly my $MAX_ROUNDS => 100_000_000; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | Readonly my $SALT_BYTES => 16; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | Readonly my $KEY_BYTES => 32; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | Readonly my $SCHEME => 'pbkdf2-sha256'; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | Readonly my $HASH_RE => qr{\A\Q$SCHEME\E\$(\d{5,9})\$([A-Za-z0-9+/]{22})\$([A-Za-z0-9+/]{43})\z}; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # What hash_password croaks with, for whoever calls it (the password command, a | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # test), in the language Database::BI::I18N chooses from the locale. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 5 | Readonly our %MESSAGES => ( | |||||||||||||||||||||||||||||||||||||||||
| 33 | error_password_required => 'hash_password: a password is required', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | error_password_rounds => 'hash_password: rounds must be a whole number from %d to %d', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | error_password_salt => 'hash_password: a salt is %d bytes', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | error_password_random => 'hash_password: Crypt::URandom gave %d random bytes, not %d', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # _text($key) -> the template for $key in the current language. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _text { return Database::BI::I18N::text(__PACKAGE__, \%MESSAGES, $_[0]) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # _pbkdf2($password, $salt, $rounds, $bytes) -> $key | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # One block is all a 32-byte key needs, but the loop over blocks is kept so | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # the function is the RFC's and its test vectors apply. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub _pbkdf2 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pbkdf2: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my ($password, $salt, $rounds, $bytes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $key = ''; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | for (my $block = 1; length($key) < $bytes; $block++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $u = hmac_sha256($salt . pack('N', $block), $password); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $t = $u; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | for (2 .. $rounds) { | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $u = hmac_sha256($u, $password); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | $t ^= $u; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $key .= $t; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return substr($key, 0, $bytes); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # _random_bytes($n) -> $bytes | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # Crypt::URandom is a prerequisite (Windows has no /dev/urandom, which this | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # once fell back to), and it croaks itself when the system has no source. | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub _random_bytes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_random_bytes: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my ($n) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | require Crypt::URandom; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $bytes = Crypt::URandom::urandom($n); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | croak sprintf(_text('error_password_random'), length($bytes // ''), $n) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $bytes and length $bytes == $n
$bytes // ""
MC/DC0%0/2
defined $bytes and length $bytes == $n
defined $bytes
length $bytes == $n
MC/DC0%0/1
$bytes // ""
$bytes
Truth table0%0/3
A and B
A= defined $bytesB= length $bytes == $n
Truth table0%0/2
A
A= $bytes
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | unless defined $bytes && length($bytes) == $n; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub _b64 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_b64: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | (my $s = encode_base64($_[0], '')) =~ s/=+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub _unb64 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unb64: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $s .= '=' x ((4 - length($s) % 4) % 4); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return decode_base64($s); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub _octets { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_octets: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my ($password) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | utf8::encode($password) if utf8::is_utf8($password); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return $password; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub hash_password { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash_password: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my ($password, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | croak _text('error_password_required') unless defined $password && length $password; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $password and length $password
MC/DC0%0/2
defined $password and length $password
defined $password
length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $rounds = $opts{rounds} // $DEFAULT_ROUNDS; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts{"rounds"} // $DEFAULT_ROUNDS
MC/DC0%0/2
$opts{"rounds"} // $DEFAULT_ROUNDS
$opts{"rounds"}
$DEFAULT_ROUNDS
Truth table0%0/3
A // B
A= $opts{"rounds"}B= $DEFAULT_ROUNDS
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | croak sprintf(_text('error_password_rounds'), $MIN_ROUNDS, $MAX_ROUNDS) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$rounds =~ /\A\d+\z/ and $rounds >= $MIN_ROUNDS and $rounds <= $MAX_ROUNDS
$rounds =~ /\A\d+\z/ and $rounds >= $MIN_ROUNDS
MC/DC0%0/3
$rounds =~ /\A\d+\z/ and $rounds >= $MIN_ROUNDS and $rounds <= $MAX_ROUNDS
$rounds =~ /\A\d+\z/
$rounds >= $MIN_ROUNDS
$rounds <= $MAX_ROUNDS
Truth table0%0/4
A and B and C
A= $rounds =~ /\A\d+\z/B= $rounds >= $MIN_ROUNDSC= $rounds <= $MAX_ROUNDS
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | unless $rounds =~ /\A\d+\z/ && $rounds >= $MIN_ROUNDS && $rounds <= $MAX_ROUNDS; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $salt = $opts{salt} // _random_bytes($SALT_BYTES); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts{"salt"} // _random_bytes($SALT_BYTES)
MC/DC0%0/2
$opts{"salt"} // _random_bytes($SALT_BYTES)
$opts{"salt"}
_random_bytes($SALT_BYTES)
Truth table0%0/3
A // B
A= $opts{"salt"}B= _random_bytes($SALT_BYTES)
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | croak sprintf(_text('error_password_salt'), $SALT_BYTES) unless length($salt) == $SALT_BYTES; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $key = _pbkdf2(_octets($password), $salt, $rounds, $KEY_BYTES); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return join '$', $SCHEME, $rounds, _b64($salt), _b64($key); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub is_password_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_password_hash: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return 0 unless defined $hash && !ref $hash && $hash =~ $HASH_RE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $hash and not ref $hash and $hash =~ /$HASH_RE/
defined $hash and not ref $hash
MC/DC0%0/3
defined $hash and not ref $hash and $hash =~ /$HASH_RE/
defined $hash
not ref $hash
$hash =~ /$HASH_RE/
Truth table0%0/4
A and not(B) and C
A= defined $hashB= not ref $hashC= $hash =~ /$HASH_RE/
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | return $1 >= $MIN_ROUNDS && $1 <= $MAX_ROUNDS ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$1 >= $MIN_ROUNDS and $1 <= $MAX_ROUNDS
MC/DC0%0/2
$1 >= $MIN_ROUNDS and $1 <= $MAX_ROUNDS
$1 >= $MIN_ROUNDS
$1 <= $MAX_ROUNDS
Truth table0%0/3
A and B
A= $1 >= $MIN_ROUNDSB= $1 <= $MAX_ROUNDS
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub verify_password { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_password: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my ($password, $hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | return 0 unless defined $password && length $password && is_password_hash($hash); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $password and length $password and is_password_hash($hash)
defined $password and length $password
MC/DC0%0/3
defined $password and length $password and is_password_hash($hash)
defined $password
length $password
is_password_hash($hash)
Truth table0%0/4
A and B and C
A= defined $passwordB= length $passwordC= is_password_hash($hash)
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my ($rounds, $salt, $want) = $hash =~ $HASH_RE; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $got = _pbkdf2(_octets($password), _unb64($salt), $rounds, $KEY_BYTES); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $want = _unb64($want); | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # Every byte is compared, so the time taken says nothing about how much | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # of a guess was right. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $diff = length($got) ^ length($want); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $diff |= ord(substr($got, $_, 1)) ^ ord(substr($want, $_, 1)) for 0 .. length($want) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return $diff == 0 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||||||||||||||||