Skip to content

Commit 847e586

Browse files
committed
ruff: fixes on syntax
1 parent 8b9f324 commit 847e586

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

ledgerblue/hexLoader.py

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
********************************************************************************
1818
"""
1919

20-
from Cryptodome.Cipher import AES
21-
import sys
22-
import struct
23-
import hashlib
2420
import binascii
25-
from .ecWrapper import PrivateKey
26-
from ecpy.curves import Curve
21+
import hashlib
2722
import os
23+
import struct
24+
25+
from Cryptodome.Cipher import AES
26+
from ecpy.curves import Curve
27+
28+
from .ecWrapper import PrivateKey
2829

2930
LOAD_SEGMENT_CHUNK_HEADER_LENGTH = 3
3031
MIN_PADDING_LENGTH = 1
@@ -43,33 +44,33 @@ def string_to_bytes(x):
4344

4445

4546
def encodelv(v):
46-
l = len(v)
47+
L = len(v)
4748
s = b""
48-
if l < 128:
49-
s += struct.pack(">B", l)
50-
elif l < 256:
49+
if L < 128:
50+
s += struct.pack(">B", L)
51+
elif L < 256:
5152
s += struct.pack(">B", 0x81)
52-
s += struct.pack(">B", l)
53-
elif l < 65536:
53+
s += struct.pack(">B", L)
54+
elif L < 65536:
5455
s += struct.pack(">B", 0x82)
55-
s += struct.pack(">H", l)
56+
s += struct.pack(">H", L)
5657
else:
5758
raise Exception("Unimplemented LV encoding")
5859
s += v
5960
return s
6061

6162

6263
def encodetlv(t, v):
63-
l = len(v)
64+
L = len(v)
6465
s = struct.pack(">B", t)
65-
if l < 128:
66-
s += struct.pack(">B", l)
67-
elif l < 256:
66+
if L < 128:
67+
s += struct.pack(">B", L)
68+
elif L < 256:
6869
s += struct.pack(">B", 0x81)
69-
s += struct.pack(">B", l)
70-
elif l < 65536:
70+
s += struct.pack(">B", L)
71+
elif L < 65536:
7172
s += struct.pack(">B", 0x82)
72-
s += struct.pack(">H", l)
73+
s += struct.pack(">H", L)
7374
else:
7475
raise Exception("Unimplemented TLV encoding")
7576
s += v
@@ -138,7 +139,7 @@ def __init__(
138139

139140
# legacy unsecure SCP (pre nanos-1.4, pre blue-2.1)
140141
self.max_mtu = 0xFE
141-
if not self.card is None:
142+
if self.card is not None:
142143
self.max_mtu = min(self.max_mtu, self.card.apduMaxDataSize())
143144
self.scpVersion = 2
144145
self.key = mutauth_result
@@ -147,22 +148,22 @@ def __init__(
147148

148149
# store the aligned block len to be transported if requested
149150
self.cleardata_block_len = cleardata_block_len
150-
if not (self.cleardata_block_len is None):
151-
if not self.card is None:
151+
if self.cleardata_block_len is not None:
152+
if self.card is not None:
152153
self.cleardata_block_len = min(
153154
self.cleardata_block_len, self.card.apduMaxDataSize()
154155
)
155156

156-
if scpv3 == True:
157+
if scpv3:
157158
self.scp_enc_key = self.scp_derive_key(mutauth_result, 0)
158159
self.scpVersion = 3
159160
self.max_mtu = 0xFE
160-
if not self.card is None:
161+
if self.card is not None:
161162
self.max_mtu = min(self.max_mtu, self.card.apduMaxDataSize() & 0xF0)
162163
return
163164

164165
# try:
165-
if type(mutauth_result) is dict and "ecdh_secret" in mutauth_result:
166+
if isinstance(mutauth_result, dict) and "ecdh_secret" in mutauth_result:
166167
self.scp_enc_key = self.scp_derive_key(mutauth_result["ecdh_secret"], 0)[
167168
0:16
168169
]
@@ -173,7 +174,7 @@ def __init__(
173174
self.scp_mac_iv = b"\x00" * 16
174175
self.scpVersion = 3
175176
self.max_mtu = 0xFE
176-
if not self.card is None:
177+
if self.card is not None:
177178
self.max_mtu = min(self.max_mtu, self.card.apduMaxDataSize() & 0xF0)
178179

179180
def crc16(self, data):
@@ -446,7 +447,7 @@ def exchange(self, cla, ins, p1, p2, data):
446447
# wrap
447448
data = self.scpWrap(data)
448449
apdu = bytearray([cla, ins, p1, p2, len(data)]) + bytearray(data)
449-
if self.card == None:
450+
if self.card is None:
450451
print("%s" % binascii.hexlify(apdu))
451452
else:
452453
# unwrap after exchanged
@@ -455,7 +456,7 @@ def exchange(self, cla, ins, p1, p2, data):
455456
def scpWrap(self, data):
456457
if not self.secure or data is None or len(data) == 0:
457458
return data
458-
if self.scpv3 == True:
459+
if self.scpv3:
459460
cipher = AES.new(self.scp_enc_key, mode=AES.MODE_SIV)
460461
ciphertext, tag = cipher.encrypt_and_digest(data)
461462
encryptedData = tag + ciphertext
@@ -500,7 +501,7 @@ def scpWrap(self, data):
500501
def scpUnwrap(self, data):
501502
if not self.secure or data is None or len(data) == 0 or len(data) == 2:
502503
return data
503-
if self.scpv3 == True:
504+
if self.scpv3:
504505
cipher = AES.new(self.scp_enc_key, mode=AES.MODE_SIV)
505506
tag = data[:16]
506507
decryptedData = cipher.decrypt_and_verify(data[16:], tag)
@@ -526,12 +527,12 @@ def scpUnwrap(self, data):
526527
cipher = AES.new(self.scp_enc_key, AES.MODE_CBC, self.scp_enc_iv)
527528
self.scp_enc_iv = bytes(data[-16:])
528529
data = cipher.decrypt(bytes(data))
529-
l = len(data) - 1
530-
while data[l] != padding_char:
531-
l -= 1
532-
if l == -1:
530+
L = len(data) - 1
531+
while data[L] != padding_char:
532+
L -= 1
533+
if L == -1:
533534
raise BaseException("Invalid SCP ENC padding")
534-
data = data[0:l]
535+
data = data[0:L]
535536
decryptedData = data
536537

537538
if SCP_DEBUG:
@@ -541,12 +542,12 @@ def scpUnwrap(self, data):
541542
decryptedData = cipher.decrypt(data)
542543
if SCP_DEBUG:
543544
print("unwrap_old: " + binascii.hexlify(decryptedData))
544-
l = len(decryptedData) - 1
545-
while decryptedData[l] != padding_char:
546-
l -= 1
547-
if l == -1:
545+
L = len(decryptedData) - 1
546+
while decryptedData[L] != padding_char:
547+
L -= 1
548+
if L == -1:
548549
raise BaseException("Invalid SCP ENC padding")
549-
decryptedData = decryptedData[0:l]
550+
decryptedData = decryptedData[0:L]
550551
self.iv = data[-16:]
551552

552553
# print ("<<")
@@ -581,13 +582,13 @@ def boot(self, bootadr, signature=None):
581582
# Force jump into Thumb mode
582583
bootadr |= 1
583584
data = b"\x09" + struct.pack(">I", bootadr)
584-
if signature != None:
585+
if signature is not None:
585586
data += struct.pack(">B", len(signature)) + signature
586587
self.exchange(self.cla, 0x00, 0x00, 0x00, data)
587588

588589
def commit(self, signature=None):
589590
data = b"\x09"
590-
if signature != None:
591+
if signature is not None:
591592
data += struct.pack(">B", len(signature)) + signature
592593
self.exchange(self.cla, 0x00, 0x00, 0x00, data)
593594

@@ -610,20 +611,20 @@ def createAppNoInstallParams(
610611
+ appname
611612
)
612613
if iconOffset is None:
613-
if not (icon is None):
614+
if icon is not None:
614615
data += struct.pack(">B", len(icon)) + icon
615616
else:
616617
data += b"\x00"
617618

618-
if not (path is None):
619+
if path is not None:
619620
data += struct.pack(">B", len(path)) + path
620621
else:
621622
data += b"\x00"
622623

623-
if not iconOffset is None:
624+
if iconOffset is not None:
624625
data += struct.pack(">I", iconOffset) + struct.pack(">H", iconSize)
625626

626-
if not appversion is None:
627+
if appversion is not None:
627628
data += struct.pack(">B", len(appversion)) + appversion
628629

629630
# in previous version, appparams are not part of the application hash yet
@@ -682,11 +683,10 @@ def createPack(self, language, code_length):
682683

683684
def loadPackSegmentChunk(self, offset, chunk):
684685
data = struct.pack(">I", offset) + chunk
685-
# print(f"Inside loadPackSegmentChunk, offset={offset}, len(chunk)={len(chunk)}")
686686
self.exchange(self.cla, 0x31, self.language, 0x00, data)
687687

688688
def commitPack(self, signature=None):
689-
if signature != None:
689+
if signature is not None:
690690
data = struct.pack(">B", len(signature)) + signature
691691
else:
692692
data = b""
@@ -885,8 +885,8 @@ def load(
885885
initialAddress = hexFile.minAddr()
886886
sha256 = hashlib.new("sha256")
887887
# stat by hashing the create app params to ensure complete app signature
888-
if targetId != None and (targetId & 0xF) > 3:
889-
if targetVersion == None:
888+
if targetId is not None and (targetId & 0xF) > 3:
889+
if targetVersion is None:
890890
print("Target version is not set, application hash will not match!")
891891
targetVersion = ""
892892
# encore targetId U4LE, and version string bytes
@@ -935,7 +935,7 @@ def load(
935935
if self.cleardata_block_len and chunkLen % self.cleardata_block_len:
936936
if chunkLen < self.cleardata_block_len:
937937
raise Exception(
938-
"Cannot transport not block aligned data with fixed block len"
938+
"Cannot transport data that is not block-aligned"
939939
)
940940
chunkLen -= chunkLen % self.cleardata_block_len
941941
# pad with 00's when not complete block and performing NENC
@@ -1010,7 +1010,7 @@ def recoverDeleteCA(self, name, key):
10101010
self.exchange(self.cla, 0x00, 0x00, 0x00, data)
10111011

10121012
def recoverValidateCertificate(self, version, role, name, key, sign, last=False):
1013-
if last == True:
1013+
if last:
10141014
p1 = b"\x80"
10151015
else:
10161016
p1 = b"\x00"

0 commit comments

Comments
 (0)