[vpicc] Python 3 updates and code fixing

- Use pycryptodome instead of pycrypto
- Fixed some unit tests asserts
- fixed some instances that sending message to vpcd the msg is considered a string object instead of byte array
- make TLVutils.pack work in python 3
This commit is contained in:
fabio rodrigues
2020-08-18 13:12:13 +01:00
parent 66719f3bf0
commit 475fad371a
4 changed files with 22 additions and 20 deletions

View File

@@ -684,8 +684,7 @@ class Security_Environment(object):
"""
from Crypto.PublicKey import RSA, DSA
from Crypto.Util.randpool import RandomPool
rnd = RandomPool()
from Crypto.Random import get_random_bytes
cipher = self.ct.algorithm
@@ -694,7 +693,7 @@ class Security_Environment(object):
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
if p1 & 0x01 == 0x00: # Generate key
PublicKey = c_class.generate(self.dst.keylength, rnd.get_bytes)
PublicKey = c_class.generate(self.dst.keylength)
self.dst.key = PublicKey
else:
pass # Read key
@@ -702,23 +701,23 @@ class Security_Environment(object):
# Encode keys
if cipher == "RSA":
# Public key
n = inttostring(PublicKey.__getstate__()['n'])
e = inttostring(PublicKey.__getstate__()['e'])
n = inttostring(PublicKey.n)
e = inttostring(PublicKey.e)
pk = ((0x81, len(n), n), (0x82, len(e), e))
result = bertlv_pack(pk)
# Private key
d = PublicKey.__getstate__()['d']
d = PublicKey.d
elif cipher == "DSA":
# DSAParams
p = inttostring(PublicKey.__getstate__()['p'])
q = inttostring(PublicKey.__getstate__()['q'])
g = inttostring(PublicKey.__getstate__()['g'])
p = inttostring(PublicKey.p)
q = inttostring(PublicKey.q)
g = inttostring(PublicKey.g)
# Public key
y = inttostring(PublicKey.__getstate__()['y'])
y = inttostring(PublicKey.y)
pk = ((0x81, len(p), p), (0x82, len(q), q), (0x83, len(g), g),
(0x84, len(y), y))
# Private key
x = inttostring(PublicKey.__getstate__()['x'])
x = inttostring(PublicKey.x)
# Add more algorithms here
# elif cipher = "ECDSA":
else:

View File

@@ -114,7 +114,7 @@ def tlv_find_tag(tlv_data, tag, num_results=None):
def pack(tlv_data, recalculate_length=False):
result = b""
result = []
for data in tlv_data:
tag, length, value = data[:3]
@@ -143,9 +143,9 @@ def pack(tlv_data, recalculate_length=False):
assert len(l) < 0x7f
l = inttostring(0x80 | len(l)) + l
result = result + t
result = result + l
result = result + value
result.append(t)
result.append(l)
result.append(value)
return b"".join(result)

View File

@@ -503,7 +503,10 @@ class VirtualICC(object):
def __sendToVPICC(self, msg):
""" Send a message to the vpcd """
self.sock.sendall(struct.pack('!H', len(msg)) + msg)
if isinstance(msg, str):
self.sock.sendall(struct.pack('!H', len(msg)) + msg.encode())
else:
self.sock.sendall(struct.pack('!H', len(msg)) + msg)
def __recvFromVPICC(self):
""" Receive a message from the vpcd """

View File

@@ -40,14 +40,14 @@ class TestSmartcardSAM(unittest.TestCase):
self.myCard.verify(0x00, 0x00, "3456".encode('ascii'))
except SwError as e:
pass
self.assertEquals(self.myCard.counter, ctr1 - 1)
self.assertEqual(self.myCard.counter, ctr1 - 1)
def test_internal_authenticate(self):
sw, challenge = self.myCard.get_challenge(0x00, 0x00, b"")
blocklen = vsCrypto.get_cipher_blocklen("DES3-ECB")
padded = vsCrypto.append_padding(blocklen, challenge)
sw, result_data = self.myCard.internal_authenticate(0x00, 0x00, padded)
self.assertEquals(sw, SW["NORMAL"])
self.assertEqual(sw, SW["NORMAL"])
def test_external_authenticate(self):
sw, challenge = self.myCard.get_challenge(0x00, 0x00, b"")
@@ -56,7 +56,7 @@ class TestSmartcardSAM(unittest.TestCase):
sw, result_data = self.myCard.internal_authenticate(0x00, 0x00, padded)
sw, result_data = self.myCard.external_authenticate(0x00, 0x00,
result_data)
self.assertEquals(sw, SW["NORMAL"])
self.assertEqual(sw, SW["NORMAL"])
def test_security_environment(self):
hash = self.secEnv.hash(0x90, 0x80, self.password)
@@ -74,7 +74,7 @@ class TestSmartcardSAM(unittest.TestCase):
self.secEnv.ct.algorithm = "RSA"
self.secEnv.dst.keylength = 1024
sw, pk = self.secEnv.generate_public_key_pair(0x00, 0x00, b"")
self.assertEquals(sw, SW["NORMAL"])
self.assertEqual(sw, SW["NORMAL"])
if __name__ == "__main__":