| 1 |
import binascii |
| 2 |
import functools |
| 3 |
import hmac |
| 4 |
import hashlib |
| 5 |
import unittest |
| 6 |
import unittest.mock |
| 7 |
import warnings |
| 8 |
|
| 9 |
from test.support import hashlib_helper, check_disallow_instantiation |
| 10 |
|
| 11 |
from _operator import _compare_digest as operator_compare_digest |
| 12 |
|
| 13 |
try: |
| 14 |
import _hashlib as _hashopenssl |
| 15 |
from _hashlib import HMAC as C_HMAC |
| 16 |
from _hashlib import hmac_new as c_hmac_new |
| 17 |
from _hashlib import compare_digest as openssl_compare_digest |
| 18 |
except ImportError: |
| 19 |
_hashopenssl = None |
| 20 |
C_HMAC = None |
| 21 |
c_hmac_new = None |
| 22 |
openssl_compare_digest = None |
| 23 |
|
| 24 |
try: |
| 25 |
import _sha256 as sha256_module |
| 26 |
except ImportError: |
| 27 |
sha256_module = None |
| 28 |
|
| 29 |
|
| 30 |
def ignore_warning(func): |
| 31 |
@functools.wraps(func) |
| 32 |
def wrapper(*args, **kwargs): |
| 33 |
with warnings.catch_warnings(): |
| 34 |
warnings.filterwarnings("ignore", |
| 35 |
category=DeprecationWarning) |
| 36 |
return func(*args, **kwargs) |
| 37 |
return wrapper |
| 38 |
|
| 39 |
|
| 40 |
class TestVectorsTestCase(unittest.TestCase): |
| 41 |
|
| 42 |
def assert_hmac_internals( |
| 43 |
self, h, digest, hashname, digest_size, block_size |
| 44 |
): |
| 45 |
self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 46 |
self.assertEqual(h.digest(), binascii.unhexlify(digest)) |
| 47 |
self.assertEqual(h.name, f"hmac-{hashname}") |
| 48 |
self.assertEqual(h.digest_size, digest_size) |
| 49 |
self.assertEqual(h.block_size, block_size) |
| 50 |
|
| 51 |
def assert_hmac( |
| 52 |
self, key, data, digest, hashfunc, hashname, digest_size, block_size |
| 53 |
): |
| 54 |
h = hmac.HMAC(key, data, digestmod=hashfunc) |
| 55 |
self.assert_hmac_internals( |
| 56 |
h, digest, hashname, digest_size, block_size |
| 57 |
) |
| 58 |
|
| 59 |
h = hmac.HMAC(key, data, digestmod=hashname) |
| 60 |
self.assert_hmac_internals( |
| 61 |
h, digest, hashname, digest_size, block_size |
| 62 |
) |
| 63 |
|
| 64 |
h = hmac.HMAC(key, digestmod=hashname) |
| 65 |
h2 = h.copy() |
| 66 |
h2.update(b"test update") |
| 67 |
h.update(data) |
| 68 |
self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 69 |
|
| 70 |
h = hmac.new(key, data, digestmod=hashname) |
| 71 |
self.assert_hmac_internals( |
| 72 |
h, digest, hashname, digest_size, block_size |
| 73 |
) |
| 74 |
|
| 75 |
h = hmac.new(key, None, digestmod=hashname) |
| 76 |
h.update(data) |
| 77 |
self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 78 |
|
| 79 |
h = hmac.new(key, digestmod=hashname) |
| 80 |
h.update(data) |
| 81 |
self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 82 |
|
| 83 |
h = hmac.new(key, data, digestmod=hashfunc) |
| 84 |
self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 85 |
|
| 86 |
self.assertEqual( |
| 87 |
hmac.digest(key, data, digest=hashname), |
| 88 |
binascii.unhexlify(digest) |
| 89 |
) |
| 90 |
self.assertEqual( |
| 91 |
hmac.digest(key, data, digest=hashfunc), |
| 92 |
binascii.unhexlify(digest) |
| 93 |
) |
| 94 |
|
| 95 |
h = hmac.HMAC.__new__(hmac.HMAC) |
| 96 |
h._init_old(key, data, digestmod=hashname) |
| 97 |
self.assert_hmac_internals( |
| 98 |
h, digest, hashname, digest_size, block_size |
| 99 |
) |
| 100 |
|
| 101 |
if c_hmac_new is not None: |
| 102 |
h = c_hmac_new(key, data, digestmod=hashname) |
| 103 |
self.assert_hmac_internals( |
| 104 |
h, digest, hashname, digest_size, block_size |
| 105 |
) |
| 106 |
|
| 107 |
h = c_hmac_new(key, digestmod=hashname) |
| 108 |
h2 = h.copy() |
| 109 |
h2.update(b"test update") |
| 110 |
h.update(data) |
| 111 |
self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 112 |
|
| 113 |
func = getattr(_hashopenssl, f"openssl_{hashname}") |
| 114 |
h = c_hmac_new(key, data, digestmod=func) |
| 115 |
self.assert_hmac_internals( |
| 116 |
h, digest, hashname, digest_size, block_size |
| 117 |
) |
| 118 |
|
| 119 |
h = hmac.HMAC.__new__(hmac.HMAC) |
| 120 |
h._init_hmac(key, data, digestmod=hashname) |
| 121 |
self.assert_hmac_internals( |
| 122 |
h, digest, hashname, digest_size, block_size |
| 123 |
) |
| 124 |
|
| 125 |
@hashlib_helper.requires_hashdigest('md5', openssl=True) |
| 126 |
def test_md5_vectors(self): |
| 127 |
# Test the HMAC module against test vectors from the RFC. |
| 128 |
|
| 129 |
def md5test(key, data, digest): |
| 130 |
self.assert_hmac( |
| 131 |
key, data, digest, |
| 132 |
hashfunc=hashlib.md5, |
| 133 |
hashname="md5", |
| 134 |
digest_size=16, |
| 135 |
block_size=64 |
| 136 |
) |
| 137 |
|
| 138 |
md5test(b"\x0b" * 16, |
| 139 |
b"Hi There", |
| 140 |
"9294727A3638BB1C13F48EF8158BFC9D") |
| 141 |
|
| 142 |
md5test(b"Jefe", |
| 143 |
b"what do ya want for nothing?", |
| 144 |
"750c783e6ab0b503eaa86e310a5db738") |
| 145 |
|
| 146 |
md5test(b"\xaa" * 16, |
| 147 |
b"\xdd" * 50, |
| 148 |
"56be34521d144c88dbb8c733f0e8b3f6") |
| 149 |
|
| 150 |
md5test(bytes(range(1, 26)), |
| 151 |
b"\xcd" * 50, |
| 152 |
"697eaf0aca3a3aea3a75164746ffaa79") |
| 153 |
|
| 154 |
md5test(b"\x0C" * 16, |
| 155 |
b"Test With Truncation", |
| 156 |
"56461ef2342edc00f9bab995690efd4c") |
| 157 |
|
| 158 |
md5test(b"\xaa" * 80, |
| 159 |
b"Test Using Larger Than Block-Size Key - Hash Key First", |
| 160 |
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") |
| 161 |
|
| 162 |
md5test(b"\xaa" * 80, |
| 163 |
(b"Test Using Larger Than Block-Size Key " |
| 164 |
b"and Larger Than One Block-Size Data"), |
| 165 |
"6f630fad67cda0ee1fb1f562db3aa53e") |
| 166 |
|
| 167 |
@hashlib_helper.requires_hashdigest('sha1', openssl=True) |
| 168 |
def test_sha_vectors(self): |
| 169 |
def shatest(key, data, digest): |
| 170 |
self.assert_hmac( |
| 171 |
key, data, digest, |
| 172 |
hashfunc=hashlib.sha1, |
| 173 |
hashname="sha1", |
| 174 |
digest_size=20, |
| 175 |
block_size=64 |
| 176 |
) |
| 177 |
|
| 178 |
shatest(b"\x0b" * 20, |
| 179 |
b"Hi There", |
| 180 |
"b617318655057264e28bc0b6fb378c8ef146be00") |
| 181 |
|
| 182 |
shatest(b"Jefe", |
| 183 |
b"what do ya want for nothing?", |
| 184 |
"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") |
| 185 |
|
| 186 |
shatest(b"\xAA" * 20, |
| 187 |
b"\xDD" * 50, |
| 188 |
"125d7342b9ac11cd91a39af48aa17b4f63f175d3") |
| 189 |
|
| 190 |
shatest(bytes(range(1, 26)), |
| 191 |
b"\xCD" * 50, |
| 192 |
"4c9007f4026250c6bc8414f9bf50c86c2d7235da") |
| 193 |
|
| 194 |
shatest(b"\x0C" * 20, |
| 195 |
b"Test With Truncation", |
| 196 |
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") |
| 197 |
|
| 198 |
shatest(b"\xAA" * 80, |
| 199 |
b"Test Using Larger Than Block-Size Key - Hash Key First", |
| 200 |
"aa4ae5e15272d00e95705637ce8a3b55ed402112") |
| 201 |
|
| 202 |
shatest(b"\xAA" * 80, |
| 203 |
(b"Test Using Larger Than Block-Size Key " |
| 204 |
b"and Larger Than One Block-Size Data"), |
| 205 |
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91") |
| 206 |
|
| 207 |
def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size): |
| 208 |
def hmactest(key, data, hexdigests): |
| 209 |
digest = hexdigests[hashfunc] |
| 210 |
|
| 211 |
self.assert_hmac( |
| 212 |
key, data, digest, |
| 213 |
hashfunc=hashfunc, |
| 214 |
hashname=hash_name, |
| 215 |
digest_size=digest_size, |
| 216 |
block_size=block_size |
| 217 |
) |
| 218 |
|
| 219 |
# 4.2. Test Case 1 |
| 220 |
hmactest(key = b'\x0b'*20, |
| 221 |
data = b'Hi There', |
| 222 |
hexdigests = { |
| 223 |
hashlib.sha224: '896fb1128abbdf196832107cd49df33f' |
| 224 |
'47b4b1169912ba4f53684b22', |
| 225 |
hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b' |
| 226 |
'881dc200c9833da726e9376c2e32cff7', |
| 227 |
hashlib.sha384: 'afd03944d84895626b0825f4ab46907f' |
| 228 |
'15f9dadbe4101ec682aa034c7cebc59c' |
| 229 |
'faea9ea9076ede7f4af152e8b2fa9cb6', |
| 230 |
hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0' |
| 231 |
'2379f4e2ce4ec2787ad0b30545e17cde' |
| 232 |
'daa833b7d6b8a702038b274eaea3f4e4' |
| 233 |
'be9d914eeb61f1702e696c203a126854', |
| 234 |
}) |
| 235 |
|
| 236 |
# 4.3. Test Case 2 |
| 237 |
hmactest(key = b'Jefe', |
| 238 |
data = b'what do ya want for nothing?', |
| 239 |
hexdigests = { |
| 240 |
hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f' |
| 241 |
'8bbea2a39e6148008fd05e44', |
| 242 |
hashlib.sha256: '5bdcc146bf60754e6a042426089575c7' |
| 243 |
'5a003f089d2739839dec58b964ec3843', |
| 244 |
hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b' |
| 245 |
'9c7ef464f5a01b47e42ec3736322445e' |
| 246 |
'8e2240ca5e69e2c78b3239ecfab21649', |
| 247 |
hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3' |
| 248 |
'87bd64222e831fd610270cd7ea250554' |
| 249 |
'9758bf75c05a994a6d034f65f8f0e6fd' |
| 250 |
'caeab1a34d4a6b4b636e070a38bce737', |
| 251 |
}) |
| 252 |
|
| 253 |
# 4.4. Test Case 3 |
| 254 |
hmactest(key = b'\xaa'*20, |
| 255 |
data = b'\xdd'*50, |
| 256 |
hexdigests = { |
| 257 |
hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264' |
| 258 |
'9365b0c1f65d69d1ec8333ea', |
| 259 |
hashlib.sha256: '773ea91e36800e46854db8ebd09181a7' |
| 260 |
'2959098b3ef8c122d9635514ced565fe', |
| 261 |
hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f' |
| 262 |
'0aa635d947ac9febe83ef4e55966144b' |
| 263 |
'2a5ab39dc13814b94e3ab6e101a34f27', |
| 264 |
hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9' |
| 265 |
'b1b5dbdd8ee81a3655f83e33b2279d39' |
| 266 |
'bf3e848279a722c806b485a47e67c807' |
| 267 |
'b946a337bee8942674278859e13292fb', |
| 268 |
}) |
| 269 |
|
| 270 |
# 4.5. Test Case 4 |
| 271 |
hmactest(key = bytes(x for x in range(0x01, 0x19+1)), |
| 272 |
data = b'\xcd'*50, |
| 273 |
hexdigests = { |
| 274 |
hashlib.sha224: '6c11506874013cac6a2abc1bb382627c' |
| 275 |
'ec6a90d86efc012de7afec5a', |
| 276 |
hashlib.sha256: '82558a389a443c0ea4cc819899f2083a' |
| 277 |
'85f0faa3e578f8077a2e3ff46729665b', |
| 278 |
hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7' |
| 279 |
'7a9981480850009cc5577c6e1f573b4e' |
| 280 |
'6801dd23c4a7d679ccf8a386c674cffb', |
| 281 |
hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7' |
| 282 |
'e576d97ff94b872de76f8050361ee3db' |
| 283 |
'a91ca5c11aa25eb4d679275cc5788063' |
| 284 |
'a5f19741120c4f2de2adebeb10a298dd', |
| 285 |
}) |
| 286 |
|
| 287 |
# 4.7. Test Case 6 |
| 288 |
hmactest(key = b'\xaa'*131, |
| 289 |
data = b'Test Using Larger Than Block-Siz' |
| 290 |
b'e Key - Hash Key First', |
| 291 |
hexdigests = { |
| 292 |
hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2' |
| 293 |
'd499f112f2d2b7273fa6870e', |
| 294 |
hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f' |
| 295 |
'8e0bc6213728c5140546040f0ee37f54', |
| 296 |
hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4' |
| 297 |
'4f9ef1012a2b588f3cd11f05033ac4c6' |
| 298 |
'0c2ef6ab4030fe8296248df163f44952', |
| 299 |
hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4' |
| 300 |
'9b46d1f41b4aeec1121b013783f8f352' |
| 301 |
'6b56d037e05f2598bd0fd2215d6a1e52' |
| 302 |
'95e64f73f63f0aec8b915a985d786598', |
| 303 |
}) |
| 304 |
|
| 305 |
# 4.8. Test Case 7 |
| 306 |
hmactest(key = b'\xaa'*131, |
| 307 |
data = b'This is a test using a larger th' |
| 308 |
b'an block-size key and a larger t' |
| 309 |
b'han block-size data. The key nee' |
| 310 |
b'ds to be hashed before being use' |
| 311 |
b'd by the HMAC algorithm.', |
| 312 |
hexdigests = { |
| 313 |
hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd' |
| 314 |
'946770db9c2b95c9f6f565d1', |
| 315 |
hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944' |
| 316 |
'bfdc63644f0713938a7f51535c3a35e2', |
| 317 |
hashlib.sha384: '6617178e941f020d351e2f254e8fd32c' |
| 318 |
'602420feb0b8fb9adccebb82461e99c5' |
| 319 |
'a678cc31e799176d3860e6110c46523e', |
| 320 |
hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd' |
| 321 |
'debd71f8867289865df5a32d20cdc944' |
| 322 |
'b6022cac3c4982b10d5eeb55c3e4de15' |
| 323 |
'134676fb6de0446065c97440fa8c6a58', |
| 324 |
}) |
| 325 |
|
| 326 |
@hashlib_helper.requires_hashdigest('sha224', openssl=True) |
| 327 |
def test_sha224_rfc4231(self): |
| 328 |
self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64) |
| 329 |
|
| 330 |
@hashlib_helper.requires_hashdigest('sha256', openssl=True) |
| 331 |
def test_sha256_rfc4231(self): |
| 332 |
self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64) |
| 333 |
|
| 334 |
@hashlib_helper.requires_hashdigest('sha384', openssl=True) |
| 335 |
def test_sha384_rfc4231(self): |
| 336 |
self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128) |
| 337 |
|
| 338 |
@hashlib_helper.requires_hashdigest('sha512', openssl=True) |
| 339 |
def test_sha512_rfc4231(self): |
| 340 |
self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128) |
| 341 |
|
| 342 |
@hashlib_helper.requires_hashdigest('sha256') |
| 343 |
def test_legacy_block_size_warnings(self): |
| 344 |
class MockCrazyHash(object): |
| 345 |
"""Ain't no block_size attribute here.""" |
| 346 |
def __init__(self, *args): |
| 347 |
self._x = hashlib.sha256(*args) |
| 348 |
self.digest_size = self._x.digest_size |
| 349 |
def update(self, v): |
| 350 |
self._x.update(v) |
| 351 |
def digest(self): |
| 352 |
return self._x.digest() |
| 353 |
|
| 354 |
with warnings.catch_warnings(): |
| 355 |
warnings.simplefilter('error', RuntimeWarning) |
| 356 |
with self.assertRaises(RuntimeWarning): |
| 357 |
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) |
| 358 |
self.fail('Expected warning about missing block_size') |
| 359 |
|
| 360 |
MockCrazyHash.block_size = 1 |
| 361 |
with self.assertRaises(RuntimeWarning): |
| 362 |
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) |
| 363 |
self.fail('Expected warning about small block_size') |
| 364 |
|
| 365 |
def test_with_digestmod_no_default(self): |
| 366 |
"""The digestmod parameter is required as of Python 3.8.""" |
| 367 |
with self.assertRaisesRegex(TypeError, r'required.*digestmod'): |
| 368 |
key = b"\x0b" * 16 |
| 369 |
data = b"Hi There" |
| 370 |
hmac.HMAC(key, data, digestmod=None) |
| 371 |
with self.assertRaisesRegex(TypeError, r'required.*digestmod'): |
| 372 |
hmac.new(key, data) |
| 373 |
with self.assertRaisesRegex(TypeError, r'required.*digestmod'): |
| 374 |
hmac.HMAC(key, msg=data, digestmod='') |
| 375 |
|
| 376 |
def test_with_fallback(self): |
| 377 |
cache = getattr(hashlib, '__builtin_constructor_cache') |
| 378 |
try: |
| 379 |
cache['foo'] = hashlib.sha256 |
| 380 |
hexdigest = hmac.digest(b'key', b'message', 'foo').hex() |
| 381 |
expected = '6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a' |
| 382 |
self.assertEqual(hexdigest, expected) |
| 383 |
finally: |
| 384 |
cache.pop('foo') |
| 385 |
|
| 386 |
|
| 387 |
class ConstructorTestCase(unittest.TestCase): |
| 388 |
|
| 389 |
expected = ( |
| 390 |
"6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdeb" |
| 391 |
) |
| 392 |
|
| 393 |
@hashlib_helper.requires_hashdigest('sha256') |
| 394 |
def test_normal(self): |
| 395 |
# Standard constructor call. |
| 396 |
try: |
| 397 |
hmac.HMAC(b"key", digestmod='sha256') |
| 398 |
except Exception: |
| 399 |
self.fail("Standard constructor call raised exception.") |
| 400 |
|
| 401 |
@hashlib_helper.requires_hashdigest('sha256') |
| 402 |
def test_with_str_key(self): |
| 403 |
# Pass a key of type str, which is an error, because it expects a key |
| 404 |
# of type bytes |
| 405 |
with self.assertRaises(TypeError): |
| 406 |
h = hmac.HMAC("key", digestmod='sha256') |
| 407 |
|
| 408 |
@hashlib_helper.requires_hashdigest('sha256') |
| 409 |
def test_dot_new_with_str_key(self): |
| 410 |
# Pass a key of type str, which is an error, because it expects a key |
| 411 |
# of type bytes |
| 412 |
with self.assertRaises(TypeError): |
| 413 |
h = hmac.new("key", digestmod='sha256') |
| 414 |
|
| 415 |
@hashlib_helper.requires_hashdigest('sha256') |
| 416 |
def test_withtext(self): |
| 417 |
# Constructor call with text. |
| 418 |
try: |
| 419 |
h = hmac.HMAC(b"key", b"hash this!", digestmod='sha256') |
| 420 |
except Exception: |
| 421 |
self.fail("Constructor call with text argument raised exception.") |
| 422 |
self.assertEqual(h.hexdigest(), self.expected) |
| 423 |
|
| 424 |
@hashlib_helper.requires_hashdigest('sha256') |
| 425 |
def test_with_bytearray(self): |
| 426 |
try: |
| 427 |
h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!"), |
| 428 |
digestmod="sha256") |
| 429 |
except Exception: |
| 430 |
self.fail("Constructor call with bytearray arguments raised exception.") |
| 431 |
self.assertEqual(h.hexdigest(), self.expected) |
| 432 |
|
| 433 |
@hashlib_helper.requires_hashdigest('sha256') |
| 434 |
def test_with_memoryview_msg(self): |
| 435 |
try: |
| 436 |
h = hmac.HMAC(b"key", memoryview(b"hash this!"), digestmod="sha256") |
| 437 |
except Exception: |
| 438 |
self.fail("Constructor call with memoryview msg raised exception.") |
| 439 |
self.assertEqual(h.hexdigest(), self.expected) |
| 440 |
|
| 441 |
@hashlib_helper.requires_hashdigest('sha256') |
| 442 |
def test_withmodule(self): |
| 443 |
# Constructor call with text and digest module. |
| 444 |
try: |
| 445 |
h = hmac.HMAC(b"key", b"", hashlib.sha256) |
| 446 |
except Exception: |
| 447 |
self.fail("Constructor call with hashlib.sha256 raised exception.") |
| 448 |
|
| 449 |
@unittest.skipUnless(C_HMAC is not None, 'need _hashlib') |
| 450 |
def test_internal_types(self): |
| 451 |
# internal types like _hashlib.C_HMAC are not constructable |
| 452 |
check_disallow_instantiation(self, C_HMAC) |
| 453 |
with self.assertRaisesRegex(TypeError, "immutable type"): |
| 454 |
C_HMAC.value = None |
| 455 |
|
| 456 |
@unittest.skipUnless(sha256_module is not None, 'need _sha256') |
| 457 |
def test_with_sha256_module(self): |
| 458 |
h = hmac.HMAC(b"key", b"hash this!", digestmod=sha256_module.sha256) |
| 459 |
self.assertEqual(h.hexdigest(), self.expected) |
| 460 |
self.assertEqual(h.name, "hmac-sha256") |
| 461 |
|
| 462 |
digest = hmac.digest(b"key", b"hash this!", sha256_module.sha256) |
| 463 |
self.assertEqual(digest, binascii.unhexlify(self.expected)) |
| 464 |
|
| 465 |
|
| 466 |
class SanityTestCase(unittest.TestCase): |
| 467 |
|
| 468 |
@hashlib_helper.requires_hashdigest('sha256') |
| 469 |
def test_exercise_all_methods(self): |
| 470 |
# Exercising all methods once. |
| 471 |
# This must not raise any exceptions |
| 472 |
try: |
| 473 |
h = hmac.HMAC(b"my secret key", digestmod="sha256") |
| 474 |
h.update(b"compute the hash of this text!") |
| 475 |
h.digest() |
| 476 |
h.hexdigest() |
| 477 |
h.copy() |
| 478 |
except Exception: |
| 479 |
self.fail("Exception raised during normal usage of HMAC class.") |
| 480 |
|
| 481 |
|
| 482 |
class UpdateTestCase(unittest.TestCase): |
| 483 |
@hashlib_helper.requires_hashdigest('sha256') |
| 484 |
def test_with_str_update(self): |
| 485 |
with self.assertRaises(TypeError): |
| 486 |
h = hmac.new(b"key", digestmod='sha256') |
| 487 |
h.update("invalid update") |
| 488 |
|
| 489 |
|
| 490 |
class CopyTestCase(unittest.TestCase): |
| 491 |
|
| 492 |
@hashlib_helper.requires_hashdigest('sha256') |
| 493 |
def test_copy(self): |
| 494 |
# Test a generic copy() and the attributes it exposes. |
| 495 |
# See https://github.com/python/cpython/issues/142451. |
| 496 |
h1 = hmac.new(b"my secret key", digestmod="sha256") |
| 497 |
h2 = h1.copy() |
| 498 |
self.assertEqual(h1.name, h2.name) |
| 499 |
self.assertEqual(h1.digest_size, h2.digest_size) |
| 500 |
self.assertEqual(h1.block_size, h2.block_size) |
| 501 |
|
| 502 |
@hashlib_helper.requires_hashdigest('sha256') |
| 503 |
def test_attributes_old(self): |
| 504 |
# Testing if attributes are of same type. |
| 505 |
h1 = hmac.HMAC.__new__(hmac.HMAC) |
| 506 |
h1._init_old(b"key", b"msg", digestmod="sha256") |
| 507 |
h2 = h1.copy() |
| 508 |
self.assertEqual(type(h1._inner), type(h2._inner), |
| 509 |
"Types of inner don't match.") |
| 510 |
self.assertEqual(type(h1._outer), type(h2._outer), |
| 511 |
"Types of outer don't match.") |
| 512 |
|
| 513 |
@hashlib_helper.requires_hashdigest('sha256') |
| 514 |
def test_realcopy_old(self): |
| 515 |
# Testing if the copy method created a real copy. |
| 516 |
h1 = hmac.HMAC.__new__(hmac.HMAC) |
| 517 |
h1._init_old(b"key", b"msg", digestmod="sha256") |
| 518 |
h2 = h1.copy() |
| 519 |
# Using id() in case somebody has overridden __eq__/__ne__. |
| 520 |
self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.") |
| 521 |
self.assertTrue(id(h1._inner) != id(h2._inner), |
| 522 |
"No real copy of the attribute 'inner'.") |
| 523 |
self.assertTrue(id(h1._outer) != id(h2._outer), |
| 524 |
"No real copy of the attribute 'outer'.") |
| 525 |
self.assertIs(h1._hmac, None) |
| 526 |
|
| 527 |
@unittest.skipIf(_hashopenssl is None, "test requires _hashopenssl") |
| 528 |
@hashlib_helper.requires_hashdigest('sha256') |
| 529 |
def test_realcopy_hmac(self): |
| 530 |
h1 = hmac.HMAC.__new__(hmac.HMAC) |
| 531 |
h1._init_hmac(b"key", b"msg", digestmod="sha256") |
| 532 |
h2 = h1.copy() |
| 533 |
self.assertTrue(id(h1._hmac) != id(h2._hmac)) |
| 534 |
|
| 535 |
@hashlib_helper.requires_hashdigest('sha256') |
| 536 |
def test_equality(self): |
| 537 |
# Testing if the copy has the same digests. |
| 538 |
h1 = hmac.HMAC(b"key", digestmod="sha256") |
| 539 |
h1.update(b"some random text") |
| 540 |
h2 = h1.copy() |
| 541 |
self.assertEqual(h1.digest(), h2.digest(), |
| 542 |
"Digest of copy doesn't match original digest.") |
| 543 |
self.assertEqual(h1.hexdigest(), h2.hexdigest(), |
| 544 |
"Hexdigest of copy doesn't match original hexdigest.") |
| 545 |
|
| 546 |
@hashlib_helper.requires_hashdigest('sha256') |
| 547 |
def test_equality_new(self): |
| 548 |
# Testing if the copy has the same digests with hmac.new(). |
| 549 |
h1 = hmac.new(b"key", digestmod="sha256") |
| 550 |
h1.update(b"some random text") |
| 551 |
h2 = h1.copy() |
| 552 |
self.assertTrue( |
| 553 |
id(h1) != id(h2), "No real copy of the HMAC instance." |
| 554 |
) |
| 555 |
self.assertEqual(h1.digest(), h2.digest(), |
| 556 |
"Digest of copy doesn't match original digest.") |
| 557 |
self.assertEqual(h1.hexdigest(), h2.hexdigest(), |
| 558 |
"Hexdigest of copy doesn't match original hexdigest.") |
| 559 |
|
| 560 |
|
| 561 |
class CompareDigestTestCase(unittest.TestCase): |
| 562 |
|
| 563 |
def test_hmac_compare_digest(self): |
| 564 |
self._test_compare_digest(hmac.compare_digest) |
| 565 |
if openssl_compare_digest is not None: |
| 566 |
self.assertIs(hmac.compare_digest, openssl_compare_digest) |
| 567 |
else: |
| 568 |
self.assertIs(hmac.compare_digest, operator_compare_digest) |
| 569 |
|
| 570 |
def test_operator_compare_digest(self): |
| 571 |
self._test_compare_digest(operator_compare_digest) |
| 572 |
|
| 573 |
@unittest.skipIf(openssl_compare_digest is None, "test requires _hashlib") |
| 574 |
def test_openssl_compare_digest(self): |
| 575 |
self._test_compare_digest(openssl_compare_digest) |
| 576 |
|
| 577 |
def _test_compare_digest(self, compare_digest): |
| 578 |
# Testing input type exception handling |
| 579 |
a, b = 100, 200 |
| 580 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 581 |
a, b = 100, b"foobar" |
| 582 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 583 |
a, b = b"foobar", 200 |
| 584 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 585 |
a, b = "foobar", b"foobar" |
| 586 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 587 |
a, b = b"foobar", "foobar" |
| 588 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 589 |
|
| 590 |
# Testing bytes of different lengths |
| 591 |
a, b = b"foobar", b"foo" |
| 592 |
self.assertFalse(compare_digest(a, b)) |
| 593 |
a, b = b"\xde\xad\xbe\xef", b"\xde\xad" |
| 594 |
self.assertFalse(compare_digest(a, b)) |
| 595 |
|
| 596 |
# Testing bytes of same lengths, different values |
| 597 |
a, b = b"foobar", b"foobaz" |
| 598 |
self.assertFalse(compare_digest(a, b)) |
| 599 |
a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea" |
| 600 |
self.assertFalse(compare_digest(a, b)) |
| 601 |
|
| 602 |
# Testing bytes of same lengths, same values |
| 603 |
a, b = b"foobar", b"foobar" |
| 604 |
self.assertTrue(compare_digest(a, b)) |
| 605 |
a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef" |
| 606 |
self.assertTrue(compare_digest(a, b)) |
| 607 |
|
| 608 |
# Testing bytearrays of same lengths, same values |
| 609 |
a, b = bytearray(b"foobar"), bytearray(b"foobar") |
| 610 |
self.assertTrue(compare_digest(a, b)) |
| 611 |
|
| 612 |
# Testing bytearrays of different lengths |
| 613 |
a, b = bytearray(b"foobar"), bytearray(b"foo") |
| 614 |
self.assertFalse(compare_digest(a, b)) |
| 615 |
|
| 616 |
# Testing bytearrays of same lengths, different values |
| 617 |
a, b = bytearray(b"foobar"), bytearray(b"foobaz") |
| 618 |
self.assertFalse(compare_digest(a, b)) |
| 619 |
|
| 620 |
# Testing byte and bytearray of same lengths, same values |
| 621 |
a, b = bytearray(b"foobar"), b"foobar" |
| 622 |
self.assertTrue(compare_digest(a, b)) |
| 623 |
self.assertTrue(compare_digest(b, a)) |
| 624 |
|
| 625 |
# Testing byte bytearray of different lengths |
| 626 |
a, b = bytearray(b"foobar"), b"foo" |
| 627 |
self.assertFalse(compare_digest(a, b)) |
| 628 |
self.assertFalse(compare_digest(b, a)) |
| 629 |
|
| 630 |
# Testing byte and bytearray of same lengths, different values |
| 631 |
a, b = bytearray(b"foobar"), b"foobaz" |
| 632 |
self.assertFalse(compare_digest(a, b)) |
| 633 |
self.assertFalse(compare_digest(b, a)) |
| 634 |
|
| 635 |
# Testing str of same lengths |
| 636 |
a, b = "foobar", "foobar" |
| 637 |
self.assertTrue(compare_digest(a, b)) |
| 638 |
|
| 639 |
# Testing str of different lengths |
| 640 |
a, b = "foo", "foobar" |
| 641 |
self.assertFalse(compare_digest(a, b)) |
| 642 |
|
| 643 |
# Testing bytes of same lengths, different values |
| 644 |
a, b = "foobar", "foobaz" |
| 645 |
self.assertFalse(compare_digest(a, b)) |
| 646 |
|
| 647 |
# Testing error cases |
| 648 |
a, b = "foobar", b"foobar" |
| 649 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 650 |
a, b = b"foobar", "foobar" |
| 651 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 652 |
a, b = b"foobar", 1 |
| 653 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 654 |
a, b = 100, 200 |
| 655 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 656 |
a, b = "fooä", "fooä" |
| 657 |
self.assertRaises(TypeError, compare_digest, a, b) |
| 658 |
|
| 659 |
# subclasses are supported by ignore __eq__ |
| 660 |
class mystr(str): |
| 661 |
def __eq__(self, other): |
| 662 |
return False |
| 663 |
|
| 664 |
a, b = mystr("foobar"), mystr("foobar") |
| 665 |
self.assertTrue(compare_digest(a, b)) |
| 666 |
a, b = mystr("foobar"), "foobar" |
| 667 |
self.assertTrue(compare_digest(a, b)) |
| 668 |
a, b = mystr("foobar"), mystr("foobaz") |
| 669 |
self.assertFalse(compare_digest(a, b)) |
| 670 |
|
| 671 |
class mybytes(bytes): |
| 672 |
def __eq__(self, other): |
| 673 |
return False |
| 674 |
|
| 675 |
a, b = mybytes(b"foobar"), mybytes(b"foobar") |
| 676 |
self.assertTrue(compare_digest(a, b)) |
| 677 |
a, b = mybytes(b"foobar"), b"foobar" |
| 678 |
self.assertTrue(compare_digest(a, b)) |
| 679 |
a, b = mybytes(b"foobar"), mybytes(b"foobaz") |
| 680 |
self.assertFalse(compare_digest(a, b)) |
| 681 |
|
| 682 |
|
| 683 |
if __name__ == "__main__": |
| 684 |
unittest.main() |