From 95b8c98980863e542db77230d5234e13f338444d Mon Sep 17 00:00:00 2001 From: mhasbini Date: Mon, 18 Feb 2019 20:49:57 +0200 Subject: [PATCH] Add unit tests for multiple modules 11700, 11750, 11760, 11800, 11850, 11860, 12001, 12100, 12200, 12300, 12600 & 12700 --- tools/test_modules/m11700.pm | 51 +++++++++++++++++++++ tools/test_modules/m11750.pm | 54 ++++++++++++++++++++++ tools/test_modules/m11760.pm | 54 ++++++++++++++++++++++ tools/test_modules/m11800.pm | 48 ++++++++++++++++++++ tools/test_modules/m11850.pm | 55 +++++++++++++++++++++++ tools/test_modules/m11860.pm | 55 +++++++++++++++++++++++ tools/test_modules/m12001.pm | 65 +++++++++++++++++++++++++++ tools/test_modules/m12100.pm | 67 +++++++++++++++++++++++++++ tools/test_modules/m12200.pm | 87 ++++++++++++++++++++++++++++++++++++ tools/test_modules/m12300.pm | 61 +++++++++++++++++++++++++ tools/test_modules/m12600.pm | 46 +++++++++++++++++++ tools/test_modules/m12700.pm | 85 +++++++++++++++++++++++++++++++++++ 12 files changed, 728 insertions(+) create mode 100644 tools/test_modules/m11700.pm create mode 100644 tools/test_modules/m11750.pm create mode 100644 tools/test_modules/m11760.pm create mode 100644 tools/test_modules/m11800.pm create mode 100644 tools/test_modules/m11850.pm create mode 100644 tools/test_modules/m11860.pm create mode 100644 tools/test_modules/m12001.pm create mode 100644 tools/test_modules/m12100.pm create mode 100644 tools/test_modules/m12200.pm create mode 100644 tools/test_modules/m12300.pm create mode 100644 tools/test_modules/m12600.pm create mode 100644 tools/test_modules/m12700.pm diff --git a/tools/test_modules/m11700.pm b/tools/test_modules/m11700.pm new file mode 100644 index 000000000..8ea510b9c --- /dev/null +++ b/tools/test_modules/m11700.pm @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +sub module_constraints { [[0, 255], [0, 255], [0, 55], [0, 55], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + + # PyGOST outputs digests in little-endian order, while the kernels + # expect them in big-endian; hence the digest[::-1] mirroring. + # Using sys.stdout.write instead of print to disable \n character. + my $python_code = <<"END_CODE"; + +import binascii +import sys +from pygost import gost34112012256 +digest = gost34112012256.new(b"$word").digest() +sys.stdout.write(binascii.hexlify(digest[::-1])) + +END_CODE + + my $hash = `python2 -c '$python_code'`; + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m11750.pm b/tools/test_modules/m11750.pm new file mode 100644 index 000000000..c6d7cc1bf --- /dev/null +++ b/tools/test_modules/m11750.pm @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +sub module_constraints { [[0, 255], [0, 255], [0, 55], [0, 55], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $python_code = <<"END_CODE"; + +import binascii +import hmac +import sys +from pygost import gost34112012256 +key = b"$word" +msg = b"$salt" +digest = hmac.new(key, msg, gost34112012256).digest() +sys.stdout.write(binascii.hexlify(digest[::-1])) + +END_CODE + + my $digest = `python2 -c '$python_code'`; + + my $hash = sprintf ("%s:%s", $digest, $salt); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $salt, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $salt; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m11760.pm b/tools/test_modules/m11760.pm new file mode 100644 index 000000000..9cad60370 --- /dev/null +++ b/tools/test_modules/m11760.pm @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +sub module_constraints { [[0, 255], [0, 55], [-1, -1], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $python_code = <<"END_CODE"; + +import binascii +import hmac +import sys +from pygost import gost34112012256 +key = b"$salt" +msg = b"$word" +digest = hmac.new(key, msg, gost34112012256).digest() +sys.stdout.write(binascii.hexlify(digest[::-1])) + +END_CODE + + my $digest = `python2 -c '$python_code'`; + + my $hash = sprintf ("%s:%s", $digest, $salt); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $salt, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $salt; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m11800.pm b/tools/test_modules/m11800.pm new file mode 100644 index 000000000..6bef8f4a2 --- /dev/null +++ b/tools/test_modules/m11800.pm @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +sub module_constraints { [[0, 255], [-1, -1], [0, 55], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + + my $python_code = <<"END_CODE"; + +import binascii +import sys +from pygost import gost34112012512 +digest = gost34112012512.new(b"$word").digest() +sys.stdout.write(binascii.hexlify(digest[::-1])) + +END_CODE + + my $hash = `python2 -c '$python_code'`; + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m11850.pm b/tools/test_modules/m11850.pm new file mode 100644 index 000000000..717fc7106 --- /dev/null +++ b/tools/test_modules/m11850.pm @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +sub module_constraints { [[0, 55], [0, 55], [-1, -1], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + + my $python_code = <<"END_CODE"; + +import binascii +import hmac +import sys +from pygost import gost34112012512 +key = b"$word" +msg = b"$salt" +digest = hmac.new(key, msg, gost34112012512).digest() +sys.stdout.write(binascii.hexlify(digest[::-1])) + +END_CODE + + my $digest = `python2 -c '$python_code'`; + + my $hash = sprintf ("%s:%s", $digest, $salt); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $salt, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $salt; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m11860.pm b/tools/test_modules/m11860.pm new file mode 100644 index 000000000..422497495 --- /dev/null +++ b/tools/test_modules/m11860.pm @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +sub module_constraints { [[0, 255], [0, 55], [-1, -1], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + + my $python_code = <<"END_CODE"; + +import binascii +import hmac +import sys +from pygost import gost34112012512 +key = b"$salt" +msg = b"$word" +digest = hmac.new(key, msg, gost34112012512).digest() +sys.stdout.write(binascii.hexlify(digest[::-1])) + +END_CODE + + my $digest = `python2 -c '$python_code'`; + + my $hash = sprintf ("%s:%s", $digest, $salt); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $salt, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $salt; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m12001.pm b/tools/test_modules/m12001.pm new file mode 100644 index 000000000..9e4a915e7 --- /dev/null +++ b/tools/test_modules/m12001.pm @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use MIME::Base64 qw (encode_base64 decode_base64); +use Crypt::PBKDF2; + +sub module_constraints { [[0, 255], [16, 16], [0, 55], [16, 16], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + + my $pbkdf2 = Crypt::PBKDF2->new + ( + hasher => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA1'), + iterations => 10000, + output_len => 32 + ); + + my $base64 = encode_base64 ($salt . $pbkdf2->PBKDF2 ($salt, $word), ""); + + my $hash = sprintf ("{PKCS5S2}%s", $base64); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($digest, $word) = split ":", $line; + + return unless defined $digest; + return unless defined $word; + + my $signature = substr ($digest, 0, 9); + + return unless ($signature eq '{PKCS5S2}'); + + my $hash = substr ($digest, 9); + + # base64 buf + + my $base64_decoded = decode_base64 ($hash); + + return if (length ($base64_decoded) != (16 + 32)); + + my $salt = substr ($base64_decoded, 0, 16); + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m12100.pm b/tools/test_modules/m12100.pm new file mode 100644 index 000000000..c90f2abcf --- /dev/null +++ b/tools/test_modules/m12100.pm @@ -0,0 +1,67 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use MIME::Base64 qw (encode_base64 decode_base64); +use Crypt::PBKDF2; + +sub module_constraints { [[0, 255], [1, 15], [0, 55], [1, 15], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $iterations = shift // 1000; + my $out_len = shift // 16; + + my $pbkdf2 = Crypt::PBKDF2->new + ( + hasher => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA2', 512), + iterations => $iterations, + output_len => $out_len + ); + + my $digest = encode_base64 ($pbkdf2->PBKDF2 ($salt, $word), ""); + + my $base64_salt = encode_base64 ($salt, ""); + + my $hash = sprintf ("sha512:%i:%s:%s", $iterations, $base64_salt, $digest); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($digest, $word) = split (/:([^:]+)$/, $line); + + return unless defined $digest; + return unless defined $word; + + my ($signature, $iterations, $salt_encoded, $hash_encoded) = split (':', $digest); + + return unless ($signature eq 'sha512'); + return unless defined $iterations; + return unless defined $salt_encoded; + return unless defined $hash_encoded; + + my $hash = decode_base64 ($hash_encoded); + my $salt = decode_base64 ($salt_encoded); + + my $out_len = length ($hash); + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt, $iterations, $out_len); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m12200.pm b/tools/test_modules/m12200.pm new file mode 100644 index 000000000..741015a11 --- /dev/null +++ b/tools/test_modules/m12200.pm @@ -0,0 +1,87 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use Digest::SHA qw (sha512); + +sub module_constraints { [[0, 255], [16, 16], [-1, -1], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $default_salt = shift // 0; + + my $iterations = 65536; + + if ($default_salt == 1) + { + $salt = "0011223344556677"; + } + + my $digest = sha512 (pack ("H*", $salt) . $word); + + for (my $i = 0; $i < $iterations; $i++) + { + $digest = sha512 ($digest); + } + + $digest = unpack ("H*", $digest); + $digest = substr ($digest, 0, 16); + + my $hash; + + if ($default_salt == 0) + { + $hash = sprintf ("\$ecryptfs\$0\$1\$%s\$%s", $salt, $digest); + } + else + { + $hash = sprintf ("\$ecryptfs\$0\$%s", $digest); + } + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash_in, $word) = split (/:/, $line); + + return unless defined $hash_in; + return unless defined $word; + + my $signature = substr ($hash_in, 0, 12); + + return unless ($signature eq '$ecryptfs$0$'); + + my $digest = substr ($hash_in, 12); + + my $default_salt = 1; + + my ($param, $hash) = split('\$', $digest); + + $default_salt = 0 if ($param eq '1'); + + my $salt; + + if ($default_salt == 0) + { + ($salt, $hash) = split('\$', $hash); + } + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt, $default_salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m12300.pm b/tools/test_modules/m12300.pm new file mode 100644 index 000000000..0cd79ad08 --- /dev/null +++ b/tools/test_modules/m12300.pm @@ -0,0 +1,61 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use Crypt::PBKDF2; +use Digest::SHA qw (sha512_hex); + +sub module_constraints { [[0, 255], [32, 32], [-1, -1], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + + my $iterations = 4096; + + my $hasher = Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA2', 512); + + my $pbkdf2 = Crypt::PBKDF2->new ( + hasher => $hasher, + iterations => $iterations, + output_len => 64 + ); + + my $salt_bin = pack ("H*", $salt); + + my $key = $pbkdf2->PBKDF2 ($salt_bin. "AUTH_PBKDF2_SPEEDY_KEY", $word); + + my $digest = sha512_hex ($key . $salt_bin); + + my $hash = sprintf ("%s%s", uc ($digest), uc ($salt)); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($digest, $word) = split (/:/, $line); + + return unless defined $digest; + return unless length ($digest) == 160; + return unless defined $word; + + my $salt = substr ($digest, 128, 32); + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m12600.pm b/tools/test_modules/m12600.pm new file mode 100644 index 000000000..5ee77b6e9 --- /dev/null +++ b/tools/test_modules/m12600.pm @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use Digest::SHA qw (sha1_hex sha256_hex); + +sub module_constraints { [[0, 255], [64, 64], [0, 55], [64, 64], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + + my $digest = sha1_hex ($word); + + $digest = sha256_hex ($salt . uc $digest); + + my $hash = sprintf ("%s:%s", $digest, $salt); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $salt, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $salt; + return unless defined $word; + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt); + + return ($new_hash, $word); +} + +1; diff --git a/tools/test_modules/m12700.pm b/tools/test_modules/m12700.pm new file mode 100644 index 000000000..47e0602b6 --- /dev/null +++ b/tools/test_modules/m12700.pm @@ -0,0 +1,85 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use Crypt::PBKDF2; +use Crypt::CBC; + +sub module_constraints { [[0, 255], [32, 32], [-1, -1], [-1, -1], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $encrypted = shift; + + my $iterations = 10; + + my $salt_bin = pack ("H*", $salt); + + my $hasher = Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA1'); + + my $pbkdf2 = Crypt::PBKDF2->new ( + hasher => $hasher, + iterations => $iterations, + output_len => 32 + ); + + my $key = $pbkdf2->PBKDF2 ($salt_bin, $word); + + my $cipher = Crypt::CBC->new ({ + key => $key, + cipher => "Crypt::Rijndael", + iv => $salt_bin, + literal_key => 1, + header => "none", + keysize => 32 + }); + + my $data = qq|{ +"guid" : "00000000-0000-0000-0000-000000000000", +"sharedKey" : "00000000-0000-0000-0000-000000000000", +"options" : {"pbkdf2_iterations":10,"fee_policy":0,"html5_notifications":false,"logout_time":600000,"tx_display":0,"always_keep_local_backup":false}|; + + unless (defined $encrypted) + { + $encrypted = unpack ("H*", $cipher->encrypt ($data)); + } + + my $hash = sprintf ("\$blockchain\$%s\$%s", length ($salt . $encrypted) / 2, $salt . $encrypted); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + my ($hash, $word) = split (':', $line); + + return unless defined $hash; + return unless defined $word; + + my (undef, $signature, $data_len, $data) = split '\$', $hash; + + return unless ($signature eq "blockchain"); + return unless (($data_len * 2) == length $data); + + my $salt = substr ($data, 0, 32); + + my $data_encrypted = substr ($data, 32); + + my $word_packed = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word_packed, $salt, $data_encrypted); + + return ($new_hash, $word); +} + +1;