Python 3 fixes
This commit is contained in:
parent
a9611d412a
commit
ed1e3a4dac
|
@ -79,7 +79,7 @@ class BLEDongle(Dongle):
|
|||
response = result[dataStart : dataLength + dataStart]
|
||||
if self.debug:
|
||||
print("<= %s%.2x" % (hexlify(response), sw))
|
||||
if sw <> 0x9000:
|
||||
if sw != 0x9000:
|
||||
raise CommException("Invalid status %04x" % sw, sw)
|
||||
return response
|
||||
|
||||
|
|
|
@ -35,6 +35,13 @@ if no certificate is specified""")
|
|||
def auto_int(x):
|
||||
return int(x, 0)
|
||||
|
||||
def hexstr(bstr):
|
||||
if (sys.version_info.major == 3):
|
||||
return binascii.hexlify(bstr).decode()
|
||||
if (sys.version_info.major == 2):
|
||||
return binascii.hexlify(bstr)
|
||||
return ""
|
||||
|
||||
def getDeployedSecretV2(dongle, masterPrivate, targetid, issuerKey):
|
||||
testMaster = PrivateKey(bytes(masterPrivate))
|
||||
testMasterPublic = bytearray(testMaster.pubkey.serialize(compressed=False))
|
||||
|
@ -114,6 +121,7 @@ if __name__ == '__main__':
|
|||
import hashlib
|
||||
import struct
|
||||
import os
|
||||
import sys
|
||||
import binascii
|
||||
|
||||
args = get_argparser().parse_args()
|
||||
|
@ -139,9 +147,9 @@ if __name__ == '__main__':
|
|||
if args.certificate == None:
|
||||
apdu = bytearray([0xe0, 0xC0, args.key, 0x00, 0x00])
|
||||
response = dongle.exchange(apdu)
|
||||
print("Public key " + str(response[0:65]).encode('hex'))
|
||||
print("Public key " + hexstr(response[0:65]))
|
||||
m = hashlib.sha256()
|
||||
m.update(bytes("\xff")) # Endorsement role
|
||||
m.update(bytes(b"\xff")) # Endorsement role
|
||||
m.update(bytes(response[0:65]))
|
||||
digest = m.digest()
|
||||
signature = publicKey.ecdsa_deserialize(bytes(response[65:]))
|
||||
|
@ -151,7 +159,7 @@ if __name__ == '__main__':
|
|||
privateKey = PrivateKey(bytes(args.privateKey.decode('hex')))
|
||||
dataToSign = bytes(bytearray([0xfe]) + response[0:65])
|
||||
signature = privateKey.ecdsa_sign(bytes(dataToSign))
|
||||
args.certificate = str(privateKey.ecdsa_serialize(signature)).encode('hex')
|
||||
args.certificate = hexstr(privateKey.ecdsa_serialize(signature))
|
||||
|
||||
if args.certificate != None:
|
||||
certificate = bytearray.fromhex(args.certificate)
|
||||
|
|
|
@ -23,6 +23,13 @@ def get_argparser():
|
|||
parser = argparse.ArgumentParser(description="Generate a Custom CA public-private keypair and print it to console.")
|
||||
return parser
|
||||
|
||||
def hexstr(bstr):
|
||||
if (sys.version_info.major == 3):
|
||||
return binascii.hexlify(bstr).decode()
|
||||
if (sys.version_info.major == 2):
|
||||
return binascii.hexlify(bstr)
|
||||
return ""
|
||||
|
||||
if __name__ == '__main__':
|
||||
from .ecWrapper import PrivateKey
|
||||
from .comm import getDongle
|
||||
|
@ -35,6 +42,6 @@ if __name__ == '__main__':
|
|||
|
||||
get_argparser().parse_args()
|
||||
privateKey = PrivateKey()
|
||||
publicKey = binascii.hexlify(privateKey.pubkey.serialize(compressed=False))
|
||||
publicKey = hexstr(privateKey.pubkey.serialize(compressed=False))
|
||||
print("Public key : %s" % publicKey)
|
||||
print("Private key: %s" % privateKey.serialize())
|
||||
|
|
|
@ -27,9 +27,17 @@ def get_argparser():
|
|||
def auto_int(x):
|
||||
return int(x, 0)
|
||||
|
||||
def hexstr(bstr):
|
||||
if (sys.version_info.major == 3):
|
||||
return binascii.hexlify(bstr).decode()
|
||||
if (sys.version_info.major == 2):
|
||||
return binascii.hexlify(bstr)
|
||||
return ""
|
||||
|
||||
if __name__ == '__main__':
|
||||
from .hexParser import IntelHexParser
|
||||
from .hexParser import IntelHexPrinter
|
||||
import sys
|
||||
import hashlib
|
||||
import binascii
|
||||
|
||||
|
@ -48,4 +56,4 @@ if __name__ == '__main__':
|
|||
m.update(a.data)
|
||||
dataToSign = m.digest()
|
||||
|
||||
print(dataToSign.encode('hex'))
|
||||
print(hexstr(dataToSign))
|
||||
|
|
|
@ -92,7 +92,7 @@ class HexLoader:
|
|||
while (len(paddedData) % 16) != 0:
|
||||
paddedData += b'\x00'
|
||||
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
|
||||
encryptedData = cipher.encrypt(str(paddedData))
|
||||
encryptedData = cipher.encrypt(paddedData)
|
||||
self.iv = encryptedData[len(encryptedData) - 16:]
|
||||
return encryptedData
|
||||
|
||||
|
|
|
@ -30,6 +30,13 @@ device. The file must be formatted as hex, with one CAPDU per line.""")
|
|||
a random one will be generated)""")
|
||||
return parser
|
||||
|
||||
def hexstr(bstr):
|
||||
if (sys.version_info.major == 3):
|
||||
return binascii.hexlify(bstr).decode()
|
||||
if (sys.version_info.major == 2):
|
||||
return binascii.hexlify(bstr)
|
||||
return ""
|
||||
|
||||
def auto_int(x):
|
||||
return int(x, 0)
|
||||
|
||||
|
@ -40,6 +47,7 @@ if __name__ == '__main__':
|
|||
from Crypto.Cipher import AES
|
||||
import sys
|
||||
import fileinput
|
||||
import binascii
|
||||
|
||||
args = get_argparser().parse_args()
|
||||
|
||||
|
@ -104,6 +112,6 @@ if __name__ == '__main__':
|
|||
result = dongle.exchange(data[0:5])
|
||||
result = scp.decryptAES(str(result))
|
||||
if args.apdu:
|
||||
print("<= Clear " + result.encode('hex'))
|
||||
print("<= Clear " + hexstr(result))
|
||||
else:
|
||||
dongle.exchange(bytearray(data))
|
||||
|
|
|
@ -28,11 +28,19 @@ def get_argparser():
|
|||
def auto_int(x):
|
||||
return int(x, 0)
|
||||
|
||||
def hexstr(bstr):
|
||||
if (sys.version_info.major == 3):
|
||||
return binascii.hexlify(bstr).decode()
|
||||
if (sys.version_info.major == 2):
|
||||
return binascii.hexlify(bstr)
|
||||
return ""
|
||||
|
||||
if __name__ == '__main__':
|
||||
from .hexParser import IntelHexParser
|
||||
from .hexParser import IntelHexPrinter
|
||||
from .ecWrapper import PrivateKey
|
||||
import hashlib
|
||||
import sys
|
||||
import binascii
|
||||
|
||||
args = get_argparser().parse_args()
|
||||
|
@ -61,4 +69,4 @@ if __name__ == '__main__':
|
|||
# test signature before printing it
|
||||
if testMaster.pubkey.ecdsa_verify(dataToSign, signature, raw=True):
|
||||
#print("Signer's public: " + binascii.hexlify(testMasterPublic))
|
||||
print(str(testMaster.ecdsa_serialize(signature)).encode('hex'))
|
||||
print(hexstr(testMaster.ecdsa_serialize(signature)))
|
||||
|
|
Loading…
Reference in New Issue