Source code for symcrypto_test
import unittest
from charm.toolbox.symcrypto import SymmetricCryptoAbstraction,AuthenticatedCryptoAbstraction, MessageAuthenticator
from charm.toolbox.pairinggroup import PairingGroup,GT
from charm.core.math.pairing import hashPair as sha2
[docs]class SymmetricCryptoAbstractionTest(unittest.TestCase):
[docs] def testAESCBC(self):
self.MsgtestAESCBC(b"hello world")
[docs] def testAESCBCLong(self):
self.MsgtestAESCBC(b"Lots of people working in cryptography have no deep \
concern with real application issues. They are trying to discover things \
clever enough to write papers about -- Whitfield Diffie.")
[docs] def testAESCBC_Seperate(self):
self.MsgTestAESCBCSeperate(b"Lots of people working in cryptography have no deep \
concern with real application issues. They are trying to discover things \
clever enough to write papers about -- Whitfield Diffie.")
[docs] def MsgtestAESCBC(self,msg):
groupObj = PairingGroup('SS512')
a = SymmetricCryptoAbstraction(sha2(groupObj.random(GT)))
ct = a.encrypt(msg)
dmsg = a.decrypt(ct);
assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
[docs] def MsgTestAESCBCSeperate(self,msg):
groupObj = PairingGroup('SS512')
ran = groupObj.random(GT)
a = SymmetricCryptoAbstraction(sha2(ran))
ct = a.encrypt(msg)
b = SymmetricCryptoAbstraction(sha2(ran))
dmsg = b.decrypt(ct);
assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
[docs]class AuthenticatedCryptoAbstractionTest(unittest.TestCase):
[docs] def testAESCBC(self):
self.MsgtestAESCBC(b"hello world")
[docs] def testAESCBCLong(self):
self.MsgtestAESCBC(b"Lots of people working in cryptography have no deep \
concern with real application issues. They are trying to discover things \
clever enough to write papers about -- Whitfield Diffie.")
[docs] def testAESCBC_Seperate(self):
self.MsgTestAESCBCSeperate(b"Lots of people working in cryptography have no deep \
concern with real application issues. They are trying to discover things \
clever enough to write papers about -- Whitfield Diffie.")
[docs] def MsgtestAESCBC(self,msg):
groupObj = PairingGroup('SS512')
a = AuthenticatedCryptoAbstraction(sha2(groupObj.random(GT)))
ct = a.encrypt(msg)
dmsg = a.decrypt(ct);
assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
[docs] def MsgTestAESCBCSeperate(self,msg):
groupObj = PairingGroup('SS512')
ran = groupObj.random(GT)
a = AuthenticatedCryptoAbstraction(sha2(ran))
ct = a.encrypt(msg)
b = AuthenticatedCryptoAbstraction(sha2(ran))
dmsg = b.decrypt(ct);
assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
[docs]class MessageAuthenticatorTest(unittest.TestCase):
[docs] def testSelfVerify(self):
key = sha2(PairingGroup('SS512').random(GT))
m = MessageAuthenticator(key)
a = m.mac('hello world')
assert m.verify(a), "expected message to verify";
[docs] def testSeperateVerify(self):
key = sha2(PairingGroup('SS512').random(GT))
m = MessageAuthenticator(key)
a = m.mac('hello world')
m1 = MessageAuthenticator(key)
assert m1.verify(a), "expected message to verify";
[docs] def testTamperData(self):
key = sha2(PairingGroup('SS512').random(GT))
m = MessageAuthenticator(key)
a = m.mac('hello world')
m1 = MessageAuthenticator(key)
a["msg"]= "tampered"
assert not m1.verify(a), "expected message to verify";
[docs] def testTamperMac(self):
key = sha2(PairingGroup('SS512').random(GT))
m = MessageAuthenticator(key)
a = m.mac('hello world')
m1 = MessageAuthenticator(key)
a["digest"]= "tampered"
assert not m1.verify(a), "expected message to verify";
[docs] def testTamperAlg(self):
key = sha2(PairingGroup('SS512').random(GT))
m = MessageAuthenticator(key)
a = m.mac('hello world')
m1 = MessageAuthenticator(key)
m1._algorithm = "alg" # bypassing the algorithm check to verify the mac is over the alg + data
a["alg"]= "alg"
assert not m1.verify(a), "expected message to verify";
if __name__ == "__main__":
unittest.main()