2018-12-20 19:18:06 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
##
|
|
|
|
## Author......: See docs/credits.txt
|
|
|
|
## License.....: MIT
|
|
|
|
##
|
|
|
|
|
|
|
|
use strict;
|
2018-12-21 09:10:23 +01:00
|
|
|
use warnings;
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
use Crypt::GCrypt;
|
|
|
|
use Crypt::PBKDF2;
|
|
|
|
use Digest::SHA qw (sha1 sha1_hex);
|
|
|
|
|
2019-02-16 16:04:37 +01:00
|
|
|
sub module_constraints { [[0, 51], [32, 32], [-1, -1], [-1, -1], [-1, -1]] }
|
2018-12-23 18:15:53 +01:00
|
|
|
|
2018-12-20 19:18:06 +01:00
|
|
|
sub module_generate_hash
|
|
|
|
{
|
|
|
|
my $word = shift;
|
2018-12-23 18:15:53 +01:00
|
|
|
my $salt = shift;
|
2018-12-20 19:18:06 +01:00
|
|
|
my $iter = shift // 100000;
|
2018-12-29 16:23:29 +01:00
|
|
|
my $iv = shift // random_hex_string (2 * 8);
|
|
|
|
my $plain = shift // random_hex_string (2 * 1024);
|
2018-12-20 19:18:06 +01:00
|
|
|
|
2018-12-22 19:29:24 +01:00
|
|
|
my $b_iv = pack ('H*', $iv);
|
|
|
|
my $b_salt = pack ('H*', $salt);
|
|
|
|
my $b_plain = pack ('H*', $plain);
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
my $kdf = Crypt::PBKDF2->new
|
|
|
|
(
|
|
|
|
hash_class => 'HMACSHA1',
|
|
|
|
iterations => $iter,
|
|
|
|
output_len => 16
|
|
|
|
);
|
|
|
|
|
|
|
|
my $pass_hash = sha1 ($word);
|
|
|
|
my $key = $kdf->PBKDF2 ($b_salt, $pass_hash);
|
|
|
|
|
2018-12-29 16:23:29 +01:00
|
|
|
my $cfb = Crypt::GCrypt->new
|
|
|
|
(
|
2018-12-20 19:18:06 +01:00
|
|
|
type => 'cipher',
|
|
|
|
algorithm => 'blowfish',
|
|
|
|
mode => 'cfb'
|
|
|
|
);
|
|
|
|
|
|
|
|
$cfb->start ('encrypting');
|
|
|
|
$cfb->setkey ($key);
|
|
|
|
$cfb->setiv ($b_iv);
|
|
|
|
|
|
|
|
my $b_cipher = $cfb->encrypt ($b_plain);
|
|
|
|
|
|
|
|
$cfb->finish ();
|
|
|
|
|
2018-12-22 19:29:24 +01:00
|
|
|
my $cipher = unpack ('H*', $b_cipher);
|
2018-12-20 19:18:06 +01:00
|
|
|
my $checksum = sha1_hex ($b_plain);
|
|
|
|
|
2020-06-13 11:19:00 +02:00
|
|
|
my $hash = '$odf$' . "*0*0*$iter*16*$checksum*8*$iv*16*$salt*0*$cipher";
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub module_verify_hash
|
|
|
|
{
|
|
|
|
my $line = shift;
|
|
|
|
|
2018-12-21 12:16:11 +01:00
|
|
|
my ($hash, $word) = split (':', $line);
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
return unless defined $hash;
|
|
|
|
return unless defined $word;
|
|
|
|
|
2018-12-27 15:49:18 +01:00
|
|
|
my $word_packed = pack_if_HEX_notation ($word);
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
# tokenize
|
|
|
|
my @data = split ('\*', $hash);
|
|
|
|
|
2019-02-19 12:50:57 +01:00
|
|
|
return unless scalar @data == 12;
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
my $signature = shift @data;
|
|
|
|
my $cipher_type = shift @data;
|
|
|
|
my $cs_type = shift @data;
|
|
|
|
my $iter = shift @data;
|
|
|
|
my $cs_len = shift @data;
|
|
|
|
my $cs = shift @data;
|
|
|
|
my $iv_len = shift @data;
|
|
|
|
my $iv = shift @data;
|
|
|
|
my $salt_len = shift @data;
|
|
|
|
my $salt = shift @data;
|
|
|
|
my $unused = shift @data;
|
|
|
|
my $cipher = shift @data;
|
|
|
|
|
|
|
|
# validate
|
|
|
|
return unless $signature eq '$odf$';
|
|
|
|
return unless $cipher_type eq '0';
|
|
|
|
return unless $cs_type eq '0';
|
|
|
|
return unless $cs_len eq '16';
|
|
|
|
return unless $iv_len eq '8';
|
|
|
|
return unless $salt_len eq '16';
|
|
|
|
return unless $unused eq '0';
|
|
|
|
return unless defined $cipher;
|
|
|
|
|
|
|
|
# decrypt
|
2018-12-22 19:29:24 +01:00
|
|
|
my $b_iv = pack ('H*', $iv);
|
|
|
|
my $b_salt = pack ('H*', $salt);
|
|
|
|
my $b_cipher = pack ('H*', $cipher);
|
2018-12-20 19:18:06 +01:00
|
|
|
|
|
|
|
my $kdf = Crypt::PBKDF2->new
|
|
|
|
(
|
|
|
|
hash_class => 'HMACSHA1',
|
|
|
|
iterations => $iter,
|
|
|
|
output_len => 16
|
|
|
|
);
|
|
|
|
|
|
|
|
my $pass_hash = sha1 ($word);
|
|
|
|
my $key = $kdf->PBKDF2 ($b_salt, $pass_hash);
|
|
|
|
|
2020-06-13 11:19:00 +02:00
|
|
|
my $cfb = Crypt::GCrypt->new (
|
2018-12-20 19:18:06 +01:00
|
|
|
type => 'cipher',
|
|
|
|
algorithm => 'blowfish',
|
|
|
|
mode => 'cfb'
|
|
|
|
);
|
|
|
|
|
|
|
|
$cfb->start ('decrypting');
|
|
|
|
$cfb->setkey ($key);
|
|
|
|
$cfb->setiv ($b_iv);
|
|
|
|
|
|
|
|
my $b_plain = $cfb->decrypt ($b_cipher);
|
|
|
|
|
|
|
|
$cfb->finish ();
|
|
|
|
|
2018-12-22 19:29:24 +01:00
|
|
|
my $plain = unpack ('H*', $b_plain);
|
2018-12-20 19:18:06 +01:00
|
|
|
|
2018-12-27 19:43:51 +01:00
|
|
|
my $new_hash = module_generate_hash ($word_packed, $salt, $iter, $iv, $plain);
|
2018-12-27 15:49:18 +01:00
|
|
|
|
|
|
|
return ($new_hash, $word);
|
2018-12-20 19:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|