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