| Guido van Rossum | f166994 | 2001-09-11 15:54:16 +0000 | [diff] [blame] | 1 | import hmac |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 2 | import hashlib |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 3 | import unittest |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 4 | import warnings |
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 6 | |
| 7 | class TestVectorsTestCase(unittest.TestCase): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 8 | |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 9 | def test_md5_vectors(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 10 | # Test the HMAC module against test vectors from the RFC. |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 11 | |
| 12 | def md5test(key, data, digest): |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 13 | h = hmac.HMAC(key, data, digestmod=hashlib.md5) |
| 14 | self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 15 | self.assertEqual(h.name, "hmac-md5") |
| 16 | self.assertEqual(h.digest_size, 16) |
| 17 | self.assertEqual(h.block_size, 64) |
| 18 | |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 19 | h = hmac.HMAC(key, data, digestmod='md5') |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 20 | self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 21 | self.assertEqual(h.name, "hmac-md5") |
| 22 | self.assertEqual(h.digest_size, 16) |
| 23 | self.assertEqual(h.block_size, 64) |
| 24 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 25 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 26 | md5test(b"\x0b" * 16, |
| 27 | b"Hi There", |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 28 | "9294727A3638BB1C13F48EF8158BFC9D") |
| 29 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 30 | md5test(b"Jefe", |
| 31 | b"what do ya want for nothing?", |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 32 | "750c783e6ab0b503eaa86e310a5db738") |
| 33 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 34 | md5test(b"\xaa" * 16, |
| 35 | b"\xdd" * 50, |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 36 | "56be34521d144c88dbb8c733f0e8b3f6") |
| 37 | |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 38 | md5test(bytes(range(1, 26)), |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 39 | b"\xcd" * 50, |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 40 | "697eaf0aca3a3aea3a75164746ffaa79") |
| 41 | |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 42 | md5test(b"\x0C" * 16, |
| 43 | b"Test With Truncation", |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 44 | "56461ef2342edc00f9bab995690efd4c") |
| 45 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 46 | md5test(b"\xaa" * 80, |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 47 | b"Test Using Larger Than Block-Size Key - Hash Key First", |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 48 | "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") |
| 49 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 50 | md5test(b"\xaa" * 80, |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 51 | (b"Test Using Larger Than Block-Size Key " |
| 52 | b"and Larger Than One Block-Size Data"), |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 53 | "6f630fad67cda0ee1fb1f562db3aa53e") |
| 54 | |
| 55 | def test_sha_vectors(self): |
| 56 | def shatest(key, data, digest): |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 57 | h = hmac.HMAC(key, data, digestmod=hashlib.sha1) |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 58 | self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 59 | self.assertEqual(h.name, "hmac-sha1") |
| 60 | self.assertEqual(h.digest_size, 20) |
| 61 | self.assertEqual(h.block_size, 64) |
| 62 | |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 63 | h = hmac.HMAC(key, data, digestmod='sha1') |
| 64 | self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 65 | self.assertEqual(h.name, "hmac-sha1") |
| 66 | self.assertEqual(h.digest_size, 20) |
| 67 | self.assertEqual(h.block_size, 64) |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 68 | |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 69 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 70 | shatest(b"\x0b" * 20, |
| 71 | b"Hi There", |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 72 | "b617318655057264e28bc0b6fb378c8ef146be00") |
| 73 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 74 | shatest(b"Jefe", |
| 75 | b"what do ya want for nothing?", |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 76 | "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") |
| 77 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 78 | shatest(b"\xAA" * 20, |
| 79 | b"\xDD" * 50, |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 80 | "125d7342b9ac11cd91a39af48aa17b4f63f175d3") |
| 81 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 82 | shatest(bytes(range(1, 26)), |
| 83 | b"\xCD" * 50, |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 84 | "4c9007f4026250c6bc8414f9bf50c86c2d7235da") |
| 85 | |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 86 | shatest(b"\x0C" * 20, |
| 87 | b"Test With Truncation", |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 88 | "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") |
| 89 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 90 | shatest(b"\xAA" * 80, |
| 91 | b"Test Using Larger Than Block-Size Key - Hash Key First", |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 92 | "aa4ae5e15272d00e95705637ce8a3b55ed402112") |
| 93 | |
| Guido van Rossum | 3f42908 | 2007-07-10 13:35:52 +0000 | [diff] [blame] | 94 | shatest(b"\xAA" * 80, |
| 95 | (b"Test Using Larger Than Block-Size Key " |
| 96 | b"and Larger Than One Block-Size Data"), |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 97 | "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") |
| 98 | |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 99 | def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size): |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 100 | def hmactest(key, data, hexdigests): |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 101 | hmac_name = "hmac-" + hash_name |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 102 | h = hmac.HMAC(key, data, digestmod=hashfunc) |
| 103 | self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc]) |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 104 | self.assertEqual(h.name, hmac_name) |
| 105 | self.assertEqual(h.digest_size, digest_size) |
| 106 | self.assertEqual(h.block_size, block_size) |
| 107 | |
| 108 | h = hmac.HMAC(key, data, digestmod=hash_name) |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 109 | self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc]) |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 110 | self.assertEqual(h.name, hmac_name) |
| 111 | self.assertEqual(h.digest_size, digest_size) |
| 112 | self.assertEqual(h.block_size, block_size) |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 113 | |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 114 | |
| 115 | # 4.2. Test Case 1 |
| 116 | hmactest(key = b'\x0b'*20, |
| 117 | data = b'Hi There', |
| 118 | hexdigests = { |
| 119 | hashlib.sha224: '896fb1128abbdf196832107cd49df33f' |
| 120 | '47b4b1169912ba4f53684b22', |
| 121 | hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b' |
| 122 | '881dc200c9833da726e9376c2e32cff7', |
| 123 | hashlib.sha384: 'afd03944d84895626b0825f4ab46907f' |
| 124 | '15f9dadbe4101ec682aa034c7cebc59c' |
| 125 | 'faea9ea9076ede7f4af152e8b2fa9cb6', |
| 126 | hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0' |
| 127 | '2379f4e2ce4ec2787ad0b30545e17cde' |
| 128 | 'daa833b7d6b8a702038b274eaea3f4e4' |
| 129 | 'be9d914eeb61f1702e696c203a126854', |
| 130 | }) |
| 131 | |
| 132 | # 4.3. Test Case 2 |
| 133 | hmactest(key = b'Jefe', |
| 134 | data = b'what do ya want for nothing?', |
| 135 | hexdigests = { |
| 136 | hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f' |
| 137 | '8bbea2a39e6148008fd05e44', |
| 138 | hashlib.sha256: '5bdcc146bf60754e6a042426089575c7' |
| 139 | '5a003f089d2739839dec58b964ec3843', |
| 140 | hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b' |
| 141 | '9c7ef464f5a01b47e42ec3736322445e' |
| 142 | '8e2240ca5e69e2c78b3239ecfab21649', |
| 143 | hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3' |
| 144 | '87bd64222e831fd610270cd7ea250554' |
| 145 | '9758bf75c05a994a6d034f65f8f0e6fd' |
| 146 | 'caeab1a34d4a6b4b636e070a38bce737', |
| 147 | }) |
| 148 | |
| 149 | # 4.4. Test Case 3 |
| 150 | hmactest(key = b'\xaa'*20, |
| 151 | data = b'\xdd'*50, |
| 152 | hexdigests = { |
| 153 | hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264' |
| 154 | '9365b0c1f65d69d1ec8333ea', |
| 155 | hashlib.sha256: '773ea91e36800e46854db8ebd09181a7' |
| 156 | '2959098b3ef8c122d9635514ced565fe', |
| 157 | hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f' |
| 158 | '0aa635d947ac9febe83ef4e55966144b' |
| 159 | '2a5ab39dc13814b94e3ab6e101a34f27', |
| 160 | hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9' |
| 161 | 'b1b5dbdd8ee81a3655f83e33b2279d39' |
| 162 | 'bf3e848279a722c806b485a47e67c807' |
| 163 | 'b946a337bee8942674278859e13292fb', |
| 164 | }) |
| 165 | |
| 166 | # 4.5. Test Case 4 |
| 167 | hmactest(key = bytes(x for x in range(0x01, 0x19+1)), |
| 168 | data = b'\xcd'*50, |
| 169 | hexdigests = { |
| 170 | hashlib.sha224: '6c11506874013cac6a2abc1bb382627c' |
| 171 | 'ec6a90d86efc012de7afec5a', |
| 172 | hashlib.sha256: '82558a389a443c0ea4cc819899f2083a' |
| 173 | '85f0faa3e578f8077a2e3ff46729665b', |
| 174 | hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7' |
| 175 | '7a9981480850009cc5577c6e1f573b4e' |
| 176 | '6801dd23c4a7d679ccf8a386c674cffb', |
| 177 | hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7' |
| 178 | 'e576d97ff94b872de76f8050361ee3db' |
| 179 | 'a91ca5c11aa25eb4d679275cc5788063' |
| 180 | 'a5f19741120c4f2de2adebeb10a298dd', |
| 181 | }) |
| 182 | |
| 183 | # 4.7. Test Case 6 |
| 184 | hmactest(key = b'\xaa'*131, |
| 185 | data = b'Test Using Larger Than Block-Siz' |
| 186 | b'e Key - Hash Key First', |
| 187 | hexdigests = { |
| 188 | hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2' |
| 189 | 'd499f112f2d2b7273fa6870e', |
| 190 | hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f' |
| 191 | '8e0bc6213728c5140546040f0ee37f54', |
| 192 | hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4' |
| 193 | '4f9ef1012a2b588f3cd11f05033ac4c6' |
| 194 | '0c2ef6ab4030fe8296248df163f44952', |
| 195 | hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4' |
| 196 | '9b46d1f41b4aeec1121b013783f8f352' |
| 197 | '6b56d037e05f2598bd0fd2215d6a1e52' |
| 198 | '95e64f73f63f0aec8b915a985d786598', |
| 199 | }) |
| 200 | |
| 201 | # 4.8. Test Case 7 |
| 202 | hmactest(key = b'\xaa'*131, |
| 203 | data = b'This is a test using a larger th' |
| 204 | b'an block-size key and a larger t' |
| 205 | b'han block-size data. The key nee' |
| 206 | b'ds to be hashed before being use' |
| 207 | b'd by the HMAC algorithm.', |
| 208 | hexdigests = { |
| 209 | hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd' |
| 210 | '946770db9c2b95c9f6f565d1', |
| 211 | hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944' |
| 212 | 'bfdc63644f0713938a7f51535c3a35e2', |
| 213 | hashlib.sha384: '6617178e941f020d351e2f254e8fd32c' |
| 214 | '602420feb0b8fb9adccebb82461e99c5' |
| 215 | 'a678cc31e799176d3860e6110c46523e', |
| 216 | hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd' |
| 217 | 'debd71f8867289865df5a32d20cdc944' |
| 218 | 'b6022cac3c4982b10d5eeb55c3e4de15' |
| 219 | '134676fb6de0446065c97440fa8c6a58', |
| 220 | }) |
| 221 | |
| 222 | def test_sha224_rfc4231(self): |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 223 | self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 224 | |
| 225 | def test_sha256_rfc4231(self): |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 226 | self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 227 | |
| 228 | def test_sha384_rfc4231(self): |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 229 | self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 230 | |
| 231 | def test_sha512_rfc4231(self): |
| Christian Heimes | c4ab110 | 2013-11-20 17:35:06 +0100 | [diff] [blame] | 232 | self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 233 | |
| 234 | def test_legacy_block_size_warnings(self): |
| 235 | class MockCrazyHash(object): |
| 236 | """Ain't no block_size attribute here.""" |
| 237 | def __init__(self, *args): |
| 238 | self._x = hashlib.sha1(*args) |
| 239 | self.digest_size = self._x.digest_size |
| 240 | def update(self, v): |
| 241 | self._x.update(v) |
| 242 | def digest(self): |
| 243 | return self._x.digest() |
| 244 | |
| Brett Cannon | 1cd0247 | 2008-09-09 01:52:27 +0000 | [diff] [blame] | 245 | with warnings.catch_warnings(): |
| Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 246 | warnings.simplefilter('error', RuntimeWarning) |
| Florent Xicluna | 41fe615 | 2010-04-02 18:52:12 +0000 | [diff] [blame] | 247 | with self.assertRaises(RuntimeWarning): |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 248 | hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 249 | self.fail('Expected warning about missing block_size') |
| 250 | |
| 251 | MockCrazyHash.block_size = 1 |
| Florent Xicluna | 41fe615 | 2010-04-02 18:52:12 +0000 | [diff] [blame] | 252 | with self.assertRaises(RuntimeWarning): |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 253 | hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 254 | self.fail('Expected warning about small block_size') |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 255 | |
| Christian Heimes | 634919a | 2013-11-20 17:23:06 +0100 | [diff] [blame] | 256 | def test_with_digestmod_warning(self): |
| 257 | with self.assertWarns(PendingDeprecationWarning): |
| 258 | key = b"\x0b" * 16 |
| 259 | data = b"Hi There" |
| 260 | digest = "9294727A3638BB1C13F48EF8158BFC9D" |
| 261 | h = hmac.HMAC(key, data) |
| 262 | self.assertEqual(h.hexdigest().upper(), digest) |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 263 | |
| Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 264 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 265 | class ConstructorTestCase(unittest.TestCase): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 266 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 267 | def test_normal(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 268 | # Standard constructor call. |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 269 | failed = 0 |
| 270 | try: |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 271 | h = hmac.HMAC(b"key") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 272 | except: |
| 273 | self.fail("Standard constructor call raised exception.") |
| 274 | |
| Antoine Pitrou | 24ef3e9 | 2012-06-30 17:27:56 +0200 | [diff] [blame] | 275 | def test_with_str_key(self): |
| 276 | # Pass a key of type str, which is an error, because it expects a key |
| 277 | # of type bytes |
| 278 | with self.assertRaises(TypeError): |
| 279 | h = hmac.HMAC("key") |
| 280 | |
| 281 | def test_dot_new_with_str_key(self): |
| 282 | # Pass a key of type str, which is an error, because it expects a key |
| 283 | # of type bytes |
| 284 | with self.assertRaises(TypeError): |
| 285 | h = hmac.new("key") |
| 286 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 287 | def test_withtext(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 288 | # Constructor call with text. |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 289 | try: |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 290 | h = hmac.HMAC(b"key", b"hash this!") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 291 | except: |
| 292 | self.fail("Constructor call with text argument raised exception.") |
| 293 | |
| Christian Heimes | 04926ae | 2013-07-01 13:08:42 +0200 | [diff] [blame] | 294 | def test_with_bytearray(self): |
| 295 | try: |
| 296 | h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!")) |
| 297 | self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') |
| 298 | except: |
| 299 | self.fail("Constructor call with bytearray arguments raised exception.") |
| 300 | |
| 301 | def test_with_memoryview_msg(self): |
| 302 | try: |
| 303 | h = hmac.HMAC(b"key", memoryview(b"hash this!")) |
| 304 | self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') |
| 305 | except: |
| 306 | self.fail("Constructor call with memoryview msg raised exception.") |
| 307 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 308 | def test_withmodule(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 309 | # Constructor call with text and digest module. |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 310 | try: |
| Guido van Rossum | a19f80c | 2007-11-06 20:51:31 +0000 | [diff] [blame] | 311 | h = hmac.HMAC(b"key", b"", hashlib.sha1) |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 312 | except: |
| Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 313 | self.fail("Constructor call with hashlib.sha1 raised exception.") |
| Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 314 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 315 | class SanityTestCase(unittest.TestCase): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 316 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 317 | def test_default_is_md5(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 318 | # Testing if HMAC defaults to MD5 algorithm. |
| Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 319 | # NOTE: this whitebox test depends on the hmac class internals |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 320 | h = hmac.HMAC(b"key") |
| Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 321 | self.assertEqual(h.digest_cons, hashlib.md5) |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 322 | |
| 323 | def test_exercise_all_methods(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 324 | # Exercising all methods once. |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 325 | # This must not raise any exceptions |
| 326 | try: |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 327 | h = hmac.HMAC(b"my secret key") |
| 328 | h.update(b"compute the hash of this text!") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 329 | dig = h.digest() |
| 330 | dig = h.hexdigest() |
| 331 | h2 = h.copy() |
| 332 | except: |
| Neal Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 333 | self.fail("Exception raised during normal usage of HMAC class.") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 334 | |
| 335 | class CopyTestCase(unittest.TestCase): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 336 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 337 | def test_attributes(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 338 | # Testing if attributes are of same type. |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 339 | h1 = hmac.HMAC(b"key") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 340 | h2 = h1.copy() |
| Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 341 | self.assertTrue(h1.digest_cons == h2.digest_cons, |
| Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 342 | "digest constructors don't match.") |
| Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 343 | self.assertEqual(type(h1.inner), type(h2.inner), |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 344 | "Types of inner don't match.") |
| Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 345 | self.assertEqual(type(h1.outer), type(h2.outer), |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 346 | "Types of outer don't match.") |
| 347 | |
| 348 | def test_realcopy(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 349 | # Testing if the copy method created a real copy. |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 350 | h1 = hmac.HMAC(b"key") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 351 | h2 = h1.copy() |
| Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 352 | # Using id() in case somebody has overridden __eq__/__ne__. |
| Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 353 | self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.") |
| 354 | self.assertTrue(id(h1.inner) != id(h2.inner), |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 355 | "No real copy of the attribute 'inner'.") |
| Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 356 | self.assertTrue(id(h1.outer) != id(h2.outer), |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 357 | "No real copy of the attribute 'outer'.") |
| 358 | |
| 359 | def test_equality(self): |
| Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 360 | # Testing if the copy has the same digests. |
| Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 361 | h1 = hmac.HMAC(b"key") |
| 362 | h1.update(b"some random text") |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 363 | h2 = h1.copy() |
| Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 364 | self.assertEqual(h1.digest(), h2.digest(), |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 365 | "Digest of copy doesn't match original digest.") |
| Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 366 | self.assertEqual(h1.hexdigest(), h2.hexdigest(), |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 367 | "Hexdigest of copy doesn't match original hexdigest.") |
| 368 | |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 369 | class CompareDigestTestCase(unittest.TestCase): |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 370 | |
| Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 371 | def test_compare_digest(self): |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 372 | # Testing input type exception handling |
| 373 | a, b = 100, 200 |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 374 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 375 | a, b = 100, b"foobar" |
| 376 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 377 | a, b = b"foobar", 200 |
| 378 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 379 | a, b = "foobar", b"foobar" |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 380 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 381 | a, b = b"foobar", "foobar" |
| 382 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 383 | |
| 384 | # Testing bytes of different lengths |
| 385 | a, b = b"foobar", b"foo" |
| 386 | self.assertFalse(hmac.compare_digest(a, b)) |
| 387 | a, b = b"\xde\xad\xbe\xef", b"\xde\xad" |
| 388 | self.assertFalse(hmac.compare_digest(a, b)) |
| 389 | |
| 390 | # Testing bytes of same lengths, different values |
| 391 | a, b = b"foobar", b"foobaz" |
| 392 | self.assertFalse(hmac.compare_digest(a, b)) |
| 393 | a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea" |
| 394 | self.assertFalse(hmac.compare_digest(a, b)) |
| 395 | |
| 396 | # Testing bytes of same lengths, same values |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 397 | a, b = b"foobar", b"foobar" |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 398 | self.assertTrue(hmac.compare_digest(a, b)) |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 399 | a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef" |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 400 | self.assertTrue(hmac.compare_digest(a, b)) |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 401 | |
| Christian Heimes | 6cea655 | 2012-06-24 13:48:32 +0200 | [diff] [blame] | 402 | # Testing bytearrays of same lengths, same values |
| 403 | a, b = bytearray(b"foobar"), bytearray(b"foobar") |
| 404 | self.assertTrue(hmac.compare_digest(a, b)) |
| 405 | |
| 406 | # Testing bytearrays of diffeent lengths |
| 407 | a, b = bytearray(b"foobar"), bytearray(b"foo") |
| 408 | self.assertFalse(hmac.compare_digest(a, b)) |
| 409 | |
| 410 | # Testing bytearrays of same lengths, different values |
| 411 | a, b = bytearray(b"foobar"), bytearray(b"foobaz") |
| 412 | self.assertFalse(hmac.compare_digest(a, b)) |
| 413 | |
| 414 | # Testing byte and bytearray of same lengths, same values |
| 415 | a, b = bytearray(b"foobar"), b"foobar" |
| 416 | self.assertTrue(hmac.compare_digest(a, b)) |
| 417 | self.assertTrue(hmac.compare_digest(b, a)) |
| 418 | |
| 419 | # Testing byte bytearray of diffeent lengths |
| 420 | a, b = bytearray(b"foobar"), b"foo" |
| 421 | self.assertFalse(hmac.compare_digest(a, b)) |
| 422 | self.assertFalse(hmac.compare_digest(b, a)) |
| 423 | |
| 424 | # Testing byte and bytearray of same lengths, different values |
| 425 | a, b = bytearray(b"foobar"), b"foobaz" |
| 426 | self.assertFalse(hmac.compare_digest(a, b)) |
| 427 | self.assertFalse(hmac.compare_digest(b, a)) |
| 428 | |
| 429 | # Testing str of same lengths |
| 430 | a, b = "foobar", "foobar" |
| 431 | self.assertTrue(hmac.compare_digest(a, b)) |
| 432 | |
| 433 | # Testing str of diffeent lengths |
| 434 | a, b = "foo", "foobar" |
| 435 | self.assertFalse(hmac.compare_digest(a, b)) |
| 436 | |
| 437 | # Testing bytes of same lengths, different values |
| 438 | a, b = "foobar", "foobaz" |
| 439 | self.assertFalse(hmac.compare_digest(a, b)) |
| 440 | |
| 441 | # Testing error cases |
| 442 | a, b = "foobar", b"foobar" |
| 443 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 444 | a, b = b"foobar", "foobar" |
| 445 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 446 | a, b = b"foobar", 1 |
| 447 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 448 | a, b = 100, 200 |
| 449 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 450 | a, b = "fooä", "fooä" |
| 451 | self.assertRaises(TypeError, hmac.compare_digest, a, b) |
| 452 | |
| 453 | # subclasses are supported by ignore __eq__ |
| 454 | class mystr(str): |
| 455 | def __eq__(self, other): |
| 456 | return False |
| 457 | |
| 458 | a, b = mystr("foobar"), mystr("foobar") |
| 459 | self.assertTrue(hmac.compare_digest(a, b)) |
| 460 | a, b = mystr("foobar"), "foobar" |
| 461 | self.assertTrue(hmac.compare_digest(a, b)) |
| 462 | a, b = mystr("foobar"), mystr("foobaz") |
| 463 | self.assertFalse(hmac.compare_digest(a, b)) |
| 464 | |
| 465 | class mybytes(bytes): |
| 466 | def __eq__(self, other): |
| 467 | return False |
| 468 | |
| 469 | a, b = mybytes(b"foobar"), mybytes(b"foobar") |
| 470 | self.assertTrue(hmac.compare_digest(a, b)) |
| 471 | a, b = mybytes(b"foobar"), b"foobar" |
| 472 | self.assertTrue(hmac.compare_digest(a, b)) |
| 473 | a, b = mybytes(b"foobar"), mybytes(b"foobaz") |
| 474 | self.assertFalse(hmac.compare_digest(a, b)) |
| 475 | |
| 476 | |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 477 | def test_main(): |
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 478 | support.run_unittest( |
| Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 479 | TestVectorsTestCase, |
| 480 | ConstructorTestCase, |
| 481 | SanityTestCase, |
| Charles-François Natali | 7feb9f4 | 2012-05-13 19:53:07 +0200 | [diff] [blame] | 482 | CopyTestCase, |
| Nick Coghlan | 807770e | 2012-06-15 21:14:08 +1000 | [diff] [blame] | 483 | CompareDigestTestCase |
| Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 484 | ) |
| Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 485 | |
| 486 | if __name__ == "__main__": |
| 487 | test_main() |