hwaes.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "sam4l_hal.h"
  2. #include "sam4l_aes.h"
  3. #define GENCLK_SRC_CLK_CPU 7
  4. /**
  5. * \brief Initialize the AES module.
  6. *
  7. * \param dev_inst Device structure pointer.
  8. * \param aesa Base address of the AESA instance.
  9. * \param cfg Pointer to AES configuration.
  10. *
  11. * \retval true if the initialization was successful.
  12. * \retval false if initialization failed.
  13. */
  14. void HW_AES128_Init(void)
  15. {
  16. periclk_aesa_init();
  17. SCIF->SCIF_GCCTRL[AESA_GCLK_NUM].SCIF_GCCTRL = SCIF_GCCTRL_OSCSEL(GENCLK_SRC_CLK_CPU) | SCIF_GCCTRL_CEN;
  18. /* AES Enable */
  19. AESA->AESA_CTRL = AESA_CTRL_ENABLE | AESA_CTRL_NEWMSG; /* Enable, auto-accept new messages */
  20. //Use with debugger to check PARAMETER register value
  21. //volatile uint32_t param = AESA->AESA_PARAMETER;
  22. /* AES Mode */
  23. //AESA->AESA_MODE = AESA_MODE_ENCRYPT | (AESA_MODE_CTYPE(0x0F)); /* Encrypt Mode, with all countermeasures */
  24. AESA->AESA_MODE = AESA_MODE_ENCRYPT; /* Encrypt Mode, without countermeasures */
  25. /* Setup random seed for countermeasures to work */
  26. AESA->AESA_DRNGSEED = 0xDEADBEEF; //A very random number
  27. }
  28. void HW_AES128_LoadKey(uint8_t * key)
  29. {
  30. AESA->AESA_KEY[0].AESA_KEY = *(WoReg *)(key + 0);
  31. AESA->AESA_KEY[1].AESA_KEY = *(WoReg *)(key + 4);
  32. AESA->AESA_KEY[2].AESA_KEY = *(WoReg *)(key + 8);
  33. AESA->AESA_KEY[3].AESA_KEY = *(WoReg *)(key + 12);
  34. }
  35. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  36. {
  37. AESA->AESA_DATABUFPTR = 0; /* Auto-intecremented */
  38. AESA->AESA_IDATA = *(WoReg *)(pt + 0);
  39. AESA->AESA_IDATA = *(WoReg *)(pt + 4);
  40. AESA->AESA_IDATA = *(WoReg *)(pt + 8);
  41. }
  42. void HW_AES128_Enc(uint8_t * pt)
  43. {
  44. AESA->AESA_IDATA = *(WoReg *)(pt + 12);
  45. /* Wait */
  46. while((AESA->AESA_SR & AESA_SR_ODATARDY) == 0);
  47. }
  48. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  49. {
  50. AESA->AESA_DATABUFPTR = 0;
  51. *(WoReg *)(pt + 0) = AESA->AESA_ODATA;
  52. *(WoReg *)(pt + 4) = AESA->AESA_ODATA;
  53. *(WoReg *)(pt + 8) = AESA->AESA_ODATA;
  54. *(WoReg *)(pt + 12) = AESA->AESA_ODATA;
  55. }