1111from _operator import _compare_digest as operator_compare_digest
1212
1313try :
14+ import _hashlib as _hashopenssl
1415from _hashlib import HMAC as C_HMAC
1516from _hashlib import hmac_new as c_hmac_new
1617from _hashlib import compare_digest as openssl_compare_digest
1718except ImportError :
19+ _hashopenssl = None
1820C_HMAC = None
1921c_hmac_new = None
2022openssl_compare_digest = None
2123
24+ try :
25+ import _sha256 as sha256_module
26+ except ImportError :
27+ sha256_module = None
28+
2229
2330def ignore_warning (func ):
2431@functools .wraps (func )
@@ -32,22 +39,27 @@ def wrapper(*args, **kwargs):
3239
3340class TestVectorsTestCase (unittest .TestCase ):
3441
35- def asssert_hmac (
36- self , key , data , digest , hashfunc ,hashname ,digest_size ,block_size
42+ def assert_hmac_internals (
43+ self , h , digest ,hashname ,digest_size ,block_size
3744 ):
38- h = hmac .HMAC (key ,data ,digestmod = hashfunc )
3945self .assertEqual (h .hexdigest ().upper (),digest .upper ())
4046self .assertEqual (h .digest (),binascii .unhexlify (digest ))
4147self .assertEqual (h .name ,f"hmac-{ hashname } " )
4248self .assertEqual (h .digest_size ,digest_size )
4349self .assertEqual (h .block_size ,block_size )
4450
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+
4559h = hmac .HMAC (key ,data ,digestmod = hashname )
46- self .assertEqual (h .hexdigest ().upper (),digest .upper ())
47- self .assertEqual (h .digest (),binascii .unhexlify (digest ))
48- self .assertEqual (h .name ,f"hmac-{ hashname } " )
49- self .assertEqual (h .digest_size ,digest_size )
50- self .assertEqual (h .block_size ,block_size )
60+ self .assert_hmac_internals (
61+ h ,digest ,hashname ,digest_size ,block_size
62+ )
5163
5264h = hmac .HMAC (key ,digestmod = hashname )
5365h2 = h .copy ()
@@ -56,11 +68,9 @@ def asssert_hmac(
5668self .assertEqual (h .hexdigest ().upper (),digest .upper ())
5769
5870h = hmac .new (key ,data ,digestmod = hashname )
59- self .assertEqual (h .hexdigest ().upper (),digest .upper ())
60- self .assertEqual (h .digest (),binascii .unhexlify (digest ))
61- self .assertEqual (h .name ,f"hmac-{ hashname } " )
62- self .assertEqual (h .digest_size ,digest_size )
63- self .assertEqual (h .block_size ,block_size )
71+ self .assert_hmac_internals (
72+ h ,digest ,hashname ,digest_size ,block_size
73+ )
6474
6575h = hmac .new (key ,None ,digestmod = hashname )
6676h .update (data )
@@ -81,36 +91,43 @@ def asssert_hmac(
8191hmac .digest (key ,data ,digest = hashfunc ),
8292binascii .unhexlify (digest )
8393 )
84- with unittest .mock .patch ('hmac._openssl_md_meths' , {}):
85- self .assertEqual (
86- hmac .digest (key ,data ,digest = hashname ),
87- binascii .unhexlify (digest )
88- )
89- self .assertEqual (
90- hmac .digest (key ,data ,digest = hashfunc ),
91- binascii .unhexlify (digest )
92- )
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+ )
93100
94101if c_hmac_new is not None :
95102h = c_hmac_new (key ,data ,digestmod = hashname )
96- self .assertEqual (h .hexdigest ().upper (),digest .upper ())
97- self .assertEqual (h .digest (),binascii .unhexlify (digest ))
98- self .assertEqual (h .name ,f"hmac-{ hashname } " )
99- self .assertEqual (h .digest_size ,digest_size )
100- self .assertEqual (h .block_size ,block_size )
103+ self .assert_hmac_internals (
104+ h ,digest ,hashname ,digest_size ,block_size
105+ )
101106
102107h = c_hmac_new (key ,digestmod = hashname )
103108h2 = h .copy ()
104109h2 .update (b"test update" )
105110h .update (data )
106111self .assertEqual (h .hexdigest ().upper (),digest .upper ())
107112
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+
108125@hashlib_helper .requires_hashdigest ('md5' ,openssl = True )
109126def test_md5_vectors (self ):
110127# Test the HMAC module against test vectors from the RFC.
111128
112129def md5test (key ,data ,digest ):
113- self .asssert_hmac (
130+ self .assert_hmac (
114131key ,data ,digest ,
115132hashfunc = hashlib .md5 ,
116133hashname = "md5" ,
@@ -150,7 +167,7 @@ def md5test(key, data, digest):
150167@hashlib_helper .requires_hashdigest ('sha1' ,openssl = True )
151168def test_sha_vectors (self ):
152169def shatest (key ,data ,digest ):
153- self .asssert_hmac (
170+ self .assert_hmac (
154171key ,data ,digest ,
155172hashfunc = hashlib .sha1 ,
156173hashname = "sha1" ,
@@ -191,7 +208,7 @@ def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size):
191208def hmactest (key ,data ,hexdigests ):
192209digest = hexdigests [hashfunc ]
193210
194- self .asssert_hmac (
211+ self .assert_hmac (
195212key ,data ,digest ,
196213hashfunc = hashfunc ,
197214hashname = hash_name ,
@@ -427,6 +444,15 @@ def test_internal_types(self):
427444 ):
428445C_HMAC ()
429446
447+ @unittest .skipUnless (sha256_module is not None ,'need _sha256' )
448+ def test_with_sha256_module (self ):
449+ h = hmac .HMAC (b"key" ,b"hash this!" ,digestmod = sha256_module .sha256 )
450+ self .assertEqual (h .hexdigest (),self .expected )
451+ self .assertEqual (h .name ,"hmac-sha256" )
452+
453+ digest = hmac .digest (b"key" ,b"hash this!" ,sha256_module .sha256 )
454+ self .assertEqual (digest ,binascii .unhexlify (self .expected ))
455+
430456
431457class SanityTestCase (unittest .TestCase ):
432458
@@ -447,39 +473,37 @@ def test_exercise_all_methods(self):
447473class CopyTestCase (unittest .TestCase ):
448474
449475@hashlib_helper .requires_hashdigest ('sha256' )
450- def test_attributes (self ):
476+ def test_attributes_old (self ):
451477# Testing if attributes are of same type.
452- h1 = hmac .HMAC (b"key" ,digestmod = "sha256" )
478+ h1 = hmac .HMAC .__new__ (hmac .HMAC )
479+ h1 ._init_old (b"key" ,b"msg" ,digestmod = "sha256" )
453480h2 = h1 .copy ()
454- self .assertTrue (h1 ._digest_cons == h2 ._digest_cons ,
455- "digest constructors don't match." )
456481self .assertEqual (type (h1 ._inner ),type (h2 ._inner ),
457482"Types of inner don't match." )
458483self .assertEqual (type (h1 ._outer ),type (h2 ._outer ),
459484"Types of outer don't match." )
460485
461486@hashlib_helper .requires_hashdigest ('sha256' )
462- def test_realcopy (self ):
487+ def test_realcopy_old (self ):
463488# Testing if the copy method created a real copy.
464- h1 = hmac .HMAC (b"key" ,digestmod = "sha256" )
489+ h1 = hmac .HMAC .__new__ (hmac .HMAC )
490+ h1 ._init_old (b"key" ,b"msg" ,digestmod = "sha256" )
465491h2 = h1 .copy ()
466492# Using id() in case somebody has overridden __eq__/__ne__.
467493self .assertTrue (id (h1 )!= id (h2 ),"No real copy of the HMAC instance." )
468494self .assertTrue (id (h1 ._inner )!= id (h2 ._inner ),
469495"No real copy of the attribute 'inner'." )
470496self .assertTrue (id (h1 ._outer )!= id (h2 ._outer ),
471497"No real copy of the attribute 'outer'." )
472- self .assertEqual (h1 ._inner ,h1 .inner )
473- self .assertEqual (h1 ._outer ,h1 .outer )
474- self .assertEqual (h1 ._digest_cons ,h1 .digest_cons )
498+ self .assertIs (h1 ._hmac ,None )
475499
500+ @unittest .skipIf (_hashopenssl is None ,"test requires _hashopenssl" )
476501@hashlib_helper .requires_hashdigest ('sha256' )
477- def test_properties (self ):
478- # deprecated properties
479- h1 = hmac .HMAC (b"key" ,digestmod = "sha256" )
480- self .assertEqual (h1 ._inner ,h1 .inner )
481- self .assertEqual (h1 ._outer ,h1 .outer )
482- self .assertEqual (h1 ._digest_cons ,h1 .digest_cons )
502+ def test_realcopy_hmac (self ):
503+ h1 = hmac .HMAC .__new__ (hmac .HMAC )
504+ h1 ._init_hmac (b"key" ,b"msg" ,digestmod = "sha256" )
505+ h2 = h1 .copy ()
506+ self .assertTrue (id (h1 ._hmac )!= id (h2 ._hmac ))
483507
484508@hashlib_helper .requires_hashdigest ('sha256' )
485509def test_equality (self ):