test_HMAC.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Hash/HMAC.py: Self-test for the HMAC module
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self-test suite for Crypto.Hash.HMAC"""
  25. __revision__ = "$Id$"
  26. from common import dict # For compatibility with Python 2.1 and 2.2
  27. from Crypto.Util.py3compat import *
  28. # This is a list of (key, data, results, description) tuples.
  29. test_data = [
  30. ## Test vectors from RFC 2202 ##
  31. # Test that the default hashmod is MD5
  32. ('0b' * 16,
  33. '4869205468657265',
  34. dict(default='9294727a3638bb1c13f48ef8158bfc9d'),
  35. 'default-is-MD5'),
  36. # Test case 1 (MD5)
  37. ('0b' * 16,
  38. '4869205468657265',
  39. dict(MD5='9294727a3638bb1c13f48ef8158bfc9d'),
  40. 'RFC 2202 #1-MD5 (HMAC-MD5)'),
  41. # Test case 1 (SHA1)
  42. ('0b' * 20,
  43. '4869205468657265',
  44. dict(SHA1='b617318655057264e28bc0b6fb378c8ef146be00'),
  45. 'RFC 2202 #1-SHA1 (HMAC-SHA1)'),
  46. # Test case 2
  47. ('4a656665',
  48. '7768617420646f2079612077616e7420666f72206e6f7468696e673f',
  49. dict(MD5='750c783e6ab0b503eaa86e310a5db738',
  50. SHA1='effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'),
  51. 'RFC 2202 #2 (HMAC-MD5/SHA1)'),
  52. # Test case 3 (MD5)
  53. ('aa' * 16,
  54. 'dd' * 50,
  55. dict(MD5='56be34521d144c88dbb8c733f0e8b3f6'),
  56. 'RFC 2202 #3-MD5 (HMAC-MD5)'),
  57. # Test case 3 (SHA1)
  58. ('aa' * 20,
  59. 'dd' * 50,
  60. dict(SHA1='125d7342b9ac11cd91a39af48aa17b4f63f175d3'),
  61. 'RFC 2202 #3-SHA1 (HMAC-SHA1)'),
  62. # Test case 4
  63. ('0102030405060708090a0b0c0d0e0f10111213141516171819',
  64. 'cd' * 50,
  65. dict(MD5='697eaf0aca3a3aea3a75164746ffaa79',
  66. SHA1='4c9007f4026250c6bc8414f9bf50c86c2d7235da'),
  67. 'RFC 2202 #4 (HMAC-MD5/SHA1)'),
  68. # Test case 5 (MD5)
  69. ('0c' * 16,
  70. '546573742057697468205472756e636174696f6e',
  71. dict(MD5='56461ef2342edc00f9bab995690efd4c'),
  72. 'RFC 2202 #5-MD5 (HMAC-MD5)'),
  73. # Test case 5 (SHA1)
  74. # NB: We do not implement hash truncation, so we only test the full hash here.
  75. ('0c' * 20,
  76. '546573742057697468205472756e636174696f6e',
  77. dict(SHA1='4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'),
  78. 'RFC 2202 #5-SHA1 (HMAC-SHA1)'),
  79. # Test case 6
  80. ('aa' * 80,
  81. '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
  82. + '65204b6579202d2048617368204b6579204669727374',
  83. dict(MD5='6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd',
  84. SHA1='aa4ae5e15272d00e95705637ce8a3b55ed402112'),
  85. 'RFC 2202 #6 (HMAC-MD5/SHA1)'),
  86. # Test case 7
  87. ('aa' * 80,
  88. '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
  89. + '65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d'
  90. + '53697a652044617461',
  91. dict(MD5='6f630fad67cda0ee1fb1f562db3aa53e',
  92. SHA1='e8e99d0f45237d786d6bbaa7965c7808bbff1a91'),
  93. 'RFC 2202 #7 (HMAC-MD5/SHA1)'),
  94. ## Test vectors from RFC 4231 ##
  95. # 4.2. Test Case 1
  96. ('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b',
  97. '4869205468657265',
  98. dict(SHA256='''
  99. b0344c61d8db38535ca8afceaf0bf12b
  100. 881dc200c9833da726e9376c2e32cff7
  101. '''),
  102. 'RFC 4231 #1 (HMAC-SHA256)'),
  103. # 4.3. Test Case 2 - Test with a key shorter than the length of the HMAC
  104. # output.
  105. ('4a656665',
  106. '7768617420646f2079612077616e7420666f72206e6f7468696e673f',
  107. dict(SHA256='''
  108. 5bdcc146bf60754e6a042426089575c7
  109. 5a003f089d2739839dec58b964ec3843
  110. '''),
  111. 'RFC 4231 #2 (HMAC-SHA256)'),
  112. # 4.4. Test Case 3 - Test with a combined length of key and data that is
  113. # larger than 64 bytes (= block-size of SHA-224 and SHA-256).
  114. ('aa' * 20,
  115. 'dd' * 50,
  116. dict(SHA256='''
  117. 773ea91e36800e46854db8ebd09181a7
  118. 2959098b3ef8c122d9635514ced565fe
  119. '''),
  120. 'RFC 4231 #3 (HMAC-SHA256)'),
  121. # 4.5. Test Case 4 - Test with a combined length of key and data that is
  122. # larger than 64 bytes (= block-size of SHA-224 and SHA-256).
  123. ('0102030405060708090a0b0c0d0e0f10111213141516171819',
  124. 'cd' * 50,
  125. dict(SHA256='''
  126. 82558a389a443c0ea4cc819899f2083a
  127. 85f0faa3e578f8077a2e3ff46729665b
  128. '''),
  129. 'RFC 4231 #4 (HMAC-SHA256)'),
  130. # 4.6. Test Case 5 - Test with a truncation of output to 128 bits.
  131. #
  132. # Not included because we do not implement hash truncation.
  133. #
  134. # 4.7. Test Case 6 - Test with a key larger than 128 bytes (= block-size of
  135. # SHA-384 and SHA-512).
  136. ('aa' * 131,
  137. '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'
  138. + '65204b6579202d2048617368204b6579204669727374',
  139. dict(SHA256='''
  140. 60e431591ee0b67f0d8a26aacbf5b77f
  141. 8e0bc6213728c5140546040f0ee37f54
  142. '''),
  143. 'RFC 4231 #6 (HMAC-SHA256)'),
  144. # 4.8. Test Case 7 - Test with a key and data that is larger than 128 bytes
  145. # (= block-size of SHA-384 and SHA-512).
  146. ('aa' * 131,
  147. '5468697320697320612074657374207573696e672061206c6172676572207468'
  148. + '616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074'
  149. + '68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565'
  150. + '647320746f20626520686173686564206265666f7265206265696e6720757365'
  151. + '642062792074686520484d414320616c676f726974686d2e',
  152. dict(SHA256='''
  153. 9b09ffa71b942fcb27635fbcd5b0e944
  154. bfdc63644f0713938a7f51535c3a35e2
  155. '''),
  156. 'RFC 4231 #7 (HMAC-SHA256)'),
  157. ]
  158. hashlib_test_data = [
  159. # Test case 8 (SHA224)
  160. ('4a656665',
  161. '7768617420646f2079612077616e74'
  162. + '20666f72206e6f7468696e673f',
  163. dict(SHA224='a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44'),
  164. 'RFC 4634 8.4 SHA224 (HMAC-SHA224)'),
  165. # Test case 9 (SHA384)
  166. ('4a656665',
  167. '7768617420646f2079612077616e74'
  168. + '20666f72206e6f7468696e673f',
  169. dict(SHA384='af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649'),
  170. 'RFC 4634 8.4 SHA384 (HMAC-SHA384)'),
  171. # Test case 10 (SHA512)
  172. ('4a656665',
  173. '7768617420646f2079612077616e74'
  174. + '20666f72206e6f7468696e673f',
  175. dict(SHA512='164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'),
  176. 'RFC 4634 8.4 SHA512 (HMAC-SHA512)'),
  177. ]
  178. def get_tests(config={}):
  179. global test_data
  180. from Crypto.Hash import HMAC, MD5, SHA as SHA1, SHA256
  181. from common import make_mac_tests
  182. hashmods = dict(MD5=MD5, SHA1=SHA1, SHA256=SHA256, default=None)
  183. try:
  184. from Crypto.Hash import SHA224, SHA384, SHA512
  185. hashmods.update(dict(SHA224=SHA224, SHA384=SHA384, SHA512=SHA512))
  186. test_data += hashlib_test_data
  187. except ImportError:
  188. import sys
  189. sys.stderr.write("SelfTest: warning: not testing HMAC-SHA224/384/512 (not available)\n")
  190. return make_mac_tests(HMAC, "HMAC", test_data, hashmods)
  191. if __name__ == '__main__':
  192. import unittest
  193. suite = lambda: unittest.TestSuite(get_tests())
  194. unittest.main(defaultTest='suite')
  195. # vim:set ts=4 sw=4 sts=4 expandtab: