Skip to content

Uhashlib fix multiple digest calls #4488

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions extmod/moduhashlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, 32);
mbedtls_sha256_finish_ret((mbedtls_sha256_context*)&self->state, (unsigned char *)vstr.buf);
mbedtls_sha256_context _temp;
memcpy(&_temp, self->state, sizeof(_temp));
mbedtls_sha256_finish_ret(&_temp, (unsigned char *)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}

Expand Down Expand Up @@ -129,7 +131,9 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA256_BLOCK_SIZE);
sha256_final((CRYAL_SHA256_CTX*)self->state, (byte*)vstr.buf);
CRYAL_SHA256_CTX _temp;
memcpy(&_temp, self->state, sizeof(_temp));
sha256_final(&_temp, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
#endif
Expand Down Expand Up @@ -179,7 +183,9 @@ STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA1_SIZE);
SHA1_Final((byte*)vstr.buf, (SHA1_CTX*)self->state);
SHA1_CTX _temp;
memcpy(&_temp, self->state, sizeof(_temp));
SHA1_Final((byte*)vstr.buf, &_temp);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
#endif
Expand Down Expand Up @@ -216,8 +222,9 @@ STATIC mp_obj_t uhashlib_sha1_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, 20);
mbedtls_sha1_finish_ret((mbedtls_sha1_context*)self->state, (byte*)vstr.buf);
mbedtls_sha1_free((mbedtls_sha1_context*)self->state);
mbedtls_sha1_context _temp;
memcpy(&_temp, self->state, sizeof(_temp));
mbedtls_sha1_finish_ret((mbedtls_sha1_context*)&_temp, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
#endif
Expand Down Expand Up @@ -266,7 +273,9 @@ STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, MD5_SIZE);
MD5_Final((byte*)vstr.buf, (MD5_CTX*)self->state);
MD5_CTX _temp;
memcpy(&_temp, self->state, sizeof(_temp));
MD5_Final((byte*)vstr.buf, &_temp);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
#endif // MICROPY_SSL_AXTLS
Expand Down Expand Up @@ -303,8 +312,9 @@ STATIC mp_obj_t uhashlib_md5_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, 16);
mbedtls_md5_finish_ret((mbedtls_md5_context*)self->state, (byte*)vstr.buf);
mbedtls_md5_free((mbedtls_md5_context*)self->state);
mbedtls_md5_context _temp;
memcpy(&_temp, self->state, sizeof(_temp));
mbedtls_md5_finish_ret(&_temp, (byte*)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
#endif // MICROPY_SSL_MBEDTLS
Expand Down
10 changes: 10 additions & 0 deletions tests/extmod/uhashlib_md5.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@
md5 = hashlib.md5(b'hello')
md5.update(b'world')
print(md5.digest())

h = hashlib.md5(b'123')
print(h.digest())
print(h.digest())

h = hashlib.md5(b'123')
print(h.digest())
h.update(b'456')
print(h.digest())

9 changes: 9 additions & 0 deletions tests/extmod/uhashlib_sha1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@
sha1 = hashlib.sha1(b'hello')
sha1.update(b'world')
print(sha1.digest())

h = hashlib.sha1(b'123')
print(h.digest())
print(h.digest())

h = hashlib.sha1(b'123')
print(h.digest())
h.update(b'456')
print(h.digest())
18 changes: 8 additions & 10 deletions tests/extmod/uhashlib_sha256.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
# 56 bytes is a boundary case in the algorithm
print(hashlib.sha256(b"\xff" * 56).digest())

# TODO: running .digest() several times in row is not supported()
#h = hashlib.sha256(b'123')
#print(h.digest())
#print(h.digest())

# TODO: partial digests are not supported
#h = hashlib.sha256(b'123')
#print(h.digest())
#h.update(b'456')
#print(h.digest())
h = hashlib.sha256(b'123')
print(h.digest())
print(h.digest())

h = hashlib.sha256(b'123')
print(h.digest())
h.update(b'456')
print(h.digest())