moduhashlib.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * This file is part of the MicroPython 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. #include <assert.h>
  28. #include <string.h>
  29. #include "py/mpconfig.h"
  30. #include MICROPY_HAL_H
  31. #include "py/runtime.h"
  32. #include "inc/hw_types.h"
  33. #include "inc/hw_ints.h"
  34. #include "inc/hw_nvic.h"
  35. #include "inc/hw_shamd5.h"
  36. #include "inc/hw_dthe.h"
  37. #include "hw_memmap.h"
  38. #include "rom_map.h"
  39. #include "prcm.h"
  40. #include "shamd5.h"
  41. #include "cryptohash.h"
  42. #include "mpexception.h"
  43. /******************************************************************************
  44. DEFINE PRIVATE TYPES
  45. ******************************************************************************/
  46. typedef struct _mp_obj_hash_t {
  47. mp_obj_base_t base;
  48. uint8_t *buffer;
  49. uint32_t b_size;
  50. uint32_t c_size;
  51. uint8_t algo;
  52. uint8_t h_size;
  53. bool fixedlen;
  54. bool digested;
  55. uint8_t hash[32];
  56. } mp_obj_hash_t;
  57. /******************************************************************************
  58. DECLARE PRIVATE FUNCTIONS
  59. ******************************************************************************/
  60. STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest);
  61. STATIC mp_obj_t hash_read (mp_obj_t self_in);
  62. /******************************************************************************
  63. DEFINE PRIVATE FUNCTIONS
  64. ******************************************************************************/
  65. STATIC void hash_update_internal(mp_obj_t self_in, mp_obj_t data, bool digest) {
  66. mp_obj_hash_t *self = self_in;
  67. mp_buffer_info_t bufinfo;
  68. if (data) {
  69. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  70. }
  71. if (digest) {
  72. CRYPTOHASH_SHAMD5Start (self->algo, self->b_size);
  73. }
  74. if (self->c_size < self->b_size || !data || !self->fixedlen) {
  75. if (digest || self->fixedlen) {
  76. // no data means we want to process our internal buffer
  77. CRYPTOHASH_SHAMD5Update (data ? bufinfo.buf : self->buffer, data ? bufinfo.len : self->b_size);
  78. self->c_size += data ? bufinfo.len : 0;
  79. } else {
  80. self->buffer = m_renew(byte, self->buffer, self->b_size, self->b_size + bufinfo.len);
  81. mp_seq_copy((byte*)self->buffer + self->b_size, bufinfo.buf, bufinfo.len, byte);
  82. self->b_size += bufinfo.len;
  83. self->digested = false;
  84. }
  85. } else {
  86. mp_raise_OSError(MP_EPERM);
  87. }
  88. }
  89. STATIC mp_obj_t hash_read (mp_obj_t self_in) {
  90. mp_obj_hash_t *self = self_in;
  91. if (!self->fixedlen) {
  92. if (!self->digested) {
  93. hash_update_internal(self, MP_OBJ_NULL, true);
  94. }
  95. } else if (self->c_size < self->b_size) {
  96. // it's a fixed len block which is still incomplete
  97. mp_raise_OSError(MP_EPERM);
  98. }
  99. if (!self->digested) {
  100. CRYPTOHASH_SHAMD5Read ((uint8_t *)self->hash);
  101. self->digested = true;
  102. }
  103. return mp_obj_new_bytes(self->hash, self->h_size);
  104. }
  105. /******************************************************************************/
  106. // MicroPython bindings
  107. /// \classmethod \constructor([data[, block_size]])
  108. /// initial data must be given if block_size wants to be passed
  109. STATIC mp_obj_t hash_make_new(mp_obj_t type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  110. mp_arg_check_num(n_args, n_kw, 0, 2, false);
  111. mp_obj_hash_t *self = m_new0(mp_obj_hash_t, 1);
  112. self->base.type = type_in;
  113. if (self->base.type->name == MP_QSTR_sha1) {
  114. self->algo = SHAMD5_ALGO_SHA1;
  115. self->h_size = 20;
  116. } else /* if (self->base.type->name == MP_QSTR_sha256) */ {
  117. self->algo = SHAMD5_ALGO_SHA256;
  118. self->h_size = 32;
  119. } /* else {
  120. self->algo = SHAMD5_ALGO_MD5;
  121. self->h_size = 32;
  122. } */
  123. if (n_args) {
  124. // CPython extension to avoid buffering the data before digesting it
  125. // Note: care must be taken to provide all intermediate blocks as multiple
  126. // of four bytes, otherwise the resulting hash will be incorrect.
  127. // the final block can be of any length
  128. if (n_args > 1) {
  129. // block size given, we will feed the data directly into the hash engine
  130. self->fixedlen = true;
  131. self->b_size = mp_obj_get_int(args[1]);
  132. hash_update_internal(self, args[0], true);
  133. } else {
  134. hash_update_internal(self, args[0], false);
  135. }
  136. }
  137. return self;
  138. }
  139. STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
  140. mp_obj_hash_t *self = self_in;
  141. hash_update_internal(self, arg, false);
  142. return mp_const_none;
  143. }
  144. MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
  145. STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
  146. return hash_read(self_in);
  147. }
  148. MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest);
  149. STATIC const mp_rom_map_elem_t hash_locals_dict_table[] = {
  150. { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hash_update_obj) },
  151. { MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hash_digest_obj) },
  152. };
  153. STATIC MP_DEFINE_CONST_DICT(hash_locals_dict, hash_locals_dict_table);
  154. //STATIC const mp_obj_type_t md5_type = {
  155. // { &mp_type_type },
  156. // .name = MP_QSTR_md5,
  157. // .make_new = hash_make_new,
  158. // .locals_dict = (mp_obj_t)&hash_locals_dict,
  159. //};
  160. STATIC const mp_obj_type_t sha1_type = {
  161. { &mp_type_type },
  162. .name = MP_QSTR_sha1,
  163. .make_new = hash_make_new,
  164. .locals_dict = (mp_obj_t)&hash_locals_dict,
  165. };
  166. STATIC const mp_obj_type_t sha256_type = {
  167. { &mp_type_type },
  168. .name = MP_QSTR_sha256,
  169. .make_new = hash_make_new,
  170. .locals_dict = (mp_obj_t)&hash_locals_dict,
  171. };
  172. STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = {
  173. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uhashlib) },
  174. //{ MP_ROM_QSTR(MP_QSTR_md5), MP_ROM_PTR(&md5_type) },
  175. { MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&sha1_type) },
  176. { MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&sha256_type) },
  177. };
  178. STATIC MP_DEFINE_CONST_DICT(mp_module_hashlib_globals, mp_module_hashlib_globals_table);
  179. const mp_obj_module_t mp_module_uhashlib = {
  180. .base = { &mp_type_module },
  181. .globals = (mp_obj_dict_t*)&mp_module_hashlib_globals,
  182. };