Okay, so I feel like I am getting a bit closer. I got the Python program from the link (http-stackoverflow.com-questions-12018920) to work, even though I don't know python, and watched exactly what it was doing.
import hmac
import hashlib
from hashlib import sha1
import binascii
import sys
A = "Pairwise key expansion"
APmac = binascii.a2b_hex("001dd0f694b0")
Clientmac = binascii.a2b_hex("489d2477179a")
ANonce = binascii.a2b_hex("87f2718bad169e4987c94255395e054bcaf77c
+8d791698bf03dc85ed3c90832a")
SNonce = binascii.a2b_hex("143fbb4333341f36e17667f88aa02c5230ab82
+c508cc4bd5947dd7e50475ad36")
B = min(APmac,Clientmac)+max(APmac,Clientmac)+min(ANonce,SNo
+nce)+max(ANonce,SNonce)
def customPRF512(key,A,B):
blen = 64
i = 0
R = ''
while i<=((blen*8+159)/160):
hmacsha1 = hmac.new(key,A+chr(0x00)+B+chr(i),sha1)
i+=1
R = R+hmacsha1.digest()
print "R: ",binascii.b2a_hex(hmacsha1.digest()),"\n"
return R[:blen]
pmk = binascii.a2b_hex("9051ba43660caec7a909fbbe6b91e4685f1457b5a2e236
+60d728afbd2c7abfba")
ptk = customPRF512(pmk,A,B)
print "pmk:\t\t",binascii.b2a_hex(pmk),"\n"
print "ptk:\t\t",binascii.b2a_hex(ptk[0:16]),"\n"
print "A: ",binascii.b2a_hex(A),"\n"
print "CHR(0x00): ",chr(0x00),"\n"
print "B: ",binascii.b2a_hex(B),"\n"
print "key: ",binascii.b2a_hex(pmk),"\n"
print "CHR(0): ",chr(0),"\n"
i = 0
string = A+chr(0x00)+B+chr(i)
print "STRING: ",binascii.b2a_hex(string);
It produced the correct PTK from my PMK using the hmac_sha1 class. You can see it from "directly from python loop" in the code.
#!/usr/bin/perl -w
use strict;
use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
my $pmk = pack("H*","9051ba43660caec7a909fbbe6b91e4685f1457b5a2e23660d
+728afbd2c7abfba");
my $a;
foreach(split("","Pairwise key expansion")){
$a .= sprintf("%x",ord($_));
} # 5061697277697365206b657920657870616e73696f6e OK
my $i = 0x00; # 00 in hex
my $smac = pack("H*","489d2477179a");
my $amac = pack("H*","001dd0f694b0");
my $snonce = pack("H*","143fbb4333341f36e17667f88aa02c5230ab82c508cc4b
+d5947dd7e50475ad36");
my $anonce = pack("H*","87f2718bad169e4987c94255395e054bcaf77c8d791698
+bf03dc85ed3c90832a");
my $b = $amac.$smac.$snonce.$anonce;
# Directly from Python code in for loop (without spaces):
# 5061697277697365206b657920657870616e73696f6e 00 001dd0f694b0
+ 489d2477179a1
# 43fbb4333341f36e17667f88aa02c5230ab82c508cc4bd5947dd7e50475a
+d36
# 87f2718bad169e4987c94255395e054bcaf77c8d791698bf03dc85ed3c90
+832a 00
my $hd = $a.$i.$b.$i;
my $digest = hmac_sha1($hd,$pmk);
print unpack("H*",$digest)."\n"; # according to docs: $digest = hmac_s
+ha1($data, $key);
# does not come out as 9287f887faade9257f5a806309a2bac8956fcbec like
+hmac_sha1 from Python ?
I am almost sure that the pack() fucntion in Perl is similar to his a2b_hex() method as I have altered his Python code to print it for each iteration. As you can see the "Pairwise key expansion" is turned into hex via each character's ascii code, which I did with sprintf and ord. That comes out to:
5061697277697365206b657920657870616e73696f6e
which is correct. Is the pack() function argument wrong? is the hmacsha1 not the same as in Python?
I am so lost right now. :(
Thanks monks! |