def command_KU(cmdObj): """ Generate Secure Message with Integrity and Optional Confidentiality and PIN Change """ """ Command Pattern: request: Message header + 'KU' + Mode Flag + Scheme ID + MK-SMI + Primary Account Number + Integrity Session Data + Plaintext Data Length + Plaintext Data + Delimiter(;) + MK-SMC + TK + Confidentiality Session Data + Offset + Cipher Data Length + Cipher Data + Delimiter(;) + Source PIN Encryption Key Type + Source PIN Encryption Key + Source PIN Block Format + Destination PIN Block Format + Primary Account Number + MK-AC + End message delimiter + Message Trailer response: Message header + 'KV' + Error code + ARPC + End Message delimiter + Message Trailer """ if len(cmdObj.MK_SMI.value) != 32: raise ValueError('MK-SMI length error') if cmdObj.MK_SMI.scheme == '': MK_SMI = decryptKeyUnderLMK('MK-SMI', unhexlify(cmdObj.MK_SMI.value)) else: MK_SMI = decryptKeyUnderLMK('MK-SMI', unhexlify(cmdObj.MK_SMI.value), cmdObj.MK_SMI.scheme) # Plain Data最后有一个分隔符 atc = cmdObj.IntegritySessionData.value[-2:] sessionKey = icSessionKey(icKeyDerivation(MK_SMI, hexlify(cmdObj.PAN.value), ''), atc) block = padDataBlock(cmdObj.PlainData.value) mac = calcMAC(sessionKey, block, '\x00'*8) respObj = CommandObj() respObj.ResponseCode = DataVariable('00') respObj.MAC = DataVariable(mac) return respObj
def command_ME(cmdObj): """ Verify a MAC and, if successful, generate a MAC on the same data with adifferent key. """ """ Command Pattern: request: Message header + 'ME' + Source TAK + Destiniation TAK + MAC + Data + End message delimiter + Message Trailer response: Message header + 'MF' + Error code + MAC + End Message delimiter + Message Trailer """ SourceTAK = decryptKeyUnderLMK('TAK', unhexlify(cmdObj.SourceTAK.value), cmdObj.SourceTAK.scheme) DestinationTAK = decryptKeyUnderLMK('TAK', unhexlify(cmdObj.DestinationTAK.value), cmdObj.DestinationTAK.scheme) mac = calcMAC(SourceTAK, cmdObj.Data.value, "\0\0\0\0\0\0\0\0") finalMAC = string.upper(hexlify(mac))[:8] respObj = CommandObj() if cmdObj.MAC.value == finalMAC: respObj.ResponseCode = DataVariable('00') mac = calcMAC(DestinationTAK, cmdObj.Data.value, "\0\0\0\0\0\0\0\0") respObj.MAC = DataVariable(string.upper(hexlify(mac))[:8]) else: # УÑéʧ°Ü²»·µ»ØMAC respObj.ResponseCode = DataVariable('01') respObj.MAC = DataVariable('00000000') return respObj
def command_MA(cmdObj): """ Generate a MAC on given data. """ """ Command Pattern: request: Message header + 'MA' + TAK + Data + End message delimiter + Message Trailer response: Message header + 'MB' + Error code + MAC + End Message delimiter + Message Trailer """ TAK = decryptKeyUnderLMK('TAK', unhexlify(cmdObj.TAK.value), cmdObj.TAK.scheme) mac = calcMAC(TAK, cmdObj.Data.value, "\0\0\0\0\0\0\0\0") respObj = CommandObj() respObj.ResponseCode = DataVariable('00') respObj.MAC = DataVariable(string.upper(hexlify(mac))[:8]) return respObj