crypto.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, uint32_t *returnlength) {
  41. uint8_t *inData = message;
  42. uint8_t *inBuf = 0x00;
  43. if(messageLength%16!=0) { // Needs to be in blocks of 16 bytes
  44. uint32_t newLength = ((messageLength/16)+1)*16;
  45. inBuf = (uint8_t) malloc(messageLength);
  46. memset(inBuf, 0, messageLength);
  47. memcpy(inBuf, message, messageLength);
  48. messageLength = newLength;
  49. inData = inBuf;
  50. }
  51. uint32_t msgLen = messageLength + aes_blocksize;
  52. uint32_t ivblocks = aes_blocksize/4;
  53. uint8_t *result = (uint8_t *) malloc(msgLen);
  54. uint32_t *IVrand = (uint32_t *) result;
  55. uint8_t *data = &result[aes_blocksize];
  56. int i;
  57. for(i=0;i<ivblocks;i++) {
  58. IVrand[i] = random();
  59. }
  60. AESCrypt(AES_DIR_ENCRYPT, result, inData, data, messageLength);
  61. if(inBuf != 0x00) free(inBuf);
  62. *returnlength = msgLen;
  63. return result;
  64. }
  65. // Message must be larger than 2 * aes_key_length (in bytes)
  66. uint8_t * AESDecrypt(uint8_t *message, uint32_t messageLength, uint32_t *returnlength) {
  67. uint8_t *data = &message[aes_blocksize];
  68. uint32_t msgLen = messageLength - aes_blocksize;
  69. uint8_t *result = (uint8_t *) malloc(msgLen);
  70. AESCrypt(AES_DIR_DECRYPT, message, data, result, msgLen);
  71. *returnlength = msgLen;
  72. return result;
  73. }
  74. void AESIntHandler(void){
  75. uint32_t uiIntStatus;
  76. // Read the AES masked interrupt status.
  77. uiIntStatus = MAP_AESIntStatus(AES_BASE, true);
  78. // Set Different flags depending on the interrupt source.
  79. if(uiIntStatus & AES_INT_CONTEXT_IN) {
  80. MAP_AESIntDisable(AES_BASE, AES_INT_CONTEXT_IN);
  81. g_bContextInIntFlag = true;
  82. }
  83. if(uiIntStatus & AES_INT_DATA_IN) {
  84. MAP_AESIntDisable(AES_BASE, AES_INT_DATA_IN);
  85. g_bDataInIntFlag = true;
  86. }
  87. if(uiIntStatus & AES_INT_CONTEXT_OUT) {
  88. MAP_AESIntDisable(AES_BASE, AES_INT_CONTEXT_OUT);
  89. g_bContextOutIntFlag = true;
  90. }
  91. if(uiIntStatus & AES_INT_DATA_OUT) {
  92. MAP_AESIntDisable(AES_BASE, AES_INT_DATA_OUT);
  93. g_bDataOutIntFlag = true;
  94. }
  95. }
  96. uint32_t getBlockSize(void) {
  97. return aes_blocksize;
  98. }
  99. void AESSetup(uint32_t key_size, uint8_t *key){
  100. aes_key_size = key_size;
  101. switch(key_size) {
  102. case AES_KEY_128BIT:
  103. aes_blocksize = 16;
  104. break;
  105. case AES_KEY_192BIT:
  106. aes_blocksize = 24;
  107. break;
  108. case AES_KEY_256BIT:
  109. aes_blocksize = 32;
  110. break;
  111. }
  112. aes_key = key;
  113. // Enable AES Module
  114. MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK);
  115. // Enable AES interrupts
  116. MAP_AESIntRegister(AES_BASE, AESIntHandler);
  117. }