1

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?

factor2
  • 155
  • 9
  • Ever figured this one out? – miran80 Dec 13 '21 at 15:28
  • 1
    I never figured out the error in the above code, but I did find a way to accomplish what I needed using Scapy: `from scapy.layers.tls.crypto import prf pseudo_random_function = prf.PRF(hash_name=hash_to_use, tls_version=771) master_secret_ = pseudo_random_function.compute_master_secret(pre_master_secret, client_hello_random, server_hello_random)` – factor2 Dec 20 '21 at 13:01

1 Answers1

0

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

mf2045
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 24 '22 at 09:41
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Feb 24 '22 at 10:02