k82f_hal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "k82f_hal.h"
  2. #include <stdio.h>
  3. #include "fsl_flexio.h"
  4. #include "fsl_gpio.h"
  5. #include "fsl_port.h"
  6. #include "fsl_clock.h"
  7. #include "clock_config.h"
  8. #include "fsl_lpuart.h"
  9. #include "fsl_mmcau.h"
  10. #include "fsl_ltc.h"
  11. #include "fsl_trng.h"
  12. #include <stdint.h>
  13. #define ROUNDS_BEFORE_RESEED 50000
  14. #define AES_KEY_SIZE 16
  15. static uint32_t AES_KEY_SCH[44];
  16. static char ltcAesKey[AES_KEY_SIZE];
  17. static uint32_t maskSeed;
  18. static uint32_t nbAesBlocks;
  19. void platform_init(void)
  20. {
  21. trng_config_t trngConfig;
  22. #if ETM_ENABLE
  23. etmtrace_enable();
  24. #endif
  25. BOARD_BootClockRUN();
  26. // Start TRNG
  27. TRNG_GetDefaultConfig(&trngConfig);
  28. TRNG_Init(TRNG0, &trngConfig);
  29. #if USE_TRUSTED_CRYPTO
  30. // Start Trusted Crypto module
  31. LTC_Init(LTC0);
  32. TRNG_GetRandomData(TRNG0, &maskSeed, sizeof(maskSeed));
  33. LTC_SetDpaMaskSeed(LTC0, maskSeed);
  34. nbAesBlocks = 0;
  35. #endif
  36. }
  37. void init_uart(void)
  38. {
  39. lpuart_config_t config;
  40. CLOCK_EnableClock(kCLOCK_PortB); //enable port e clock
  41. /* CLOCK_EnableClock(kCLOCK_Uart1); //enable UART clock */
  42. LPUART_GetDefaultConfig(&config);
  43. config.baudRate_Bps = 38400U;
  44. config.enableTx = true;
  45. config.enableRx = true;
  46. PORT_SetPinMux(PORTB, 16, kPORT_MuxAlt3);
  47. PORT_SetPinMux(PORTB, 17, kPORT_MuxAlt3);
  48. /* volatile uint32_t uartclock = CLOCK_GetFreq(LPUART0_CLK_SRC); */
  49. LPUART_Init(LPUART0, &config, 7370000U);
  50. }
  51. void trigger_setup(void)
  52. {
  53. PORT_SetPinMux(PORTB, 19, kPORT_MuxAsGpio);
  54. gpio_pin_config_t trigconf = {
  55. kGPIO_DigitalOutput, 0
  56. };
  57. GPIO_PinInit(GPIOB, 19, &trigconf);
  58. GPIO_WritePinOutput(GPIOB, 19, 0);
  59. }
  60. void trigger_high(void)
  61. {
  62. GPIO_WritePinOutput(GPIOB, 19, 1);
  63. }
  64. void trigger_low(void)
  65. {
  66. GPIO_WritePinOutput(GPIOB, 19, 0);
  67. }
  68. char getch(void)
  69. {
  70. char c;
  71. LPUART_ReadBlocking(LPUART0, &c, 1);
  72. return c;
  73. }
  74. void putch(char c)
  75. {
  76. LPUART_WriteBlocking(LPUART0, &c, 1);
  77. }
  78. uint32_t get_rand(void)
  79. {
  80. uint32_t value;
  81. TRNG_GetRandomData(TRNG0, &value, sizeof(value));
  82. return value;
  83. }
  84. //nothing needed?
  85. void HW_AES128_Init(void)
  86. {
  87. }
  88. void HW_AES128_LoadKey(uint8_t* key)
  89. {
  90. #if USE_TRUSTED_CRYPTO
  91. memcpy(ltcAesKey, key, AES_KEY_SIZE);
  92. #else
  93. MMCAU_AES_SetKey(key, AES_KEY_SIZE, AES_KEY_SCH);
  94. #endif
  95. }
  96. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  97. {
  98. ;
  99. }
  100. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  101. {
  102. #if USE_TRUSTED_CRYPTO
  103. nbAesBlocks++;
  104. if (nbAesBlocks >= ROUNDS_BEFORE_RESEED) {
  105. nbAesBlocks = 0;
  106. TRNG_GetRandomData(TRNG0, &maskSeed, sizeof(maskSeed));
  107. LTC_SetDpaMaskSeed(LTC0, maskSeed);
  108. }
  109. #endif
  110. }
  111. void HW_AES128_Enc(uint8_t* pt)
  112. {
  113. #if USE_TRUSTED_CRYPTO
  114. LTC_AES_EncryptEcb(LTC0, pt, pt, LTC_AES_BLOCK_SIZE, ltcAesKey,
  115. AES_KEY_SIZE);
  116. #else
  117. MMCAU_AES_EncryptEcb(pt, AES_KEY_SCH, 10, pt);
  118. #endif
  119. }
  120. void HW_AES128_Dec(uint8_t *pt)
  121. {
  122. #if USE_TRUSTED_CRYPTO
  123. LTC_AES_DecryptEcb(LTC0, pt, pt, LTC_AES_BLOCK_SIZE, ltcAesKey,
  124. AES_KEY_SIZE, kLTC_DecryptKey);
  125. nbAesBlocks++;
  126. if (nbAesBlocks >= ROUNDS_BEFORE_RESEED) {
  127. nbAesBlocks = 0;
  128. TRNG_GetRandomData(TRNG0, &maskSeed, sizeof(maskSeed));
  129. LTC_SetDpaMaskSeed(LTC0, maskSeed);
  130. }
  131. #else
  132. MMCAU_AES_DecryptEcb(pt, AES_KEY_SCH, 10, pt);
  133. #endif
  134. }