Skip to content

hmac code not compatible with hashlib/uhashlib implementations #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tmmorin opened this issue Feb 3, 2020 · 6 comments
Closed

hmac code not compatible with hashlib/uhashlib implementations #369

tmmorin opened this issue Feb 3, 2020 · 6 comments

Comments

@tmmorin
Copy link

tmmorin commented Feb 3, 2020

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).

@tmmorin
Copy link
Author

tmmorin commented Feb 3, 2020

@peter-conalgo: perhaps are you interested in this issue ?

@tmmorin tmmorin changed the title hmac code not compatible with uhashlib implementations hmac code not compatible with hashlib/uhashlib implementations Feb 3, 2020
@doc-hex
Copy link

doc-hex commented Feb 3, 2020

Somewhat. I used this code as an example, and made my own when I needed HMAC recently. Pared down features I didn't need so that in the end, I didn't need to copy state.

My version:

https://github.com/Coldcard/firmware/blob/119d7f79d82d98d35e53a941bbc065fa81182e50/shared/hmac.py

@tmmorin
Copy link
Author

tmmorin commented Feb 3, 2020

Thanks @peter-conalgo. I'll try your code !

@cositotito
Copy link

cositotito commented Apr 24, 2020

hello the code error digest_size.

#384

@jimmo
Copy link
Member

jimmo commented Aug 8, 2022

See #515 for a fix for this (sorry this has taken so long)

@dpgeorge
Copy link
Member

dpgeorge commented Aug 8, 2022

Fixed by f95260d

@dpgeorge dpgeorge closed this as completed Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants