Thanks to visit codestin.com
Credit goes to github.com

Skip to content

hmac code not compatible with hashlib/uhashlib implementations #369

Closed
@tmmorin

Description

@tmmorin

I noticed that micropython's hmac implementation relies on the existence of .copy() on instances of shaXX, which works fine with the implementation provided in hashlib._sha224/256/384/512, but does not work with the implementations that hashlib auto-loads from uhashlib when uhashlib is present (since this change).

I tested recent versions micropython-lib master and micropython 1.12).

$ cat test2.py 
import hmac
from hashlib import sha256
print(hmac.new(b'test', msg=b'test', digestmod=sha256).hexdigest())
$ micropython test2.py 
Warning: No block_size attribute on given digest object; Assuming 64.
Traceback (most recent call last):
  File "test2.py", line 4, in <module>
  File "/home/treizh/.micropython/lib/hmac.py", line 137, in hexdigest
  File "/home/treizh/.micropython/lib/hmac.py", line 120, in _current
AttributeError: 'sha256' object has no attribute 'copy'

Forcing the use of the implementation under hashlib._sha256 works though:

$ cat test.py 
import hmac
from hashlib._sha256 import sha256
print(hmac.new(b'test', msg=b'test', digestmod=sha256).hexdigest())
$ micropython test.py 
88cd2108b5347d973cf39cdf9053d7dd42704876d8c9a9bd8e2d168259d3ddf7

(this workaround unfortunately does not work for SHA1 since SHA1 has no implementation in hashlib)

I presume that whether hmac should be implemented without using relying on copy(), or at least should not autoload hashlib (this last solution would leave HMAC SHA1 unimplemented).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions