hacc_export.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * (c) MediaTek Inc. 2011
  3. */
  4. #include "sec_osal.h"
  5. /*#include <mach/mt_typedefs.h>*/
  6. #include "sec_hal.h"
  7. #include "sec_osal_light.h"
  8. #include "sec_boot_lib.h"
  9. #include "sec_error.h"
  10. #include "hacc_mach.h"
  11. /******************************************************************************
  12. * This file provide the HACC operation function to secure library
  13. * All the functions should be general ...
  14. ******************************************************************************/
  15. #define MOD "ASF"
  16. /******************************************************************************
  17. * GLOBAL VARIABLES
  18. ******************************************************************************/
  19. bool bHACC_HWWrapKeyInit = FALSE;
  20. bool bHACC_SWKeyInit = FALSE;
  21. /******************************************************************************
  22. * INTERNAL VARIABLES
  23. ******************************************************************************/
  24. static const unsigned int g_HACC_CFG_1[8] = {
  25. 0x9ED40400, 0x00E884A1, 0xE3F083BD, 0x2F4E6D8A,
  26. 0xFF838E5C, 0xE940A0E3, 0x8D4DECC6, 0x45FC0989
  27. };
  28. static const unsigned int g_HACC_CFG_2[8] = {
  29. 0xAA542CDA, 0x55522114, 0xE3F083BD, 0x55522114,
  30. 0xAA542CDA, 0xAA542CDA, 0x55522114, 0xAA542CDA
  31. };
  32. static const unsigned int g_HACC_CFG_3[8] = {
  33. 0x2684B690, 0xEB67A8BE, 0xA113144C, 0x177B1215,
  34. 0x168BEE66, 0x1284B684, 0xDF3BCE3A, 0x217F6FA2
  35. };
  36. /******************************************************************************
  37. * INTERNAL ENGINE
  38. ******************************************************************************/
  39. static unsigned char *sp_hacc_internal(unsigned char *buf, unsigned int size, bool bAC,
  40. HACC_USER user, bool bDoLock, AES_OPS aes_type, bool bEn)
  41. {
  42. unsigned int err = 0;
  43. /* ---------------------------- */
  44. /* get hacc lock */
  45. /* ---------------------------- */
  46. if (TRUE == bDoLock) {
  47. /* If the semaphore is successfully acquired, this function returns 0. */
  48. err = osal_hacc_lock();
  49. if (err) {
  50. err = ERR_SBOOT_HACC_LOCK_FAIL;
  51. goto _err;
  52. }
  53. }
  54. /* ---------------------------- */
  55. /* ciphering and force AC */
  56. /* ---------------------------- */
  57. switch (user) {
  58. case HACC_USER1:
  59. /* ---------------------------- */
  60. /* use smart phone hacc function 1 */
  61. /* ---------------------------- */
  62. HACC_V3_Init(bEn, g_HACC_CFG_1);
  63. HACC_V3_Run((unsigned int *)buf, size, (unsigned int *)buf);
  64. HACC_V3_Terminate();
  65. break;
  66. case HACC_USER2:
  67. /* ---------------------------- */
  68. /* use smart phone hacc function 2 */
  69. /* ---------------------------- */
  70. HACC_V3_Init(bEn, g_HACC_CFG_2);
  71. HACC_V3_Run((unsigned int *)buf, size, (unsigned int *)buf);
  72. HACC_V3_Terminate();
  73. break;
  74. case HACC_USER3:
  75. /* use smart phone hacc function 3 */
  76. /* ---------------------------- */
  77. if (FALSE == bHACC_HWWrapKeyInit) {
  78. err = ERR_HACC_HW_WRAP_KEY_NOT_INIT;
  79. goto _err;
  80. }
  81. err = hacc_set_key(AES_HW_WRAP_KEY, AES_KEY_256);
  82. if (SEC_OK != err)
  83. goto _err;
  84. err = hacc_do_aes(aes_type, buf, buf, AES_BLK_SZ_ALIGN(size));
  85. if (SEC_OK != err)
  86. goto _err;
  87. break;
  88. case HACC_USER4:
  89. /* ---------------------------- */
  90. /* use smart phone hacc function 4 */
  91. /* ---------------------------- */
  92. HACC_V3_Init(bEn, g_HACC_CFG_3);
  93. HACC_V3_Run((unsigned int *)buf, size, (unsigned int *)buf);
  94. HACC_V3_Terminate();
  95. break;
  96. default:
  97. err = ERR_HACC_UNKNOWN_USER;
  98. goto _err;
  99. }
  100. /* ---------------------------- */
  101. /* release hacc lock */
  102. /* ---------------------------- */
  103. if (TRUE == bDoLock)
  104. osal_hacc_unlock();
  105. return buf;
  106. _err:
  107. if (TRUE == bDoLock)
  108. osal_hacc_unlock();
  109. pr_debug("[%s] HACC Fail (0x%x)\n", MOD, err);
  110. BUG_ON(!(0));
  111. return buf;
  112. }
  113. /******************************************************************************
  114. * ENCRYPTION
  115. ******************************************************************************/
  116. unsigned char *masp_hal_sp_hacc_enc(unsigned char *buf, unsigned int size, unsigned char bAC,
  117. HACC_USER user, unsigned char bDoLock)
  118. {
  119. return sp_hacc_internal(buf, size, TRUE, user, bDoLock, AES_ENC, TRUE);
  120. }
  121. /******************************************************************************
  122. * DECRYPTION
  123. ******************************************************************************/
  124. unsigned char *masp_hal_sp_hacc_dec(unsigned char *buf, unsigned int size, unsigned char bAC,
  125. HACC_USER user, unsigned char bDoLock)
  126. {
  127. return sp_hacc_internal(buf, size, TRUE, user, bDoLock, AES_DEC, FALSE);
  128. }
  129. /******************************************************************************
  130. * HACC BLK SIZE
  131. ******************************************************************************/
  132. unsigned int masp_hal_sp_hacc_blk_sz(void)
  133. {
  134. return AES_BLK_SZ;
  135. }
  136. /******************************************************************************
  137. * HACC INITIALIZATION
  138. ******************************************************************************/
  139. unsigned int masp_hal_sp_hacc_init(unsigned char *sec_seed, unsigned int size)
  140. {
  141. AES_KEY_SEED keyseed;
  142. unsigned int i = 0;
  143. if (_CRYPTO_SEED_LEN != size)
  144. return ERR_HACC_SEED_LEN_ERROR;
  145. keyseed.size = HACC_AES_MAX_KEY_SZ;
  146. for (i = 0; i < HACC_AES_MAX_KEY_SZ / 2; i++) {
  147. keyseed.seed[i] = sec_seed[i];
  148. keyseed.seed[HACC_AES_MAX_KEY_SZ - i - 1] = sec_seed[i] + MTK_HACC_SEED;
  149. }
  150. pr_debug("0x%x,0x%x,0x%x,0x%x\n", keyseed.seed[0], keyseed.seed[1], keyseed.seed[2],
  151. keyseed.seed[3]);
  152. return hacc_init(&keyseed);
  153. }