Annotation of palm/Palm-Keyring/keyring2 with v5 databases/pbkdf2.pl, Revision 1.1
1.1 ! andrew 1: use Digest::HMAC_SHA1 qw(hmac_sha1);
! 2: use Digest::SHA1 qw(sha1);
! 3:
! 4: # Usage pbkdf2(password, salt, iter, keylen, prf)
! 5: # iter is number of iterations
! 6: # keylen is length of generated key in bytes
! 7: # prf is the pseudo random function (e.g. hmac_sha1)
! 8: # returns the key.
! 9: sub pbkdf2($$$$$)
! 10: {
! 11: my ($password, $salt, $iter, $keylen, $prf) = @_;
! 12: my $k, $t, $u, $ui, $i;
! 13: $t = "";
! 14: for ($k = 1; length($t) < $keylen; $k++) {
! 15: $u = $ui = &$prf($salt.pack('N', $k), $password);
! 16: for ($i = 1; $i < $iter; $i++) {
! 17: $ui = &$prf($ui, $password);
! 18: $u ^= $ui;
! 19: }
! 20: $t .= $u;
! 21: }
! 22: return substr($t, 0, $keylen);
! 23: }
! 24:
! 25: sub DES_odd_parity($) {
! 26: my $key = $_[0];
! 27: my $r, $i;
! 28: my @odd_parity = (
! 29: 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
! 30: 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
! 31: 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
! 32: 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
! 33: 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
! 34: 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
! 35: 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
! 36: 112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
! 37: 128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
! 38: 145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
! 39: 161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
! 40: 176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
! 41: 193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
! 42: 208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
! 43: 224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
! 44: 241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254);
! 45: for ($i = 0; $i< length($key); $i++) {
! 46: $r .= chr($odd_parity[ord(substr($key, $i, 1))]);
! 47: }
! 48: return $r;
! 49: }
! 50:
! 51:
! 52: # Test (iter = 250, cipher = 3 (AES256), password = "abc")
! 53: #
! 54: # Key: 2379f9ac841153ae53172889472f662332cef9306e32d86bbdb57391b7d71d90
! 55: # Hash: c7e5af2ae1839e62
! 56: #
! 57: $salt = pack("H*", "EDCA7AFF86136532");
! 58: $key = pbkdf2("abc", $salt, 250, 32, \&hmac_sha1);
! 59: print "Key: ". unpack("H*", $key)."\n";
! 60: print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";
! 61:
! 62: # Test (iter = 500, cipher = 0 (None))
! 63: # Key: 313144cb8ac50852
! 64: # Hash: 68d7b4ea640c471e
! 65: $salt = pack("H*", "D7765039E75C83A2");
! 66: $key = pbkdf2("abc", $salt, 500, 8, \&hmac_sha1);
! 67: print "Key: ". unpack("H*", $key)."\n";
! 68: print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";
! 69:
! 70: # Test (iter = 1000, cipher = 1 (3DES))
! 71: # Key: ba67012668adf72a85fd340816ab6d265107043befb6c802
! 72: # Hash: 4bc102dd640c650e
! 73: $salt = pack("H*", "A1A365AB82175012");
! 74: $key = pbkdf2("abc", $salt, 1000, 24, \&hmac_sha1);
! 75: $key = DES_odd_parity($key);
! 76: print "Key: ". unpack("H*", $key)."\n";
! 77: print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";
! 78:
! 79: # Test (iter = 100, cipher = 2 (AES))
! 80: # Key: 55673aa0a1f799c1ca19994c127f371f
! 81: # Hash: 5d3c51c7d5625454
! 82: $salt = pack("H*", "82A674A790E3ABA1");
! 83: $key = pbkdf2("abc", $salt, 100, 16, \&hmac_sha1);
! 84: print "Key: ". unpack("H*", $key)."\n";
! 85: print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";
! 86:
! 87: # Test (iter = 1000, cipher = 3 (AES256))
! 88: # Key: 165784e3322e942b6d2f1bab4114b9f5cbf392a6aed26eb61b02c972ead5d2e5
! 89: # Hash: 87e5a115033afcb4
! 90: $salt = pack("H*", "C973472374948C96");
! 91: $key = pbkdf2("abc", $salt, 1000, 32, \&hmac_sha1);
! 92: print "Key: ". unpack("H*", $key)."\n";
! 93: print "Hash: ". unpack("H*", substr(sha1($key.$salt),0, 8))."\n";
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>