test_CTR.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. import unittest
  31. from Crypto.SelfTest.st_common import list_test_cases
  32. from Crypto.Util.py3compat import tobytes, b, unhexlify, bchr
  33. from Crypto.Cipher import AES, DES3
  34. from Crypto.Hash import SHAKE128
  35. from Crypto.Util import Counter
  36. def get_tag_random(tag, length):
  37. return SHAKE128.new(data=tobytes(tag)).read(length)
  38. class CtrTests(unittest.TestCase):
  39. key_128 = get_tag_random("key_128", 16)
  40. key_192 = get_tag_random("key_192", 24)
  41. nonce_32 = get_tag_random("nonce_32", 4)
  42. nonce_64 = get_tag_random("nonce_64", 8)
  43. ctr_64 = Counter.new(32, prefix=nonce_32)
  44. ctr_128 = Counter.new(64, prefix=nonce_64)
  45. def test_loopback_128(self):
  46. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  47. pt = get_tag_random("plaintext", 16 * 100)
  48. ct = cipher.encrypt(pt)
  49. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  50. pt2 = cipher.decrypt(ct)
  51. self.assertEqual(pt, pt2)
  52. def test_loopback_64(self):
  53. cipher = DES3.new(self.key_192, DES3.MODE_CTR, counter=self.ctr_64)
  54. pt = get_tag_random("plaintext", 8 * 100)
  55. ct = cipher.encrypt(pt)
  56. cipher = DES3.new(self.key_192, DES3.MODE_CTR, counter=self.ctr_64)
  57. pt2 = cipher.decrypt(ct)
  58. self.assertEqual(pt, pt2)
  59. def test_invalid_counter_parameter(self):
  60. # Counter object is required for ciphers with short block size
  61. self.assertRaises(TypeError, DES3.new, self.key_192, AES.MODE_CTR)
  62. # Positional arguments are not allowed (Counter must be passed as
  63. # keyword)
  64. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CTR, self.ctr_128)
  65. def test_nonce_attribute(self):
  66. # Nonce attribute is the prefix passed to Counter (DES3)
  67. cipher = DES3.new(self.key_192, DES3.MODE_CTR, counter=self.ctr_64)
  68. self.assertEqual(cipher.nonce, self.nonce_32)
  69. # Nonce attribute is the prefix passed to Counter (AES)
  70. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  71. self.assertEqual(cipher.nonce, self.nonce_64)
  72. # Nonce attribute is not defined if suffix is used in Counter
  73. counter = Counter.new(64, prefix=self.nonce_32, suffix=self.nonce_32)
  74. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
  75. self.failIf(hasattr(cipher, "nonce"))
  76. def test_nonce_parameter(self):
  77. # Nonce parameter becomes nonce attribute
  78. cipher1 = AES.new(self.key_128, AES.MODE_CTR, nonce=self.nonce_64)
  79. self.assertEqual(cipher1.nonce, self.nonce_64)
  80. counter = Counter.new(64, prefix=self.nonce_64, initial_value=0)
  81. cipher2 = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
  82. self.assertEqual(cipher1.nonce, cipher2.nonce)
  83. pt = get_tag_random("plaintext", 65536)
  84. self.assertEqual(cipher1.encrypt(pt), cipher2.encrypt(pt))
  85. # Nonce is implicitly created (for AES) when no parameters are passed
  86. nonce1 = AES.new(self.key_128, AES.MODE_CTR).nonce
  87. nonce2 = AES.new(self.key_128, AES.MODE_CTR).nonce
  88. self.assertNotEqual(nonce1, nonce2)
  89. self.assertEqual(len(nonce1), 8)
  90. # Nonce can be zero-length
  91. cipher = AES.new(self.key_128, AES.MODE_CTR, nonce=b(""))
  92. self.assertEqual(b(""), cipher.nonce)
  93. # Nonce and Counter are mutually exclusive
  94. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CTR,
  95. counter=self.ctr_128, nonce=self.nonce_64)
  96. def test_initial_value_parameter(self):
  97. # Test with nonce parameter
  98. cipher1 = AES.new(self.key_128, AES.MODE_CTR,
  99. nonce=self.nonce_64, initial_value=0xFFFF)
  100. counter = Counter.new(64, prefix=self.nonce_64, initial_value=0xFFFF)
  101. cipher2 = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
  102. pt = get_tag_random("plaintext", 65536)
  103. self.assertEqual(cipher1.encrypt(pt), cipher2.encrypt(pt))
  104. # Test without nonce parameter
  105. cipher1 = AES.new(self.key_128, AES.MODE_CTR,
  106. initial_value=0xFFFF)
  107. counter = Counter.new(64, prefix=cipher1.nonce, initial_value=0xFFFF)
  108. cipher2 = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
  109. pt = get_tag_random("plaintext", 65536)
  110. self.assertEqual(cipher1.encrypt(pt), cipher2.encrypt(pt))
  111. # Initial_value and Counter are mutually exclusive
  112. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CTR,
  113. counter=self.ctr_128, initial_value=0)
  114. def test_iv_with_matching_length(self):
  115. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CTR,
  116. counter=Counter.new(120))
  117. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CTR,
  118. counter=Counter.new(136))
  119. def test_block_size_128(self):
  120. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  121. self.assertEqual(cipher.block_size, AES.block_size)
  122. def test_block_size_64(self):
  123. cipher = DES3.new(self.key_192, DES3.MODE_CTR, counter=self.ctr_64)
  124. self.assertEqual(cipher.block_size, DES3.block_size)
  125. def test_unaligned_data_128(self):
  126. plaintexts = [ b("7777777") ] * 100
  127. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  128. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  129. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  130. self.assertEqual(b("").join(ciphertexts), cipher.encrypt(b("").join(plaintexts)))
  131. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  132. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  133. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  134. self.assertEqual(b("").join(ciphertexts), cipher.encrypt(b("").join(plaintexts)))
  135. def test_unaligned_data_64(self):
  136. plaintexts = [ b("7777777") ] * 100
  137. cipher = DES3.new(self.key_192, AES.MODE_CTR, counter=self.ctr_64)
  138. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  139. cipher = DES3.new(self.key_192, AES.MODE_CTR, counter=self.ctr_64)
  140. self.assertEqual(b("").join(ciphertexts), cipher.encrypt(b("").join(plaintexts)))
  141. cipher = DES3.new(self.key_192, AES.MODE_CTR, counter=self.ctr_64)
  142. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  143. cipher = DES3.new(self.key_192, AES.MODE_CTR, counter=self.ctr_64)
  144. self.assertEqual(b("").join(ciphertexts), cipher.encrypt(b("").join(plaintexts)))
  145. def test_unknown_parameters(self):
  146. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CTR,
  147. 7, counter=self.ctr_128)
  148. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CTR,
  149. counter=self.ctr_128, unknown=7)
  150. # But some are only known by the base cipher (e.g. use_aesni consumed by the AES module)
  151. AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128, use_aesni=False)
  152. def test_null_encryption_decryption(self):
  153. for func in "encrypt", "decrypt":
  154. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  155. result = getattr(cipher, func)(b(""))
  156. self.assertEqual(result, b(""))
  157. def test_either_encrypt_or_decrypt(self):
  158. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  159. cipher.encrypt(b(""))
  160. self.assertRaises(TypeError, cipher.decrypt, b(""))
  161. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=self.ctr_128)
  162. cipher.decrypt(b(""))
  163. self.assertRaises(TypeError, cipher.encrypt, b(""))
  164. def test_wrap_around(self):
  165. counter = Counter.new(8, prefix=bchr(9) * 15)
  166. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
  167. cipher.encrypt(bchr(9) * 16 * 255)
  168. self.assertRaises(OverflowError, cipher.encrypt, bchr(9) * 16)
  169. cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
  170. cipher.decrypt(bchr(9) * 16 * 255)
  171. self.assertRaises(OverflowError, cipher.decrypt, bchr(9) * 16)
  172. class SP800TestVectors(unittest.TestCase):
  173. """Class exercising the CTR test vectors found in Section F.3
  174. of NIST SP 800-3A"""
  175. def test_aes_128(self):
  176. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  177. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  178. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  179. 'f69f2445df4f9b17ad2b417be66c3710'
  180. ciphertext = '874d6191b620e3261bef6864990db6ce' +\
  181. '9806f66b7970fdff8617187bb9fffdff' +\
  182. '5ae4df3edbd5d35e5b4f09020db03eab' +\
  183. '1e031dda2fbe03d1792170a0f3009cee'
  184. key = '2b7e151628aed2a6abf7158809cf4f3c'
  185. counter = Counter.new(nbits=16,
  186. prefix=unhexlify('f0f1f2f3f4f5f6f7f8f9fafbfcfd'),
  187. initial_value=0xfeff)
  188. key = unhexlify(key)
  189. plaintext = unhexlify(plaintext)
  190. ciphertext = unhexlify(ciphertext)
  191. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  192. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  193. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  194. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  195. def test_aes_192(self):
  196. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  197. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  198. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  199. 'f69f2445df4f9b17ad2b417be66c3710'
  200. ciphertext = '1abc932417521ca24f2b0459fe7e6e0b' +\
  201. '090339ec0aa6faefd5ccc2c6f4ce8e94' +\
  202. '1e36b26bd1ebc670d1bd1d665620abf7' +\
  203. '4f78a7f6d29809585a97daec58c6b050'
  204. key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
  205. counter = Counter.new(nbits=16,
  206. prefix=unhexlify('f0f1f2f3f4f5f6f7f8f9fafbfcfd'),
  207. initial_value=0xfeff)
  208. key = unhexlify(key)
  209. plaintext = unhexlify(plaintext)
  210. ciphertext = unhexlify(ciphertext)
  211. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  212. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  213. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  214. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  215. def test_aes_256(self):
  216. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  217. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  218. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  219. 'f69f2445df4f9b17ad2b417be66c3710'
  220. ciphertext = '601ec313775789a5b7a7f504bbf3d228' +\
  221. 'f443e3ca4d62b59aca84e990cacaf5c5' +\
  222. '2b0930daa23de94ce87017ba2d84988d' +\
  223. 'dfc9c58db67aada613c2dd08457941a6'
  224. key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
  225. counter = Counter.new(nbits=16,
  226. prefix=unhexlify('f0f1f2f3f4f5f6f7f8f9fafbfcfd'),
  227. initial_value=0xfeff)
  228. key = unhexlify(key)
  229. plaintext = unhexlify(plaintext)
  230. ciphertext = unhexlify(ciphertext)
  231. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  232. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  233. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  234. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  235. class RFC3686TestVectors(unittest.TestCase):
  236. # Each item is a test vector with:
  237. # - plaintext
  238. # - ciphertext
  239. # - key (AES 128, 192 or 256 bits)
  240. # - counter prefix
  241. data = (
  242. ('53696e676c6520626c6f636b206d7367',
  243. 'e4095d4fb7a7b3792d6175a3261311b8',
  244. 'ae6852f8121067cc4bf7a5765577f39e',
  245. '00000030'+'0000000000000000'),
  246. ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
  247. '5104a106168a72d9790d41ee8edad388eb2e1efc46da57c8fce630df9141be28',
  248. '7e24067817fae0d743d6ce1f32539163',
  249. '006cb6dbc0543b59da48d90b'),
  250. ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223',
  251. 'c1cf48a89f2ffdd9cf4652e9efdb72d74540a42bde6d7836d59a5ceaaef3105325b2072f',
  252. '7691be035e5020a8ac6e618529f9a0dc',
  253. '00e0017b27777f3f4a1786f0'),
  254. ('53696e676c6520626c6f636b206d7367',
  255. '4b55384fe259c9c84e7935a003cbe928',
  256. '16af5b145fc9f579c175f93e3bfb0eed863d06ccfdb78515',
  257. '0000004836733c147d6d93cb'),
  258. ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
  259. '453243fc609b23327edfaafa7131cd9f8490701c5ad4a79cfc1fe0ff42f4fb00',
  260. '7c5cb2401b3dc33c19e7340819e0f69c678c3db8e6f6a91a',
  261. '0096b03b020c6eadc2cb500d'),
  262. ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223',
  263. '96893fc55e5c722f540b7dd1ddf7e758d288bc95c69165884536c811662f2188abee0935',
  264. '02bf391ee8ecb159b959617b0965279bf59b60a786d3e0fe',
  265. '0007bdfd5cbd60278dcc0912'),
  266. ('53696e676c6520626c6f636b206d7367',
  267. '145ad01dbf824ec7560863dc71e3e0c0',
  268. '776beff2851db06f4c8a0542c8696f6c6a81af1eec96b4d37fc1d689e6c1c104',
  269. '00000060db5672c97aa8f0b2'),
  270. ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
  271. 'f05e231b3894612c49ee000b804eb2a9b8306b508f839d6a5530831d9344af1c',
  272. 'f6d66d6bd52d59bb0796365879eff886c66dd51a5b6a99744b50590c87a23884',
  273. '00faac24c1585ef15a43d875'),
  274. ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223',
  275. 'eb6c52821d0bbbf7ce7594462aca4faab407df866569fd07f48cc0b583d6071f1ec0e6b8',
  276. 'ff7a617ce69148e4f1726e2f43581de2aa62d9f805532edff1eed687fb54153d',
  277. '001cc5b751a51d70a1c11148')
  278. )
  279. bindata = []
  280. for tv in data:
  281. bindata.append([unhexlify(x) for x in tv])
  282. def runTest(self):
  283. for pt, ct, key, prefix in self.bindata:
  284. counter = Counter.new(32, prefix=prefix)
  285. cipher = AES.new(key, AES.MODE_CTR, counter=counter)
  286. result = cipher.encrypt(pt)
  287. self.assertEqual(ct, result)
  288. def get_tests(config={}):
  289. tests = []
  290. tests += list_test_cases(CtrTests)
  291. tests += list_test_cases(SP800TestVectors)
  292. tests += [ RFC3686TestVectors() ]
  293. return tests
  294. if __name__ == '__main__':
  295. suite = lambda: unittest.TestSuite(get_tests())
  296. unittest.main(defaultTest='suite')