[BACK]Return to pbkdf2.pl CVS log [TXT][DIR] Up to [local] / palm / Palm-Keyring / keyring2 with v5 databases

File: [local] / palm / Palm-Keyring / keyring2 with v5 databases / pbkdf2.pl (download)

Revision 1.2, Sun Feb 18 05:32:58 2007 UTC (17 years, 3 months ago) by andrew
Branch: MAIN
CVS Tags: PALM_KEYRING_0_96_06, HEAD
Changes since 1.1: +2 -0 lines

add the shebang line.

#!/usr/bin/perl

use Digest::HMAC_SHA1 qw(hmac_sha1);
use Digest::SHA1 qw(sha1);

# Usage pbkdf2(password, salt, iter, keylen, prf)
# iter is number of iterations
# keylen is length of generated key in bytes
# prf is the pseudo random function (e.g. hmac_sha1)
# returns the key.
sub pbkdf2($$$$$)
{
    my ($password, $salt, $iter, $keylen, $prf) = @_;
    my $k, $t, $u, $ui, $i;
    $t = "";
    for ($k = 1; length($t) <  $keylen; $k++) {
	$u = $ui = &$prf($salt.pack('N', $k), $password);
	for ($i = 1; $i < $iter; $i++) {
	    $ui = &$prf($ui, $password);
	    $u ^= $ui;
	}
	$t .= $u;
    }
    return substr($t, 0, $keylen);
}

sub DES_odd_parity($) {
    my $key = $_[0];
    my $r, $i;
    my @odd_parity = (
  1,  1,  2,  2,  4,  4,  7,  7,  8,  8, 11, 11, 13, 13, 14, 14,
 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254);
    for ($i = 0; $i< length($key); $i++) {
	$r .= chr($odd_parity[ord(substr($key, $i, 1))]);
    }
    return $r;
}


# Test (iter = 250, cipher = 3 (AES256), password = "abc")
#
# Key:  2379f9ac841153ae53172889472f662332cef9306e32d86bbdb57391b7d71d90
# Hash: c7e5af2ae1839e62
#
$salt = pack("H*", "EDCA7AFF86136532");
$key = pbkdf2("abc", $salt, 250, 32, \&hmac_sha1);
print "Key:  ". unpack("H*", $key)."\n";
print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";

# Test (iter = 500, cipher = 0 (None))
# Key:  313144cb8ac50852
# Hash: 68d7b4ea640c471e
$salt = pack("H*", "D7765039E75C83A2");
$key = pbkdf2("abc", $salt, 500, 8, \&hmac_sha1);
print "Key:  ". unpack("H*", $key)."\n";
print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";

# Test (iter = 1000, cipher = 1 (3DES))
# Key:  ba67012668adf72a85fd340816ab6d265107043befb6c802
# Hash: 4bc102dd640c650e
$salt = pack("H*", "A1A365AB82175012");
$key = pbkdf2("abc", $salt, 1000, 24, \&hmac_sha1);
$key = DES_odd_parity($key);
print "Key:  ". unpack("H*", $key)."\n";
print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";

# Test (iter = 100, cipher = 2 (AES))
# Key:  55673aa0a1f799c1ca19994c127f371f
# Hash: 5d3c51c7d5625454
$salt = pack("H*", "82A674A790E3ABA1");
$key = pbkdf2("abc", $salt, 100, 16, \&hmac_sha1);
print "Key:  ". unpack("H*", $key)."\n";
print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";

# Test (iter = 1000, cipher = 3 (AES256))
# Key:  165784e3322e942b6d2f1bab4114b9f5cbf392a6aed26eb61b02c972ead5d2e5
# Hash: 87e5a115033afcb4
$salt = pack("H*", "C973472374948C96");
$key = pbkdf2("abc", $salt, 1000, 32, \&hmac_sha1);
print "Key:  ". unpack("H*", $key)."\n";
print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";