[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:
@@ -684,8 +684,7 @@ class Security_Environment(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from Crypto.PublicKey import RSA, DSA
|
from Crypto.PublicKey import RSA, DSA
|
||||||
from Crypto.Util.randpool import RandomPool
|
from Crypto.Random import get_random_bytes
|
||||||
rnd = RandomPool()
|
|
||||||
|
|
||||||
cipher = self.ct.algorithm
|
cipher = self.ct.algorithm
|
||||||
|
|
||||||
@@ -694,7 +693,7 @@ class Security_Environment(object):
|
|||||||
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
|
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
|
||||||
|
|
||||||
if p1 & 0x01 == 0x00: # Generate key
|
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
|
self.dst.key = PublicKey
|
||||||
else:
|
else:
|
||||||
pass # Read key
|
pass # Read key
|
||||||
@@ -702,23 +701,23 @@ class Security_Environment(object):
|
|||||||
# Encode keys
|
# Encode keys
|
||||||
if cipher == "RSA":
|
if cipher == "RSA":
|
||||||
# Public key
|
# Public key
|
||||||
n = inttostring(PublicKey.__getstate__()['n'])
|
n = inttostring(PublicKey.n)
|
||||||
e = inttostring(PublicKey.__getstate__()['e'])
|
e = inttostring(PublicKey.e)
|
||||||
pk = ((0x81, len(n), n), (0x82, len(e), e))
|
pk = ((0x81, len(n), n), (0x82, len(e), e))
|
||||||
result = bertlv_pack(pk)
|
result = bertlv_pack(pk)
|
||||||
# Private key
|
# Private key
|
||||||
d = PublicKey.__getstate__()['d']
|
d = PublicKey.d
|
||||||
elif cipher == "DSA":
|
elif cipher == "DSA":
|
||||||
# DSAParams
|
# DSAParams
|
||||||
p = inttostring(PublicKey.__getstate__()['p'])
|
p = inttostring(PublicKey.p)
|
||||||
q = inttostring(PublicKey.__getstate__()['q'])
|
q = inttostring(PublicKey.q)
|
||||||
g = inttostring(PublicKey.__getstate__()['g'])
|
g = inttostring(PublicKey.g)
|
||||||
# Public key
|
# Public key
|
||||||
y = inttostring(PublicKey.__getstate__()['y'])
|
y = inttostring(PublicKey.y)
|
||||||
pk = ((0x81, len(p), p), (0x82, len(q), q), (0x83, len(g), g),
|
pk = ((0x81, len(p), p), (0x82, len(q), q), (0x83, len(g), g),
|
||||||
(0x84, len(y), y))
|
(0x84, len(y), y))
|
||||||
# Private key
|
# Private key
|
||||||
x = inttostring(PublicKey.__getstate__()['x'])
|
x = inttostring(PublicKey.x)
|
||||||
# Add more algorithms here
|
# Add more algorithms here
|
||||||
# elif cipher = "ECDSA":
|
# elif cipher = "ECDSA":
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ def tlv_find_tag(tlv_data, tag, num_results=None):
|
|||||||
|
|
||||||
|
|
||||||
def pack(tlv_data, recalculate_length=False):
|
def pack(tlv_data, recalculate_length=False):
|
||||||
result = b""
|
result = []
|
||||||
|
|
||||||
for data in tlv_data:
|
for data in tlv_data:
|
||||||
tag, length, value = data[:3]
|
tag, length, value = data[:3]
|
||||||
@@ -143,9 +143,9 @@ def pack(tlv_data, recalculate_length=False):
|
|||||||
assert len(l) < 0x7f
|
assert len(l) < 0x7f
|
||||||
l = inttostring(0x80 | len(l)) + l
|
l = inttostring(0x80 | len(l)) + l
|
||||||
|
|
||||||
result = result + t
|
result.append(t)
|
||||||
result = result + l
|
result.append(l)
|
||||||
result = result + value
|
result.append(value)
|
||||||
|
|
||||||
return b"".join(result)
|
return b"".join(result)
|
||||||
|
|
||||||
|
|||||||
@@ -503,7 +503,10 @@ class VirtualICC(object):
|
|||||||
|
|
||||||
def __sendToVPICC(self, msg):
|
def __sendToVPICC(self, msg):
|
||||||
""" Send a message to the vpcd """
|
""" 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):
|
def __recvFromVPICC(self):
|
||||||
""" Receive a message from the vpcd """
|
""" Receive a message from the vpcd """
|
||||||
|
|||||||
@@ -40,14 +40,14 @@ class TestSmartcardSAM(unittest.TestCase):
|
|||||||
self.myCard.verify(0x00, 0x00, "3456".encode('ascii'))
|
self.myCard.verify(0x00, 0x00, "3456".encode('ascii'))
|
||||||
except SwError as e:
|
except SwError as e:
|
||||||
pass
|
pass
|
||||||
self.assertEquals(self.myCard.counter, ctr1 - 1)
|
self.assertEqual(self.myCard.counter, ctr1 - 1)
|
||||||
|
|
||||||
def test_internal_authenticate(self):
|
def test_internal_authenticate(self):
|
||||||
sw, challenge = self.myCard.get_challenge(0x00, 0x00, b"")
|
sw, challenge = self.myCard.get_challenge(0x00, 0x00, b"")
|
||||||
blocklen = vsCrypto.get_cipher_blocklen("DES3-ECB")
|
blocklen = vsCrypto.get_cipher_blocklen("DES3-ECB")
|
||||||
padded = vsCrypto.append_padding(blocklen, challenge)
|
padded = vsCrypto.append_padding(blocklen, challenge)
|
||||||
sw, result_data = self.myCard.internal_authenticate(0x00, 0x00, padded)
|
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):
|
def test_external_authenticate(self):
|
||||||
sw, challenge = self.myCard.get_challenge(0x00, 0x00, b"")
|
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.internal_authenticate(0x00, 0x00, padded)
|
||||||
sw, result_data = self.myCard.external_authenticate(0x00, 0x00,
|
sw, result_data = self.myCard.external_authenticate(0x00, 0x00,
|
||||||
result_data)
|
result_data)
|
||||||
self.assertEquals(sw, SW["NORMAL"])
|
self.assertEqual(sw, SW["NORMAL"])
|
||||||
|
|
||||||
def test_security_environment(self):
|
def test_security_environment(self):
|
||||||
hash = self.secEnv.hash(0x90, 0x80, self.password)
|
hash = self.secEnv.hash(0x90, 0x80, self.password)
|
||||||
@@ -74,7 +74,7 @@ class TestSmartcardSAM(unittest.TestCase):
|
|||||||
self.secEnv.ct.algorithm = "RSA"
|
self.secEnv.ct.algorithm = "RSA"
|
||||||
self.secEnv.dst.keylength = 1024
|
self.secEnv.dst.keylength = 1024
|
||||||
sw, pk = self.secEnv.generate_public_key_pair(0x00, 0x00, b"")
|
sw, pk = self.secEnv.generate_public_key_pair(0x00, 0x00, b"")
|
||||||
self.assertEquals(sw, SW["NORMAL"])
|
self.assertEqual(sw, SW["NORMAL"])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user