Skip to content

Commit ff790da

Browse files
Andy Valenciadpgeorge
authored andcommitted
esp32: Add hardware SHA1/SHA256 support via mbedtls API.
Code lineage: a copy of extmod/moduhashlib with the API invocation details edited. Total derivative work.
1 parent e60b3bc commit ff790da

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ SRC_C = \
128128
modnetwork.c \
129129
modsocket.c \
130130
modesp.c \
131+
moduhashlib.c \
131132
espneopixel.c \
132133
machine_hw_spi.c \
133134
mpthreadport.c \

esp32/moduhashlib.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#include "py/runtime.h"
27+
28+
#include "mbedtls/sha256.h"
29+
#include "mbedtls/sha1.h"
30+
31+
union sha_ctxs {
32+
mbedtls_sha256_context sha256;
33+
mbedtls_sha1_context sha1;
34+
};
35+
typedef struct _mp_obj_hash_t {
36+
mp_obj_base_t base;
37+
union sha_ctxs state;
38+
} mp_obj_hash_t;
39+
40+
STATIC mp_obj_t sha256_update(mp_obj_t self_in, mp_obj_t arg);
41+
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg);
42+
43+
STATIC mp_obj_t sha256_make_new(const mp_obj_type_t *type,
44+
size_t n_args, size_t n_kw, const mp_obj_t *args) {
45+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
46+
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(union sha_ctxs));
47+
o->base.type = type;
48+
mbedtls_sha256_init(&o->state.sha256);
49+
if (n_args == 1) {
50+
sha256_update(MP_OBJ_FROM_PTR(o), args[0]);
51+
}
52+
return MP_OBJ_FROM_PTR(o);
53+
}
54+
55+
STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type,
56+
size_t n_args, size_t n_kw, const mp_obj_t *args) {
57+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
58+
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(union sha_ctxs));
59+
o->base.type = type;
60+
mbedtls_sha1_init(&o->state.sha1);
61+
if (n_args == 1) {
62+
sha1_update(MP_OBJ_FROM_PTR(o), args[0]);
63+
}
64+
return MP_OBJ_FROM_PTR(o);
65+
}
66+
67+
STATIC mp_obj_t sha256_update(mp_obj_t self_in, mp_obj_t arg) {
68+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
69+
mp_buffer_info_t bufinfo;
70+
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
71+
mbedtls_sha256_update(&self->state.sha256, bufinfo.buf, bufinfo.len);
72+
return mp_const_none;
73+
}
74+
MP_DEFINE_CONST_FUN_OBJ_2(sha256_update_obj, sha256_update);
75+
76+
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) {
77+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
78+
mp_buffer_info_t bufinfo;
79+
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
80+
mbedtls_sha1_update(&self->state.sha1, bufinfo.buf, bufinfo.len);
81+
return mp_const_none;
82+
}
83+
MP_DEFINE_CONST_FUN_OBJ_2(sha1_update_obj, sha1_update);
84+
85+
STATIC mp_obj_t sha256_digest(mp_obj_t self_in) {
86+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
87+
vstr_t vstr;
88+
vstr_init_len(&vstr, 32);
89+
mbedtls_sha256_finish(&self->state.sha256, (unsigned char *)vstr.buf);
90+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
91+
}
92+
MP_DEFINE_CONST_FUN_OBJ_1(sha256_digest_obj, sha256_digest);
93+
94+
STATIC mp_obj_t sha1_digest(mp_obj_t self_in) {
95+
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
96+
vstr_t vstr;
97+
vstr_init_len(&vstr, 20);
98+
mbedtls_sha1_finish(&self->state.sha1, (unsigned char *)vstr.buf);
99+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
100+
}
101+
MP_DEFINE_CONST_FUN_OBJ_1(sha1_digest_obj, sha1_digest);
102+
103+
STATIC const mp_rom_map_elem_t sha256_locals_dict_table[] = {
104+
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&sha256_update_obj) },
105+
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&sha256_digest_obj) },
106+
};
107+
108+
STATIC MP_DEFINE_CONST_DICT(sha256_locals_dict, sha256_locals_dict_table);
109+
110+
STATIC const mp_obj_type_t sha256_type = {
111+
{ &mp_type_type },
112+
.name = MP_QSTR_sha256,
113+
.make_new = sha256_make_new,
114+
.locals_dict = (void*)&sha256_locals_dict,
115+
};
116+
117+
STATIC const mp_rom_map_elem_t sha1_locals_dict_table[] = {
118+
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&sha1_update_obj) },
119+
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&sha1_digest_obj) },
120+
};
121+
STATIC MP_DEFINE_CONST_DICT(sha1_locals_dict, sha1_locals_dict_table);
122+
123+
STATIC const mp_obj_type_t sha1_type = {
124+
{ &mp_type_type },
125+
.name = MP_QSTR_sha1,
126+
.make_new = sha1_make_new,
127+
.locals_dict = (void*)&sha1_locals_dict,
128+
};
129+
130+
STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = {
131+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uhashlib) },
132+
{ MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&sha256_type) },
133+
{ MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&sha1_type) },
134+
};
135+
136+
STATIC MP_DEFINE_CONST_DICT(mp_module_hashlib_globals,
137+
mp_module_hashlib_globals_table);
138+
139+
const mp_obj_module_t mp_module_uhashlib = {
140+
.base = { &mp_type_module },
141+
.globals = (mp_obj_dict_t*)&mp_module_hashlib_globals,
142+
};

esp32/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
#define MICROPY_PY_UJSON (1)
119119
#define MICROPY_PY_URE (1)
120120
#define MICROPY_PY_UHEAPQ (1)
121-
#define MICROPY_PY_UHASHLIB (1)
121+
#define MICROPY_PY_UHASHLIB (0) // We use the ESP32 version
122122
#define MICROPY_PY_UHASHLIB_SHA1 (MICROPY_PY_USSL && MICROPY_SSL_AXTLS)
123123
#define MICROPY_PY_UBINASCII (1)
124124
#define MICROPY_PY_UBINASCII_CRC32 (1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy