HMAC with digestmode SHA1

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
bare-01
Posts: 3
Joined: Fri Feb 08, 2019 8:49 am

HMAC with digestmode SHA1

Post by bare-01 » Thu Apr 16, 2020 5:48 pm

Hi,

I'm trying to make a hmac hash with digestmode SHA1.
But I'm stuck at the digestmode..

Code: Select all

import hmac
from uhashlib import sha1

word = bytes("word", "UTF-8")
key = bytes("key", "UTF-8")

hash = hmac.new(key, word, digestmod=sha1)

AttributeError: 'sha1' object has no attribute 'digest_size'
Trying to make something compatible so changing the digestmode is not an option?
Does somebody have a workaround or some advice on how to tackle this? Really appreciate it!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: HMAC with digestmode SHA1

Post by jimmo » Fri Apr 17, 2020 4:14 am

I spent a few minutes digging into this but like many things micropython-lib related I'm more confused than when I started.

I think the answer is that the hmac module requires that you're using the micropython-lib hashlib, rather than uhashlib directly. It looks like it extends from builtin uhashlib, but also seems to implement a large part of the algorithm from scratch in Python.

But then it's not clear that it includes SHA1...

https://github.com/micropython/micropyt ... ib/hashlib

I guess another thing on the list to clean up if we go ahead with https://github.com/micropython/micropython-lib/pull/376

bare-01
Posts: 3
Joined: Fri Feb 08, 2019 8:49 am

Re: HMAC with digestmode SHA1

Post by bare-01 » Fri Apr 17, 2020 8:17 pm

Thx for your feedback Jimmo!

So I thought OK, no worries.. we'll find a workaround and started reading Native machine code in .mpy files.

First I wrote a .c file that gives me a HMAC with SHA1 using OpenSSL.
It compiles and works using:

Code: Select all

gcc sha.c -lssl -lcrypto
So adapted the .c-file using the example and try to build with

Code: Select all

make ARCH=x64
But always get an error:

Code: Select all

GEN build/hmacsha1.config.h
CC sha_mpy.c
LINK build/sha_mpy.o
LinkError: build/sha_mpy.o: undefined symbol: OPENSSL_init_crypto
../../../py/dynruntime.mk:139: recipe for target 'build/hmacsha1.native.mpy' failed
make: *** [build/hmacsha1.native.mpy] Error 1
It doesn't matter if I add the libs to the Makefile

Code: Select all

LDFLAGS += -lssl -lcrypto
I can't find out why it starts complaining about OpenSSL while I could compile before.
Should this start in a new thread?

bare-01
Posts: 3
Joined: Fri Feb 08, 2019 8:49 am

Re: HMAC with digestmode SHA1

Post by bare-01 » Fri Apr 17, 2020 10:10 pm

Nevermind, found a workaround in CircuitPython

https://learn.adafruit.com/circuitpytho ... d/software

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: HMAC with digestmode SHA1

Post by jimmo » Fri Apr 17, 2020 11:56 pm

bare-01 wrote:
Fri Apr 17, 2020 8:17 pm
I can't find out why it starts complaining about OpenSSL while I could compile before.
Glad you found a workaround, but the reason the first compile worked is because it wasn't linking, only compiling.

When you run the full mpy makefile it also needs to link it.

My guess is that you'll need to set the lib path or something.

But this isn't going to work anyway when you compile for arm/esp because you'll need a compatible version of openssl for that target.

nathanmurfey
Posts: 1
Joined: Fri Oct 09, 2020 12:47 am

Re: HMAC with digestmode SHA1

Post by nathanmurfey » Fri Apr 29, 2022 3:08 am

If people are interested in this thread currently, it seems that the micropython-lib hashlib will import sha algorithms from itself if it doesn't find the implementation algorithm in uhashlib. Sha1 is in uhashlib so then it is not imported. sha256 is in uhashlib therefore is not imported, but 244 is. Using sha244 works.

Code: Select all

>>> import hmac
>>> h = hmac.new(b"key", b"nathan", hashlib.sha
sha1            sha256          sha512          sha384
sha224
>>> h = hmac.new(b"key", b"nathan", hashlib.sha224)
>>>
the uhashlib modules do not have the digest size attribute

Post Reply