当前位置: 首页>>代码示例>>Python>>正文


Python CryptoUtils.append_padding方法代码示例

本文整理汇总了Python中virtualsmartcard.CryptoUtils.append_padding方法的典型用法代码示例。如果您正苦于以下问题:Python CryptoUtils.append_padding方法的具体用法?Python CryptoUtils.append_padding怎么用?Python CryptoUtils.append_padding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在virtualsmartcard.CryptoUtils的用法示例。


在下文中一共展示了CryptoUtils.append_padding方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: external_authenticate

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
    def external_authenticate(self, p1, p2, resp_data):
        """Performs the basic access control protocol as defined in
        the ICAO MRTD standard"""
        rnd_icc = self.last_challenge

        # Receive Mutual Authenticate APDU from terminal
        # Decrypt data and check MAC
        Eifd = resp_data[:-8]
        padded_Eifd = vsCrypto.append_padding(self.current_SE.cct.blocklength,
                                              Eifd)
        Mifd = vsCrypto.crypto_checksum("CC", self.KMac, padded_Eifd)
        # Check the MAC
        if not Mifd == resp_data[-8:]:
            raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
        # Decrypt the data
        plain = vsCrypto.decrypt("DES3-CBC", self.KEnc, resp_data[:-8])
        if plain[8:16] != rnd_icc:
            raise SwError(SW["WARN_NOINFO63"])
        # Extract keying material from IFD, generate ICC keying material
        Kifd = plain[16:]
        rnd_ifd = plain[:8]
        Kicc = urandom(16)
        # Generate Answer
        data = plain[8:16] + plain[:8] + Kicc
        Eicc = vsCrypto.encrypt("DES3-CBC", self.KEnc, data)
        padded_Eicc = vsCrypto.append_padding(self.current_SE.cct.blocklength,
                                              Eicc)
        Micc = vsCrypto.crypto_checksum("CC", self.KMac, padded_Eicc)
        # Derive the final keys and set the current SE
        KSseed = vsCrypto.operation_on_string(Kicc, Kifd, lambda a, b: a ^ b)
        self.current_SE.ct.key = self.derive_key(KSseed, 1)
        self.current_SE.cct.key = self.derive_key(KSseed, 2)
        self.current_SE.ssc = stringtoint(rnd_icc[-4:] + rnd_ifd[-4:])
        return SW["NORMAL"], Eicc + Micc
开发者ID:Snyper4277,项目名称:vsmartcard,代码行数:36,代码来源:ePass.py

示例2: encipher

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
    def encipher(self, p1, p2, data):
        padded = vsCrypto.append_padding(self.cct.blocklength, data)
        cipher = eac.EAC_encrypt(self.eac_ctx, padded)
        if not cipher:
            eac.print_ossl_err()
            raise SwError(SW["ERR_NOINFO69"]) 

        return cipher
开发者ID:Jdi99y515,项目名称:vsmartcard,代码行数:10,代码来源:nPA.py

示例3: encipher

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
 def encipher(self, p1, p2, data):
     """
     Encipher data using key, algorithm, IV and Padding specified
     by the current Security environment.
     
     :returns: raw data (no TLV coding).
     """
     algo = self.ct.algorithm
     key = self.ct.key
     if key == None or algo == None:
         raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
     else:
         padded = vsCrypto.append_padding(vsCrypto.get_cipher_blocklen(algo), data)
         crypted = vsCrypto.encrypt(algo, key, padded, self.ct.iv)
         return crypted
开发者ID:12019,项目名称:vsmartcard,代码行数:17,代码来源:SEutils.py

