|
| 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 | + |
0 commit comments