cc3200: Add uhashlib. Supports SHA1 and SHA256.

pull/1277/head
Daniel Campora 2015-05-20 00:23:13 +02:00
rodzic 5e38b48dd6
commit e800db562f
11 zmienionych plików z 290 dodań i 25 usunięć

Wyświetl plik

@ -86,6 +86,7 @@ APP_MISC_SRC_C = $(addprefix misc/,\
APP_MODS_SRC_C = $(addprefix mods/,\
modnetwork.c \
moduhashlib.c \
modpyb.c \
moduos.c \
modusocket.c \
@ -124,6 +125,7 @@ APP_TELNET_SRC_C = $(addprefix telnet/,\
)
APP_UTIL_SRC_C = $(addprefix util/,\
cryptohash.c \
fifo.c \
gccollect.c \
random.c \

Wyświetl plik

@ -50,7 +50,7 @@ BOOT_SL_SRC_C = $(addprefix simplelink/,\
)
BOOT_UTIL_SRC_C = $(addprefix util/,\
hash.c \
cryptohash.c \
)
BOOT_MAIN_SRC_C = \

Wyświetl plik

@ -39,6 +39,7 @@ SECTIONS
{
_text = .;
KEEP(*(.intvecs))
*(.boot*)
*(.text*)
*(.rodata*)
*(.ARM.extab* .gnu.linkonce.armextab.*)

Wyświetl plik

@ -26,6 +26,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "std.h"
#include "py/mpconfig.h"
@ -47,7 +48,7 @@
#include "flc.h"
#include "bootmgr.h"
#include "shamd5.h"
#include "hash.h"
#include "cryptohash.h"
#include "utils.h"
#include "cc3200_hal.h"
#include "debug.h"
@ -151,7 +152,7 @@ static void bootmgr_board_init(void) {
mperror_bootloader_check_reset_cause();
// Enable the Data Hashing Engine
HASH_Init();
CRYPTOHASH_Init();
// Init the system led and the system switch
mperror_init0();
@ -175,7 +176,7 @@ static bool bootmgr_verify (void) {
if (FsFileInfo.FileLen > BOOTMGR_HASH_SIZE) {
FsFileInfo.FileLen -= BOOTMGR_HASH_SIZE;
HASH_SHAMD5Start(BOOTMGR_HASH_ALGO, FsFileInfo.FileLen);
CRYPTOHASH_SHAMD5Start(BOOTMGR_HASH_ALGO, FsFileInfo.FileLen);
do {
if ((FsFileInfo.FileLen - offset) > BOOTMGR_BUFF_SIZE) {
reqlen = BOOTMGR_BUFF_SIZE;
@ -185,10 +186,10 @@ static bool bootmgr_verify (void) {
}
offset += sl_FsRead(fHandle, offset, bootmgr_file_buf, reqlen);
HASH_SHAMD5Update(bootmgr_file_buf, reqlen);
CRYPTOHASH_SHAMD5Update(bootmgr_file_buf, reqlen);
} while (offset < FsFileInfo.FileLen);
HASH_SHAMD5Read (bootmgr_file_buf);
CRYPTOHASH_SHAMD5Read (bootmgr_file_buf);
// convert the resulting hash to hex
for (_u32 i = 0; i < (BOOTMGR_HASH_SIZE / 2); i++) {

Wyświetl plik

@ -0,0 +1,215 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Paul Sokolovsky
* Copyright (c) 2015 Daniel Campora
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <assert.h>
#include <string.h>
#include "py/mpconfig.h"
#include MICROPY_HAL_H
#include "py/nlr.h"
#include "py/runtime.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_nvic.h"
#include "inc/hw_shamd5.h"
#include "inc/hw_dthe.h"
#include "hw_memmap.h"
#include "rom_map.h"
#include "prcm.h"
#include "shamd5.h"
#include "cryptohash.h"
#include "mpexception.h"
/******************************************************************************
DEFINE PRIVATE TYPES
******************************************************************************/
typedef struct _mp_obj_hash_t {
mp_obj_base_t base;
uint8_t *buffer;
uint32_t b_size;
uint32_t c_size;
uint8_t algo;
uint8_t h_size;
bool fixedlen;
bool digested;
uint8_t hash[32];
} mp_obj_hash_t;
/******************************************************************************
DECLARE PRIVATE FUNCTIONS
******************************************************************************/
STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest);
STATIC mp_obj_t hash_read (mp_obj_t self_in);
/******************************************************************************
DEFINE PRIVATE FUNCTIONS
******************************************************************************/
STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest) {
mp_obj_hash_t *self = self_in;
mp_buffer_info_t bufinfo;
if (data) {
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
}
if (digest) {
CRYPTOHASH_SHAMD5Start (self->algo, self->b_size);
}
if (self->c_size < self->b_size || !data || !self->fixedlen) {
if (digest || self->fixedlen) {
// no data means we want to process our internal buffer
CRYPTOHASH_SHAMD5Update (data ? bufinfo.buf : self->buffer, data ? bufinfo.len : self->b_size);
self->c_size += data ? bufinfo.len : 0;
} else {
self->buffer = m_renew(byte, self->buffer, self->b_size, self->b_size + bufinfo.len);
mp_seq_copy((byte*)self->buffer + self->b_size, bufinfo.buf, bufinfo.len, byte);
self->b_size += bufinfo.len;
self->digested = false;
}
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
}
}
STATIC mp_obj_t hash_read (mp_obj_t self_in) {
mp_obj_hash_t *self = self_in;
if (!self->fixedlen) {
if (!self->digested) {
hash_update_internal(self, MP_OBJ_NULL, true);
}
} else if (self->c_size < self->b_size) {
// it's a fixed len block which is still incomplete
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
}
if (!self->digested) {
CRYPTOHASH_SHAMD5Read ((uint8_t *)self->hash);
self->digested = true;
}
return mp_obj_new_bytes(self->hash, self->h_size);
}
/******************************************************************************/
// Micro Python bindings
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) {
mp_arg_check_num(n_args, n_kw, 0, 2, false);
mp_obj_hash_t *self = m_new0(mp_obj_hash_t, 1);
self->base.type = type_in;
if (self->base.type->name == MP_QSTR_sha1) {
self->algo = SHAMD5_ALGO_SHA1;
self->h_size = 20;
} else /* if (self->base.type->name == MP_QSTR_sha256) */ {
self->algo = SHAMD5_ALGO_SHA256;
self->h_size = 32;
} /* else {
self->algo = SHAMD5_ALGO_MD5;
self->h_size = 32;
} */
if (n_args) {
// CPython extension to avoid buffering the data before digesting it
// note: care must be taken to provide all intermidiate blocks as multiple
// of four bytes, otherwise the resulted hash will be incorrect.
// the final block can be of any length
if (n_args > 1) {
// block size given, we will feed the data directly into the hash engine
self->fixedlen = true;
self->b_size = mp_obj_get_int(args[1]);
hash_update_internal(self, args[0], true);
} else {
hash_update_internal(self, args[0], false);
}
}
return self;
}
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hash_t *self = self_in;
hash_update_internal(self, arg, false);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
return hash_read(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
STATIC mp_obj_t hash_hexdigest(mp_obj_t self_in) {
(void)self_in;
mp_not_implemented("");
}
MP_DEFINE_CONST_FUN_OBJ_1(hash_hexdigest_obj, hash_hexdigest);
STATIC const mp_map_elem_t hash_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t) &hash_update_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_digest), (mp_obj_t) &hash_digest_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_hexdigest), (mp_obj_t) &hash_hexdigest_obj },
};
STATIC MP_DEFINE_CONST_DICT(hash_locals_dict, hash_locals_dict_table);
//STATIC const mp_obj_type_t md5_type = {
// { &mp_type_type },
// .name = MP_QSTR_md5,
// .make_new = hash_make_new,
// .locals_dict = (mp_obj_t)&hash_locals_dict,
//};
STATIC const mp_obj_type_t sha1_type = {
{ &mp_type_type },
.name = MP_QSTR_sha1,
.make_new = hash_make_new,
.locals_dict = (mp_obj_t)&hash_locals_dict,
};
STATIC const mp_obj_type_t sha256_type = {
{ &mp_type_type },
.name = MP_QSTR_sha256,
.make_new = hash_make_new,
.locals_dict = (mp_obj_t)&hash_locals_dict,
};
STATIC const mp_map_elem_t mp_module_hashlib_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib) },
// { MP_OBJ_NEW_QSTR(MP_QSTR_md5), (mp_obj_t)&md5_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sha1), (mp_obj_t)&sha1_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sha256), (mp_obj_t)&sha256_type },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_hashlib_globals, mp_module_hashlib_globals_table);
const mp_obj_module_t mp_module_uhashlib = {
.base = { &mp_type_module },
.name = MP_QSTR_uhashlib,
.globals = (mp_obj_dict_t*)&mp_module_hashlib_globals,
};

