blob: 4e4ef0ec0e866aa2670ee8a695ca58a92aecb1c4 [file] [log] [blame]
Christian Heimes2f050c72018-01-27 09:53:43 +01001import binascii
Christian Heimes217f5c42013-11-24 23:14:16 +01002import functools
Guido van Rossumf1669942001-09-11 15:54:16 +00003import hmac
Guido van Rossuma19f80c2007-11-06 20:51:31 +00004import hashlib
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00005import unittest
Christian Heimes2f050c72018-01-27 09:53:43 +01006import unittest.mock
Guido van Rossuma19f80c2007-11-06 20:51:31 +00007import warnings
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00008
Christian Heimes217f5c42013-11-24 23:14:16 +01009
10def ignore_warning(func):
11 @functools.wraps(func)
12 def wrapper(*args, **kwargs):
13 with warnings.catch_warnings():
14 warnings.filterwarnings("ignore",
15 category=PendingDeprecationWarning)
16 return func(*args, **kwargs)
17 return wrapper
18
19
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000020class TestVectorsTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000021
Jeremy Hylton893801e2003-05-27 16:16:41 +000022 def test_md5_vectors(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000023 # Test the HMAC module against test vectors from the RFC.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000024
25 def md5test(key, data, digest):
Christian Heimes634919a2013-11-20 17:23:06 +010026 h = hmac.HMAC(key, data, digestmod=hashlib.md5)
27 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimes2f050c72018-01-27 09:53:43 +010028 self.assertEqual(h.digest(), binascii.unhexlify(digest))
Christian Heimesc4ab1102013-11-20 17:35:06 +010029 self.assertEqual(h.name, "hmac-md5")
30 self.assertEqual(h.digest_size, 16)
31 self.assertEqual(h.block_size, 64)
32
Christian Heimes634919a2013-11-20 17:23:06 +010033 h = hmac.HMAC(key, data, digestmod='md5')
Jeremy Hylton893801e2003-05-27 16:16:41 +000034 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimes2f050c72018-01-27 09:53:43 +010035 self.assertEqual(h.digest(), binascii.unhexlify(digest))
Christian Heimesc4ab1102013-11-20 17:35:06 +010036 self.assertEqual(h.name, "hmac-md5")
37 self.assertEqual(h.digest_size, 16)
38 self.assertEqual(h.block_size, 64)
39
Christian Heimes2f050c72018-01-27 09:53:43 +010040 self.assertEqual(
41 hmac.digest(key, data, digest='md5'),
42 binascii.unhexlify(digest)
43 )
44 with unittest.mock.patch('hmac._openssl_md_meths', {}):
45 self.assertEqual(
46 hmac.digest(key, data, digest='md5'),
47 binascii.unhexlify(digest)
48 )
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000049
Guido van Rossum3f429082007-07-10 13:35:52 +000050 md5test(b"\x0b" * 16,
51 b"Hi There",
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000052 "9294727A3638BB1C13F48EF8158BFC9D")
53
Guido van Rossum3f429082007-07-10 13:35:52 +000054 md5test(b"Jefe",
55 b"what do ya want for nothing?",
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000056 "750c783e6ab0b503eaa86e310a5db738")
57
Guido van Rossum3f429082007-07-10 13:35:52 +000058 md5test(b"\xaa" * 16,
59 b"\xdd" * 50,
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000060 "56be34521d144c88dbb8c733f0e8b3f6")
61
Guido van Rossum39478e82007-08-27 17:23:59 +000062 md5test(bytes(range(1, 26)),
Guido van Rossum3f429082007-07-10 13:35:52 +000063 b"\xcd" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000064 "697eaf0aca3a3aea3a75164746ffaa79")
65
Guido van Rossum39478e82007-08-27 17:23:59 +000066 md5test(b"\x0C" * 16,
67 b"Test With Truncation",
Jeremy Hylton893801e2003-05-27 16:16:41 +000068 "56461ef2342edc00f9bab995690efd4c")
69
Guido van Rossum3f429082007-07-10 13:35:52 +000070 md5test(b"\xaa" * 80,
Guido van Rossum39478e82007-08-27 17:23:59 +000071 b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton893801e2003-05-27 16:16:41 +000072 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
73
Guido van Rossum3f429082007-07-10 13:35:52 +000074 md5test(b"\xaa" * 80,
Guido van Rossum39478e82007-08-27 17:23:59 +000075 (b"Test Using Larger Than Block-Size Key "
76 b"and Larger Than One Block-Size Data"),
Jeremy Hylton893801e2003-05-27 16:16:41 +000077 "6f630fad67cda0ee1fb1f562db3aa53e")
78
79 def test_sha_vectors(self):
80 def shatest(key, data, digest):
Guido van Rossuma19f80c2007-11-06 20:51:31 +000081 h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
Jeremy Hylton893801e2003-05-27 16:16:41 +000082 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimes2f050c72018-01-27 09:53:43 +010083 self.assertEqual(h.digest(), binascii.unhexlify(digest))
Christian Heimesc4ab1102013-11-20 17:35:06 +010084 self.assertEqual(h.name, "hmac-sha1")
85 self.assertEqual(h.digest_size, 20)
86 self.assertEqual(h.block_size, 64)
87
Christian Heimes634919a2013-11-20 17:23:06 +010088 h = hmac.HMAC(key, data, digestmod='sha1')
89 self.assertEqual(h.hexdigest().upper(), digest.upper())
Christian Heimes2f050c72018-01-27 09:53:43 +010090 self.assertEqual(h.digest(), binascii.unhexlify(digest))
Christian Heimesc4ab1102013-11-20 17:35:06 +010091 self.assertEqual(h.name, "hmac-sha1")
92 self.assertEqual(h.digest_size, 20)
93 self.assertEqual(h.block_size, 64)
Christian Heimes634919a2013-11-20 17:23:06 +010094
Christian Heimes2f050c72018-01-27 09:53:43 +010095 self.assertEqual(
96 hmac.digest(key, data, digest='sha1'),
97 binascii.unhexlify(digest)
98 )
99
Jeremy Hylton893801e2003-05-27 16:16:41 +0000100
Guido van Rossum3f429082007-07-10 13:35:52 +0000101 shatest(b"\x0b" * 20,
102 b"Hi There",
Jeremy Hylton893801e2003-05-27 16:16:41 +0000103 "b617318655057264e28bc0b6fb378c8ef146be00")
104
Guido van Rossum3f429082007-07-10 13:35:52 +0000105 shatest(b"Jefe",
106 b"what do ya want for nothing?",
Jeremy Hylton893801e2003-05-27 16:16:41 +0000107 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
108
Guido van Rossum3f429082007-07-10 13:35:52 +0000109 shatest(b"\xAA" * 20,
110 b"\xDD" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +0000111 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
112
Guido van Rossum3f429082007-07-10 13:35:52 +0000113 shatest(bytes(range(1, 26)),
114 b"\xCD" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +0000115 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
116
Guido van Rossum39478e82007-08-27 17:23:59 +0000117 shatest(b"\x0C" * 20,
118 b"Test With Truncation",
Jeremy Hylton893801e2003-05-27 16:16:41 +0000119 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
120
Guido van Rossum3f429082007-07-10 13:35:52 +0000121 shatest(b"\xAA" * 80,
122 b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton893801e2003-05-27 16:16:41 +0000123 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
124
Guido van Rossum3f429082007-07-10 13:35:52 +0000125 shatest(b"\xAA" * 80,
126 (b"Test Using Larger Than Block-Size Key "
127 b"and Larger Than One Block-Size Data"),
Jeremy Hylton893801e2003-05-27 16:16:41 +0000128 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
129
Christian Heimesc4ab1102013-11-20 17:35:06 +0100130 def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000131 def hmactest(key, data, hexdigests):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100132 hmac_name = "hmac-" + hash_name
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000133 h = hmac.HMAC(key, data, digestmod=hashfunc)
134 self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
Christian Heimesc4ab1102013-11-20 17:35:06 +0100135 self.assertEqual(h.name, hmac_name)
136 self.assertEqual(h.digest_size, digest_size)
137 self.assertEqual(h.block_size, block_size)
138
139 h = hmac.HMAC(key, data, digestmod=hash_name)
Christian Heimes634919a2013-11-20 17:23:06 +0100140 self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
Christian Heimesc4ab1102013-11-20 17:35:06 +0100141 self.assertEqual(h.name, hmac_name)
142 self.assertEqual(h.digest_size, digest_size)
143 self.assertEqual(h.block_size, block_size)
Christian Heimes634919a2013-11-20 17:23:06 +0100144
Christian Heimes2f050c72018-01-27 09:53:43 +0100145 self.assertEqual(
146 hmac.digest(key, data, digest=hashfunc),
147 binascii.unhexlify(hexdigests[hashfunc])
148 )
149 self.assertEqual(
150 hmac.digest(key, data, digest=hash_name),
151 binascii.unhexlify(hexdigests[hashfunc])
152 )
153
154 with unittest.mock.patch('hmac._openssl_md_meths', {}):
155 self.assertEqual(
156 hmac.digest(key, data, digest=hashfunc),
157 binascii.unhexlify(hexdigests[hashfunc])
158 )
159 self.assertEqual(
160 hmac.digest(key, data, digest=hash_name),
161 binascii.unhexlify(hexdigests[hashfunc])
162 )
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000163
164 # 4.2. Test Case 1
165 hmactest(key = b'\x0b'*20,
166 data = b'Hi There',
167 hexdigests = {
168 hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
169 '47b4b1169912ba4f53684b22',
170 hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
171 '881dc200c9833da726e9376c2e32cff7',
172 hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
173 '15f9dadbe4101ec682aa034c7cebc59c'
174 'faea9ea9076ede7f4af152e8b2fa9cb6',
175 hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
176 '2379f4e2ce4ec2787ad0b30545e17cde'
177 'daa833b7d6b8a702038b274eaea3f4e4'
178 'be9d914eeb61f1702e696c203a126854',
179 })
180
181 # 4.3. Test Case 2
182 hmactest(key = b'Jefe',
183 data = b'what do ya want for nothing?',
184 hexdigests = {
185 hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
186 '8bbea2a39e6148008fd05e44',
187 hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
188 '5a003f089d2739839dec58b964ec3843',
189 hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
190 '9c7ef464f5a01b47e42ec3736322445e'
191 '8e2240ca5e69e2c78b3239ecfab21649',
192 hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
193 '87bd64222e831fd610270cd7ea250554'
194 '9758bf75c05a994a6d034f65f8f0e6fd'
195 'caeab1a34d4a6b4b636e070a38bce737',
196 })
197
198 # 4.4. Test Case 3
199 hmactest(key = b'\xaa'*20,
200 data = b'\xdd'*50,
201 hexdigests = {
202 hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
203 '9365b0c1f65d69d1ec8333ea',
204 hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
205 '2959098b3ef8c122d9635514ced565fe',
206 hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
207 '0aa635d947ac9febe83ef4e55966144b'
208 '2a5ab39dc13814b94e3ab6e101a34f27',
209 hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
210 'b1b5dbdd8ee81a3655f83e33b2279d39'
211 'bf3e848279a722c806b485a47e67c807'
212 'b946a337bee8942674278859e13292fb',
213 })
214
215 # 4.5. Test Case 4
216 hmactest(key = bytes(x for x in range(0x01, 0x19+1)),
217 data = b'\xcd'*50,
218 hexdigests = {
219 hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
220 'ec6a90d86efc012de7afec5a',
221 hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
222 '85f0faa3e578f8077a2e3ff46729665b',
223 hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
224 '7a9981480850009cc5577c6e1f573b4e'
225 '6801dd23c4a7d679ccf8a386c674cffb',
226 hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
227 'e576d97ff94b872de76f8050361ee3db'
228 'a91ca5c11aa25eb4d679275cc5788063'
229 'a5f19741120c4f2de2adebeb10a298dd',
230 })
231
232 # 4.7. Test Case 6
233 hmactest(key = b'\xaa'*131,
234 data = b'Test Using Larger Than Block-Siz'
235 b'e Key - Hash Key First',
236 hexdigests = {
237 hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
238 'd499f112f2d2b7273fa6870e',
239 hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
240 '8e0bc6213728c5140546040f0ee37f54',
241 hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
242 '4f9ef1012a2b588f3cd11f05033ac4c6'
243 '0c2ef6ab4030fe8296248df163f44952',
244 hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
245 '9b46d1f41b4aeec1121b013783f8f352'
246 '6b56d037e05f2598bd0fd2215d6a1e52'
247 '95e64f73f63f0aec8b915a985d786598',
248 })
249
250 # 4.8. Test Case 7
251 hmactest(key = b'\xaa'*131,
252 data = b'This is a test using a larger th'
253 b'an block-size key and a larger t'
254 b'han block-size data. The key nee'
255 b'ds to be hashed before being use'
256 b'd by the HMAC algorithm.',
257 hexdigests = {
258 hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
259 '946770db9c2b95c9f6f565d1',
260 hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
261 'bfdc63644f0713938a7f51535c3a35e2',
262 hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
263 '602420feb0b8fb9adccebb82461e99c5'
264 'a678cc31e799176d3860e6110c46523e',
265 hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
266 'debd71f8867289865df5a32d20cdc944'
267 'b6022cac3c4982b10d5eeb55c3e4de15'
268 '134676fb6de0446065c97440fa8c6a58',
269 })
270
271 def test_sha224_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100272 self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000273
274 def test_sha256_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100275 self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000276
277 def test_sha384_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100278 self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000279
280 def test_sha512_rfc4231(self):
Christian Heimesc4ab1102013-11-20 17:35:06 +0100281 self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000282
283 def test_legacy_block_size_warnings(self):
284 class MockCrazyHash(object):
285 """Ain't no block_size attribute here."""
286 def __init__(self, *args):
287 self._x = hashlib.sha1(*args)
288 self.digest_size = self._x.digest_size
289 def update(self, v):
290 self._x.update(v)
291 def digest(self):
292 return self._x.digest()
293
Brett Cannon1cd02472008-09-09 01:52:27 +0000294 with warnings.catch_warnings():
Christian Heimese25f35e2008-03-20 10:49:03 +0000295 warnings.simplefilter('error', RuntimeWarning)
Florent Xicluna41fe6152010-04-02 18:52:12 +0000296 with self.assertRaises(RuntimeWarning):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000297 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000298 self.fail('Expected warning about missing block_size')
299
300 MockCrazyHash.block_size = 1
Florent Xicluna41fe6152010-04-02 18:52:12 +0000301 with self.assertRaises(RuntimeWarning):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000302 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000303 self.fail('Expected warning about small block_size')
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000304
Christian Heimes634919a2013-11-20 17:23:06 +0100305 def test_with_digestmod_warning(self):
306 with self.assertWarns(PendingDeprecationWarning):
307 key = b"\x0b" * 16
308 data = b"Hi There"
309 digest = "9294727A3638BB1C13F48EF8158BFC9D"
310 h = hmac.HMAC(key, data)
311 self.assertEqual(h.hexdigest().upper(), digest)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000312
Jeremy Hylton893801e2003-05-27 16:16:41 +0000313
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000314class ConstructorTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000315
Christian Heimes217f5c42013-11-24 23:14:16 +0100316 @ignore_warning
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000317 def test_normal(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000318 # Standard constructor call.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000319 failed = 0
320 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000321 h = hmac.HMAC(b"key")
Christian Heimes217f5c42013-11-24 23:14:16 +0100322 except Exception:
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000323 self.fail("Standard constructor call raised exception.")
324
Christian Heimes217f5c42013-11-24 23:14:16 +0100325 @ignore_warning
Antoine Pitrou24ef3e92012-06-30 17:27:56 +0200326 def test_with_str_key(self):
327 # Pass a key of type str, which is an error, because it expects a key
328 # of type bytes
329 with self.assertRaises(TypeError):
330 h = hmac.HMAC("key")
331
Christian Heimes217f5c42013-11-24 23:14:16 +0100332 @ignore_warning
Antoine Pitrou24ef3e92012-06-30 17:27:56 +0200333 def test_dot_new_with_str_key(self):
334 # Pass a key of type str, which is an error, because it expects a key
335 # of type bytes
336 with self.assertRaises(TypeError):
337 h = hmac.new("key")
338
Christian Heimes217f5c42013-11-24 23:14:16 +0100339 @ignore_warning
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000340 def test_withtext(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000341 # Constructor call with text.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000342 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000343 h = hmac.HMAC(b"key", b"hash this!")
Christian Heimes217f5c42013-11-24 23:14:16 +0100344 except Exception:
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000345 self.fail("Constructor call with text argument raised exception.")
Christian Heimes217f5c42013-11-24 23:14:16 +0100346 self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000347
Christian Heimes04926ae2013-07-01 13:08:42 +0200348 def test_with_bytearray(self):
349 try:
Christian Heimes217f5c42013-11-24 23:14:16 +0100350 h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!"),
351 digestmod="md5")
352 except Exception:
Christian Heimes04926ae2013-07-01 13:08:42 +0200353 self.fail("Constructor call with bytearray arguments raised exception.")
Christian Heimes217f5c42013-11-24 23:14:16 +0100354 self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
Christian Heimes04926ae2013-07-01 13:08:42 +0200355
356 def test_with_memoryview_msg(self):
357 try:
Christian Heimes217f5c42013-11-24 23:14:16 +0100358 h = hmac.HMAC(b"key", memoryview(b"hash this!"), digestmod="md5")
359 except Exception:
Christian Heimes04926ae2013-07-01 13:08:42 +0200360 self.fail("Constructor call with memoryview msg raised exception.")
Christian Heimes217f5c42013-11-24 23:14:16 +0100361 self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
Christian Heimes04926ae2013-07-01 13:08:42 +0200362
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000363 def test_withmodule(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000364 # Constructor call with text and digest module.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000365 try:
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000366 h = hmac.HMAC(b"key", b"", hashlib.sha1)
Christian Heimes217f5c42013-11-24 23:14:16 +0100367 except Exception:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000368 self.fail("Constructor call with hashlib.sha1 raised exception.")
Tim Peters88768482001-11-13 21:51:26 +0000369
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000370class SanityTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000371
Christian Heimes217f5c42013-11-24 23:14:16 +0100372 @ignore_warning
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000373 def test_default_is_md5(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000374 # Testing if HMAC defaults to MD5 algorithm.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000375 # NOTE: this whitebox test depends on the hmac class internals
Guido van Rossum39478e82007-08-27 17:23:59 +0000376 h = hmac.HMAC(b"key")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000377 self.assertEqual(h.digest_cons, hashlib.md5)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000378
379 def test_exercise_all_methods(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000380 # Exercising all methods once.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000381 # This must not raise any exceptions
382 try:
Christian Heimes217f5c42013-11-24 23:14:16 +0100383 h = hmac.HMAC(b"my secret key", digestmod="md5")
Guido van Rossum39478e82007-08-27 17:23:59 +0000384 h.update(b"compute the hash of this text!")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000385 dig = h.digest()
386 dig = h.hexdigest()
387 h2 = h.copy()
Christian Heimes217f5c42013-11-24 23:14:16 +0100388 except Exception:
Neal Norwitz28bb5722002-04-01 19:00:50 +0000389 self.fail("Exception raised during normal usage of HMAC class.")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000390
391class CopyTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000392
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000393 def test_attributes(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000394 # Testing if attributes are of same type.
Christian Heimes217f5c42013-11-24 23:14:16 +0100395 h1 = hmac.HMAC(b"key", digestmod="md5")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000396 h2 = h1.copy()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000397 self.assertTrue(h1.digest_cons == h2.digest_cons,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000398 "digest constructors don't match.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000399 self.assertEqual(type(h1.inner), type(h2.inner),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000400 "Types of inner don't match.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000401 self.assertEqual(type(h1.outer), type(h2.outer),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000402 "Types of outer don't match.")
403
404 def test_realcopy(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000405 # Testing if the copy method created a real copy.
Christian Heimes217f5c42013-11-24 23:14:16 +0100406 h1 = hmac.HMAC(b"key", digestmod="md5")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000407 h2 = h1.copy()
Mark Dickinsona56c4672009-01-27 18:17:45 +0000408 # Using id() in case somebody has overridden __eq__/__ne__.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000409 self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
410 self.assertTrue(id(h1.inner) != id(h2.inner),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000411 "No real copy of the attribute 'inner'.")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000412 self.assertTrue(id(h1.outer) != id(h2.outer),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000413 "No real copy of the attribute 'outer'.")
414
415 def test_equality(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000416 # Testing if the copy has the same digests.
Christian Heimes217f5c42013-11-24 23:14:16 +0100417 h1 = hmac.HMAC(b"key", digestmod="md5")
Guido van Rossum39478e82007-08-27 17:23:59 +0000418 h1.update(b"some random text")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000419 h2 = h1.copy()
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000420 self.assertEqual(h1.digest(), h2.digest(),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000421 "Digest of copy doesn't match original digest.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000422 self.assertEqual(h1.hexdigest(), h2.hexdigest(),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000423 "Hexdigest of copy doesn't match original hexdigest.")
424
Nick Coghlan807770e2012-06-15 21:14:08 +1000425class CompareDigestTestCase(unittest.TestCase):
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200426
Christian Heimes6cea6552012-06-24 13:48:32 +0200427 def test_compare_digest(self):
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200428 # Testing input type exception handling
429 a, b = 100, 200
Nick Coghlan807770e2012-06-15 21:14:08 +1000430 self.assertRaises(TypeError, hmac.compare_digest, a, b)
431 a, b = 100, b"foobar"
432 self.assertRaises(TypeError, hmac.compare_digest, a, b)
433 a, b = b"foobar", 200
434 self.assertRaises(TypeError, hmac.compare_digest, a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200435 a, b = "foobar", b"foobar"
Nick Coghlan807770e2012-06-15 21:14:08 +1000436 self.assertRaises(TypeError, hmac.compare_digest, a, b)
437 a, b = b"foobar", "foobar"
438 self.assertRaises(TypeError, hmac.compare_digest, a, b)
Nick Coghlan807770e2012-06-15 21:14:08 +1000439
440 # Testing bytes of different lengths
441 a, b = b"foobar", b"foo"
442 self.assertFalse(hmac.compare_digest(a, b))
443 a, b = b"\xde\xad\xbe\xef", b"\xde\xad"
444 self.assertFalse(hmac.compare_digest(a, b))
445
446 # Testing bytes of same lengths, different values
447 a, b = b"foobar", b"foobaz"
448 self.assertFalse(hmac.compare_digest(a, b))
449 a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea"
450 self.assertFalse(hmac.compare_digest(a, b))
451
452 # Testing bytes of same lengths, same values
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200453 a, b = b"foobar", b"foobar"
Nick Coghlan807770e2012-06-15 21:14:08 +1000454 self.assertTrue(hmac.compare_digest(a, b))
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200455 a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef"
Nick Coghlan807770e2012-06-15 21:14:08 +1000456 self.assertTrue(hmac.compare_digest(a, b))
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200457
Christian Heimes6cea6552012-06-24 13:48:32 +0200458 # Testing bytearrays of same lengths, same values
459 a, b = bytearray(b"foobar"), bytearray(b"foobar")
460 self.assertTrue(hmac.compare_digest(a, b))
461
462 # Testing bytearrays of diffeent lengths
463 a, b = bytearray(b"foobar"), bytearray(b"foo")
464 self.assertFalse(hmac.compare_digest(a, b))
465
466 # Testing bytearrays of same lengths, different values
467 a, b = bytearray(b"foobar"), bytearray(b"foobaz")
468 self.assertFalse(hmac.compare_digest(a, b))
469
470 # Testing byte and bytearray of same lengths, same values
471 a, b = bytearray(b"foobar"), b"foobar"
472 self.assertTrue(hmac.compare_digest(a, b))
473 self.assertTrue(hmac.compare_digest(b, a))
474
475 # Testing byte bytearray of diffeent lengths
476 a, b = bytearray(b"foobar"), b"foo"
477 self.assertFalse(hmac.compare_digest(a, b))
478 self.assertFalse(hmac.compare_digest(b, a))
479
480 # Testing byte and bytearray of same lengths, different values
481 a, b = bytearray(b"foobar"), b"foobaz"
482 self.assertFalse(hmac.compare_digest(a, b))
483 self.assertFalse(hmac.compare_digest(b, a))
484
485 # Testing str of same lengths
486 a, b = "foobar", "foobar"
487 self.assertTrue(hmac.compare_digest(a, b))
488
489 # Testing str of diffeent lengths
490 a, b = "foo", "foobar"
491 self.assertFalse(hmac.compare_digest(a, b))
492
493 # Testing bytes of same lengths, different values
494 a, b = "foobar", "foobaz"
495 self.assertFalse(hmac.compare_digest(a, b))
496
497 # Testing error cases
498 a, b = "foobar", b"foobar"
499 self.assertRaises(TypeError, hmac.compare_digest, a, b)
500 a, b = b"foobar", "foobar"
501 self.assertRaises(TypeError, hmac.compare_digest, a, b)
502 a, b = b"foobar", 1
503 self.assertRaises(TypeError, hmac.compare_digest, a, b)
504 a, b = 100, 200
505 self.assertRaises(TypeError, hmac.compare_digest, a, b)
506 a, b = "fooä", "fooä"
507 self.assertRaises(TypeError, hmac.compare_digest, a, b)
508
509 # subclasses are supported by ignore __eq__
510 class mystr(str):
511 def __eq__(self, other):
512 return False
513
514 a, b = mystr("foobar"), mystr("foobar")
515 self.assertTrue(hmac.compare_digest(a, b))
516 a, b = mystr("foobar"), "foobar"
517 self.assertTrue(hmac.compare_digest(a, b))
518 a, b = mystr("foobar"), mystr("foobaz")
519 self.assertFalse(hmac.compare_digest(a, b))
520
521 class mybytes(bytes):
522 def __eq__(self, other):
523 return False
524
525 a, b = mybytes(b"foobar"), mybytes(b"foobar")
526 self.assertTrue(hmac.compare_digest(a, b))
527 a, b = mybytes(b"foobar"), b"foobar"
528 self.assertTrue(hmac.compare_digest(a, b))
529 a, b = mybytes(b"foobar"), mybytes(b"foobaz")
530 self.assertFalse(hmac.compare_digest(a, b))
531
532
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000533if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500534 unittest.main()