10000 extmod/moduhashlib: Add support for SHA1 (based on axTLS). · lispmeister/micropython@ee1656e · GitHub
[go: up one dir, main page]

Skip to content

Commit ee1656e

Browse files
committed
extmod/moduhashlib: Add support for SHA1 (based on axTLS).
SHA1 is used in a number of protocols and algorithm originated 5 years ago or so, in other words, it's in "wide use", and only newer protocols use SHA2. The implementation depends on axTLS enabled. TODO: Make separate config option specifically for sha1().
1 parent df4ce93 commit ee1656e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

extmod/moduhashlib.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#if MICROPY_PY_UHASHLIB
3434

3535
#include "crypto-algorithms/sha256.h"
36+
#if MICROPY_PY_USSL
37+
#include "lib/axtls/crypto/crypto.h"
38+
#endif
3639

3740
typedef struct _mp_obj_hash_t {
3841
mp_obj_base_t base;
@@ -52,6 +55,21 @@ STATIC mp_obj_t hash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
5255
return MP_OBJ_FROM_PTR(o);
5356
}
5457

58+
#if MICROPY_PY_USSL
59+
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg);
60+
61+
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) {
62+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
63+
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA1_CTX));
64+
o->base.type = type;
65+
SHA1_Init((SHA1_CTX*)o->state);
66+
if (n_args == 1) {
67+
sha1_update(MP_OBJ_FROM_PTR(o), args[0]);
68+
}
69+ return MP_OBJ_FROM_PTR(o);
70+
}
71+
#endif
72+
5573
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
5674
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
5775
mp_buffer_info_t bufinfo;
@@ -61,6 +79,17 @@ STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
6179
}
6280
MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
6381

82+
#if MICROPY_PY_USSL
83+
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) {
84+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
85+
mp_buffer_info_t bufinfo;
86+
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
87+
SHA1_Update((SHA1_CTX*)self->state, bufinfo.buf, bufinfo.len);
88+
return mp_const_none;
89+
}
90+
MP_DEFINE_CONST_FUN_OBJ_2(sha1_update_obj, sha1_update);
91+
#endif
92+
6493
STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
6594
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
6695
vstr_t vstr;
@@ -70,6 +99,17 @@ STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
7099
}
71100
MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
72101

102+
#if MICROPY_PY_USSL
103+
STATIC mp_obj_t sha1_digest(mp_obj_t self_in) {
104+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
105+
vstr_t vstr;
106+
vstr_init_len(&vstr, SHA1_SIZE);
107+
SHA1_Final((byte*)vstr.buf, (SHA1_CTX*)self->state);
108+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
109+
}
110+
MP_DEFINE_CONST_FUN_OBJ_1(sha1_digest_obj, sha1_digest);
111+
#endif
112+
73113
STATIC const mp_rom_map_elem_t hash_locals_dict_table[] = {
74114
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hash_update_obj) },
75115
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hash_digest_obj) },
@@ -84,9 +124,27 @@ STATIC const mp_obj_type_t sha256_type = {
84124
.locals_dict = (void*)&hash_locals_dict,
85125
};
86126

127+
#if MICROPY_PY_USSL
128+
STATIC const mp_rom_map_elem_t sha1_locals_dict_table[] = {
129+
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&sha1_update_obj) },
130+
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&sha1_digest_obj) },
131+
};
132+
STATIC MP_DEFINE_CONST_DICT(sha1_locals_dict, sha1_locals_dict_table);
133+
134+
STATIC const mp_obj_type_t sha1_type = {
135+
{ &mp_type_type },
136+
.name = MP_QSTR_sha1,
137+
.make_new = sha1_make_new,
138+
.locals_dict = (void*)&sha1_locals_dict,
139+
};
140+
#endif
141+
87142
STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = {
88143
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uhashlib) },
89144
{ MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&sha256_type) },
145+
#if MICROPY_PY_USSL
146+
{ MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&sha1_type) },
147+
#endif
90148
};
91149

92150
STATIC MP_DEFINE_CONST_DICT(mp_module_hashlib_globals, mp_module_hashlib_globals_table);

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ Q(uhashlib)
614614
Q(update)
615615
Q(digest)
616616
Q(sha256)
617+
Q(sha1)
617618
#endif
618619

619620
#if MICROPY_PY_UBINASCII

0 commit comments

Comments
 (0)
0