[vpicc] Python 3 linting

Some additional code linting
- remove unused imports
- remove unecessary parenthesis from if statements
- created TLVutils TAG dictionary in creation time
This commit is contained in:
fabio rodrigues
2020-08-18 15:19:23 +01:00
parent 475fad371a
commit 919c13c907
3 changed files with 45 additions and 45 deletions

View File

@@ -239,11 +239,11 @@ class Security_Environment(object):
if se & 0x08: if se & 0x08:
self.external_auth = True self.external_auth = True
return self._set_SE(p2, data) return self._set_SE(p2, data)
elif(cmd == 0x02): elif cmd == 0x02:
return self.sam.store_SE(p2) return self.sam.store_SE(p2)
elif(cmd == 0x03): elif cmd == 0x03:
return self.sam.restore_SE(p2) return self.sam.restore_SE(p2)
elif(cmd == 0x04): elif cmd == 0x04:
return self.sam.erase_SE(p2) return self.sam.erase_SE(p2)
else: else:
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
@@ -684,7 +684,6 @@ class Security_Environment(object):
""" """
from Crypto.PublicKey import RSA, DSA from Crypto.PublicKey import RSA, DSA
from Crypto.Random import get_random_bytes
cipher = self.ct.algorithm cipher = self.ct.algorithm

View File

@@ -229,7 +229,7 @@ class SAM(object):
blocklen = vsCrypto.get_cipher_blocklen(cipher) blocklen = vsCrypto.get_cipher_blocklen(cipher)
reference = vsCrypto.append_padding(blocklen, self.last_challenge) reference = vsCrypto.append_padding(blocklen, self.last_challenge)
reference = vsCrypto.encrypt(cipher, key, reference) reference = vsCrypto.encrypt(cipher, key, reference)
if(reference == data): if reference == data:
# Invalidate last challenge # Invalidate last challenge
self.last_challenge is None self.last_challenge is None
return SW["NORMAL"], "" return SW["NORMAL"], ""
@@ -249,20 +249,20 @@ class SAM(object):
key = self._get_referenced_key(p1, p2) key = self._get_referenced_key(p1, p2)
card_number = self.get_card_number() card_number = self.get_card_number()
if (key is None): if key is None:
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
if p1 == 0x00: # No information given if p1 == 0x00: # No information given
cipher = get_referenced_cipher(self.cipher) cipher = get_referenced_cipher(self.cipher)
else: else:
cipher = get_referenced_cipher(p1) cipher = get_referenced_cipher(p1)
if (cipher is None): if cipher is None:
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
plain = vsCrypto.decrypt(cipher, key, mutual_challenge) plain = vsCrypto.decrypt(cipher, key, mutual_challenge)
last_challenge_len = len(self.last_challenge) last_challenge_len = len(self.last_challenge)
terminal_challenge = plain[:last_challenge_len-1] terminal_challenge = plain[:last_challenge_len - 1]
card_challenge = plain[last_challenge_len:-len(card_number)-1] card_challenge = plain[last_challenge_len:-len(card_number) - 1]
serial_number = plain[-len(card_number):] serial_number = plain[-len(card_number):]
if terminal_challenge != self.last_challenge: if terminal_challenge != self.last_challenge:
@@ -277,7 +277,7 @@ class SAM(object):
""" """
Generate a random number of maximum 8 Byte and return it. Generate a random number of maximum 8 Byte and return it.
""" """
if (p1 != 0x00 or p2 != 0x00): # RFU if p1 != 0x00 or p2 != 0x00: # RFU
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
length = 8 # Length of the challenge in Byte length = 8 # Length of the challenge in Byte
@@ -315,7 +315,7 @@ class SAM(object):
algo = get_referenced_cipher(p1) algo = get_referenced_cipher(p1)
keylength = vsCrypto.get_cipher_keylen(algo) keylength = vsCrypto.get_cipher_keylen(algo)
if (p2 == 0x00): # No information given, use the global card key if p2 == 0x00: # No information given, use the global card key
key = self.cardSecret key = self.cardSecret
# We treat global and specific reference data alike # We treat global and specific reference data alike
else: else:

View File

