Skip to content

Commit e800db5

Browse files
author
Daniel Campora
committed
cc3200: Add uhashlib. Supports SHA1 and SHA256.
1 parent 5e38b48 commit e800db5

11 files changed

Lines changed: 290 additions & 25 deletions

File tree

cc3200/application.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ APP_MISC_SRC_C = $(addprefix misc/,\
8686

8787
APP_MODS_SRC_C = $(addprefix mods/,\
8888
modnetwork.c \
89+
moduhashlib.c \
8990
modpyb.c \
9091
moduos.c \
9192
modusocket.c \
@@ -124,6 +125,7 @@ APP_TELNET_SRC_C = $(addprefix telnet/,\
124125
)
125126

126127
APP_UTIL_SRC_C = $(addprefix util/,\
128+
cryptohash.c \
127129
fifo.c \
128130
gccollect.c \
129131
random.c \

cc3200/bootmgr/bootloader.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BOOT_SL_SRC_C = $(addprefix simplelink/,\
5050
)
5151

5252
BOOT_UTIL_SRC_C = $(addprefix util/,\
53-
hash.c \
53+
cryptohash.c \
5454
)
5555

5656
BOOT_MAIN_SRC_C = \

cc3200/bootmgr/bootmgr.lds

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SECTIONS
3939
{
4040
_text = .;
4141
KEEP(*(.intvecs))
42+
*(.boot*)
4243
*(.text*)
4344
*(.rodata*)
4445
*(.ARM.extab* .gnu.linkonce.armextab.*)

cc3200/bootmgr/main.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdint.h>
2828
#include <stdbool.h>
29+
2930
#include "std.h"
3031

3132
#include "py/mpconfig.h"
@@ -47,7 +48,7 @@
4748
#include "flc.h"
4849
#include "bootmgr.h"
4950
#include "shamd5.h"
50-
#include "hash.h"
51+
#include "cryptohash.h"
5152
#include "utils.h"
5253
#include "cc3200_hal.h"
5354
#include "debug.h"
@@ -151,7 +152,7 @@ static void bootmgr_board_init(void) {
151152
mperror_bootloader_check_reset_cause();
152153

153154
// Enable the Data Hashing Engine
154-
HASH_Init();
155+
CRYPTOHASH_Init();
155156

156157
// Init the system led and the system switch
157158
mperror_init0();
@@ -175,7 +176,7 @@ static bool bootmgr_verify (void) {
175176

176177
if (FsFileInfo.FileLen > BOOTMGR_HASH_SIZE) {
177178
FsFileInfo.FileLen -= BOOTMGR_HASH_SIZE;
178-
HASH_SHAMD5Start(BOOTMGR_HASH_ALGO, FsFileInfo.FileLen);
179+
CRYPTOHASH_SHAMD5Start(BOOTMGR_HASH_ALGO, FsFileInfo.FileLen);
179180
do {
180181
if ((FsFileInfo.FileLen - offset) > BOOTMGR_BUFF_SIZE) {
181182
reqlen = BOOTMGR_BUFF_SIZE;
@@ -185,10 +186,10 @@ static bool bootmgr_verify (void) {
185186
}
186187

187188
offset += sl_FsRead(fHandle, offset, bootmgr_file_buf, reqlen);
188-
HASH_SHAMD5Update(bootmgr_file_buf, reqlen);
189+
CRYPTOHASH_SHAMD5Update(bootmgr_file_buf, reqlen);
189190
} while (offset < FsFileInfo.FileLen);
190191

191-
HASH_SHAMD5Read (bootmgr_file_buf);
192+
CRYPTOHASH_SHAMD5Read (bootmgr_file_buf);
192193

193194
// convert the resulting hash to hex
194195
for (_u32 i = 0; i < (BOOTMGR_HASH_SIZE / 2); i++) {

cc3200/mods/moduhashlib.c

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
* Copyright (c) 2015 Daniel Campora
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <assert.h>
29+
#include <string.h>
30+
31+
#include "py/mpconfig.h"
32+
#include MICROPY_HAL_H
33+
#include "py/nlr.h"
34+
#include "py/runtime.h"
35+
#include "inc/hw_types.h"
36+
#include "inc/hw_ints.h"
37+
#include "inc/hw_nvic.h"
38+
#include "inc/hw_shamd5.h"
39+
#include "inc/hw_dthe.h"
40+
#include "hw_memmap.h"
41+
#include "rom_map.h"
42+
#include "prcm.h"
43+
#include "shamd5.h"
44+
#include "cryptohash.h"
45+
#include "mpexception.h"
46+
47+
48+
/******************************************************************************
49+
DEFINE PRIVATE TYPES
50+
******************************************************************************/
51+
typedef struct _mp_obj_hash_t {
52+
mp_obj_base_t base;
53+
uint8_t *buffer;
54+
uint32_t b_size;
55+
uint32_t c_size;
56+
uint8_t algo;
57+
uint8_t h_size;
58+
bool fixedlen;
59+
bool digested;
60+
uint8_t hash[32];
61+
} mp_obj_hash_t;
62+
63+
/******************************************************************************
64+
DECLARE PRIVATE FUNCTIONS
65+
******************************************************************************/
66+
STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest);
67+
STATIC mp_obj_t hash_read (mp_obj_t self_in);
68+
69+
/******************************************************************************
70+
DEFINE PRIVATE FUNCTIONS
71+
******************************************************************************/
72+
STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest) {
73+
mp_obj_hash_t *self = self_in;
74+
mp_buffer_info_t bufinfo;
75+
76+
if (data) {
77+
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
78+
}
79+
80+
if (digest) {
81+
CRYPTOHASH_SHAMD5Start (self->algo, self->b_size);
82+
}
83+
84+
if (self->c_size < self->b_size || !data || !self->fixedlen) {
85+
if (digest || self->fixedlen) {
86+
// no data means we want to process our internal buffer
87+
CRYPTOHASH_SHAMD5Update (data ? bufinfo.buf : self->buffer, data ? bufinfo.len : self->b_size);
88+
self->c_size += data ? bufinfo.len : 0;
89+
} else {
90+
self->buffer = m_renew(byte, self->buffer, self->b_size, self->b_size + bufinfo.len);
91+
mp_seq_copy((byte*)self->buffer + self->b_size, bufinfo.buf, bufinfo.len, byte);
92+
self->b_size += bufinfo.len;
93+
self->digested = false;
94+
}
95+
} else {
96+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
97+
}
98+
}
99+
100+
STATIC mp_obj_t hash_read (mp_obj_t self_in) {
101+
mp_obj_hash_t *self = self_in;
102+
103+
if (!self->fixedlen) {
104+
if (!self->digested) {
105+
hash_update_internal(self, MP_OBJ_NULL, true);
106+
}
107+
} else if (self->c_size < self->b_size) {
108+
// it's a fixed len block which is still incomplete
109+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
110+
}
111+
112+
if (!self->digested) {
113+
CRYPTOHASH_SHAMD5Read ((uint8_t *)self->hash);
114+
self->digested = true;
115+
}
116+
return mp_obj_new_bytes(self->hash, self->h_size);
117+
}
118+
119+
/******************************************************************************/
120+
// Micro Python bindings
121+
122+
STATIC mp_obj_t hash_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
123+
mp_arg_check_num(n_args, n_kw, 0, 2, false);
124+
mp_obj_hash_t *self = m_new0(mp_obj_hash_t, 1);
125+
self->base.type = type_in;
126+
if (self->base.type->name == MP_QSTR_sha1) {
127+
self->algo = SHAMD5_ALGO_SHA1;
128+
self->h_size = 20;
129+
} else /* if (self->base.type->name == MP_QSTR_sha256) */ {
130+
self->algo = SHAMD5_ALGO_SHA256;
131+
self->h_size = 32;
132+
} /* else {
133+
self->algo = SHAMD5_ALGO_MD5;
134+
self->h_size = 32;
135+
} */
136+
137+
if (n_args) {
138+
// CPython extension to avoid buffering the data before digesting it
139+
// note: care must be taken to provide all intermidiate blocks as multiple
140+
// of four bytes, otherwise the resulted hash will be incorrect.
141+
// the final block can be of any length
142+
if (n_args > 1) {
143+
// block size given, we will feed the data directly into the hash engine
144+
self->fixedlen = true;
145+
self->b_size = mp_obj_get_int(args[1]);
146+
hash_update_internal(self, args[0], true);
147+
} else {
148+
hash_update_internal(self, args[0], false);
149+
}
150+
}
151+
return self;
152+
}
153+
154+
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
155+
mp_obj_hash_t *self = self_in;
156+
hash_update_internal(self, arg, false);
157+
return mp_const_none;
158+
}
159+
MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
160+
161+
STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
162+
return hash_read(self_in);
163+
}
164+
MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
165+
166+
STATIC mp_obj_t hash_hexdigest(mp_obj_t self_in) {
167+
(void)self_in;
168+
mp_not_implemented("");
169+
}
170+
MP_DEFINE_CONST_FUN_OBJ_1(hash_hexdigest_obj, hash_hexdigest);
171+
172+
STATIC const mp_map_elem_t hash_locals_dict_table[] = {
173+
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t) &hash_update_obj },
174+
{ MP_OBJ_NEW_QSTR(MP_QSTR_digest), (mp_obj_t) &hash_digest_obj },
175+
{ MP_OBJ_NEW_QSTR(MP_QSTR_hexdigest), (mp_obj_t) &hash_hexdigest_obj },
176+
};
177+
178+
STATIC MP_DEFINE_CONST_DICT(hash_locals_dict, hash_locals_dict_table);
179+
180+
//STATIC const mp_obj_type_t md5_type = {
181+
// { &mp_type_type },
182+
// .name = MP_QSTR_md5,
183+
// .make_new = hash_make_new,
184+
// .locals_dict = (mp_obj_t)&hash_locals_dict,
185+
//};
186+
187+
STATIC const mp_obj_type_t sha1_type = {
188+
{ &mp_type_type },
189+
.name = MP_QSTR_sha1,
190+
.make_new = hash_make_new,
191+
.locals_dict = (mp_obj_t)&hash_locals_dict,
192+
};
193+
194+
STATIC const mp_obj_type_t sha256_type = {
195+
{ &mp_type_type },
196+
.name = MP_QSTR_sha256,
197+
.make_new = hash_make_new,
198+
.locals_dict = (mp_obj_t)&hash_locals_dict,
199+
};
200+
201+
STATIC const mp_map_elem_t mp_module_hashlib_globals_table[] = {
202+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib) },
203+
// { MP_OBJ_NEW_QSTR(MP_QSTR_md5), (mp_obj_t)&md5_type },
204+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sha1), (mp_obj_t)&sha1_type },
205+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sha256), (mp_obj_t)&sha256_type },
206+
};
207+
208+
STATIC MP_DEFINE_CONST_DICT(mp_module_hashlib_globals, mp_module_hashlib_globals_table);
209+
210+
const mp_obj_module_t mp_module_uhashlib = {
211+
.base = { &mp_type_module },
212+
.name = MP_QSTR_uhashlib,
213+
.globals = (mp_obj_dict_t*)&mp_module_hashlib_globals,
214+
};
215+

