#!/usr/bin/python ''' author: tlviewer@yahoo.com keywords: convert array hash digest hmac crypt win32 3des description: Microsoft 3DES/AES key derivation for (md5, sha) ''' import sys, os import string as st import array as ay import md5, sha #import __builtin__ as bi # if you want to correct the key for parity before using # the prf, then use the lookup table below ParConv = ["01", "01", "02", "02", "04", "04", "07", "07", "08", "08", "0b", "0b", "0d", "0d", "0e", "0e", "10", "10", "13", "13", "15", "15", "16", "16", "19", "19", "1a", "1a", "1c", "1c", "1f", "1f", "20", "20", "23", "23", "25", "25", "26", "26", "29", "29", "2a", "2a", "2c", "2c", "2f", "2f", "31", "31", "32", "32", "34", "34", "37", "37", "38", "38", "3b", "3b", "3d", "3d", "3e", "3e", "40", "40", "43", "43", "45", "45", "46", "46", "49", "49", "4a", "4a", "4c", "4c", "4f", "4f", "51", "51", "52", "52", "54", "54", "57", "57", "58", "58", "5b", "5b", "5d", "5d", "5e", "5e", "61", "61", "62", "62", "64", "64", "67", "67", "68", "68", "6b", "6b", "6d", "6d", "6e", "6e", "70", "70", "73", "73", "75", "75", "76", "76", "79", "79", "7a", "7a", "7c", "7c", "7f", "7f", "80", "80", "83", "83", "85", "85", "86", "86", "89", "89", "8a", "8a", "8c", "8c", "8f", "8f", "91", "91", "92", "92", "94", "94", "97", "97", "98", "98", "9b", "9b", "9d", "9d", "9e", "9e", "a1", "a1", "a2", "a2", "a4", "a4", "a7", "a7", "a8", "a8", "ab", "ab", "ad", "ad", "ae", "ae", "b0", "b0", "b3", "b3", "b5", "b5", "b6", "b6", "b9", "b9", "ba", "ba", "bc", "bc", "bf", "bf", "c1", "c1", "c2", "c2", "c4", "c4", "c7", "c7", "c8", "c8", "cb", "cb", "cd", "cd", "ce", "ce", "d0", "d0", "d3", "d3", "d5", "d5", "d6", "d6", "d9", "d9", "da", "da", "dc", "dc", "df", "df", "e0", "e0", "e3", "e3", "e5", "e5", "e6", "e6", "e9", "e9", "ea", "ea", "ec", "ec", "ef", "ef", "f1", "f1", "f2", "f2", "f4", "f4", "f7", "f7", "f8", "f8", "fb", "fb", "fd", "fd", "fe", "fe"] def _hmac( hMask, sIn): ar1 = ay.array('B',64 * hMask) ar2 = ay.array('B', sIn) # ay.array('B',(64-len(sIn)) * [0x0]) for j in xrange( 0, len( sIn)): ar1[j] = ar1[j] ^ ar2[j] return ar1 def MsExtKey( digmod, key, klen): hsh = digmod.new(key).digest() inner = _hmac( [0x36], hsh) print type( inner) outer = _hmac( [0x5C], hsh) print len(outer) #print inner # sum the hash of the inner and outer oHash1 = digmod.new( inner.tostring() ) oHash2 = digmod.new( outer.tostring()) #print type( oHash1.digest() ) return ( oHash1.hexdigest() + oHash2.hexdigest() )[:2*klen] # synopsis -- 24 bytes works for the 192 bit key for 3DES print MsExtKey( md5, "1134-kelp", 24) # print ''.join([ '%02x' %x for x in range(1,25)]) print MsExtKey( md5, ay.array('B', range(1 , 25) ).tostring() , 24) # 63e6439d99747204676546e21ec5b7c55716481851ac235d # md5: # f6c070df3ec25797fb014e1f74ff3ae3ff285ff1af3ed57f # sha: # 5ab48b8def0bdca77f16f8c4f4781823e92ecc1c4d40f762 ''' def hconv( dec): return "%02x" % (dec) def utf8(str1): return unicode(str(str1), "iso-8859-1").encode("utf-8") if dec > 9: shex = st.join(ay.array( 'c', hex(dec))[2:4], '') else: return shex '''