hacc_tee.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <linux/types.h>
  2. #include "sec_hal.h"
  3. #include "sec_osal.h"
  4. #include "hacc_mach.h"
  5. #include "hacc_tee.h"
  6. #include "sec_error.h"
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define BOOL uint8_t
  10. /* To turn on HACC module clock if required */
  11. unsigned char masp_hal_secure_algo_init(void)
  12. {
  13. bool ret = TRUE;
  14. return ret;
  15. }
  16. /* To turn off HACC module clock if required */
  17. unsigned char masp_hal_secure_algo_deinit(void)
  18. {
  19. bool ret = TRUE;
  20. return ret;
  21. }
  22. /* This function will not work in TEE case */
  23. unsigned int masp_hal_sp_hacc_init(unsigned char *sec_seed, unsigned int size)
  24. {
  25. /* No implemtation is required in TEE's case */
  26. return 0;
  27. }
  28. unsigned int masp_hal_sp_hacc_blk_sz(void)
  29. {
  30. return AES_BLK_SZ;
  31. }
  32. static char *hacc_secure_request(HACC_USER user, unsigned char *buf, unsigned int buf_size,
  33. BOOL bEncrypt, BOOL bDoLock, unsigned char *sec_seed,
  34. unsigned int seed_size)
  35. {
  36. unsigned int ret = SEC_OK;
  37. /* get hacc lock */
  38. if (TRUE == bDoLock) {
  39. /* If the semaphore is successfully acquired, this function returns 0. */
  40. ret = osal_hacc_lock();
  41. if (ret) {
  42. ret = ERR_SBOOT_HACC_LOCK_FAIL;
  43. goto _exit;
  44. }
  45. }
  46. /* turn on clock */
  47. masp_hal_secure_algo_init();
  48. if (buf_size != 0) {
  49. /* try to open connection to TEE */
  50. if (open_sdriver_connection() < 0) {
  51. ret = ERR_HACC_OPEN_SECURE_CONNECTION_FAIL;
  52. goto _exit;
  53. }
  54. /* send request to TEE */
  55. ret =
  56. tee_secure_request((unsigned int)user, buf, buf_size, (unsigned int)bEncrypt,
  57. sec_seed, seed_size);
  58. if (ret != SEC_OK) {
  59. ret = ERR_HACC_REQUEST_SECURE_SERVICE_FAIL;
  60. goto _exit;
  61. }
  62. if (close_sdriver_connection() < 0) {
  63. ret = ERR_HACC_CLOSE_SECURE_CONNECTION_FAIL;
  64. goto _exit;
  65. }
  66. } else {
  67. pr_debug
  68. ("[HACC] hacc_secure_request - buffer size is 0, no encryption or decyrption is performed\n");
  69. }
  70. _exit:
  71. /* turn off clock */
  72. masp_hal_secure_algo_deinit();
  73. /* release hacc lock */
  74. if (TRUE == bDoLock)
  75. osal_hacc_unlock();
  76. if (ret) {
  77. pr_debug("[HACC] hacc_secure_request fail (0x%x) (don't ASSERT)\n", ret);
  78. /* ASSERT(0); */
  79. }
  80. return buf;
  81. }
  82. void masp_hal_secure_algo(unsigned char Direction, unsigned char *ContentAddr,
  83. unsigned int ContentLen, unsigned char *CustomSeed,
  84. unsigned char *ResText)
  85. {
  86. unsigned int err = 0;
  87. unsigned char *src, *dst;
  88. unsigned int i = 0;
  89. /* try to get hacc lock */
  90. do {
  91. /* If the semaphore is successfully acquired, this function returns 0. */
  92. err = osal_hacc_lock();
  93. } while (0 != err);
  94. /* initialize source and destination address */
  95. src = (unsigned char *)ContentAddr;
  96. dst = (unsigned char *)ResText;
  97. /* according to input parameter to encrypt or decrypt */
  98. switch (Direction) {
  99. case TRUE:
  100. dst = hacc_secure_request(HACC_USER3, (unsigned char *)src,
  101. ContentLen, TRUE, FALSE, CustomSeed, _CRYPTO_SEED_LEN); /* encrypt */
  102. break;
  103. case FALSE:
  104. dst = hacc_secure_request(HACC_USER3, (unsigned char *)src,
  105. ContentLen, FALSE, FALSE, CustomSeed, _CRYPTO_SEED_LEN); /* decrypt */
  106. break;
  107. default:
  108. err = ERR_KER_CRYPTO_INVALID_MODE;
  109. goto _wrong_direction;
  110. }
  111. /* copy result */
  112. for (i = 0; i < ContentLen; i++)
  113. *(ResText + i) = *(dst + i);
  114. _wrong_direction:
  115. /* try to release hacc lock */
  116. osal_hacc_unlock();
  117. if (err) {
  118. pr_debug("[HACC] masp_hal_secure_algo error (0x%x) (don't ASSERT)\n", err);
  119. /* ASSERT(0); */
  120. }
  121. }
  122. /*
  123. * For SECRO (user1), this function will help to get hacc lock
  124. * For SECCFG (user1-sbchk), it should get hacc lock via ioctl command before using this function
  125. * For MD NVRAM (user3), it should get hacc lock before using this function
  126. * For AP NVRAM (user2), it should get hacc lock via ioctl command before using this function
  127. */
  128. unsigned char *masp_hal_sp_hacc_enc(unsigned char *buf, unsigned int size, unsigned char bAC,
  129. HACC_USER user, unsigned char bDoLock)
  130. {
  131. return hacc_secure_request(user, buf, size, TRUE, bDoLock, NULL, 0);
  132. }
  133. unsigned char *masp_hal_sp_hacc_dec(unsigned char *buf, unsigned int size, unsigned char bAC,
  134. HACC_USER user, unsigned char bDoLock)
  135. {
  136. return hacc_secure_request(user, buf, size, FALSE, bDoLock, NULL, 0);
  137. }