cc3200/mods/moduhashlib.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* Copyright (c) 2015 Daniel Campora
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
#ifndef MODUHASHLIB_H_
28+
#define MODUHASHLIB_H_
29+
30+
extern const mp_obj_module_t mp_module_uhashlib;
31+
32+
#endif // MODUHASHLIB_H_

cc3200/mpconfigport.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ extern const struct _mp_obj_module_t mp_module_usocket;
108108
extern const struct _mp_obj_module_t mp_module_network;
109109

110110
#define MICROPY_PORT_BUILTIN_MODULES \
111-
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
112-
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \
113-
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
114-
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
115-
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \
116-
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
111+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
112+
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \
113+
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
114+
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
115+
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \
116+
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
117+
{ MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \
117118

118119
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
119120
{ MP_OBJ_NEW_QSTR(MP_QSTR_binascii), (mp_obj_t)&mp_module_ubinascii }, \
@@ -124,6 +125,7 @@ extern const struct _mp_obj_module_t mp_module_network;
124125
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_utime }, \
125126
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_module_uselect }, \
126127
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&mp_module_usocket }, \
128+
{ MP_OBJ_NEW_QSTR(MP_QSTR_hashlib), (mp_obj_t)&mp_module_uhashlib }, \
127129

128130
// extra constants
129131
#define MICROPY_PORT_CONSTANTS \

cc3200/mptask.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "pybsleep.h"
6666
#include "pybtimer.h"
6767
#include "mpcallback.h"
68+
#include "cryptohash.h"
6869

6970
/******************************************************************************
7071
DECLARE PRIVATE CONSTANTS
@@ -289,6 +290,8 @@ STATIC void mptask_pre_init (void) {
289290
pybsd_init0();
290291
#endif
291292

293+
CRYPTOHASH_Init();
294+
292295
#ifdef DEBUG
293296
ASSERT (OSI_OK == osi_TaskCreate(TASK_Servers,
294297
(const signed char *)"Servers",

cc3200/qstrdefsport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,12 @@ Q(PWM)
349349
Q(POSITIVE)
350350
Q(NEGATIVE)
351351

352+
// for uhashlib module
353+
Q(hashlib)
354+
Q(uhashlib)
355+
Q(update)
356+
Q(digest)
357+
Q(hexdigest)
358+
//Q(md5)
359+
Q(sha1)
360+
Q(sha256)

0 commit comments

Comments
 (0)