示例4: protect_response

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
    def protect_response(self, sw, result):
        """
        This method protects a response APDU using secure messaging mechanisms

        :returns: the protected data and the SW bytes
        """

        return_data = ""
        # if sw == SW["NORMAL"]:
        #    sw = inttostring(sw)
        #    length = len(sw)
        #    tag = SM_Class["PLAIN_PROCESSING_STATUS"]
        #    tlv_sw = pack([(tag,length,sw)])
        #    return_data += tlv_sw

        if result != "":  # Encrypt the data included in the RAPDU
            encrypted = self.encipher(0x82, 0x80, result)
            encrypted = "\x01" + encrypted
            encrypted_tlv = pack([(
                                SM_Class["CRYPTOGRAM_PADDING_INDICATOR_ODD"],
                                len(encrypted),
                                encrypted)])
            return_data += encrypted_tlv

        if sw == SW["NORMAL"]:
            if self.cct.algorithm is None:
                raise SwError(SW["CONDITIONSNOTSATISFIED"])
            elif self.cct.algorithm == "CC":
                tag = SM_Class["CHECKSUM"]
                padded = vsCrypto.append_padding(self.cct.blocklength,
                                                 return_data)
                auth = self.compute_cryptographic_checksum(0x8E, 0x80, padded)
                length = len(auth)
                return_data += pack([(tag, length, auth)])
            elif self.cct.algorithm == "SIGNATURE":
                tag = SM_Class["DIGITAL_SIGNATURE"]
                hash = self.hash(0x90, 0x80, return_data)
                auth = self.compute_digital_signature(0x9E, 0x9A, hash)
                length = len(auth)
                return_data += pack([(tag, length, auth)])

        return sw, return_data
开发者ID:frankmorgner,项目名称:vsmartcard,代码行数:44,代码来源:SEutils.py

示例5: external_authenticate

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
 def external_authenticate(self, p1, p2, data):
     """
     Authenticate the terminal to the card. Check whether Terminal correctly
     encrypted the given challenge or not
     """
     if self.last_challenge is None:
         raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
     
     key = self._get_referenced_key(p1, p2) 
     if p1 == 0x00: #No information given
         cipher = get_referenced_cipher(self.cipher)   
     else:
         cipher = get_referenced_cipher(p1)     
     
     reference = vsCrypto.append_padding(cipher, self.last_challenge)
     reference = vsCrypto.encrypt(cipher, key, reference)
     if(reference == data):
         #Invalidate last challenge
         self.last_challenge = None
         return SW["NORMAL"], ""
     else:
         raise SwError(SW["WARN_NOINFO63"])
开发者ID:Churro,项目名称:vsmartcard,代码行数:24,代码来源:SmartcardSAM.py

