Calculate TLS PRF Master key using pre master key in Python

723 views Asked by At

I have the following information:

    "hashAlg": "SHA2-256",
    "tlsVersion": "v1.2",
    "keyBlockLength": 1024,
    "preMasterSecretLength": 384,
        "preMasterSecret": "2DE53DAC84478C9822D0536FF4ACB173B3FC46FD6BFC7E5292C2934CCFAE484EFA7B29415922157C49DD361B24EB7A3F",
        "clientHelloRandom": "15E63D4BBF368C77FA8A877C8FE66F19364C713A9DD158B8AC61E2EDCA8D8E10",
        "serverHelloRandom": "7B60A1AB6152930389B055573C6B114026E73382163C005090C532B86E5BCE99",
        "clientRandom": "AFE563DF246AD11D44BEA9D1397E0FA44478744A7C3F55AAE77EDD166C04BC72",
        "serverRandom": "46075972454EBFFDC9A927229CBD0E1B8F3F34160192849263107B2FC5A3C19F"
      },

Using the above, I need to calculate the master key and the keyblock. The correct answer should be:

        "masterSecret": "37E354C738B7157EB79F5F790C23D7B3D79FFD47D61C56E52696412176C3B43ADB736DC362447DBB8EC64258B9ACE55F",
        "keyBlock": "CB38007C89A03DD020A91684A3419084637356E76C9B9EFF57325816419A8312409DA4C358E731C10BDEB65B29C0DBB0916EF39C443E4E8437A2B84256232ADB1DA6547580EA510F134CE1812F06CDEBAFF6B9404213C1A4843EF35599549FAEF1014C4D0B8F9335D82F3EF5D85CBFB779D4D56D6539A31538946FCD5213ED66"

My Code is as follows:

def prf(secret,seed,numblocks):
    seed=seed
    output = b''
    a=hmac.new(secret,msg=seed,digestmod=hashlib.sha256).digest()
    for j in range(numblocks):
        output += hmac.new(secret,msg=a+seed,digestmod=hashlib.sha256).digest()
        a=hmac.new(secret,msg=a,digestmod=hashlib.sha256).digest()
    return output

def master_secret(pms,client_random,server_random):
    out=prf(pms,client_random+server_random,2)
    return out[:48]

def keyblock(ms,client_random,server_random):
    u=prf(ms,server_random+client_random,4)
    return u

But I end up getting the wrong answer with this code:

>>> import prf
>>> pre_master_secret = bytes.fromhex("2DE53DAC84478C9822D0536FF4ACB173B3FC46FD6BFC7E5292C2934CCFAE484EFA7B29415922157C49DD361B24EB7A3F")
>>> client_random = bytes.fromhex("AFE563DF246AD11D44BEA9D1397E0FA44478744A7C3F55AAE77EDD166C04BC72")
>>> server_random = bytes.fromhex("46075972454EBFFDC9A927229CBD0E1B8F3F34160192849263107B2FC5A3C19F")
>>> prf.master_secret(pre_master_secret, client_random, server_random).hex()
'8e0a0a801f5eb2083e51df6cee753c3c6c380b0b17875219e87c0d95e61207fdd55d2fd9785a3721e8726958955a41f4'

Does anybody see where I am going wrong here?

1

There are 1 answers

2
mf2045 On

out=prf(pms,label+client_random+server_random,2),label=b'master secret'