crypto.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "crypto.h"
  2. #include "rng.h"
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include "inc/hw_types.h"
  6. #include "inc/hw_aes.h"
  7. #include "inc/hw_memmap.h"
  8. #include "driverlib/aes.h"
  9. #include "driverlib/rom_map.h"
  10. #include "driverlib/prcm.h"
  11. static volatile bool g_bContextInIntFlag;
  12. static volatile bool g_bDataInIntFlag;
  13. static volatile bool g_bContextOutIntFlag;
  14. static volatile bool g_bDataOutIntFlag;
  15. static uint32_t aes_mode=AES_CFG_MODE_CBC, aes_key_size, aes_blocksize;
  16. static uint8_t *aes_key;
  17. void AESCrypt(uint32_t dir, uint8_t *iv,
  18. uint8_t *data, uint8_t *result,
  19. uint32_t dataLength) {
  20. // Step1: Enable Interrupts
  21. // Step2: Wait for Context Ready Interrupt
  22. // Step3: Set the Configuration Parameters (Direction,AES Mode and Key Size)
  23. // Step4: Set the Initialisation Vector
  24. // Step5: Write Key
  25. // Step6: Start the crypt process
  26. // Clear the flags.
  27. g_bContextInIntFlag = false;
  28. g_bDataInIntFlag = false;
  29. g_bContextOutIntFlag = false;
  30. g_bDataOutIntFlag = false;
  31. // Enable all interrupts.
  32. MAP_AESIntEnable(AES_BASE, AES_INT_CONTEXT_IN | AES_INT_CONTEXT_OUT | AES_INT_DATA_IN | AES_INT_DATA_OUT);
  33. // Wait for the context in flag, the flag will be set in the Interrupt handler.
  34. while(!g_bContextInIntFlag) {}
  35. MAP_AESConfigSet(AES_BASE, dir | aes_mode | aes_key_size);
  36. MAP_AESIVSet(AES_BASE, iv);
  37. MAP_AESKey1Set(AES_BASE, aes_key, aes_key_size);
  38. MAP_AESDataProcess(AES_BASE, (unsigned char *) data, (unsigned char *) result, dataLength);
  39. }
  40. uint8_t * AESEncrypt(uint8_t *message, uint32_t messageLength) {
  41. uint32_t msgLen = messageLength + aes_blocksize;
  42. uint32_t ivblocks = aes_blocksize/4;
  43. uint8_t *result = (uint8_t *) malloc(msgLen);
  44. uint32_t *IVrand = (uint32_t *) result;
  45. uint8_t *data = &result[aes_blocksize];
  46. int i;
  47. for(i=0;i<ivblocks;i++) {
  48. IVrand[i] = random();
  49. }
  50. AESCrypt(AES_DIR_ENCRYPT, result, message, data, messageLength);
  51. return result;
  52. }
  53. // Message must be larger than 2 * aes_key_length (in bytes)
  54. uint8_t * AESDecrypt(uint8_t *message, uint32_t messageLength) {
  55. uint8_t *data = &message[aes_blocksize];
  56. uint32_t msgLen = messageLength - aes_blocksize;
  57. uint8_t *result = (uint8_t *) malloc(msgLen);
  58. AESCrypt(AES_DIR_DECRYPT, message, data, result, msgLen);
  59. return result;
  60. }
  61. void AESIntHandler(void){
  62. uint32_t uiIntStatus;
  63. // Read the AES masked interrupt status.
  64. uiIntStatus = MAP_AESIntStatus(AES_BASE, true);
  65. // Set Different flags depending on the interrupt source.
  66. if(uiIntStatus & AES_INT_CONTEXT_IN) {
  67. MAP_AESIntDisable(AES_BASE, AES_INT_CONTEXT_IN);
  68. g_bContextInIntFlag = true;
  69. }
  70. if(uiIntStatus & AES_INT_DATA_IN) {
  71. MAP_AESIntDisable(AES_BASE, AES_INT_DATA_IN);
  72. g_bDataInIntFlag = true;
  73. }
  74. if(uiIntStatus & AES_INT_CONTEXT_OUT) {
  75. MAP_AESIntDisable(AES_BASE, AES_INT_CONTEXT_OUT);
  76. g_bContextOutIntFlag = true;
  77. }
  78. if(uiIntStatus & AES_INT_DATA_OUT) {
  79. MAP_AESIntDisable(AES_BASE, AES_INT_DATA_OUT);
  80. g_bDataOutIntFlag = true;
  81. }
  82. }
  83. uint32_t getBlockSize(void) {
  84. return aes_blocksize;
  85. }
  86. void AESSetup(uint32_t key_size, uint8_t *key){
  87. aes_key_size = key_size;
  88. switch(key_size) {
  89. case AES_KEY_128BIT:
  90. aes_blocksize = 16;
  91. break;
  92. case AES_KEY_192BIT:
  93. aes_blocksize = 24;
  94. break;
  95. case AES_KEY_256BIT:
  96. aes_blocksize = 32;
  97. break;
  98. }
  99. aes_key = key;
  100. // Enable AES Module
  101. MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK);
  102. // Enable AES interrupts
  103. MAP_AESIntRegister(AES_BASE, AESIntHandler);
  104. }