-Propagate changes to append_padding to all files
-Adjust strip_padding to match append_padding git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@683 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
@@ -106,7 +106,7 @@ def append_padding(blocklen, data, padding_class=0x01):
|
|||||||
|
|
||||||
return data + padding
|
return data + padding
|
||||||
|
|
||||||
def strip_padding(cipherspec, data, padding_class=0x01):
|
def strip_padding(blocklen, data, padding_class=0x01):
|
||||||
"""
|
"""
|
||||||
Strip the padding of decrypted data. Returns data without padding
|
Strip the padding of decrypted data. Returns data without padding
|
||||||
"""
|
"""
|
||||||
@@ -214,7 +214,8 @@ def protect_string(string, password, cipherspec=None):
|
|||||||
cipherspec = "AES-CBC"
|
cipherspec = "AES-CBC"
|
||||||
else:
|
else:
|
||||||
pass #TODO: Sanity check for cipher
|
pass #TODO: Sanity check for cipher
|
||||||
paddedString = append_padding(cipherspec, string)
|
blocklength = get_cipher_blocklen(cipherspec)
|
||||||
|
paddedString = append_padding(blocklength, string)
|
||||||
cryptedString = cipher(True, cipherspec, key, paddedString)
|
cryptedString = cipher(True, cipherspec, key, paddedString)
|
||||||
hmac = crypto_checksum("HMAC", key, cryptedString)
|
hmac = crypto_checksum("HMAC", key, cryptedString)
|
||||||
protectedString = "$p5k2$$" + salt + "$" + cryptedString + hmac
|
protectedString = "$p5k2$$" + salt + "$" + cryptedString + hmac
|
||||||
@@ -253,7 +254,7 @@ def read_protected_string(string, password, cipherspec=None):
|
|||||||
cipherspec = "AES-CBC"
|
cipherspec = "AES-CBC"
|
||||||
decrypted = cipher(False, cipherspec, key, crypted)
|
decrypted = cipher(False, cipherspec, key, crypted)
|
||||||
|
|
||||||
return strip_padding(cipherspec, decrypted)
|
return strip_padding(get_cipher_blocklen(cipherspec), decrypted)
|
||||||
|
|
||||||
## *******************************************************************
|
## *******************************************************************
|
||||||
## * Cyberflex specific methods *
|
## * Cyberflex specific methods *
|
||||||
@@ -264,7 +265,7 @@ def calculate_MAC(session_key, message, iv=CYBERFLEX_IV):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
cipher = DES3.new(session_key, DES3.MODE_CBC, iv)
|
cipher = DES3.new(session_key, DES3.MODE_CBC, iv)
|
||||||
padded = append_padding("DES3", message, 0x01)
|
padded = append_padding(8, message, 0x01)
|
||||||
block_count = len(padded) / cipher.block_size
|
block_count = len(padded) / cipher.block_size
|
||||||
crypted = cipher.encrypt(padded)
|
crypted = cipher.encrypt(padded)
|
||||||
|
|
||||||
@@ -606,14 +607,14 @@ def test_pbkdf2():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
too_short = binascii.a2b_hex("".join("89 45 19 BF".split()))
|
too_short = binascii.a2b_hex("".join("89 45 19 BF".split()))
|
||||||
padded = append_padding("DES3-ECB", too_short)
|
padded = append_padding(8, too_short)
|
||||||
print "Padded data: " + hexdump(padded)
|
print "Padded data: " + hexdump(padded)
|
||||||
unpadded = strip_padding("DES3-ECB", padded)
|
unpadded = strip_padding(8, padded)
|
||||||
print "Without padding: " + hexdump(unpadded)
|
print "Without padding: " + hexdump(unpadded)
|
||||||
|
|
||||||
teststring = "DEADBEEFistatsyksdvhwohfwoehcowc8hw8rogfq8whv75tsgohsav8wress"
|
teststring = "DEADBEEFistatsyksdvhwohfwoehcowc8hw8rogfq8whv75tsgohsav8wress"
|
||||||
foo = append_padding("AES", teststring)
|
foo = append_padding(16, teststring)
|
||||||
assert(strip_padding("AES", foo) == teststring)
|
assert(strip_padding(16, foo) == teststring)
|
||||||
|
|
||||||
testpass = "SomeRandomPassphrase"
|
testpass = "SomeRandomPassphrase"
|
||||||
protectedString = protect_string(teststring, testpass)
|
protectedString = protect_string(teststring, testpass)
|
||||||
|
|||||||
@@ -361,7 +361,7 @@ class Security_Environment(object):
|
|||||||
"""
|
"""
|
||||||
padding_indicator = stringtoint(value[0])
|
padding_indicator = stringtoint(value[0])
|
||||||
sw, plain = self.decipher(tag, 0x80, value[1:])
|
sw, plain = self.decipher(tag, 0x80, value[1:])
|
||||||
plain = vsCrypto.strip_padding(self.ct.algorithm, plain,
|
plain = vsCrypto.strip_padding(self.ct.blocklength, plain,
|
||||||
padding_indicator)
|
padding_indicator)
|
||||||
return_data.append(plain)
|
return_data.append(plain)
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ class PassportSAM(SAM):
|
|||||||
#Receive Mutual Authenticate APDU from terminal
|
#Receive Mutual Authenticate APDU from terminal
|
||||||
#Decrypt data and check MAC
|
#Decrypt data and check MAC
|
||||||
Eifd = resp_data[:-8]
|
Eifd = resp_data[:-8]
|
||||||
padded_Eifd = vsCrypto.append_padding("DES", Eifd)
|
padded_Eifd = vsCrypto.append_padding(self.current_se.cct.blocklength, Eifd)
|
||||||
Mifd = vsCrypto.crypto_checksum("CC", self.KMac, padded_Eifd)
|
Mifd = vsCrypto.crypto_checksum("CC", self.KMac, padded_Eifd)
|
||||||
#Check the MAC
|
#Check the MAC
|
||||||
if not Mifd == resp_data[-8:]:
|
if not Mifd == resp_data[-8:]:
|
||||||
@@ -120,7 +120,7 @@ class PassportSAM(SAM):
|
|||||||
#Generate Answer
|
#Generate Answer
|
||||||
data = plain[8:16] + plain[:8] + Kicc
|
data = plain[8:16] + plain[:8] + Kicc
|
||||||
Eicc = vsCrypto.encrypt("DES3-CBC", self.KEnc, data)
|
Eicc = vsCrypto.encrypt("DES3-CBC", self.KEnc, data)
|
||||||
padded_Eicc = vsCrypto.append_padding("DES", Eicc)
|
padded_Eicc = vsCrypto.append_padding(self.current_se.cct.blocklength, Eicc)
|
||||||
Micc = vsCrypto.crypto_checksum("CC", self.KMac, padded_Eicc)
|
Micc = vsCrypto.crypto_checksum("CC", self.KMac, padded_Eicc)
|
||||||
#Derive the final keys and set the current SE
|
#Derive the final keys and set the current SE
|
||||||
KSseed = vsCrypto.operation_on_string(Kicc, Kifd, lambda a, b: a^b)
|
KSseed = vsCrypto.operation_on_string(Kicc, Kifd, lambda a, b: a^b)
|
||||||
|
|||||||
Reference in New Issue
Block a user