From 96c981b1ee95ab391d55a83fead1d50107814320 Mon Sep 17 00:00:00 2001 From: Christopher Cooper Date: Sun, 13 Aug 2017 07:13:19 +0000 Subject: [PATCH] hmac: Calculate correct digest when non-trivial key is used. This incorrect behavior was a result of the function that performs table-driven byte translation. The function first used the chr(...) function to convert each resulting byte, represented as an integer, to a string of length one. Then, the .encode(...) function was used to convert the string to a byte string with an intended length of one. That didn't work well for bytes with high bit set, as they were trated as UTF-8 chars. Instead, perform operations directly on bytes. This was an artifact of porting this to MicroPython, as the original CPython module uses bytes.translate(...) method (not available in uPy). --- hmac/hmac.py | 2 +- hmac/metadata.txt | 2 +- hmac/setup.py | 2 +- hmac/test_hmac.py | 26 ++++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/hmac/hmac.py b/hmac/hmac.py index c2ce23b1..af1feade 100644 --- a/hmac/hmac.py +++ b/hmac/hmac.py @@ -13,7 +13,7 @@ trans_5C = bytes((x ^ 0x5C) for x in range(256)) trans_36 = bytes((x ^ 0x36) for x in range(256)) def translate(d, t): - return b''.join([ chr(t[x]).encode('ascii') for x in d ]) + return bytes(t[x] for x in d) # The size of the digests returned by HMAC depends on the underlying # hashing module used. Use digest_size from the instance of HMAC instead. diff --git a/hmac/metadata.txt b/hmac/metadata.txt index ba6f514e..1f744f8f 100644 --- a/hmac/metadata.txt +++ b/hmac/metadata.txt @@ -1,4 +1,4 @@ srctype = cpython type = module -version = 3.4.2-2 +version = 3.4.2-3 depends = warnings, hashlib diff --git a/hmac/setup.py b/hmac/setup.py index 274f6b92..1b9a9321 100644 --- a/hmac/setup.py +++ b/hmac/setup.py @@ -7,7 +7,7 @@ sys.path.append("..") import optimize_upip setup(name='micropython-hmac', - version='3.4.2-2', + version='3.4.2-3', description='CPython hmac module ported to MicroPython', long_description='This is a module ported from CPython standard library to be compatible with\nMicroPython interpreter. Usually, this means applying small patches for\nfeatures not supported (yet, or at all) in MicroPython. Sometimes, heavier\nchanges are required. Note that CPython modules are written with availability\nof vast resources in mind, and may not work for MicroPython ports with\nlimited heap. If you are affected by such a case, please help reimplement\nthe module from scratch.', url='https://github.com/micropython/micropython-lib', diff --git a/hmac/test_hmac.py b/hmac/test_hmac.py index a2c1349c..52fe57e6 100644 --- a/hmac/test_hmac.py +++ b/hmac/test_hmac.py @@ -20,3 +20,29 @@ print(dig) if dig != '59942f31b6f5473fb4eb630fabf5358a49bc11d24ebc83b114b4af30d6ef47ea14b673f478586f520a0b9c53b27c8f8dd618c165ef586195bd4e98293d34df1a': raise Exception("Error") +key = b'\x06\x1au\x90|Xz;o\x1b<\xafGL\xbfn\x8a\xc94YPfC^\xb9\xdd)\x7f\xaf\x85\xa1\xed\x82\xbexp\xaf\x13\x1a\x9d' + +dig = hmac.new(key[:20], msg=msg, digestmod=sha256).hexdigest() + +print('59e332b881df09fdecf569c8b142b27fc989638720aeda2813f82442b6e3d91b') +print(dig) + +if dig != '59e332b881df09fdecf569c8b142b27fc989638720aeda2813f82442b6e3d91b': + raise Exception("Error") + +dig = hmac.new(key[:32], msg=msg, digestmod=sha256).hexdigest() + +print('b72fed815cd71acfa3a2f5cf2343679565fa18e7cd92226ab443aabd1fd7b7b0') +print(dig) + +if dig != 'b72fed815cd71acfa3a2f5cf2343679565fa18e7cd92226ab443aabd1fd7b7b0': + raise Exception("Error") + +dig = hmac.new(key, msg=msg, digestmod=sha256).hexdigest() + +print('4e51beae6c2b0f90bb3e99d8e93a32d168b6c1e9b7d2130e2d668a3b3e10358d') +print(dig) + +if dig != '4e51beae6c2b0f90bb3e99d8e93a32d168b6c1e9b7d2130e2d668a3b3e10358d': + raise Exception("Error") +