from Crypto.Cipher import AES
from Crypto.Hash import CMAC
# Step 1: Calculate Full CMAC using SV2
def calculate_full_cmac():
# Define the input values for Step 1
key = bytes.fromhex("00000000000000000000000000000001") # Master key
sv2 = bytes.fromhex("3CC30001008004AA5AAA1E1890180000") # SV2 value
# Initialize CMAC with AES
cmac = CMAC.new(key, ciphermod=AES)
cmac.update(sv2)
# Compute the full CMAC (16 bytes)
full_cmac = cmac.digest()
full_cmac_hex = full_cmac.hex().upper()
print("Step 1: Full CMAC (16 bytes):", full_cmac_hex)
return full_cmac
# Step 2: Calculate SDMMAC using Full CMAC
def calculate_sdmmac(full_cmac
# Key for the calculation (KSesSDMFileReadMAC is Full CMAC from Step 1)
key = full_cmac
# Zero-length input (empty input for SDMMAC)
input_data = b''
# Create a CMAC object using the key and AES
cmac = CMAC.new(key, ciphermod=AES)
# Update the CMAC with the input data (zero-length here)
cmac.update(input_data)
# Calculate the full CMAC
cmac_full = cmac.digest()
# Shorten the CMAC to take every second byte (as in the Java implementation)
sdmmac = cmac_full[1::2]
sdmmac_hex = sdmmac.hex().upper()
print("Step 2: SDMMAC (8 bytes, shortened):", sdmmac_hex)
return sdmmac
# Execute the steps
full_cmac = calculate_full_cmac()
sdmmac = calculate_sdmmac(full_cmac)