Wyświetl plik

@ -0,0 +1,32 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Paul Sokolovsky
* Copyright (c) 2015 Daniel Campora
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MODUHASHLIB_H_
#define MODUHASHLIB_H_
extern const mp_obj_module_t mp_module_uhashlib;
#endif // MODUHASHLIB_H_

Wyświetl plik

@ -108,12 +108,13 @@ extern const struct _mp_obj_module_t mp_module_usocket;
extern const struct _mp_obj_module_t mp_module_network;
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&mp_module_uos }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
{ 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;
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_utime }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_module_uselect }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&mp_module_usocket }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_hashlib), (mp_obj_t)&mp_module_uhashlib }, \
// extra constants
#define MICROPY_PORT_CONSTANTS \

Wyświetl plik

@ -65,6 +65,7 @@
#include "pybsleep.h"
#include "pybtimer.h"
#include "mpcallback.h"
#include "cryptohash.h"
/******************************************************************************
DECLARE PRIVATE CONSTANTS
@ -289,6 +290,8 @@ STATIC void mptask_pre_init (void) {
pybsd_init0();
#endif
CRYPTOHASH_Init();
#ifdef DEBUG
ASSERT (OSI_OK == osi_TaskCreate(TASK_Servers,
(const signed char *)"Servers",

Wyświetl plik

@ -349,3 +349,12 @@ Q(PWM)
Q(POSITIVE)
Q(NEGATIVE)
// for uhashlib module
Q(hashlib)
Q(uhashlib)
Q(update)
Q(digest)
Q(hexdigest)
//Q(md5)
Q(sha1)
Q(sha256)

Wyświetl plik

@ -35,20 +35,20 @@
#include "rom_map.h"
#include "prcm.h"
#include "shamd5.h"
#include "hash.h"
#include "simplelink.h"
#include "cryptohash.h"
/******************************************************************************
DEFINE PUBLIC FUNCTIONS
******************************************************************************/
void HASH_Init (void) {
__attribute__ ((section (".boot")))
void CRYPTOHASH_Init (void) {
// Enable the Data Hashing and Transform Engine
MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
MAP_PRCMPeripheralReset(PRCM_DTHE);
}
void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
void CRYPTOHASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
// wait until the context is ready
while ((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_CONTEXT_READY) == 0);
@ -64,12 +64,12 @@ void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
HWREG(SHAMD5_BASE + SHAMD5_O_LENGTH) = blocklen;
}
void HASH_SHAMD5Update (uint8_t *data, uint32_t datalen) {
void CRYPTOHASH_SHAMD5Update (uint8_t *data, uint32_t datalen) {
// write the data
SHAMD5DataWriteMultiple(data, datalen);
}
void HASH_SHAMD5Read (uint8_t *hash) {
void CRYPTOHASH_SHAMD5Read (uint8_t *hash) {
// wait for the output to be ready
while((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_OUTPUT_READY) == 0);
// read the result

Wyświetl plik

@ -24,15 +24,15 @@
* THE SOFTWARE.
*/
#ifndef HASH_H_
#define HASH_H_
#ifndef CRYPTOHASH_H_
#define CRYPTOHASH_H_
/******************************************************************************
DECLARE PUBLIC FUNCTIONS
******************************************************************************/
extern void HASH_Init (void);
extern void HASH_SHAMD5Start (uint32_t algo, uint32_t blocklen);
extern void HASH_SHAMD5Update (uint8_t *data, uint32_t datalen);
extern void HASH_SHAMD5Read (uint8_t *hash);
extern void CRYPTOHASH_Init (void);
extern void CRYPTOHASH_SHAMD5Start (uint32_t algo, uint32_t blocklen);
extern void CRYPTOHASH_SHAMD5Update (uint8_t *data, uint32_t datalen);
extern void CRYPTOHASH_SHAMD5Read (uint8_t *hash);
#endif /* HASH_H_ */
#endif /* CRYPTOHASH_H_ */