test_BLAKE2.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, 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 os
  31. import re
  32. import unittest
  33. import warnings
  34. from binascii import unhexlify, hexlify
  35. from Crypto.Util.py3compat import tobytes
  36. from Crypto.Util.strxor import strxor_c
  37. from Crypto.SelfTest.st_common import list_test_cases
  38. from Crypto.Hash import BLAKE2b, BLAKE2s
  39. class Blake2Test(unittest.TestCase):
  40. def test_new_positive(self):
  41. h = self.BLAKE2.new(digest_bits=self.max_bits)
  42. for new_func in self.BLAKE2.new, h.new:
  43. for dbits in range(8, self.max_bits + 1, 8):
  44. hobj = new_func(digest_bits=dbits)
  45. self.assertEqual(hobj.digest_size, dbits // 8)
  46. for dbytes in range(1, self.max_bytes + 1):
  47. hobj = new_func(digest_bytes=dbytes)
  48. self.assertEqual(hobj.digest_size, dbytes)
  49. digest1 = new_func(data=b"\x90", digest_bytes=self.max_bytes).digest()
  50. digest2 = new_func(digest_bytes=self.max_bytes).update(b"\x90").digest()
  51. self.assertEqual(digest1, digest2)
  52. new_func(data=b"A", key=b"5", digest_bytes=self.max_bytes)
  53. hobj = h.new()
  54. self.assertEqual(hobj.digest_size, self.max_bytes)
  55. def test_new_negative(self):
  56. h = self.BLAKE2.new(digest_bits=self.max_bits)
  57. for new_func in self.BLAKE2.new, h.new:
  58. self.assertRaises(TypeError, new_func,
  59. digest_bytes=self.max_bytes,
  60. digest_bits=self.max_bits)
  61. self.assertRaises(ValueError, new_func, digest_bytes=0)
  62. self.assertRaises(ValueError, new_func,
  63. digest_bytes=self.max_bytes + 1)
  64. self.assertRaises(ValueError, new_func, digest_bits=7)
  65. self.assertRaises(ValueError, new_func, digest_bits=15)
  66. self.assertRaises(ValueError, new_func,
  67. digest_bits=self.max_bits + 1)
  68. self.assertRaises(TypeError, new_func,
  69. digest_bytes=self.max_bytes,
  70. key=u"string")
  71. self.assertRaises(TypeError, new_func,
  72. digest_bytes=self.max_bytes,
  73. data=u"string")
  74. def test_default_digest_size(self):
  75. digest = self.BLAKE2.new(data=b'abc').digest()
  76. self.assertEquals(len(digest), self.max_bytes)
  77. def test_update(self):
  78. pieces = [b"\x0A" * 200, b"\x14" * 300]
  79. h = self.BLAKE2.new(digest_bytes=self.max_bytes)
  80. h.update(pieces[0]).update(pieces[1])
  81. digest = h.digest()
  82. h = self.BLAKE2.new(digest_bytes=self.max_bytes)
  83. h.update(pieces[0] + pieces[1])
  84. self.assertEqual(h.digest(), digest)
  85. def test_update_negative(self):
  86. h = self.BLAKE2.new(digest_bytes=self.max_bytes)
  87. self.assertRaises(TypeError, h.update, u"string")
  88. def test_digest(self):
  89. h = self.BLAKE2.new(digest_bytes=self.max_bytes)
  90. digest = h.digest()
  91. # hexdigest does not change the state
  92. self.assertEqual(h.digest(), digest)
  93. # digest returns a byte string
  94. self.failUnless(isinstance(digest, type(b"digest")))
  95. def test_update_after_digest(self):
  96. msg = b"rrrrttt"
  97. # Normally, update() cannot be done after digest()
  98. h = self.BLAKE2.new(digest_bits=256, data=msg[:4])
  99. dig1 = h.digest()
  100. self.assertRaises(TypeError, h.update, msg[4:])
  101. dig2 = self.BLAKE2.new(digest_bits=256, data=msg).digest()
  102. # With the proper flag, it is allowed
  103. h = self.BLAKE2.new(digest_bits=256, data=msg[:4], update_after_digest=True)
  104. self.assertEquals(h.digest(), dig1)
  105. # ... and the subsequent digest applies to the entire message
  106. # up to that point
  107. h.update(msg[4:])
  108. self.assertEquals(h.digest(), dig2)
  109. def test_hex_digest(self):
  110. mac = self.BLAKE2.new(digest_bits=self.max_bits)
  111. digest = mac.digest()
  112. hexdigest = mac.hexdigest()
  113. # hexdigest is equivalent to digest
  114. self.assertEqual(hexlify(digest), tobytes(hexdigest))
  115. # hexdigest does not change the state
  116. self.assertEqual(mac.hexdigest(), hexdigest)
  117. # hexdigest returns a string
  118. self.failUnless(isinstance(hexdigest, type("digest")))
  119. def test_verify(self):
  120. h = self.BLAKE2.new(digest_bytes=self.max_bytes, key=b"4")
  121. mac = h.digest()
  122. h.verify(mac)
  123. wrong_mac = strxor_c(mac, 255)
  124. self.assertRaises(ValueError, h.verify, wrong_mac)
  125. def test_hexverify(self):
  126. h = self.BLAKE2.new(digest_bytes=self.max_bytes, key=b"4")
  127. mac = h.hexdigest()
  128. h.hexverify(mac)
  129. self.assertRaises(ValueError, h.hexverify, "4556")
  130. def test_oid(self):
  131. prefix = "1.3.6.1.4.1.1722.12.2." + self.oid_variant + "."
  132. for digest_bits in self.digest_bits_oid:
  133. h = self.BLAKE2.new(digest_bits=digest_bits)
  134. self.assertEqual(h.oid, prefix + str(digest_bits // 8))
  135. h = self.BLAKE2.new(digest_bits=digest_bits, key=b"secret")
  136. self.assertRaises(AttributeError, lambda: h.oid)
  137. for digest_bits in (8, self.max_bits):
  138. if digest_bits in self.digest_bits_oid:
  139. continue
  140. self.assertRaises(AttributeError, lambda: h.oid)
  141. def test_bytearray(self):
  142. key = b'0' * 16
  143. data = b"\x00\x01\x02"
  144. # Data and key can be a bytearray (during initialization)
  145. key_ba = bytearray(key)
  146. data_ba = bytearray(data)
  147. h1 = self.BLAKE2.new(data=data, key=key)
  148. h2 = self.BLAKE2.new(data=data_ba, key=key_ba)
  149. key_ba[:1] = b'\xFF'
  150. data_ba[:1] = b'\xFF'
  151. self.assertEqual(h1.digest(), h2.digest())
  152. # Data can be a bytearray (during operation)
  153. data_ba = bytearray(data)
  154. h1 = self.BLAKE2.new()
  155. h2 = self.BLAKE2.new()
  156. h1.update(data)
  157. h2.update(data_ba)
  158. data_ba[:1] = b'\xFF'
  159. self.assertEqual(h1.digest(), h2.digest())
  160. def test_memoryview(self):
  161. key = b'0' * 16
  162. data = b"\x00\x01\x02"
  163. def get_mv_ro(data):
  164. return memoryview(data)
  165. def get_mv_rw(data):
  166. return memoryview(bytearray(data))
  167. for get_mv in (get_mv_ro, get_mv_rw):
  168. # Data and key can be a memoryview (during initialization)
  169. key_mv = get_mv(key)
  170. data_mv = get_mv(data)
  171. h1 = self.BLAKE2.new(data=data, key=key)
  172. h2 = self.BLAKE2.new(data=data_mv, key=key_mv)
  173. if not data_mv.readonly:
  174. data_mv[:1] = b'\xFF'
  175. key_mv[:1] = b'\xFF'
  176. self.assertEqual(h1.digest(), h2.digest())
  177. # Data can be a memoryview (during operation)
  178. data_mv = get_mv(data)
  179. h1 = self.BLAKE2.new()
  180. h2 = self.BLAKE2.new()
  181. h1.update(data)
  182. h2.update(data_mv)
  183. if not data_mv.readonly:
  184. data_mv[:1] = b'\xFF'
  185. self.assertEqual(h1.digest(), h2.digest())
  186. class Blake2bTest(Blake2Test):
  187. #: Module
  188. BLAKE2 = BLAKE2b
  189. #: Max output size (in bits)
  190. max_bits = 512
  191. #: Max output size (in bytes)
  192. max_bytes = 64
  193. #: Bit size of the digests for which an ASN OID exists
  194. digest_bits_oid = (160, 256, 384, 512)
  195. # http://tools.ietf.org/html/draft-saarinen-blake2-02
  196. oid_variant = "1"
  197. class Blake2sTest(Blake2Test):
  198. #: Module
  199. BLAKE2 = BLAKE2s
  200. #: Max output size (in bits)
  201. max_bits = 256
  202. #: Max output size (in bytes)
  203. max_bytes = 32
  204. #: Bit size of the digests for which an ASN OID exists
  205. digest_bits_oid = (128, 160, 224, 256)
  206. # http://tools.ietf.org/html/draft-saarinen-blake2-02
  207. oid_variant = "2"
  208. class Blake2OfficialTestVector(unittest.TestCase):
  209. def _load_tests(self, test_vector_file):
  210. expected = "in"
  211. test_vectors = []
  212. with open(test_vector_file, "rt") as test_vector_fd:
  213. for line_number, line in enumerate(test_vector_fd):
  214. if line.strip() == "" or line.startswith("#"):
  215. continue
  216. res = re.match("%s:\t([0-9A-Fa-f]*)" % expected, line)
  217. if not res:
  218. raise ValueError("Incorrect test vector format (line %d)"
  219. % line_number)
  220. if res.group(1):
  221. bin_value = unhexlify(tobytes(res.group(1)))
  222. else:
  223. bin_value = b""
  224. if expected == "in":
  225. input_data = bin_value
  226. expected = "key"
  227. elif expected == "key":
  228. key = bin_value
  229. expected = "hash"
  230. else:
  231. result = bin_value
  232. expected = "in"
  233. test_vectors.append((input_data, key, result))
  234. return test_vectors
  235. def setUp(self):
  236. dir_comps = ("Hash", self.name)
  237. file_name = self.name.lower() + "-test.txt"
  238. self.description = "%s tests" % self.name
  239. try:
  240. import pycryptodome_test_vectors # type: ignore
  241. except ImportError:
  242. warnings.warn("Warning: skipping extended tests for %s" % self.name,
  243. UserWarning)
  244. self.test_vectors = []
  245. return
  246. init_dir = os.path.dirname(pycryptodome_test_vectors.__file__)
  247. full_file_name = os.path.join(os.path.join(init_dir, *dir_comps), file_name)
  248. self.test_vectors = self._load_tests(full_file_name)
  249. def runTest(self):
  250. for (input_data, key, result) in self.test_vectors:
  251. mac = self.BLAKE2.new(key=key, digest_bytes=self.max_bytes)
  252. mac.update(input_data)
  253. self.assertEqual(mac.digest(), result)
  254. class Blake2bOfficialTestVector(Blake2OfficialTestVector):
  255. #: Module
  256. BLAKE2 = BLAKE2b
  257. #: Hash name
  258. name = "BLAKE2b"
  259. #: Max digest size
  260. max_bytes = 64
  261. class Blake2sOfficialTestVector(Blake2OfficialTestVector):
  262. #: Module
  263. BLAKE2 = BLAKE2s
  264. #: Hash name
  265. name = "BLAKE2s"
  266. #: Max digest size
  267. max_bytes = 32
  268. class Blake2TestVector1(unittest.TestCase):
  269. def _load_tests(self, test_vector_file):
  270. test_vectors = []
  271. with open(test_vector_file, "rt") as test_vector_fd:
  272. for line_number, line in enumerate(test_vector_fd):
  273. if line.strip() == "" or line.startswith("#"):
  274. continue
  275. res = re.match("digest: ([0-9A-Fa-f]*)", line)
  276. if not res:
  277. raise ValueError("Incorrect test vector format (line %d)"
  278. % line_number)
  279. test_vectors.append(unhexlify(tobytes(res.group(1))))
  280. return test_vectors
  281. def setUp(self):
  282. dir_comps = ("Hash", self.name)
  283. file_name = "tv1.txt"
  284. self.description = "%s tests" % self.name
  285. try:
  286. import pycryptodome_test_vectors
  287. except ImportError:
  288. warnings.warn("Warning: skipping extended tests for %s" % self.name,
  289. UserWarning)
  290. self.test_vectors = []
  291. return
  292. init_dir = os.path.dirname(pycryptodome_test_vectors.__file__)
  293. full_file_name = os.path.join(os.path.join(init_dir, *dir_comps), file_name)
  294. self.test_vectors = self._load_tests(full_file_name)
  295. def runTest(self):
  296. for tv in self.test_vectors:
  297. digest_bytes = len(tv)
  298. next_data = b""
  299. for _ in range(100):
  300. h = self.BLAKE2.new(digest_bytes=digest_bytes)
  301. h.update(next_data)
  302. next_data = h.digest() + next_data
  303. self.assertEqual(h.digest(), tv)
  304. class Blake2bTestVector1(Blake2TestVector1):
  305. #: Module
  306. BLAKE2 = BLAKE2b
  307. #: Hash name
  308. name = "BLAKE2b"
  309. class Blake2sTestVector1(Blake2TestVector1):
  310. #: Module
  311. BLAKE2 = BLAKE2s
  312. #: Hash name
  313. name = "BLAKE2s"
  314. class Blake2TestVector2(unittest.TestCase):
  315. def _load_tests(self, test_vector_file):
  316. test_vectors = []
  317. with open(test_vector_file, "rt") as test_vector_fd:
  318. for line_number, line in enumerate(test_vector_fd):
  319. if line.strip() == "" or line.startswith("#"):
  320. continue
  321. res = re.match(r"digest\(([0-9]+)\): ([0-9A-Fa-f]*)", line)
  322. if not res:
  323. raise ValueError("Incorrect test vector format (line %d)"
  324. % line_number)
  325. key_size = int(res.group(1))
  326. result = unhexlify(tobytes(res.group(2)))
  327. test_vectors.append((key_size, result))
  328. return test_vectors
  329. def setUp(self):
  330. dir_comps = ("Hash", self.name)
  331. file_name = "tv2.txt"
  332. self.description = "%s tests" % self.name
  333. try:
  334. import pycryptodome_test_vectors # type: ignore
  335. except ImportError:
  336. warnings.warn("Warning: skipping extended tests for %s" % self.name,
  337. UserWarning)
  338. self.test_vectors = []
  339. return
  340. init_dir = os.path.dirname(pycryptodome_test_vectors.__file__)
  341. full_file_name = os.path.join(os.path.join(init_dir, *dir_comps), file_name)
  342. self.test_vectors = self._load_tests(full_file_name)
  343. def runTest(self):
  344. for key_size, result in self.test_vectors:
  345. next_data = b""
  346. for _ in range(100):
  347. h = self.BLAKE2.new(digest_bytes=self.max_bytes,
  348. key=b"A" * key_size)
  349. h.update(next_data)
  350. next_data = h.digest() + next_data
  351. self.assertEqual(h.digest(), result)
  352. class Blake2bTestVector2(Blake2TestVector1):
  353. #: Module
  354. BLAKE2 = BLAKE2b
  355. #: Hash name
  356. name = "BLAKE2b"
  357. #: Max digest size in bytes
  358. max_bytes = 64
  359. class Blake2sTestVector2(Blake2TestVector1):
  360. #: Module
  361. BLAKE2 = BLAKE2s
  362. #: Hash name
  363. name = "BLAKE2s"
  364. #: Max digest size in bytes
  365. max_bytes = 32
  366. def get_tests(config={}):
  367. tests = []
  368. tests += list_test_cases(Blake2bTest)
  369. tests.append(Blake2bOfficialTestVector())
  370. tests.append(Blake2bTestVector1())
  371. tests.append(Blake2bTestVector2())
  372. tests += list_test_cases(Blake2sTest)
  373. tests.append(Blake2sOfficialTestVector())
  374. tests.append(Blake2sTestVector1())
  375. tests.append(Blake2sTestVector2())
  376. return tests
  377. if __name__ == '__main__':
  378. import unittest
  379. def suite():
  380. return unittest.TestSuite(get_tests())
  381. unittest.main(defaultTest='suite')