From 6a387bf704fb36bdae23b077d7f0d4b8c8e25c7d Mon Sep 17 00:00:00 2001 From: jsteube Date: Fri, 15 Feb 2019 21:10:02 +0100 Subject: [PATCH] Add -m 7200 unit test --- tools/test_modules/m07100.pm | 36 +++++++++++-------- tools/test_modules/m07200.pm | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 tools/test_modules/m07200.pm diff --git a/tools/test_modules/m07100.pm b/tools/test_modules/m07100.pm index f5094ca7e..2d3712cea 100644 --- a/tools/test_modules/m07100.pm +++ b/tools/test_modules/m07100.pm @@ -10,14 +10,18 @@ use warnings; use Crypt::PBKDF2; -sub module_constraints { [[0, 255], [-1, -1], [0, 55], [-1, -1], [-1, -1]] } +sub module_constraints { [[0, 255], [-1, -1], [0, 255], [-1, -1], [-1, -1]] } sub module_generate_hash { - my $word = shift; - my $unused = shift; - my $salt = shift // random_hex_string (64); - my $iter = shift // 1024; + my $word = shift; + my $salt = shift; + my $iter = shift // 1024; + + if (length $salt == 0) + { + $salt = random_hex_string (64, 64); + } my $pbkdf2 = Crypt::PBKDF2->new ( @@ -27,9 +31,9 @@ sub module_generate_hash my $hash_buf = unpack ("H*", $pbkdf2->PBKDF2 (pack ("H*", $salt), $word)); - my $tmp_hash = sprintf ("\$ml\$%i\$%s\$%0128s", $iter, $salt, $hash_buf); + my $hash = sprintf ("\$ml\$%i\$%s\$%0128s", $iter, $salt, $hash_buf); - return $tmp_hash; + return $hash; } sub module_verify_hash @@ -40,24 +44,28 @@ sub module_verify_hash return if $index1 < 1; - my $hash = substr ($line, 0, $index1); + my $hash_in = substr ($line, 0, $index1); + my $word = substr ($line, $index1 + 1); - my $index2 = index ($hash, "\$", 5); + my $index2 = index ($hash_in, "\$", 5); return if $index2 < 1; - my $index3 = index ($hash, "\$", $index2 + 1); + my $index3 = index ($hash_in, "\$", $index2 + 1); - my $salt = substr ($hash, $index2 + 1, $index3 - $index2 - 1); + my $salt = substr ($hash_in, $index2 + 1, $index3 - $index2 - 1); - my $iter = substr ($hash, 4, $index2 - 4); + my $iter = substr ($hash_in, 4, $index2 - 4); return if (int ($iter) < 1); - my $word_packed = pack_if_HEX_notation ($word); + return unless defined $salt; + return unless defined $word; - my $new_hash = module_generate_hash ($word_packed, undef, $salt, $iter); + $word = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word, $salt, $iter); return ($new_hash, $word); } diff --git a/tools/test_modules/m07200.pm b/tools/test_modules/m07200.pm new file mode 100644 index 000000000..0d8eae575 --- /dev/null +++ b/tools/test_modules/m07200.pm @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use Crypt::PBKDF2; + +sub module_constraints { [[0, 255], [-1, -1], [0, 255], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $iter = shift // 1024; + + if (length $salt == 0) + { + $salt = random_hex_string (128, 128); + } + + my $pbkdf2 = Crypt::PBKDF2->new ( + hasher => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA2', 512), + iterations => $iter + ); + + my $hash_buf = unpack ("H*", $pbkdf2->PBKDF2 (pack ("H*", $salt), $word)); + + my $hash = sprintf ("grub.pbkdf2.sha512.%i.%s.%0128s", $iter, $salt, $hash_buf); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my $index1 = index ($line, ":"); + + return if $index1 < 1; + + my $hash_in = substr ($line, 0, $index1); + + my $word = substr ($line, $index1 + 1); + + my $index2 = index ($hash_in, ".", 19); + + return if $index2 < 1; + + my $index3 = index ($hash_in, ".", $index2 + 1); + + my $salt = substr ($hash_in, $index2 + 1, $index3 - $index2 - 1); + + my $iter = substr ($hash_in, 19, $index2 - 19); + + return unless defined $salt; + return unless defined $word; + + $word = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word, $salt); + + return ($new_hash, $word); +} + +1;