From 468e01d16472b3442551849199edacb711f3e0ba Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Mon, 29 Aug 2022 22:29:19 +0200 Subject: [PATCH] Correct hmac import from Python stdlib Commit 82c0c4fdd2d99150ed11c188fbc0586512b13edb ("Fallback to hashlib instead of deprecated libs") had changed the hmac import to try the import from the hashlib module, but there appears to be 'hmac' underneath hashlib in neither Python 2 nor Python 3, but it's a separate module. Test on Debian testing with Python 2.7.18: $ python2 Python 2.7.18 (default, Aug 1 2022, 06:23:55) [GCC 12.1.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. Jedi is not installed, falling back to readline >>> from hashlib import hmac as HMAC Traceback (most recent call last): File "", line 1, in ImportError: cannot import name hmac >>> import hmac as HMAC >>> The same with Python 3.10.6: $ python3 Python 3.10.6 (main, Aug 10 2022, 11:19:32) [GCC 12.1.0] on linux Type "help", "copyright", "credits" or "license" for more information. Using Jedi for tab completion. >>> from hashlib import hmac as HMAC Traceback (most recent call last): File "", line 1, in ImportError: cannot import name 'hmac' from 'hashlib' (/usr/lib/python3.10/hashlib.py) >>> import hmac as HMAC >>> Even the official Python 3.10.6 documentation for the hashlib module use plain hmac import [1] in its example: >>> import hmac, hashlib >>> m = hmac.new(b'secret key', digestmod=hashlib.blake2s) >>> m.update(b'message') >>> m.hexdigest() 'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142' Therefore, revert that change to the import and use the direct hmac import again to make the case where PyCrypto is not installed work again. [1] https://docs.python.org/3/library/hashlib.html Fixes: #223 --- virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py index 23a6cc7..07a095f 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py @@ -31,7 +31,7 @@ try: except ImportError: # PyCrypto not available. Use the Python standard library. - from hashlib import hmac as HMAC + import hmac as HMAC CYBERFLEX_IV = b'\x00' * 8