diff options
author | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-10-10 16:58:12 -0400 |
---|---|---|
committer | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-10-12 14:41:13 -0400 |
commit | c8c14100548e92cf1d35db56371b4210593b4c93 (patch) | |
tree | d5a9bf96ba8f7b9781932150da6cf2201eb44d03 | |
parent | c10e7eca40f0fced06db293d15f041a1b5309a18 (diff) | |
download | pycrypto-c8c14100548e92cf1d35db56371b4210593b4c93.tar.gz |
SelfTest: Test stream cipher (and MODE_CTR) API behaviour
-rw-r--r-- | lib/Crypto/SelfTest/Cipher/common.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/Crypto/SelfTest/Cipher/common.py b/lib/Crypto/SelfTest/Cipher/common.py index 33c6fdf..b795fb1 100644 --- a/lib/Crypto/SelfTest/Cipher/common.py +++ b/lib/Crypto/SelfTest/Cipher/common.py @@ -118,6 +118,30 @@ class CipherSelfTest(unittest.TestCase): self.assertEqual(self.plaintext, pt1) # decrypt self.assertEqual(self.plaintext, pt2) # decrypt (second time) +class CipherStreamingSelfTest(CipherSelfTest): + + def runTest(self): + plaintext = a2b_hex(self.plaintext) + ciphertext = a2b_hex(self.ciphertext) + + # The cipher should work like a stream cipher + + # Test counter mode encryption, 3 bytes at a time + ct3 = [] + cipher = self._new() + for i in range(0, len(plaintext), 3): + ct3.append(cipher.encrypt(plaintext[i:i+3])) + ct3 = b2a_hex("".join(ct3)) + self.assertEqual(self.ciphertext, ct3) # encryption (3 bytes at a time) + + # Test counter mode decryption, 3 bytes at a time + pt3 = [] + cipher = self._new() + for i in range(0, len(ciphertext), 3): + pt3.append(cipher.encrypt(ciphertext[i:i+3])) + pt3 = b2a_hex("".join(pt3)) + self.assertEqual(self.plaintext, pt3) # decryption (3 bytes at a time) + class CTRSegfaultTest(unittest.TestCase): def __init__(self, module, params): @@ -187,6 +211,8 @@ def make_block_tests(module, module_name, test_data): CFBSegmentSizeTest(module, params), ] extra_tests_added = 1 + if p_mode == 'CTR': + tests.append(CipherStreamingSelfTest(module, params)) return tests def make_stream_tests(module, module_name, test_data): @@ -224,6 +250,7 @@ def make_stream_tests(module, module_name, test_data): # Add the test to the test suite tests.append(CipherSelfTest(module, params)) + tests.append(CipherStreamingSelfTest(module, params)) return tests # vim:set ts=4 sw=4 sts=4 expandtab: |