test_Poly1305.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #
  2. # SelfTest/Hash/test_Poly1305.py: Self-test for the Poly1305 module
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2018, Helder Eijs <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. """Self-test suite for Crypto.Hash._Poly1305"""
  34. import json
  35. import unittest
  36. from binascii import unhexlify, hexlify
  37. from .common import make_mac_tests
  38. from tls.Crypto.SelfTest.st_common import list_test_cases
  39. from tls.Crypto.Hash import Poly1305
  40. from tls.Crypto.Cipher import AES, ChaCha20
  41. from tls.Crypto.Util.py3compat import tobytes
  42. from tls.Crypto.Util.strxor import strxor_c
  43. # This is a list of (r+s keypair, data, result, description, keywords) tuples.
  44. test_data_basic = [
  45. (
  46. "85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b",
  47. hexlify(b"Cryptographic Forum Research Group").decode(),
  48. "a8061dc1305136c6c22b8baf0c0127a9",
  49. "RFC7539"
  50. ),
  51. (
  52. "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
  53. "0000000000000000000000000000000000000000000000000000000000000000",
  54. "49ec78090e481ec6c26b33b91ccc0307",
  55. "https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-00#section-7 A",
  56. ),
  57. (
  58. "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
  59. "48656c6c6f20776f726c6421",
  60. "a6f745008f81c916a20dcc74eef2b2f0",
  61. "https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-00#section-7 B",
  62. ),
  63. (
  64. "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
  65. "",
  66. "6b657920666f7220506f6c7931333035",
  67. "Generated with pure Python",
  68. ),
  69. (
  70. "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
  71. "FF",
  72. "f7e4e0ef4c46d106219da3d1bdaeb3ff",
  73. "Generated with pure Python",
  74. ),
  75. (
  76. "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
  77. "FF00",
  78. "7471eceeb22988fc936da1d6e838b70e",
  79. "Generated with pure Python",
  80. ),
  81. (
  82. "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
  83. "AA" * 17,
  84. "32590bc07cb2afaccca3f67f122975fe",
  85. "Generated with pure Python",
  86. ),
  87. (
  88. "00" * 32,
  89. "00" * 64,
  90. "00" * 16,
  91. "RFC7539 A.3 #1",
  92. ),
  93. (
  94. "0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e",
  95. hexlify(
  96. b"Any submission t"
  97. b"o the IETF inten"
  98. b"ded by the Contr"
  99. b"ibutor for publi"
  100. b"cation as all or"
  101. b" part of an IETF"
  102. b" Internet-Draft "
  103. b"or RFC and any s"
  104. b"tatement made wi"
  105. b"thin the context"
  106. b" of an IETF acti"
  107. b"vity is consider"
  108. b"ed an \"IETF Cont"
  109. b"ribution\". Such "
  110. b"statements inclu"
  111. b"de oral statemen"
  112. b"ts in IETF sessi"
  113. b"ons, as well as "
  114. b"written and elec"
  115. b"tronic communica"
  116. b"tions made at an"
  117. b"y time or place,"
  118. b" which are addre"
  119. b"ssed to").decode(),
  120. "36e5f6b5c5e06070f0efca96227a863e",
  121. "RFC7539 A.3 #2",
  122. ),
  123. (
  124. "36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000",
  125. hexlify(
  126. b"Any submission t"
  127. b"o the IETF inten"
  128. b"ded by the Contr"
  129. b"ibutor for publi"
  130. b"cation as all or"
  131. b" part of an IETF"
  132. b" Internet-Draft "
  133. b"or RFC and any s"
  134. b"tatement made wi"
  135. b"thin the context"
  136. b" of an IETF acti"
  137. b"vity is consider"
  138. b"ed an \"IETF Cont"
  139. b"ribution\". Such "
  140. b"statements inclu"
  141. b"de oral statemen"
  142. b"ts in IETF sessi"
  143. b"ons, as well as "
  144. b"written and elec"
  145. b"tronic communica"
  146. b"tions made at an"
  147. b"y time or place,"
  148. b" which are addre"
  149. b"ssed to").decode(),
  150. "f3477e7cd95417af89a6b8794c310cf0",
  151. "RFC7539 A.3 #3",
  152. ),
  153. (
  154. "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",
  155. "2754776173206272696c6c69672c2061"
  156. "6e642074686520736c6974687920746f"
  157. "7665730a446964206779726520616e64"
  158. "2067696d626c6520696e207468652077"
  159. "6162653a0a416c6c206d696d73792077"
  160. "6572652074686520626f726f676f7665"
  161. "732c0a416e6420746865206d6f6d6520"
  162. "7261746873206f757467726162652e",
  163. "4541669a7eaaee61e708dc7cbcc5eb62",
  164. "RFC7539 A.3 #4",
  165. ),
  166. (
  167. "02" + "00" * 31,
  168. "FF" * 16,
  169. "03" + "00" * 15,
  170. "RFC7539 A.3 #5",
  171. ),
  172. (
  173. "02" + "00" * 15 + "FF" * 16,
  174. "02" + "00" * 15,
  175. "03" + "00" * 15,
  176. "RFC7539 A.3 #6",
  177. ),
  178. (
  179. "01" + "00" * 31,
  180. "FF" * 16 + "F0" + "FF" * 15 + "11" + "00" * 15,
  181. "05" + "00" * 15,
  182. "RFC7539 A.3 #7",
  183. ),
  184. (
  185. "01" + "00" * 31,
  186. "FF" * 16 + "FB" + "FE" * 15 + "01" * 16,
  187. "00" * 16,
  188. "RFC7539 A.3 #8",
  189. ),
  190. (
  191. "02" + "00" * 31,
  192. "FD" + "FF" * 15,
  193. "FA" + "FF" * 15,
  194. "RFC7539 A.3 #9",
  195. ),
  196. (
  197. "01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00"
  198. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
  199. "E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00"
  200. "33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00"
  201. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
  202. "01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
  203. "14 00 00 00 00 00 00 00 55 00 00 00 00 00 00 00",
  204. "RFC7539 A.3 #10",
  205. ),
  206. (
  207. "01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00"
  208. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
  209. "E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00"
  210. "33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00"
  211. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
  212. "13" + "00" * 15,
  213. "RFC7539 A.3 #11",
  214. ),
  215. ]
  216. # This is a list of (key(k+r), data, result, description, keywords) tuples.
  217. test_data_aes = [
  218. (
  219. "ec074c835580741701425b623235add6851fc40c3467ac0be05cc20404f3f700",
  220. "f3f6",
  221. "f4c633c3044fc145f84f335cb81953de",
  222. "http://cr.yp.to/mac/poly1305-20050329.pdf",
  223. { 'cipher':AES, 'nonce':unhexlify("fb447350c4e868c52ac3275cf9d4327e") }
  224. ),
  225. (
  226. "75deaa25c09f208e1dc4ce6b5cad3fbfa0f3080000f46400d0c7e9076c834403",
  227. "",
  228. "dd3fab2251f11ac759f0887129cc2ee7",
  229. "http://cr.yp.to/mac/poly1305-20050329.pdf",
  230. { 'cipher':AES, 'nonce':unhexlify("61ee09218d29b0aaed7e154a2c5509cc") }
  231. ),
  232. (
  233. "6acb5f61a7176dd320c5c1eb2edcdc7448443d0bb0d21109c89a100b5ce2c208",
  234. "663cea190ffb83d89593f3f476b6bc24"
  235. "d7e679107ea26adb8caf6652d0656136",
  236. "0ee1c16bb73f0f4fd19881753c01cdbe",
  237. "http://cr.yp.to/mac/poly1305-20050329.pdf",
  238. { 'cipher':AES, 'nonce':unhexlify("ae212a55399729595dea458bc621ff0e") }
  239. ),
  240. (
  241. "e1a5668a4d5b66a5f68cc5424ed5982d12976a08c4426d0ce8a82407c4f48207",
  242. "ab0812724a7f1e342742cbed374d94d1"
  243. "36c6b8795d45b3819830f2c04491faf0"
  244. "990c62e48b8018b2c3e4a0fa3134cb67"
  245. "fa83e158c994d961c4cb21095c1bf9",
  246. "5154ad0d2cb26e01274fc51148491f1b",
  247. "http://cr.yp.to/mac/poly1305-20050329.pdf",
  248. { 'cipher':AES, 'nonce':unhexlify("9ae831e743978d3a23527c7128149e3a") }
  249. ),
  250. ]
  251. test_data_chacha20 = [
  252. (
  253. "00" * 32,
  254. "FF" * 15,
  255. "13cc5bbadc36b03a5163928f0bcb65aa",
  256. "RFC7539 A.4 #1",
  257. { 'cipher':ChaCha20, 'nonce':unhexlify("00" * 12) }
  258. ),
  259. (
  260. "00" * 31 + "01",
  261. "FF" * 15,
  262. "0baf33c1d6df211bdd50a6767e98e00a",
  263. "RFC7539 A.4 #2",
  264. { 'cipher':ChaCha20, 'nonce':unhexlify("00" * 11 + "02") }
  265. ),
  266. (
  267. "1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0"
  268. "47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0",
  269. "FF" * 15,
  270. "e8b4c6db226cd8939e65e02eebf834ce",
  271. "RFC7539 A.4 #3",
  272. { 'cipher':ChaCha20, 'nonce':unhexlify("00" * 11 + "02") }
  273. ),
  274. (
  275. "1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0"
  276. "47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0",
  277. "f3 33 88 86 00 00 00 00 00 00 4e 91 00 00 00 00"
  278. "64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd"
  279. "5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2"
  280. "4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0"
  281. "bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf"
  282. "33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81"
  283. "14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55"
  284. "97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38"
  285. "36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4"
  286. "b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9"
  287. "90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e"
  288. "af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a"
  289. "0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a"
  290. "0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e"
  291. "ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10"
  292. "49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30"
  293. "30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29"
  294. "a6 ad 5c b4 02 2b 02 70 9b 00 00 00 00 00 00 00"
  295. "0c 00 00 00 00 00 00 00 09 01 00 00 00 00 00 00",
  296. "ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38",
  297. "RFC7539 A.5",
  298. { 'cipher':ChaCha20, 'nonce':unhexlify("000000000102030405060708") }
  299. ),
  300. ]
  301. class Poly1305Test_AES(unittest.TestCase):
  302. key = b'\x11' * 32
  303. def test_new_positive(self):
  304. data = b'r' * 100
  305. h1 = Poly1305.new(key=self.key, cipher=AES)
  306. self.assertEqual(h1.digest_size, 16)
  307. self.assertEqual(len(h1.nonce), 16)
  308. d1 = h1.update(data).digest()
  309. self.assertEqual(len(d1), 16)
  310. h2 = Poly1305.new(key=self.key, nonce=h1.nonce, data=data, cipher=AES)
  311. d2 = h2.digest()
  312. self.assertEqual(h1.nonce, h2.nonce)
  313. self.assertEqual(d1, d2)
  314. def test_new_negative(self):
  315. from tls.Crypto.Cipher import DES3
  316. self.assertRaises(ValueError, Poly1305.new, key=self.key[:31], cipher=AES)
  317. self.assertRaises(ValueError, Poly1305.new, key=self.key, cipher=DES3)
  318. self.assertRaises(ValueError, Poly1305.new, key=self.key, nonce=b'1' * 15, cipher=AES)
  319. self.assertRaises(TypeError, Poly1305.new, key=u"2" * 32, cipher=AES)
  320. self.assertRaises(TypeError, Poly1305.new, key=self.key, data=u"2" * 100, cipher=AES)
  321. def test_update(self):
  322. pieces = [b"\x0A" * 200, b"\x14" * 300]
  323. h1 = Poly1305.new(key=self.key, cipher=AES)
  324. h1.update(pieces[0]).update(pieces[1])
  325. d1 = h1.digest()
  326. h2 = Poly1305.new(key=self.key, cipher=AES, nonce=h1.nonce)
  327. h2.update(pieces[0] + pieces[1])
  328. d2 = h2.digest()
  329. self.assertEqual(d1, d2)
  330. def test_update_negative(self):
  331. h = Poly1305.new(key=self.key, cipher=AES)
  332. self.assertRaises(TypeError, h.update, u"string")
  333. def test_digest(self):
  334. h = Poly1305.new(key=self.key, cipher=AES)
  335. digest = h.digest()
  336. # hexdigest does not change the state
  337. self.assertEqual(h.digest(), digest)
  338. # digest returns a byte string
  339. self.failUnless(isinstance(digest, type(b"digest")))
  340. def test_update_after_digest(self):
  341. msg=b"rrrrttt"
  342. # Normally, update() cannot be done after digest()
  343. h = Poly1305.new(key=self.key, data=msg[:4], cipher=AES)
  344. h.digest()
  345. self.assertRaises(TypeError, h.update, msg[4:])
  346. def test_hex_digest(self):
  347. mac = Poly1305.new(key=self.key, cipher=AES)
  348. digest = mac.digest()
  349. hexdigest = mac.hexdigest()
  350. # hexdigest is equivalent to digest
  351. self.assertEqual(hexlify(digest), tobytes(hexdigest))
  352. # hexdigest does not change the state
  353. self.assertEqual(mac.hexdigest(), hexdigest)
  354. # hexdigest returns a string
  355. self.failUnless(isinstance(hexdigest, type("digest")))
  356. def test_verify(self):
  357. h = Poly1305.new(key=self.key, cipher=AES)
  358. mac = h.digest()
  359. h.verify(mac)
  360. wrong_mac = strxor_c(mac, 255)
  361. self.assertRaises(ValueError, h.verify, wrong_mac)
  362. def test_hexverify(self):
  363. h = Poly1305.new(key=self.key, cipher=AES)
  364. mac = h.hexdigest()
  365. h.hexverify(mac)
  366. self.assertRaises(ValueError, h.hexverify, "4556")
  367. def test_bytearray(self):
  368. data = b"\x00\x01\x02"
  369. h0 = Poly1305.new(key=self.key, data=data, cipher=AES)
  370. d_ref = h0.digest()
  371. # Data and key can be a bytearray (during initialization)
  372. key_ba = bytearray(self.key)
  373. data_ba = bytearray(data)
  374. h1 = Poly1305.new(key=self.key, data=data, cipher=AES, nonce=h0.nonce)
  375. h2 = Poly1305.new(key=key_ba, data=data_ba, cipher=AES, nonce=h0.nonce)
  376. key_ba[:1] = b'\xFF'
  377. data_ba[:1] = b'\xEE'
  378. self.assertEqual(h1.digest(), d_ref)
  379. self.assertEqual(h2.digest(), d_ref)
  380. # Data can be a bytearray (during operation)
  381. data_ba = bytearray(data)
  382. h1 = Poly1305.new(key=self.key, cipher=AES)
  383. h2 = Poly1305.new(key=self.key, cipher=AES, nonce=h1.nonce)
  384. h1.update(data)
  385. h2.update(data_ba)
  386. data_ba[:1] = b'\xFF'
  387. self.assertEqual(h1.digest(), h2.digest())
  388. def test_memoryview(self):
  389. data = b"\x00\x01\x02"
  390. def get_mv_ro(data):
  391. return memoryview(data)
  392. def get_mv_rw(data):
  393. return memoryview(bytearray(data))
  394. for get_mv in (get_mv_ro, get_mv_rw):
  395. # Data and key can be a memoryview (during initialization)
  396. key_mv = get_mv(self.key)
  397. data_mv = get_mv(data)
  398. h1 = Poly1305.new(key=self.key, data=data, cipher=AES)
  399. h2 = Poly1305.new(key=key_mv, data=data_mv, cipher=AES,
  400. nonce=h1.nonce)
  401. if not data_mv.readonly:
  402. data_mv[:1] = b'\xFF'
  403. key_mv[:1] = b'\xFF'
  404. self.assertEqual(h1.digest(), h2.digest())
  405. # Data can be a memoryview (during operation)
  406. data_mv = get_mv(data)
  407. h1 = Poly1305.new(key=self.key, cipher=AES)
  408. h2 = Poly1305.new(key=self.key, cipher=AES, nonce=h1.nonce)
  409. h1.update(data)
  410. h2.update(data_mv)
  411. if not data_mv.readonly:
  412. data_mv[:1] = b'\xFF'
  413. self.assertEqual(h1.digest(), h2.digest())
  414. import sys
  415. if sys.version[:3] == "2.6":
  416. del test_memoryview
  417. class Poly1305Test_ChaCha20(unittest.TestCase):
  418. key = b'\x11' * 32
  419. def test_new_positive(self):
  420. data = b'r' * 100
  421. h1 = Poly1305.new(key=self.key, cipher=ChaCha20)
  422. self.assertEqual(h1.digest_size, 16)
  423. self.assertEqual(len(h1.nonce), 12)
  424. h2 = Poly1305.new(key=self.key, cipher=ChaCha20, nonce = b'8' * 8)
  425. self.assertEqual(len(h2.nonce), 8)
  426. self.assertEqual(h2.nonce, b'8' * 8)
  427. def test_new_negative(self):
  428. self.assertRaises(ValueError, Poly1305.new, key=self.key, nonce=b'1' * 7, cipher=ChaCha20)
  429. #
  430. # make_mac_tests() expect a new() function with signature new(key, data,
  431. # **kwargs), and we need to adapt Poly1305's, as it only uses keywords
  432. #
  433. class Poly1305_New(object):
  434. @staticmethod
  435. def new(key, *data, **kwds):
  436. _kwds = dict(kwds)
  437. if len(data) == 1:
  438. _kwds['data'] = data[0]
  439. _kwds['key'] = key
  440. return Poly1305.new(**_kwds)
  441. class Poly1305_Basic(object):
  442. @staticmethod
  443. def new(key, *data, **kwds):
  444. from tls.Crypto.Hash.Poly1305 import Poly1305_MAC
  445. if len(data) == 1:
  446. msg = data[0]
  447. else:
  448. msg = None
  449. return Poly1305_MAC(key[:16], key[16:], msg)
  450. class Poly1305AES_MC(unittest.TestCase):
  451. def runTest(self):
  452. tag = unhexlify(b"fb447350c4e868c52ac3275cf9d4327e")
  453. msg = b''
  454. for msg_len in range(5000 + 1):
  455. key = tag + strxor_c(tag, 0xFF)
  456. nonce = tag[::-1]
  457. if msg_len > 0:
  458. msg = msg + tobytes(tag[0])
  459. auth = Poly1305.new(key=key, nonce=nonce, cipher=AES, data=msg)
  460. tag = auth.digest()
  461. # Compare against output of original DJB's poly1305aes-20050218
  462. self.assertEqual("CDFA436DDD629C7DC20E1128530BAED2", auth.hexdigest().upper())
  463. def get_tests(config={}):
  464. tests = make_mac_tests(Poly1305_Basic, "Poly1305", test_data_basic)
  465. tests += make_mac_tests(Poly1305_New, "Poly1305", test_data_aes)
  466. tests += make_mac_tests(Poly1305_New, "Poly1305", test_data_chacha20)
  467. tests += [ Poly1305AES_MC() ]
  468. tests += list_test_cases(Poly1305Test_AES)
  469. tests += list_test_cases(Poly1305Test_ChaCha20)
  470. return tests
  471. if __name__ == '__main__':
  472. suite = lambda: unittest.TestSuite(get_tests())
  473. unittest.main(defaultTest='suite')