@@ -19,35 +19,36 @@
from virtualsmartcard.utils import stringtoint, inttostring from virtualsmartcard.utils import stringtoint, inttostring
TAG = {} TAG = {
TAG["FILECONTROLPARAMETERS"] = 0x62 "FILECONTROLPARAMETERS": 0x62,
TAG["FILEMANAGEMENTDATA"] = 0x64 "FILEMANAGEMENTDATA": 0x64,
TAG["FILECONTROLINFORMATION"] = 0x6F "FILECONTROLINFORMATION": 0x6F,
TAG["BYTES_EXCLUDINGSTRUCTURE"] = 0x80 "BYTES_EXCLUDINGSTRUCTURE": 0x80,
TAG["BYTES_INCLUDINGSTRUCTURE"] = 0x81 "BYTES_INCLUDINGSTRUCTURE": 0x81,
TAG["FILEDISCRIPTORBYTE"] = 0x82 "FILEDISCRIPTORBYTE": 0x82,
TAG["FILEIDENTIFIER"] = 0x83 "FILEIDENTIFIER": 0x83,
TAG["DFNAME"] = 0x84 "DFNAME": 0x84,
TAG["PROPRIETARY_NOTBERTLV"] = 0x85 "PROPRIETARY_NOTBERTLV": 0x85,
TAG["PROPRIETARY_SECURITY"] = 0x86 "PROPRIETARY_SECURITY": 0x86,
TAG["FIDEF_CONTAININGFCI"] = 0x87 "FIDEF_CONTAININGFCI": 0x87,
TAG["SHORTFID"] = 0x88 "SHORTFID": 0x88,
TAG["LIFECYCLESTATUS"] = 0x8A "LIFECYCLESTATUS": 0x8A,
TAG["SA_EXPANDEDFORMAT"] = 0x8B "SA_EXPANDEDFORMAT": 0x8B,
TAG["SA_COMPACTFORMAT"] = 0x8C "SA_COMPACTFORMAT": 0x8C,
TAG["FIDEF_CONTAININGSET"] = 0x8D "FIDEF_CONTAININGSET": 0x8D,
TAG["CHANNELSECURITY"] = 0x8E "CHANNELSECURITY": 0x8E,
TAG["SA_DATAOBJECTS"] = 0xA0 "SA_DATAOBJECTS": 0xA0,
TAG["PROPRIETARY_SECURITYTEMP"] = 0xA1 "PROPRIETARY_SECURITYTEMP": 0xA1,
TAG["PROPRIETARY_BERTLV"] = 0xA5 "PROPRIETARY_BERTLV": 0xA5,
TAG["SA_EXPANDEDFORMAT_TEMP"] = 0xAB "SA_EXPANDEDFORMAT_TEMP": 0xAB,
TAG["CRYPTIDENTIFIER_TEMP"] = 0xAC "CRYPTIDENTIFIER_TEMP": 0xAC,
TAG["DISCRETIONARY_DATA"] = 0x53 "DISCRETIONARY_DATA": 0x53,
TAG["DISCRETIONARY_TEMPLATE"] = 0x73 "DISCRETIONARY_TEMPLATE": 0x73,
TAG["OFFSET_DATA"] = 0x54 "OFFSET_DATA": 0x54,
TAG["TAG_LIST"] = 0x5C "TAG_LIST": 0x5C,
TAG["HEADER_LIST"] = 0x5D "HEADER_LIST": 0x5D,
TAG["EXTENDED_HEADER_LIST"] = 0x4D "EXTENDED_HEADER_LIST": 0x4D
}
def tlv_unpack(data): def tlv_unpack(data):
@@ -180,7 +181,7 @@ def unpack(data, with_marks=None, offset=0, include_filler=False):
for type, mark_start, mark_stop in with_marks: for type, mark_start, mark_stop in with_marks:
if (mark_start, mark_stop) == (start, stop): if (mark_start, mark_stop) == (start, stop):
marks.append(type) marks.append(type)
marks = (marks, ) marks = (marks,)
else: else:
marks = () marks = ()
@@ -239,11 +240,11 @@ def simpletlv_unpack(data):
length = rest[1] length = rest[1]
if length == 0xff: if length == 0xff:
length = (rest[2] << 8) + rest[3] length = (rest[2] << 8) + rest[3]
newvalue = rest[4:4+length] newvalue = rest[4:4 + length]
rest = rest[4+length:] rest = rest[4 + length:]
else: else:
newvalue = rest[2:2+length] newvalue = rest[2:2 + length]
rest = rest[2+length:] rest = rest[2 + length:]
result.append((tag, length, newvalue)) result.append((tag, length, newvalue))
return result return result