Added new hash-modes Mozilla key3.db and key4.db

This commit is contained in:
Jens Steube
2021-04-17 21:24:27 +02:00
parent b9604b6f22
commit ee7d8ef0e7
10 changed files with 3510 additions and 0 deletions

View File

@@ -0,0 +1,148 @@
#!/usr/bin/env perl
##
## Author......: See docs/credits.txt
## License.....: MIT
##
use strict;
use warnings;
use Crypt::DES;
use Crypt::DES_EDE3;
use Digest::SHA qw (sha1);
use Digest::HMAC qw (hmac);
sub module_constraints { [[0, 256], [40, 40], [-1, -1], [-1, -1], [-1, -1]] }
sub module_generate_hash
{
my $word = shift;
my $global_salt = shift;
my $entry_salt = shift // random_hex_string (40);
my $ct = shift;
my $global_salt_bin = pack ("H*", $global_salt);
my $entry_salt_bin = pack ("H*", $entry_salt);
my $hp = sha1 ($global_salt_bin . $word);
my $pes = substr ($entry_salt_bin . ("\x00" x 20), 0, 20);
my $chp = sha1 ($hp . $entry_salt_bin);
my $k1 = hmac ($pes . $entry_salt_bin, $chp, \&sha1, 64);
my $tk = hmac ($pes, $chp, \&sha1, 64);
my $k2 = hmac ($tk . $entry_salt_bin, $chp, \&sha1, 64);
my $k = $k1 . $k2;
my $key = substr ($k, 0, 24);
my $iv = substr ($k, 32, 8);
my $pt;
if (defined $ct)
{
my $ct_bin = pack ("H*", $ct);
my $ede3 = Crypt::DES_EDE3->new ($key);
my $ct1_bin = substr ($ct_bin, 0, 8);
my $ct2_bin = substr ($ct_bin, 8, 8);
my $pt1 = $ede3->decrypt ($ct1_bin);
$pt1 = exclusive_or ($pt1, $iv);
print unpack ("H*", $ct1_bin), "\n";
print unpack ("H*", $ct2_bin), "\n";
my $pt2 = $ede3->decrypt ($ct2_bin);
print unpack ("H*", $pt2), "\n";
$pt2 = exclusive_or ($pt2, $ct1_bin);
print unpack ("H*", $pt2), "\n";
$pt = $pt1 . $pt2;
if ($pt ne "password-check\x02\x02")
{
$pt = "\xff" x 16;
}
}
else
{
$pt = "password-check\x02\x02";
}
my $ede3 = Crypt::DES_EDE3->new ($key);
my $pt1 = substr ($pt, 0, 8);
my $pt2 = substr ($pt, 8, 8);
$pt1 = exclusive_or ($pt1, $iv);
my $ct1_bin = $ede3->encrypt ($pt1);
$pt2 = exclusive_or ($pt2, $ct1_bin);
my $ct2_bin = $ede3->encrypt ($pt2);
my $ct_bin = $ct1_bin . $ct2_bin;
my $hash = sprintf ('$mozilla$*3DES*%s*%s*%s', unpack ("H*", $global_salt_bin), unpack ("H*", $entry_salt_bin), unpack ("H*", $ct_bin));
return $hash;
}
sub module_verify_hash
{
my $line = shift;
my $idx = index ($line, ':');
return unless $idx >= 0;
my $hash = substr ($line, 0, $idx);
my $word = substr ($line, $idx + 1);
return unless substr ($hash, 0, 9) eq '$mozilla$';
my ($signature, $type, $global_salt, $entry_salt, $ct) = split '\*', $hash;
return unless defined $signature;
return unless defined $type;
return unless defined $global_salt;
return unless defined $entry_salt;
return unless defined $ct;
return unless $type eq '3DES';
my $word_packed = pack_if_HEX_notation ($word);
my $new_hash = module_generate_hash ($word_packed, $global_salt, $entry_salt, $ct);
return ($new_hash, $word);
}
sub exclusive_or
{
my $in1 = shift;
my $in2 = shift;
my $out = "";
for (my $i = 0; $i < length ($in1); $i++) # $i < $len
{
$out .= chr (ord (substr ($in1, $i, 1)) ^ ord (substr ($in2, $i, 1)));
}
return $out;
}
1;

View File

@@ -0,0 +1,120 @@
#!/usr/bin/env perl
##
## Author......: See docs/credits.txt
## License.....: MIT
##
use strict;
use warnings;
use Crypt::PBKDF2;
use Crypt::CBC;
use Digest::SHA qw (sha1);
sub module_constraints { [[0, 256], [40, 40], [-1, -1], [-1, -1], [-1, -1]] }
sub module_generate_hash
{
my $word = shift;
my $global_salt = shift;
my $entry_salt = shift // random_hex_string (64);
my $iter = shift // 10000;
my $iv = shift // random_hex_string (32);
my $ct = shift;
my $kdf = Crypt::PBKDF2->new
(
hasher => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA2', 256),
iterations => $iter,
output_len => 32
);
my $global_salt_bin = pack ("H*", $global_salt);
my $global_key = sha1 ($global_salt_bin . $word);
my $entry_salt_bin = pack ("H*", $entry_salt);
my $entry_key = $kdf->PBKDF2 ($entry_salt_bin, $global_key);
my $iv_bin = pack ("H*", $iv);
my $pt;
if (defined $ct)
{
my $aes_cbc = Crypt::CBC->new ({
cipher => "Crypt::Rijndael",
iv => $iv_bin,
key => $entry_key,
keysize => 32,
literal_key => 1,
header => "none",
padding => "none"
});
my $ct_bin = pack ("H*", $ct);
$pt = $aes_cbc->decrypt ($ct_bin);
if ($pt ne "password-check\x02\x02")
{
$pt = "\xff" x 16;
}
}
else
{
$pt = "password-check\x02\x02";
}
my $aes_cbc = Crypt::CBC->new ({
cipher => "Crypt::Rijndael",
iv => $iv_bin,
key => $entry_key,
keysize => 32,
literal_key => 1,
header => "none",
padding => "none"
});
my $ct_bin = $aes_cbc->encrypt ($pt);
my $hash = sprintf ('$mozilla$*AES*%s*%s*%d*%s*%s', unpack ("H*", $global_salt_bin), unpack ("H*", $entry_salt_bin), $iter, unpack ("H*", $iv_bin), unpack ("H*", $ct_bin));
return $hash;
}
sub module_verify_hash
{
my $line = shift;
my $idx = index ($line, ':');
return unless $idx >= 0;
my $hash = substr ($line, 0, $idx);
my $word = substr ($line, $idx + 1);
return unless substr ($hash, 0, 9) eq '$mozilla$';
my ($signature, $type, $global_salt, $entry_salt, $iter, $iv, $ct) = split '\*', $hash;
return unless defined $signature;
return unless defined $type;
return unless defined $global_salt;
return unless defined $entry_salt;
return unless defined $iter;
return unless defined $iv;
return unless defined $ct;
return unless $type eq 'AES';
my $word_packed = pack_if_HEX_notation ($word);
my $new_hash = module_generate_hash ($word_packed, $global_salt, $entry_salt, $iter, $iv, $ct);
return ($new_hash, $word);
}
1;