hacc_sk.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * (c) MediaTek Inc. 2011
  3. */
  4. /*#include <mach/mt_typedefs.h>
  5. #include <mach/mt_reg_base.h>
  6. */
  7. #include "sec_error.h"
  8. #include "hacc_mach.h"
  9. /******************************************************************************
  10. * this file contains the hardware secure engine low-level operations
  11. * note that : all the functions in this file are ONLY for HACC internal usages.
  12. ******************************************************************************/
  13. /******************************************************************************
  14. * CONSTANT DEFINITIONS
  15. ******************************************************************************/
  16. #define MOD "HACC"
  17. #define HACC_TEST (0)
  18. /******************************************************************************
  19. * DEBUG
  20. ******************************************************************************/
  21. #define SEC_DEBUG (0)
  22. #define SMSG printk
  23. #if SEC_DEBUG
  24. #define DMSG printk
  25. #else
  26. #define DMSG
  27. #endif
  28. /******************************************************************************
  29. * LOCAL VERIABLE
  30. ******************************************************************************/
  31. static struct hacc_context hacc_ctx;
  32. /******************************************************************************
  33. * LOCAL FUNCTIONS
  34. ******************************************************************************/
  35. #if HACC_TEST
  36. static void hacc_test(void)
  37. {
  38. unsigned int i, test_sz = HACC_AES_MAX_KEY_SZ * 24;
  39. unsigned int test_keysz = AES_KEY_256;
  40. unsigned char *test_src = (unsigned char *)HACC_AES_TEST_SRC;
  41. unsigned char *test_dst = (unsigned char *)HACC_AES_TEST_DST;
  42. unsigned char *test_tmp = (unsigned char *)HACC_AES_TEST_TMP;
  43. /* prepare data */
  44. for (i = 0; i < test_sz; i++)
  45. test_src[i] = i + 1;
  46. hacc_set_key(AES_HW_WRAP_KEY, test_keysz);
  47. hacc_do_aes(AES_ENC, test_src, test_tmp, test_sz);
  48. hacc_set_key(AES_HW_WRAP_KEY, test_keysz);
  49. hacc_do_aes(AES_DEC, test_tmp, test_dst, test_sz);
  50. for (i = 0; i < test_sz; i++) {
  51. if (test_src[i] != test_dst[i]) {
  52. DMSG("[%s] test_src[%d] = 0x%x != test_dst[%d] = 0x%x\n", MOD, i,
  53. test_src[i], i, test_dst[i]);
  54. DMSG(0);
  55. }
  56. }
  57. DMSG("[%s] encrypt & descrypt unit test pass. (Key = %dbits)\n", MOD, test_keysz << 3);
  58. }
  59. #else
  60. #define hacc_test() do {} while (0)
  61. #endif
  62. /******************************************************************************
  63. * GLOBAL FUNCTIONS
  64. ******************************************************************************/
  65. static unsigned int hacc_set_cfg(AES_CFG *cfg)
  66. {
  67. memcpy(&hacc_ctx.cfg, cfg, sizeof(AES_CFG));
  68. return SEC_OK;
  69. }
  70. static unsigned int hacc_set_mode(AES_MODE mode)
  71. {
  72. AES_CFG cfg;
  73. DRV_ClrReg32(HACC_ACON, HACC_AES_MODE_MASK);
  74. switch (mode) {
  75. case AES_ECB_MODE:
  76. /* no need cfg */
  77. memset(&cfg.config[0], 0, sizeof(cfg.config));
  78. DRV_SetReg32(HACC_ACON, HACC_AES_ECB);
  79. break;
  80. case AES_CBC_MODE:
  81. DRV_SetReg32(HACC_ACON, HACC_AES_CBC);
  82. break;
  83. default:
  84. return ERR_HACC_MODE_INVALID;
  85. }
  86. return SEC_OK;
  87. }
  88. unsigned int hacc_set_key(AES_KEY_ID id, AES_KEY key)
  89. {
  90. unsigned int i, acon = 0;
  91. unsigned int akey;
  92. unsigned char *tkey;
  93. switch (key) {
  94. case AES_KEY_128:
  95. acon |= HACC_AES_128;
  96. break;
  97. case AES_KEY_192:
  98. acon |= HACC_AES_192;
  99. break;
  100. case AES_KEY_256:
  101. acon |= HACC_AES_256;
  102. break;
  103. default:
  104. return ERR_HACC_KEY_INVALID;
  105. }
  106. /* set aes block size */
  107. hacc_ctx.blk_sz = key;
  108. /* set aes key length */
  109. DRV_ClrReg32(HACC_ACON, HACC_AES_TYPE_MASK);
  110. DRV_SetReg32(HACC_ACON, acon);
  111. /* clear key */
  112. for (i = 0; i < HACC_AES_MAX_KEY_SZ; i += 4)
  113. DRV_WriteReg32(HACC_AKEY0 + i, 0);
  114. /* set aes key */
  115. switch (id) {
  116. case AES_HW_KEY:
  117. DRV_SetReg32(HACC_ACONK, HACC_AES_BK2C);
  118. return 0;
  119. case AES_HW_WRAP_KEY:
  120. tkey = &hacc_ctx.hw_key[0];
  121. break;
  122. case AES_SW_KEY:
  123. default:
  124. tkey = &hacc_ctx.sw_key[0];
  125. break;
  126. }
  127. /* non hardware binding key */
  128. DRV_ClrReg32(HACC_ACONK, HACC_AES_BK2C);
  129. /* update key. note that don't use key directly */
  130. for (i = 0; i < HACC_AES_MAX_KEY_SZ; i += 4) {
  131. akey = (tkey[i] << 24) | (tkey[i + 1] << 16) | (tkey[i + 2] << 8) | (tkey[i + 3]);
  132. DRV_WriteReg32(HACC_AKEY0 + i, akey);
  133. }
  134. return SEC_OK;
  135. }
  136. unsigned int hacc_do_aes(AES_OPS ops, unsigned char *src, unsigned char *dst, unsigned int size)
  137. {
  138. unsigned int i;
  139. unsigned int *ds, *dt, *vt;
  140. /* make sure size is aligned to aes block size */
  141. if ((size % AES_BLK_SZ) != 0) {
  142. SMSG("[%s] size = %d is not %d bytes alignment\n", MOD, size, AES_BLK_SZ);
  143. return ERR_HACC_DATA_UNALIGNED;
  144. }
  145. vt = (unsigned int *)&hacc_ctx.cfg.config[0];
  146. /* erase src, cfg, out register */
  147. DRV_SetReg32(HACC_ACON2, HACC_AES_CLR);
  148. /* set init config */
  149. for (i = 0; i < AES_CFG_SZ; i += 4)
  150. DRV_WriteReg32(HACC_ACFG0 + i, *vt++);
  151. if (ops == AES_ENC)
  152. DRV_SetReg32(HACC_ACON, HACC_AES_ENC);
  153. else
  154. DRV_ClrReg32(HACC_ACON, HACC_AES_ENC);
  155. ds = (unsigned int *)src;
  156. dt = (unsigned int *)dst;
  157. do {
  158. /* fill in the data */
  159. for (i = 0; i < AES_BLK_SZ; i += 4)
  160. DRV_WriteReg32(HACC_ASRC0 + i, *ds++);
  161. /* start aes engine */
  162. DRV_SetReg32(HACC_ACON2, HACC_AES_START);
  163. /* wait for aes engine ready */
  164. while ((DRV_Reg32(HACC_ACON2) & HACC_AES_RDY) == 0)
  165. ;
  166. /* read out the data */
  167. for (i = 0; i < AES_BLK_SZ; i += 4)
  168. *dt++ = DRV_Reg32(HACC_AOUT0 + i);
  169. if (size == 0)
  170. goto _end;
  171. size -= AES_BLK_SZ;
  172. } while (size != 0);
  173. _end:
  174. return SEC_OK;
  175. }
  176. unsigned int hacc_deinit(void)
  177. {
  178. unsigned int ret = 0;
  179. /* clear aes module */
  180. DRV_SetReg32(HACC_ACON2, HACC_AES_CLR);
  181. return ret;
  182. }
  183. unsigned int hacc_init(AES_KEY_SEED *keyseed)
  184. {
  185. unsigned int i = 0;
  186. unsigned int *config;
  187. unsigned int ret = 0;
  188. hacc_deinit();
  189. /* DRV_WriteReg32(HACC_SECINIT0, HACC_SECINIT0_MAGIC); */
  190. /* DRV_WriteReg32(HACC_SECINIT1, HACC_SECINIT1_MAGIC); */
  191. /* DRV_WriteReg32(HACC_SECINIT2, HACC_SECINIT2_MAGIC); */
  192. /* clear aes module */
  193. DRV_SetReg32(HACC_ACON2, HACC_AES_CLR);
  194. /* set aes module in cbc mode with no byte order change */
  195. DRV_ClrReg32(HACC_ACON2, HACC_AES_CHG_BO_MASK | HACC_AES_MODE_MASK);
  196. DRV_SetReg32(HACC_ACON2, HACC_AES_CHG_BO_OFF | HACC_AES_CBC);
  197. /* aes secure initialiation */
  198. memset(&hacc_ctx, 0, sizeof(struct hacc_context));
  199. for (i = 0; i < keyseed->size; i++)
  200. hacc_ctx.sw_key[i] = keyseed->seed[i];
  201. config = (unsigned int *)&hacc_ctx.cfg.config[0];
  202. *config++ = HACC_CFG_0;
  203. *config++ = HACC_CFG_1;
  204. *config++ = HACC_CFG_2;
  205. *config = HACC_CFG_3;
  206. ret = hacc_set_cfg(&hacc_ctx.cfg);
  207. if (SEC_OK != ret)
  208. goto _end;
  209. ret = hacc_set_mode(AES_CBC_MODE);
  210. if (SEC_OK != ret)
  211. goto _end;
  212. /* derive the hardware wrapper key */
  213. ret = hacc_set_key(AES_HW_KEY, HACC_HW_KEY_SZ);
  214. if (SEC_OK != ret)
  215. goto _end;
  216. ret = hacc_do_aes(AES_ENC, &hacc_ctx.sw_key[0], &hacc_ctx.hw_key[0], AES_KEY_256);
  217. if (SEC_OK != ret)
  218. goto _end;
  219. ret = hacc_set_key(AES_HW_WRAP_KEY, AES_KEY_256);
  220. if (SEC_OK != ret)
  221. goto _end;
  222. hacc_test();
  223. /* from now on, HACC HW wrap key can be used */
  224. bHACC_HWWrapKeyInit = 1;
  225. /* from now on, HACC SW key can be used */
  226. bHACC_SWKeyInit = 1;
  227. _end:
  228. return ret;
  229. }