示例6: parse_SM_CAPDU

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
    def parse_SM_CAPDU(self, CAPDU, authenticate_header):
        """
        This methods parses a data field including Secure Messaging objects.
        SM_header indicates whether or not the header of the message shall be 
        authenticated. It returns an unprotected command APDU
        
        :param CAPDU: The protected CAPDU to be parsed
        :param authenticate_header: Whether or not the header should be
            included in authentication mechanisms 
        :returns: Unprotected command APDU
        """
            
        structure = unpack(CAPDU.data)
        return_data = ["",]
        
        cla = None
        ins = None
        p1 = None
        p2 = None
        le = None
        
        if authenticate_header:
            to_authenticate = inttostring(CAPDU.cla) + inttostring(CAPDU.ins)+\
                              inttostring(CAPDU.p1) + inttostring(CAPDU.p2)
            to_authenticate = vsCrypto.append_padding(self.cct.blocklength, to_authenticate)
        else:
            to_authenticate = ""

        for tlv in structure:
            tag, length, value = tlv
            
            if tag % 2 == 1: #Include object in checksum calculation
                to_authenticate += bertlv_pack([[tag, length, value]])
            
            #SM data objects for encapsulating plain values
            if tag in (SM_Class["PLAIN_VALUE_NO_TLV"],
                       SM_Class["PLAIN_VALUE_NO_TLV_ODD"]):
                return_data.append(value) #FIXME: Need TLV coding?
            #Encapsulated SM objects. Parse them
            #FIXME: Need to pack value into a dummy CAPDU
            elif tag in (SM_Class["PLAIN_VALUE_TLV_INCULDING_SM"],
                         SM_Class["PLAIN_VALUE_TLV_INCULDING_SM_ODD"]):
                return_data.append(self.parse_SM_CAPDU(value, authenticate_header)) 
            #Encapsulated plaintext BER-TLV objects
            elif tag in (SM_Class["PLAIN_VALUE_TLV_NO_SM"],
                         SM_Class["PLAIN_VALUE_TLV_NO_SM_ODD"]):
                return_data.append(value)
            elif tag in (SM_Class["Ne"], SM_Class["Ne_ODD"]):
                le = value
            elif tag == SM_Class["PLAIN_COMMAND_HEADER"]:
                if len(value) != 8:
                    raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
                else:
                    cla = value[:2]
                    ins = value[2:4]
                    p1 = value[4:6]
                    p2 = value[6:8]

            #SM data objects for confidentiality
            if tag in (SM_Class["CRYPTOGRAM_PLAIN_TLV_INCLUDING_SM"],
                       SM_Class["CRYPTOGRAM_PLAIN_TLV_INCLUDING_SM_ODD"]):
                #The cryptogram includes SM objects. 
                #We decrypt them and parse the objects.
                plain = self.decipher(tag, 0x80, value)
                #TODO: Need Le = length
                return_data.append(self.parse_SM_CAPDU(plain, authenticate_header))
            elif tag in (SM_Class["CRYPTOGRAM_PLAIN_TLV_NO_SM"],
                         SM_Class["CRYPTOGRAM_PLAIN_TLV_NO_SM_ODD"]):
                #The cryptogram includes BER-TLV encoded plaintext. 
                #We decrypt them and return the objects.
                plain = self.decipher(tag, 0x80, value)
                return_data.append(plain)
            elif tag in (SM_Class["CRYPTOGRAM_PADDING_INDICATOR"],
                         SM_Class["CRYPTOGRAM_PADDING_INDICATOR_ODD"]):
                #The first byte of the data field indicates the padding to use:
                """
                Value        Meaning
                '00'     No further indication
                '01'     Padding as specified in 6.2.3.1
                '02'     No padding
                '1X'     One to four secret keys for enciphering information,
                         not keys ('X' is a bitmap with any value from '0' to 'F')
                '11'     indicates the first key (e.g., an "even" control word
                         in a pay TV system)
                '12'     indicates the second key (e.g., an "odd" control word
                         in a pay TV system)
                '13'     indicates the first key followed by the second key
                         (e.g., a pair of control words in a pay TV system)
                '2X'     Secret key for enciphering keys, not information
                         ('X' is a reference with any value from '0' to 'F')
                         (e.g., in a pay TV system, either an operational key
                         for enciphering control words, or a management key for
                         enciphering operational keys)
                '3X'     Private key of an asymmetric key pair ('X' is a
                         reference with any value from '0' to 'F')
                '4X'     Password ('X' is a reference with any value from '0' to
                         'F')
            '80' to '8E' Proprietary
                """
                padding_indicator = stringtoint(value[0])
#.........这里部分代码省略.........
开发者ID:12019,项目名称:vsmartcard,代码行数:103,代码来源:SEutils.py

示例7: SAM

# 需要导入模块: from virtualsmartcard import CryptoUtils [as 别名]
# 或者: from virtualsmartcard.CryptoUtils import append_padding [as 别名]
if __name__ == "__main__":

    password = "DUMMYKEYDUMMYKEY"

    MyCard = SAM("1234", "1234567890")
    try:
        print(MyCard.verify(0x00, 0x00, "5678"))
    except SwError as e:
        print(e.message)

    print("Counter = " + str(MyCard.counter))
    print(MyCard.verify(0x00,  0x00, "1234"))
    print("Counter = " + str(MyCard.counter))
    sw, challenge = MyCard.get_challenge(0x00, 0x00, "")
    print("Before encryption: " + challenge)
    padded = vsCrypto.append_padding("DES3-ECB", challenge)
    sw, result_data = MyCard.internal_authenticate(0x00, 0x00, padded)
    print("Internal Authenticate status code: %x" % sw)

    try:
        sw, res = MyCard.external_authenticate(0x00, 0x00, result_data)
    except SwError as e:
        print(e.message)
        sw = e.sw
    print("Decryption Status code: %x" % sw)

    #SE = Security_Environment(None)
    #testvektor = "foobar"
    #print "Testvektor = %s" % testvektor
    #sw, hash = SE.hash(0x90,0x80,testvektor)
    #print "SW after hashing = %s" % sw
开发者ID:Churro,项目名称:vsmartcard,代码行数:33,代码来源:SmartcardSAM.py


注:本文中的virtualsmartcard.CryptoUtils.append_padding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。