Merge pull request #8 from xenithorb/master

Fix indent errors and print function for python3 builds
This commit is contained in:
Nicolas Bacca 2017-01-09 20:40:51 +01:00 committed by GitHub
commit e73a8a6389
3 changed files with 73 additions and 65 deletions

View File

@ -21,12 +21,15 @@ from .comm import getDongle
import binascii import binascii
import argparse import argparse
def auto_int(x): def auto_int(x):
return int(x, 0) return int(x, 0)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--key", help="Reference of the endorsement key to setup (1 or 2)", type=auto_int) parser.add_argument(
parser.add_argument("--certificate", help="Certificate to store if finalizing the endorsement (hex encoded)") "--key", help="Reference of the endorsement key to setup (1 or 2)", type=auto_int)
parser.add_argument(
"--certificate", help="Certificate to store if finalizing the endorsement (hex encoded)")
parser.add_argument("--apdu", help="Display APDU log", action='store_true') parser.add_argument("--apdu", help="Display APDU log", action='store_true')
args = parser.parse_args() args = parser.parse_args()
@ -40,10 +43,10 @@ dongle = getDongle(args.apdu)
if args.certificate == None: if args.certificate == None:
apdu = bytearray([0xe0, 0xC0, args.key, 0x00, 0x00]) apdu = bytearray([0xe0, 0xC0, args.key, 0x00, 0x00])
response = dongle.exchange(apdu) response = dongle.exchange(apdu)
print "Public key " + str(response[0:65]).encode('hex') print("Public key " + str(response[0:65]).encode('hex'))
print "Certificate " + str(response[65:]).encode('hex') print("Certificate " + str(response[65:]).encode('hex'))
else: else:
certificate = bytearray.fromhex(args.certificate) certificate = bytearray.fromhex(args.certificate)
apdu = bytearray([0xe0, 0xC2, 0x00, 0x00, len(certificate)]) + certificate apdu = bytearray([0xe0, 0xC2, 0x00, 0x00, len(certificate)]) + certificate
dongle.exchange(apdu) dongle.exchange(apdu)
print "Endorsement setup finalized" print("Endorsement setup finalized")

View File

@ -44,4 +44,4 @@ for a in parser.getAreas():
m.update(a.data) m.update(a.data)
dataToSign = m.digest() dataToSign = m.digest()
print dataToSign.encode('hex') print(dataToSign.encode('hex'))

View File

@ -25,13 +25,15 @@ import argparse
import sys import sys
import fileinput import fileinput
def auto_int(x): def auto_int(x):
return int(x, 0) return int(x, 0)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("--fileName", help="Set the file name to load") parser.add_argument("--fileName", help="Set the file name to load")
parser.add_argument("--apdu", help="Display APDU log", action='store_true') parser.add_argument("--apdu", help="Display APDU log", action='store_true')
parser.add_argument("--scp", help="open secure channel to exchange apdu", action='store_true') parser.add_argument(
"--scp", help="open secure channel to exchange apdu", action='store_true')
parser.add_argument("--targetId", help="Set the chip target ID", type=auto_int) parser.add_argument("--targetId", help="Set the chip target ID", type=auto_int)
parser.add_argument("--rootPrivateKey", help="Set the root private key") parser.add_argument("--rootPrivateKey", help="Set the root private key")
@ -47,6 +49,7 @@ else:
class SCP: class SCP:
def __init__(self, dongle, targetId, rootPrivateKey): def __init__(self, dongle, targetId, rootPrivateKey):
self.key = getDeployedSecretV2(dongle, rootPrivateKey, targetId) self.key = getDeployedSecretV2(dongle, rootPrivateKey, targetId)
self.iv = b'\x00' * 16 self.iv = b'\x00' * 16
@ -67,7 +70,7 @@ class SCP:
decryptedData = cipher.decrypt(data) decryptedData = cipher.decrypt(data)
l = len(decryptedData) - 1 l = len(decryptedData) - 1
while (decryptedData[l] != chr(0x80)): while (decryptedData[l] != chr(0x80)):
l-=1 l -= 1
decryptedData = decryptedData[0:l] decryptedData = decryptedData[0:l]
self.iv = data[len(data) - 16:] self.iv = data[len(data) - 16:]
return decryptedData return decryptedData
@ -76,7 +79,8 @@ dongle = getDongle(args.apdu)
if args.scp: if args.scp:
if args.rootPrivateKey is None: if args.rootPrivateKey is None:
privateKey = PrivateKey() privateKey = PrivateKey()
publicKey = binascii.hexlify(privateKey.pubkey.serialize(compressed=False)) publicKey = binascii.hexlify(
privateKey.pubkey.serialize(compressed=False))
print("Generated random root public key : %s" % publicKey) print("Generated random root public key : %s" % publicKey)
args.rootPrivateKey = privateKey.serialize() args.rootPrivateKey = privateKey.serialize()
scp = SCP(dongle, args.targetId, bytearray.fromhex(args.rootPrivateKey)) scp = SCP(dongle, args.targetId, bytearray.fromhex(args.rootPrivateKey))
@ -87,10 +91,11 @@ for data in file:
continue continue
if args.scp: if args.scp:
data = bytearray(data) data = bytearray(data)
if data[4] > 0 and len(data)>5: if data[4] > 0 and len(data) > 5:
apduData = data[5 : 5 + data[4]] apduData = data[5: 5 + data[4]]
apduData = scp.encryptAES(str(apduData)) apduData = scp.encryptAES(str(apduData))
result = dongle.exchange(data[0:4] + bytearray([len(apduData)]) + bytearray(apduData)) result = dongle.exchange(
data[0:4] + bytearray([len(apduData)]) + bytearray(apduData))
else: else:
result = dongle.exchange(data[0:5]) result = dongle.exchange(data[0:5])
result = scp.decryptAES(str(result)) result = scp.decryptAES(str(result))