def get_dap_signature(self, key, key_type, hashAlgorithm='SHA1'): ''' Returns the DAP Signature of this load file using the specified key, key type and the Hash algorithm ("SHA1" by default) ''' #1. Get the hash loadFileHash = self.getLoadFileDataHash(hashAlgorithm) #2. Performs the signature signature = None if key_type == 'AES': signature = AES_CMAC(loadFileHash, key) elif key_type == 'DES': #2. Perform Padding if any padded_loadFileHash = crypto.ISO_9797_M2_Padding(loadFileHash) #Perform a MAC 33 on this block with ICV = 00 value1 = crypto.MAC(padded_loadFileHash, keyA, "00 00 00 00 00 00 00 00") #DES-1 (key B) value2 = crypto.DES_INV_ECB(value1, keyB) #DES ECB signature = crypto.DES_ECB(value2, keyA) elif key_type == 'RSA': # TODO pass else: pass return signature
def encipher_data_SCP03(data, key, iv): ''' encipher message according to SCP03 protocol. :param str data : The message to authenticate. :param str key : A AES key used to encipher :param str iv : The initial chaining vector :returns: (str): The enciphered data ''' data = crypto.ISO_9797_M2_Padding(data, 8) return crypto.AES_CBC(data, key, iv)
def calculate_card_cryptogram_SCP03(cardChallenge, hostChallenge, session_mac_key): ''' Calculates the card cryptogram for SCP03. :param cardChallenge (str): The card challenge. :param hostChallenge (str): The host challenge. :param key (str) : The Secure Channel Message Authentication Code Key. :returns: str: The card cryptogram if no error occurs, None otherwize. ''' SCP03_CST_DERIVATION_CARD_CRYPTO = '00' logger.log_start("calculate_card_cryptogram_SCP03") data = hostChallenge + cardChallenge # 1. first build the derivation data der_data = '' der_data = '00 00 00 00 00 00 00 00 00 00 00' + SCP03_CST_DERIVATION_CARD_CRYPTO der_data += '00' der_data += '0040' der_data += '01' der_data += data logger.log_debug("\tCalculates card cryptogram using %s " % der_data) # 2. calculate cryptogram cryptogram = __SCP03_KDF_CounterMode__(der_data, session_mac_key, 0x01) logger.log_debug("\tCard cryptogram: %s " % cryptogram) logger.log_end("calculate_card_cryptogram_SCP03") return cryptogram # padd data if needed data = crypto.ISO_9797_M2_Padding(data, 8) logger.log_debug("\tData padded: %s " % data) #calculate the cryptogram cryptogram = crypto.MAC33(data, session_enc_key, crypto.ICV_NULL_8) logger.log_debug("\tCard cryptogram: %s " % cryptogram) logger.log_end("calculate_card_cryptogram_SCP02") return cryptogram
def calculate_host_cryptogram_SCP02(sequenceCounter, cardChallenge, hostChallenge, session_enc_key): # padd data if needed logger.log_start("calculate_host_cryptogram_SCP02") data = sequenceCounter + cardChallenge + hostChallenge logger.log_debug(" Calculates host cryptogram using %s " % data) data = crypto.ISO_9797_M2_Padding(data, 8) cryptogram = crypto.MAC33(data, session_enc_key, crypto.ICV_NULL_8) logger.log_debug("host cryptogram: %s " % cryptogram) logger.log_end("calculate_host_cryptogram_SCP02") return cryptogram
def calculate_card_cryptogram_SCP02(sequenceCounter, cardChallenge, hostChallenge, session_enc_key): ''' Calculates the card cryptogram for SCP02. :param sequenceCounter (str): The sequence counter. :param cardChallenge (str): The card challenge. :param hostChallenge (str): The host challenge. :param session_enc_key (str) : The Session Encryption Key for calculating the card cryptogram. :returns: (tuple): tuple containing: - class:`ErrorStatus` with error status ERROR_STATUS_SUCCESS if no error occurs, otherwise error code and error message are contained in the class :class:`ErrorStatus`. - The card cryptogram (str) if no error occurs, None otherwize. ''' logger.log_start("calculate_card_cryptogram_SCP02") data = hostChallenge + sequenceCounter + cardChallenge logger.log_debug("\tCalculates card cryptogram using %s " % data) # padd data if needed data = crypto.ISO_9797_M2_Padding(data, 8) logger.log_debug("\tData padded: %s " % data) #calculate the cryptogram cryptogram = crypto.MAC33(data, session_enc_key, crypto.ICV_NULL_8) logger.log_debug("\tCard cryptogram: %s " % cryptogram) logger.log_end("calculate_card_cryptogram_SCP02") return cryptogram