Skip to content

Commit 7413b3c

Browse files
ccooper21pfalcon
authored andcommitted
extmod/moduhashlib: Enable SHA1 hashing when using "mbedtls" library.
The SHA1 hashing functionality is provided via the "axtls" library's implementation, and hence is unavailable when the "axtls" library is not being used. This change provides the same SHA1 hashing functionality when using the "mbedtls" library by using its implementation instead.
1 parent 0535d03 commit 7413b3c

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

extmod/moduhashlib.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@
3232
#if MICROPY_PY_UHASHLIB
3333

3434
#include "crypto-algorithms/sha256.h"
35+
3536
#if MICROPY_PY_UHASHLIB_SHA1
37+
38+
#if MICROPY_SSL_AXTLS
3639
#include "lib/axtls/crypto/crypto.h"
3740
#endif
3841

42+
#if MICROPY_SSL_MBEDTLS
43+
#include "mbedtls/sha1.h"
44+
#endif
45+
46+
#endif
47+
3948
typedef struct _mp_obj_hash_t {
4049
mp_obj_base_t base;
4150
char state[0];
@@ -57,6 +66,7 @@ STATIC mp_obj_t hash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
5766
#if MICROPY_PY_UHASHLIB_SHA1
5867
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg);
5968

69+
#if MICROPY_SSL_AXTLS
6070
STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
6171
mp_arg_check_num(n_args, n_kw, 0, 1, false);
6272
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA1_CTX));
@@ -69,6 +79,22 @@ STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
6979
}
7080
#endif
7181

82+
#if MICROPY_SSL_MBEDTLS
83+
STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
84+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
85+
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(mbedtls_sha1_context));
86+
o->base.type = type;
87+
mbedtls_sha1_init((mbedtls_sha1_context*)o->state);
88+
mbedtls_sha1_starts((mbedtls_sha1_context*)o->state);
89+
if (n_args == 1) {
90+
sha1_update(MP_OBJ_FROM_PTR(o), args[0]);
91+
}
92+
return MP_OBJ_FROM_PTR(o);
93+
}
94+
#endif
95+
96+
#endif
97+
7298
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
7399
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
74100
mp_buffer_info_t bufinfo;
@@ -79,13 +105,27 @@ STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
79105
MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
80106

81107
#if MICROPY_PY_UHASHLIB_SHA1
108+
109+
#if MICROPY_SSL_AXTLS
82110
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) {
83111
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
84112
mp_buffer_info_t bufinfo;
85113
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
86114
SHA1_Update((SHA1_CTX*)self->state, bufinfo.buf, bufinfo.len);
87115
return mp_const_none;
88116
}
117+
#endif
118+
119+
#if MICROPY_SSL_MBEDTLS
120+
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) {
121+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
122+
mp_buffer_info_t bufinfo;
123+
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
124+
mbedtls_sha1_update((mbedtls_sha1_context*)self->state, bufinfo.buf, bufinfo.len);
125+
return mp_const_none;
126+
}
127+
#endif
128+
89129
MP_DEFINE_CONST_FUN_OBJ_2(sha1_update_obj, sha1_update);
90130
#endif
91131

@@ -99,13 +139,28 @@ STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
99139
MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
100140

101141
#if MICROPY_PY_UHASHLIB_SHA1
142+
143+
#if MICROPY_SSL_AXTLS
102144
STATIC mp_obj_t sha1_digest(mp_obj_t self_in) {
103145
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
104146
vstr_t vstr;
105147
vstr_init_len(&vstr, SHA1_SIZE);
106148
SHA1_Final((byte*)vstr.buf, (SHA1_CTX*)self->state);
107149
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
108150
}
151+
#endif
152+
153+
#if MICROPY_SSL_MBEDTLS
154+
STATIC mp_obj_t sha1_digest(mp_obj_t self_in) {
155+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
156+
vstr_t vstr;
157+
vstr_init_len(&vstr, 20);
158+
mbedtls_sha1_finish((mbedtls_sha1_context*)self->state, (byte*)vstr.buf);
159+
mbedtls_sha1_free((mbedtls_sha1_context*)self->state);
160+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
161+
}
162+
#endif
163+
109164
MP_DEFINE_CONST_FUN_OBJ_1(sha1_digest_obj, sha1_digest);
110165
#endif
111166

ports/unix/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
#define MICROPY_PY_UHEAPQ (1)
122122
#define MICROPY_PY_UTIMEQ (1)
123123
#define MICROPY_PY_UHASHLIB (1)
124-
#if MICROPY_PY_USSL && MICROPY_SSL_AXTLS
124+
#if MICROPY_PY_USSL
125125
#define MICROPY_PY_UHASHLIB_SHA1 (1)
126126
#endif
127127
#define MICROPY_PY_UBINASCII (1)

0 commit comments

Comments
 (0)