psoc62_hal.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. This file is part of the ChipWhisperer Example Targets
  3. Copyright (C) 2019 NewAE Technology Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Portions of this HAL are based on Atmel ASF.
  15. */
  16. #include "psoc62_hal.h"
  17. #include "cy_crypto_common.h"
  18. #include "cy_crypto_core_aes.h"
  19. cy_stc_scb_uart_context_t uart_ctx;
  20. void platform_init(void)
  21. {
  22. init_cycfg_all();
  23. /* Enable the Crypto block */
  24. Cy_Crypto_Core_Enable(CRYPTO);
  25. }
  26. void init_uart(void)
  27. {
  28. Cy_SCB_UART_Init(SCB1, &scb_1_config, &uart_ctx);
  29. Cy_SysClk_PeriphAssignDivider(PCLK_SCB1_CLOCK, CY_SYSCLK_DIV_16_BIT, 0);
  30. Cy_SCB_UART_Enable(SCB1);
  31. }
  32. void putch(char c)
  33. {
  34. Cy_SCB_UART_Put(SCB1, c);
  35. while (!Cy_SCB_UART_IsTxComplete(SCB1));
  36. }
  37. char getch(void)
  38. {
  39. uint32_t val = CY_SCB_UART_RX_NO_DATA;
  40. while(val == CY_SCB_UART_RX_NO_DATA) {
  41. val = Cy_SCB_UART_Get(SCB1);
  42. }
  43. return (char)val;
  44. }
  45. cy_stc_crypto_aes_state_t aesState;
  46. /* Key used for AES encryption*/
  47. CY_ALIGN(4) uint8_t AES_Key[16]={};
  48. CY_ALIGN(4) uint8_t message[16];
  49. CY_ALIGN(4) uint8_t encrypted_msg[16];
  50. void HW_AES128_Init(void)
  51. {
  52. ;//Init is done in the main function instead
  53. }
  54. void HW_AES128_Enc_pretrigger(uint8_t * pt)
  55. {
  56. for(int i = 0; i < 16; i++){
  57. message[i] = pt[i];
  58. }
  59. Cy_Crypto_Core_Aes_Init(CRYPTO, AES_Key, CY_CRYPTO_KEY_AES_128, &aesState);
  60. }
  61. void HW_AES128_Enc(uint8_t * pt)
  62. {
  63. Cy_Crypto_Core_Aes_Ecb(CRYPTO, CY_CRYPTO_ENCRYPT, encrypted_msg, message, &aesState);
  64. /* Wait for Crypto Block to be available */
  65. Cy_Crypto_Core_WaitForReady(CRYPTO);
  66. }
  67. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  68. {
  69. for(int i = 0; i < 16; i++){
  70. pt[i] = encrypted_msg[i];
  71. }
  72. }
  73. void HW_AES128_LoadKey(uint8_t * key)
  74. {
  75. for(int i = 0; i < 16; i++){
  76. AES_Key[i] = key[i];
  77. }
  78. }