diff options
author | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-10-10 12:56:25 -0400 |
---|---|---|
committer | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-10-12 14:41:01 -0400 |
commit | c10e7eca40f0fced06db293d15f041a1b5309a18 (patch) | |
tree | 8ee103a38300a182530e3be0c731ddf3331e909a | |
parent | 3b4338d285b9d12caf8863c5cb510264661f2676 (diff) | |
download | pycrypto-c10e7eca40f0fced06db293d15f041a1b5309a18.tar.gz |
SelfTest: Add AES-CTR tests
-rw-r--r-- | lib/Crypto/SelfTest/Cipher/common.py | 19 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/Cipher/test_AES.py | 51 |
2 files changed, 66 insertions, 4 deletions
diff --git a/lib/Crypto/SelfTest/Cipher/common.py b/lib/Crypto/SelfTest/Cipher/common.py index 0771b8b..33c6fdf 100644 --- a/lib/Crypto/SelfTest/Cipher/common.py +++ b/lib/Crypto/SelfTest/Cipher/common.py @@ -81,15 +81,28 @@ class CipherSelfTest(unittest.TestCase): return self.description def _new(self): + params = self.extra_params.copy() + + # Handle CTR mode parameters. By default, we use Counter.new(self.module.block_size) + if hasattr(self.module, "MODE_CTR") and self.mode == self.module.MODE_CTR: + from Crypto.Util import Counter + ctr_class = _extract(params, 'ctr_class', Counter.new) + ctr_params = _extract(params, 'ctr_params', {}).copy() + if ctr_params.has_key('prefix'): ctr_params['prefix'] = a2b_hex(ctr_params['prefix']) + if ctr_params.has_key('suffix'): ctr_params['suffix'] = a2b_hex(ctr_params['suffix']) + if not ctr_params.has_key('nbits'): + ctr_params['nbits'] = 8*(self.module.block_size - len(ctr_params.get('prefix', '')) - len(ctr_params.get('suffix', ''))) + params['counter'] = ctr_class(**ctr_params) + if self.mode is None: # Stream cipher - return self.module.new(a2b_hex(self.key), **self.extra_params) + return self.module.new(a2b_hex(self.key), **params) elif self.iv is None: # Block cipher without iv - return self.module.new(a2b_hex(self.key), self.mode, **self.extra_params) + return self.module.new(a2b_hex(self.key), self.mode, **params) else: # Block cipher with iv - return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.iv), **self.extra_params) + return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.iv), **params) def runTest(self): plaintext = a2b_hex(self.plaintext) diff --git a/lib/Crypto/SelfTest/Cipher/test_AES.py b/lib/Crypto/SelfTest/Cipher/test_AES.py index 70959e7..c96dd10 100644 --- a/lib/Crypto/SelfTest/Cipher/test_AES.py +++ b/lib/Crypto/SelfTest/Cipher/test_AES.py @@ -26,7 +26,9 @@ __revision__ = "$Id$" -# This is a list of (plaintext, ciphertext, key) tuples. +from common import dict # For compatibility with Python 2.1 and 2.2 + +# This is a list of (plaintext, ciphertext, key[, description[, params]]) tuples. test_data = [ # FIPS PUB 197 test vectors # http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf @@ -1075,6 +1077,53 @@ test_data = [ ('050407067477767956575051221d1c1f', '7444527095838fe080fc2bcdd30847eb', '50515253555657585a5b5c5d5f60616264656667696a6b6c6e6f707173747576', 'ecb-tbl-256: I=128'), + + # RFC 3686 test vectors + # This is a list of (plaintext, ciphertext, key[, description[, params]]) tuples. + ('53696e676c6520626c6f636b206d7367', 'e4095d4fb7a7b3792d6175a3261311b8', + 'ae6852f8121067cc4bf7a5765577f39e', + 'RFC 3686 Test Vector #1: Encrypting 16 octets using AES-CTR with 128-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="00000030"+"0000000000000000"))), + ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', + '5104a106168a72d9790d41ee8edad388eb2e1efc46da57c8fce630df9141be28', + '7e24067817fae0d743d6ce1f32539163', + 'RFC 3686 Test Vector #2: Encrypting 32 octets using AES-CTR with 128-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="006cb6db"+"c0543b59da48d90b"))), + ('000102030405060708090a0b0c0d0e0f'+'101112131415161718191a1b1c1d1e1f'+'20212223', + 'c1cf48a89f2ffdd9cf4652e9efdb72d7'+'4540a42bde6d7836d59a5ceaaef31053'+'25b2072f', + '7691be035e5020a8ac6e618529f9a0dc', + 'RFC 3686 Test Vector #3: Encrypting 36 octets using AES-CTR with 128-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="00e0017b"+"27777f3f4a1786f0"))), + ('53696e676c6520626c6f636b206d7367', + '4b55384fe259c9c84e7935a003cbe928', + '16af5b145fc9f579c175f93e3bfb0eed'+'863d06ccfdb78515', + 'RFC 3686 Test Vector #4: Encrypting 16 octets using AES-CTR with 192-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="00000048"+"36733c147d6d93cb"))), + ('000102030405060708090a0b0c0d0e0f'+'101112131415161718191a1b1c1d1e1f', + '453243fc609b23327edfaafa7131cd9f'+'8490701c5ad4a79cfc1fe0ff42f4fb00', + '7c5cb2401b3dc33c19e7340819e0f69c'+'678c3db8e6f6a91a', + 'RFC 3686 Test Vector #5: Encrypting 32 octets using AES-CTR with 192-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="0096b03b"+"020c6eadc2cb500d"))), + ('000102030405060708090a0b0c0d0e0f'+'101112131415161718191a1b1c1d1e1f'+'20212223', + '96893fc55e5c722f540b7dd1ddf7e758'+'d288bc95c69165884536c811662f2188'+'abee0935', + '02bf391ee8ecb159b959617b0965279b'+'f59b60a786d3e0fe', + 'RFC 3686 Test Vector #6: Encrypting 36 octets using AES-CTR with 192-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="0007bdfd"+"5cbd60278dcc0912"))), + ('53696e676c6520626c6f636b206d7367', + '145ad01dbf824ec7560863dc71e3e0c0', + '776beff2851db06f4c8a0542c8696f6c'+'6a81af1eec96b4d37fc1d689e6c1c104', + 'RFC 3686 Test Vector #7: Encrypting 16 octets using AES-CTR with 256-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="00000060"+"db5672c97aa8f0b2"))), + ('000102030405060708090a0b0c0d0e0f'+'101112131415161718191a1b1c1d1e1f', + 'f05e231b3894612c49ee000b804eb2a9'+'b8306b508f839d6a5530831d9344af1c', + 'f6d66d6bd52d59bb0796365879eff886'+'c66dd51a5b6a99744b50590c87a23884', + 'RFC 3686 Test Vector #8: Encrypting 32 octets using AES-CTR with 256-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="00faac24"+"c1585ef15a43d875"))), + ('000102030405060708090a0b0c0d0e0f'+'101112131415161718191a1b1c1d1e1f'+'20212223', + 'eb6c52821d0bbbf7ce7594462aca4faa'+'b407df866569fd07f48cc0b583d6071f'+'1ec0e6b8', + 'ff7a617ce69148e4f1726e2f43581de2'+'aa62d9f805532edff1eed687fb54153d', + 'RFC 3686 Test Vector #9: Encrypting 36 octets using AES-CTR with 256-bit key', + dict(mode='CTR', ctr_params=dict(nbits=32, prefix="001cc5b7"+"51a51d70a1c11148"))), ] def get_tests(config={}): |