Calculating K

View as MarkdownOpen in Claude

To calculate our live session token, we need to use modulus division to receive a K value for a HMAC hash.

Begin by pulling the base value, B, which will be our dh_response value from our /live_session_token response, using a leading 0 as a sign bit for the hex string.

We will then need to retrieve a BigInteger value of the dh_response value.

Our exponent value would be equivalent to the same dh_random value used for our /live_session_token request.

Our modulus, p, would be the same as our dh_modulus or dh_prime value from our Diffie-Hellman file.

We would finally calculate K using the formula B^a modulo p.

1# K will be used to hash the prepend bytestring (the decrypted access token) to produce the LST.
2B = int(dh_response, 16)
3= dh_random
4= dh_prime
5K = pow(B, a, p)

Once K is receive, convert the integer to its hex string representation before converting it to a byte array. In some cases, the resultant K value will have an odd number of leading characters that should be prepended by an additional 0.

1# Generate hex string representation of integer K.
2hex_str_K = hex(K)[2:]
3
4# If hex string K has odd number of chars, add a leading 0, because all Python hex bytes must contain two hex digits (0x01 not 0x1).
5if len(hex_str_K) % 2:
6 print("adding leading 0 for even number of chars")
7 hex_str_K = "0" + hex_str_K
8
9# Generate hex bytestring from hex string K.
10hex_bytes_K = bytes.fromhex(hex_str_K)
11
12# Prepend a null byte to hex bytestring K if lacking sign bit.
13if len(bin(K)[2:]) % 8 == 0:
14 hex_bytes_K = bytes(1) + hex_bytes_K