Skip to content

Commit 96c981b

Browse files
ccooper21pfalcon
authored andcommitted
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 <str>.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).
1 parent 6bf4207 commit 96c981b

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

hmac/hmac.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
trans_36 = bytes((x ^ 0x36) for x in range(256))
1414

1515
def translate(d, t):
16-
return b''.join([ chr(t[x]).encode('ascii') for x in d ])
16+
return bytes(t[x] for x in d)
1717

1818
# The size of the digests returned by HMAC depends on the underlying
1919
# hashing module used. Use digest_size from the instance of HMAC instead.

hmac/metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
srctype = cpython
22
type = module
3-
version = 3.4.2-2
3+
version = 3.4.2-3
44
depends = warnings, hashlib

hmac/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import optimize_upip
88

99
setup(name='micropython-hmac',
10-
version='3.4.2-2',
10+
version='3.4.2-3',
1111
description='CPython hmac module ported to MicroPython',
1212
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.',
1313
url='https://github.com/micropython/micropython-lib',

hmac/test_hmac.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,29 @@
2020
if dig != '59942f31b6f5473fb4eb630fabf5358a49bc11d24ebc83b114b4af30d6ef47ea14b673f478586f520a0b9c53b27c8f8dd618c165ef586195bd4e98293d34df1a':
2121
raise Exception("Error")
2222

23+
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'
24+
25+
dig = hmac.new(key[:20], msg=msg, digestmod=sha256).hexdigest()
26+
27+
print('59e332b881df09fdecf569c8b142b27fc989638720aeda2813f82442b6e3d91b')
28+
print(dig)
29+
30+
if dig != '59e332b881df09fdecf569c8b142b27fc989638720aeda2813f82442b6e3d91b':
31+
raise Exception("Error")
32+
33+
dig = hmac.new(key[:32], msg=msg, digestmod=sha256).hexdigest()
34+
35+
print('b72fed815cd71acfa3a2f5cf2343679565fa18e7cd92226ab443aabd1fd7b7b0')
36+
print(dig)
37+
38+
if dig != 'b72fed815cd71acfa3a2f5cf2343679565fa18e7cd92226ab443aabd1fd7b7b0':
39+
raise Exception("Error")
40+
41+
dig = hmac.new(key, msg=msg, digestmod=sha256).hexdigest()
42+
43+
print('4e51beae6c2b0f90bb3e99d8e93a32d168b6c1e9b7d2130e2d668a3b3e10358d')
44+
print(dig)
45+
46+
if dig != '4e51beae6c2b0f90bb3e99d8e93a32d168b6c1e9b7d2130e2d668a3b3e10358d':
47+
raise Exception("Error")
48+

0 commit comments

Comments
 (0)