cryptohash.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  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 <stdint.h>
  27. #include <stdbool.h>
  28. #include "inc/hw_types.h"
  29. #include "inc/hw_ints.h"
  30. #include "inc/hw_nvic.h"
  31. #include "inc/hw_shamd5.h"
  32. #include "inc/hw_dthe.h"
  33. #include "hw_memmap.h"
  34. #include "rom_map.h"
  35. #include "prcm.h"
  36. #include "shamd5.h"
  37. #include "cryptohash.h"
  38. /******************************************************************************
  39. DEFINE PUBLIC FUNCTIONS
  40. ******************************************************************************/
  41. void CRYPTOHASH_Init (void) {
  42. // Enable the Data Hashing and Transform Engine
  43. MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  44. MAP_PRCMPeripheralReset(PRCM_DTHE);
  45. }
  46. void CRYPTOHASH_SHAMD5Start (uint32_t algo, uint32_t blocklen) {
  47. // wait until the context is ready
  48. while ((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_CONTEXT_READY) == 0);
  49. // Configure the SHA/MD5 module algorithm
  50. MAP_SHAMD5ConfigSet(SHAMD5_BASE, algo);
  51. // if not a multiple of 64 bytes, close the hash
  52. if (blocklen % 64) {
  53. HWREG(SHAMD5_BASE + SHAMD5_O_MODE) |= SHAMD5_MODE_CLOSE_HASH;
  54. }
  55. // set the lenght
  56. HWREG(SHAMD5_BASE + SHAMD5_O_LENGTH) = blocklen;
  57. }
  58. void CRYPTOHASH_SHAMD5Update (uint8_t *data, uint32_t datalen) {
  59. // write the data
  60. SHAMD5DataWriteMultiple(data, datalen);
  61. }
  62. void CRYPTOHASH_SHAMD5Read (uint8_t *hash) {
  63. // wait for the output to be ready
  64. while((HWREG(SHAMD5_BASE + SHAMD5_O_IRQSTATUS) & SHAMD5_INT_OUTPUT_READY) == 0);
  65. // read the result
  66. MAP_SHAMD5ResultRead(SHAMD5_BASE, hash);
  67. }