test_CMAC.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #
  2. # SelfTest/Hash/CMAC.py: Self-test for the CMAC module
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <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.CMAC"""
  34. import unittest
  35. from Crypto.Util.py3compat import tobytes
  36. from Crypto.Hash import CMAC
  37. from Crypto.Cipher import AES, DES3
  38. from Crypto.Hash import SHAKE128
  39. # This is a list of (key, data, result, description, module) tuples.
  40. test_data = [
  41. ## Test vectors from RFC 4493 ##
  42. ## The are also in NIST SP 800 38B D.2 ##
  43. ( '2b7e151628aed2a6abf7158809cf4f3c',
  44. '',
  45. 'bb1d6929e95937287fa37d129b756746',
  46. 'RFC 4493 #1',
  47. AES
  48. ),
  49. ( '2b7e151628aed2a6abf7158809cf4f3c',
  50. '6bc1bee22e409f96e93d7e117393172a',
  51. '070a16b46b4d4144f79bdd9dd04a287c',
  52. 'RFC 4493 #2',
  53. AES
  54. ),
  55. ( '2b7e151628aed2a6abf7158809cf4f3c',
  56. '6bc1bee22e409f96e93d7e117393172a'+
  57. 'ae2d8a571e03ac9c9eb76fac45af8e51'+
  58. '30c81c46a35ce411',
  59. 'dfa66747de9ae63030ca32611497c827',
  60. 'RFC 4493 #3',
  61. AES
  62. ),
  63. ( '2b7e151628aed2a6abf7158809cf4f3c',
  64. '6bc1bee22e409f96e93d7e117393172a'+
  65. 'ae2d8a571e03ac9c9eb76fac45af8e51'+
  66. '30c81c46a35ce411e5fbc1191a0a52ef'+
  67. 'f69f2445df4f9b17ad2b417be66c3710',
  68. '51f0bebf7e3b9d92fc49741779363cfe',
  69. 'RFC 4493 #4',
  70. AES
  71. ),
  72. ## The rest of Appendix D of NIST SP 800 38B
  73. ## was not totally correct.
  74. ## Values in Examples 14, 15, 18, and 19 were wrong.
  75. ## The updated test values are published in:
  76. ## http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
  77. ( '8e73b0f7da0e6452c810f32b809079e5'+
  78. '62f8ead2522c6b7b',
  79. '',
  80. 'd17ddf46adaacde531cac483de7a9367',
  81. 'NIST SP 800 38B D.2 Example 5',
  82. AES
  83. ),
  84. ( '8e73b0f7da0e6452c810f32b809079e5'+
  85. '62f8ead2522c6b7b',
  86. '6bc1bee22e409f96e93d7e117393172a',
  87. '9e99a7bf31e710900662f65e617c5184',
  88. 'NIST SP 800 38B D.2 Example 6',
  89. AES
  90. ),
  91. ( '8e73b0f7da0e6452c810f32b809079e5'+
  92. '62f8ead2522c6b7b',
  93. '6bc1bee22e409f96e93d7e117393172a'+
  94. 'ae2d8a571e03ac9c9eb76fac45af8e51'+
  95. '30c81c46a35ce411',
  96. '8a1de5be2eb31aad089a82e6ee908b0e',
  97. 'NIST SP 800 38B D.2 Example 7',
  98. AES
  99. ),
  100. ( '8e73b0f7da0e6452c810f32b809079e5'+
  101. '62f8ead2522c6b7b',
  102. '6bc1bee22e409f96e93d7e117393172a'+
  103. 'ae2d8a571e03ac9c9eb76fac45af8e51'+
  104. '30c81c46a35ce411e5fbc1191a0a52ef'+
  105. 'f69f2445df4f9b17ad2b417be66c3710',
  106. 'a1d5df0eed790f794d77589659f39a11',
  107. 'NIST SP 800 38B D.2 Example 8',
  108. AES
  109. ),
  110. ( '603deb1015ca71be2b73aef0857d7781'+
  111. '1f352c073b6108d72d9810a30914dff4',
  112. '',
  113. '028962f61b7bf89efc6b551f4667d983',
  114. 'NIST SP 800 38B D.3 Example 9',
  115. AES
  116. ),
  117. ( '603deb1015ca71be2b73aef0857d7781'+
  118. '1f352c073b6108d72d9810a30914dff4',
  119. '6bc1bee22e409f96e93d7e117393172a',
  120. '28a7023f452e8f82bd4bf28d8c37c35c',
  121. 'NIST SP 800 38B D.3 Example 10',
  122. AES
  123. ),
  124. ( '603deb1015ca71be2b73aef0857d7781'+
  125. '1f352c073b6108d72d9810a30914dff4',
  126. '6bc1bee22e409f96e93d7e117393172a'+
  127. 'ae2d8a571e03ac9c9eb76fac45af8e51'+
  128. '30c81c46a35ce411',
  129. 'aaf3d8f1de5640c232f5b169b9c911e6',
  130. 'NIST SP 800 38B D.3 Example 11',
  131. AES
  132. ),
  133. ( '603deb1015ca71be2b73aef0857d7781'+
  134. '1f352c073b6108d72d9810a30914dff4',
  135. '6bc1bee22e409f96e93d7e117393172a'+
  136. 'ae2d8a571e03ac9c9eb76fac45af8e51'+
  137. '30c81c46a35ce411e5fbc1191a0a52ef'+
  138. 'f69f2445df4f9b17ad2b417be66c3710',
  139. 'e1992190549f6ed5696a2c056c315410',
  140. 'NIST SP 800 38B D.3 Example 12',
  141. AES
  142. ),
  143. ( '8aa83bf8cbda1062'+
  144. '0bc1bf19fbb6cd58'+
  145. 'bc313d4a371ca8b5',
  146. '',
  147. 'b7a688e122ffaf95',
  148. 'NIST SP 800 38B D.4 Example 13',
  149. DES3
  150. ),
  151. ( '8aa83bf8cbda1062'+
  152. '0bc1bf19fbb6cd58'+
  153. 'bc313d4a371ca8b5',
  154. '6bc1bee22e409f96',
  155. '8e8f293136283797',
  156. 'NIST SP 800 38B D.4 Example 14',
  157. DES3
  158. ),
  159. ( '8aa83bf8cbda1062'+
  160. '0bc1bf19fbb6cd58'+
  161. 'bc313d4a371ca8b5',
  162. '6bc1bee22e409f96'+
  163. 'e93d7e117393172a'+
  164. 'ae2d8a57',
  165. '743ddbe0ce2dc2ed',
  166. 'NIST SP 800 38B D.4 Example 15',
  167. DES3
  168. ),
  169. ( '8aa83bf8cbda1062'+
  170. '0bc1bf19fbb6cd58'+
  171. 'bc313d4a371ca8b5',
  172. '6bc1bee22e409f96'+
  173. 'e93d7e117393172a'+
  174. 'ae2d8a571e03ac9c'+
  175. '9eb76fac45af8e51',
  176. '33e6b1092400eae5',
  177. 'NIST SP 800 38B D.4 Example 16',
  178. DES3
  179. ),
  180. ( '4cf15134a2850dd5'+
  181. '8a3d10ba80570d38',
  182. '',
  183. 'bd2ebf9a3ba00361',
  184. 'NIST SP 800 38B D.7 Example 17',
  185. DES3
  186. ),
  187. ( '4cf15134a2850dd5'+
  188. '8a3d10ba80570d38',
  189. '6bc1bee22e409f96',
  190. '4ff2ab813c53ce83',
  191. 'NIST SP 800 38B D.7 Example 18',
  192. DES3
  193. ),
  194. ( '4cf15134a2850dd5'+
  195. '8a3d10ba80570d38',
  196. '6bc1bee22e409f96'+
  197. 'e93d7e117393172a'+
  198. 'ae2d8a57',
  199. '62dd1b471902bd4e',
  200. 'NIST SP 800 38B D.7 Example 19',
  201. DES3
  202. ),
  203. ( '4cf15134a2850dd5'+
  204. '8a3d10ba80570d38',
  205. '6bc1bee22e409f96'+
  206. 'e93d7e117393172a'+
  207. 'ae2d8a571e03ac9c'+
  208. '9eb76fac45af8e51',
  209. '31b1e431dabc4eb8',
  210. 'NIST SP 800 38B D.7 Example 20',
  211. DES3
  212. ),
  213. ]
  214. def get_tag_random(tag, length):
  215. return SHAKE128.new(data=tobytes(tag)).read(length)
  216. class MultipleUpdates(unittest.TestCase):
  217. """Verify that internal caching is implemented correctly"""
  218. def runTest(self):
  219. data_to_mac = get_tag_random("data_to_mac", 128)
  220. key = get_tag_random("key", 16)
  221. ref_mac = CMAC.new(key, msg=data_to_mac, ciphermod=AES).digest()
  222. # Break up in chunks of different length
  223. # The result must always be the same
  224. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  225. chunks = [data_to_mac[i:i+chunk_length] for i in
  226. range(0, len(data_to_mac), chunk_length)]
  227. mac = CMAC.new(key, ciphermod=AES)
  228. for chunk in chunks:
  229. mac.update(chunk)
  230. self.assertEqual(ref_mac, mac.digest())
  231. def get_tests(config={}):
  232. global test_data
  233. from common import make_mac_tests
  234. # Add new() parameters to the back of each test vector
  235. params_test_data = []
  236. for row in test_data:
  237. t = list(row)
  238. t[4] = dict(ciphermod=t[4])
  239. params_test_data.append(t)
  240. tests = make_mac_tests(CMAC, "CMAC", params_test_data)
  241. tests.append(MultipleUpdates())
  242. return tests
  243. if __name__ == '__main__':
  244. import unittest
  245. suite = lambda: unittest.TestSuite(get_tests())
  246. unittest.main(defaultTest='suite')