11
11
from _operator import _compare_digest as operator_compare_digest
12
12
13
13
try :
14
+ import _hashlib as _hashopenssl
14
15
from _hashlib import HMAC as C_HMAC
15
16
from _hashlib import hmac_new as c_hmac_new
16
17
from _hashlib import compare_digest as openssl_compare_digest
17
18
except ImportError :
19
+ _hashopenssl = None
18
20
C_HMAC = None
19
21
c_hmac_new = None
20
22
openssl_compare_digest = None
21
23
24
+ try :
25
+ import _sha256 as sha256_module
26
+ except ImportError :
27
+ sha256_module = None
28
+
22
29
23
30
def ignore_warning (func ):
24
31
@functools .wraps (func )
@@ -32,22 +39,27 @@ def wrapper(*args, **kwargs):
32
39
33
40
class TestVectorsTestCase (unittest .TestCase ):
34
41
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
37
44
):
38
- h = hmac .HMAC (key , data , digestmod = hashfunc )
39
45
self .assertEqual (h .hexdigest ().upper (), digest .upper ())
40
46
self .assertEqual (h .digest (), binascii .unhexlify (digest ))
41
47
self .assertEqual (h .name , f"hmac-{ hashname } " )
42
48
self .assertEqual (h .digest_size , digest_size )
43
49
self .assertEqual (h .block_size , block_size )
44
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
+
45
59
h = 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
+ )
51
63
52
64
h = hmac .HMAC (key , digestmod = hashname )
53
65
h2 = h .copy ()
@@ -56,11 +68,9 @@ def asssert_hmac(
56
68
self .assertEqual (h .hexdigest ().upper (), digest .upper ())
57
69
58
70
h = 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
+ )
64
74
65
75
h = hmac .new (key , None , digestmod = hashname )
66
76
h .update (data )
@@ -81,36 +91,43 @@ def asssert_hmac(
81
91
hmac .digest (key , data , digest = hashfunc ),
82
92
binascii .unhexlify (digest )
83
93
)
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
+ )
93
100
94
101
if c_hmac_new is not None :
95
102
h = 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
+ )
101
106
102
107
h = c_hmac_new (key , digestmod = hashname )
103
108
h2 = h .copy ()
104
109
h2 .update (b"test update" )
105
110
h .update (data )
106
111
self .assertEqual (h .hexdigest ().upper (), digest .upper ())
107
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
+
108
125
@hashlib_helper .requires_hashdigest ('md5' , openssl = True )
109
126
def test_md5_vectors (self ):
110
127
# Test the HMAC module against test vectors from the RFC.
111
128
112
129
def md5test (key , data , digest ):
113
- self .asssert_hmac (
130
+ self .assert_hmac (
114
131
key , data , digest ,
115
132
hashfunc = hashlib .md5 ,
116
133
hashname = "md5" ,
@@ -150,7 +167,7 @@ def md5test(key, data, digest):
150
167
@hashlib_helper .requires_hashdigest ('sha1' , openssl = True )
151
168
def test_sha_vectors (self ):
152
169
def shatest (key , data , digest ):
153
- self .asssert_hmac (
170
+ self .assert_hmac (
154
171
key , data , digest ,
155
172
hashfunc = hashlib .sha1 ,
156
173
hashname = "sha1" ,
@@ -191,7 +208,7 @@ def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size):
191
208
def hmactest (key , data , hexdigests ):
192
209
digest = hexdigests [hashfunc ]
193
210
194
- self .asssert_hmac (
211
+ self .assert_hmac (
195
212
key , data , digest ,
196
213
hashfunc = hashfunc ,
197
214
hashname = hash_name ,
@@ -427,6 +444,15 @@ def test_internal_types(self):
427
444
):
428
445
C_HMAC ()
429
446
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
+
430
456
431
457
class SanityTestCase (unittest .TestCase ):
432
458
@@ -447,39 +473,37 @@ def test_exercise_all_methods(self):
447
473
class CopyTestCase (unittest .TestCase ):
448
474
449
475
@hashlib_helper .requires_hashdigest ('sha256' )
450
- def test_attributes (self ):
476
+ def test_attributes_old (self ):
451
477
# 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" )
453
480
h2 = h1 .copy ()
454
- self .assertTrue (h1 ._digest_cons == h2 ._digest_cons ,
455
- "digest constructors don't match." )
456
481
self .assertEqual (type (h1 ._inner ), type (h2 ._inner ),
457
482
"Types of inner don't match." )
458
483
self .assertEqual (type (h1 ._outer ), type (h2 ._outer ),
459
484
"Types of outer don't match." )
460
485
461
486
@hashlib_helper .requires_hashdigest ('sha256' )
462
- def test_realcopy (self ):
487
+ def test_realcopy_old (self ):
463
488
# 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" )
465
491
h2 = h1 .copy ()
466
492
# Using id() in case somebody has overridden __eq__/__ne__.
467
493
self .assertTrue (id (h1 ) != id (h2 ), "No real copy of the HMAC instance." )
468
494
self .assertTrue (id (h1 ._inner ) != id (h2 ._inner ),
469
495
"No real copy of the attribute 'inner'." )
470
496
self .assertTrue (id (h1 ._outer ) != id (h2 ._outer ),
471
497
"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 )
475
499
500
+ @unittest .skipIf (_hashopenssl is None , "test requires _hashopenssl" )
476
501
@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 ))
483
507
484
508
@hashlib_helper .requires_hashdigest ('sha256' )
485
509
def test_equality (self ):
0 commit comments