jobdesc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * SEC Descriptor Construction Library
  3. * Basic job descriptor construction
  4. *
  5. * Copyright 2014 Freescale Semiconductor, Inc.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. */
  10. #include <common.h>
  11. #include <fsl_sec.h>
  12. #include "desc_constr.h"
  13. #include "jobdesc.h"
  14. #include "rsa_caam.h"
  15. #if defined(CONFIG_MX6) || defined(CONFIG_MX7)
  16. /*!
  17. * Secure memory run command
  18. *
  19. * @param sec_mem_cmd Secure memory command register
  20. * @return cmd_status Secure memory command status register
  21. */
  22. uint32_t secmem_set_cmd(uint32_t sec_mem_cmd)
  23. {
  24. uint32_t temp_reg;
  25. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  26. uint32_t sm_vid = SM_VERSION(sec_in32(&sec->smvid));
  27. uint32_t jr_id = 0;
  28. sec_out32(CAAM_SMCJR(sm_vid, jr_id), sec_mem_cmd);
  29. do {
  30. temp_reg = sec_in32(CAAM_SMCSJR(sm_vid, jr_id));
  31. } while (temp_reg & CMD_COMPLETE);
  32. return temp_reg;
  33. }
  34. /*!
  35. * CAAM page allocation:
  36. * Allocates a partition from secure memory, with the id
  37. * equal to partition_num. This will de-allocate the page
  38. * if it is already allocated. The partition will have
  39. * full access permissions. The permissions are set before,
  40. * running a job descriptor. A memory page of secure RAM
  41. * is allocated for the partition.
  42. *
  43. * @param page Number of the page to allocate.
  44. * @param partition Number of the partition to allocate.
  45. * @return 0 on success, ERROR_IN_PAGE_ALLOC otherwise
  46. */
  47. int caam_page_alloc(uint8_t page_num, uint8_t partition_num)
  48. {
  49. uint32_t temp_reg;
  50. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  51. uint32_t sm_vid = SM_VERSION(sec_in32(&sec->smvid));
  52. uint32_t jr_id = 0;
  53. /*
  54. * De-Allocate partition_num if already allocated to ARM core
  55. */
  56. if (sec_in32(CAAM_SMPO_0) & PARTITION_OWNER(partition_num)) {
  57. temp_reg = secmem_set_cmd(PARTITION(partition_num) |
  58. CMD_PART_DEALLOC);
  59. if (temp_reg & SMCSJR_AERR) {
  60. printf("Error: De-allocation status 0x%X\n", temp_reg);
  61. return ERROR_IN_PAGE_ALLOC;
  62. }
  63. }
  64. /* set the access rights to allow full access */
  65. sec_out32(CAAM_SMAG1JR(sm_vid, jr_id, partition_num), 0xF);
  66. sec_out32(CAAM_SMAG2JR(sm_vid, jr_id, partition_num), 0xF);
  67. sec_out32(CAAM_SMAPJR(sm_vid, jr_id, partition_num), 0xFF);
  68. /* Now need to allocate partition_num of secure RAM. */
  69. /* De-Allocate page_num by starting with a page inquiry command */
  70. temp_reg = secmem_set_cmd(PAGE(page_num) | CMD_INQUIRY);
  71. /* if the page is owned, de-allocate it */
  72. if ((temp_reg & SMCSJR_PO) == PAGE_OWNED) {
  73. temp_reg = secmem_set_cmd(PAGE(page_num) | CMD_PAGE_DEALLOC);
  74. if (temp_reg & SMCSJR_AERR) {
  75. printf("Error: Allocation status 0x%X\n", temp_reg);
  76. return ERROR_IN_PAGE_ALLOC;
  77. }
  78. }
  79. /* Allocate page_num to partition_num */
  80. temp_reg = secmem_set_cmd(PAGE(page_num) | PARTITION(partition_num)
  81. | CMD_PAGE_ALLOC);
  82. if (temp_reg & SMCSJR_AERR) {
  83. printf("Error: Allocation status 0x%X\n", temp_reg);
  84. return ERROR_IN_PAGE_ALLOC;
  85. }
  86. /* page inquiry command to ensure that the page was allocated */
  87. temp_reg = secmem_set_cmd(PAGE(page_num) | CMD_INQUIRY);
  88. /* if the page is not owned => problem */
  89. if ((temp_reg & SMCSJR_PO) != PAGE_OWNED) {
  90. printf("Allocation of page %d in partition %d failed 0x%X\n",
  91. temp_reg, page_num, partition_num);
  92. return ERROR_IN_PAGE_ALLOC;
  93. }
  94. return 0;
  95. }
  96. int inline_cnstr_jobdesc_blob_dek(uint32_t *desc, const uint8_t *plain_txt,
  97. uint8_t *dek_blob, uint32_t in_sz)
  98. {
  99. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  100. uint32_t sm_vid = SM_VERSION(sec_in32(&sec->smvid));
  101. uint32_t jr_id = 0;
  102. uint32_t ret = 0;
  103. u32 aad_w1, aad_w2;
  104. /* output blob will have 32 bytes key blob in beginning and
  105. * 16 byte HMAC identifier at end of data blob */
  106. uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
  107. /* Setting HDR for blob */
  108. uint8_t wrapped_key_hdr[8] = {HDR_TAG, 0x00, WRP_HDR_SIZE + out_sz,
  109. HDR_PAR, HAB_MOD, HAB_ALG, in_sz, HAB_FLG};
  110. /* initialize the blob array */
  111. memset(dek_blob, 0, out_sz + 8);
  112. /* Copy the header into the DEK blob buffer */
  113. memcpy(dek_blob, wrapped_key_hdr, sizeof(wrapped_key_hdr));
  114. /* allocating secure memory */
  115. ret = caam_page_alloc(PAGE_1, PARTITION_1);
  116. if (ret)
  117. return ret;
  118. /* Write DEK to secure memory */
  119. memcpy((uint32_t *)SEC_MEM_PAGE1, (uint32_t *)plain_txt, in_sz);
  120. unsigned long start = (unsigned long)SEC_MEM_PAGE1 &
  121. ~(ARCH_DMA_MINALIGN - 1);
  122. unsigned long end = ALIGN(start + 0x1000, ARCH_DMA_MINALIGN);
  123. flush_dcache_range(start, end);
  124. /* Now configure the access rights of the partition */
  125. sec_out32(CAAM_SMAG1JR(sm_vid, jr_id, PARTITION_1), KS_G1);
  126. sec_out32(CAAM_SMAG2JR(sm_vid, jr_id, PARTITION_1), 0);
  127. sec_out32(CAAM_SMAPJR(sm_vid, jr_id, PARTITION_1), PERM);
  128. /* construct aad for AES */
  129. aad_w1 = (in_sz << OP_ALG_ALGSEL_SHIFT) | KEY_AES_SRC | LD_CCM_MODE;
  130. aad_w2 = 0x0;
  131. init_job_desc(desc, 0);
  132. append_cmd(desc, CMD_LOAD | CLASS_2 | KEY_IMM | KEY_ENC |
  133. (0x0c << LDST_OFFSET_SHIFT) | 0x08);
  134. append_u32(desc, aad_w1);
  135. append_u32(desc, aad_w2);
  136. append_cmd_ptr(desc, (dma_addr_t)SEC_MEM_PAGE1, in_sz, CMD_SEQ_IN_PTR);
  137. append_cmd_ptr(desc, (dma_addr_t)dek_blob + 8, out_sz, CMD_SEQ_OUT_PTR);
  138. append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB |
  139. OP_PCLID_SECMEM);
  140. return ret;
  141. }
  142. #endif
  143. void inline_cnstr_jobdesc_hash(uint32_t *desc,
  144. const uint8_t *msg, uint32_t msgsz, uint8_t *digest,
  145. u32 alg_type, uint32_t alg_size, int sg_tbl)
  146. {
  147. /* SHA 256 , output is of length 32 words */
  148. uint32_t storelen = alg_size;
  149. u32 options;
  150. dma_addr_t dma_addr_in, dma_addr_out;
  151. dma_addr_in = virt_to_phys((void *)msg);
  152. dma_addr_out = virt_to_phys((void *)digest);
  153. init_job_desc(desc, 0);
  154. append_operation(desc, OP_TYPE_CLASS2_ALG |
  155. OP_ALG_AAI_HASH | OP_ALG_AS_INITFINAL |
  156. OP_ALG_ENCRYPT | OP_ALG_ICV_OFF | alg_type);
  157. options = LDST_CLASS_2_CCB | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2;
  158. if (sg_tbl)
  159. options |= FIFOLDST_SGF;
  160. if (msgsz > 0xffff) {
  161. options |= FIFOLDST_EXT;
  162. append_fifo_load(desc, dma_addr_in, 0, options);
  163. append_cmd(desc, msgsz);
  164. } else {
  165. append_fifo_load(desc, dma_addr_in, msgsz, options);
  166. }
  167. append_store(desc, dma_addr_out, storelen,
  168. LDST_CLASS_2_CCB | LDST_SRCDST_BYTE_CONTEXT);
  169. }
  170. void inline_cnstr_jobdesc_blob_encap(uint32_t *desc, uint8_t *key_idnfr,
  171. uint8_t *plain_txt, uint8_t *enc_blob,
  172. uint32_t in_sz)
  173. {
  174. dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
  175. uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
  176. /* output blob will have 32 bytes key blob in beginning and
  177. * 16 byte HMAC identifier at end of data blob */
  178. uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
  179. dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
  180. dma_addr_in = virt_to_phys((void *)plain_txt);
  181. dma_addr_out = virt_to_phys((void *)enc_blob);
  182. init_job_desc(desc, 0);
  183. append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
  184. append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
  185. append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
  186. append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB);
  187. }
  188. void inline_cnstr_jobdesc_blob_decap(uint32_t *desc, uint8_t *key_idnfr,
  189. uint8_t *enc_blob, uint8_t *plain_txt,
  190. uint32_t out_sz)
  191. {
  192. dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
  193. uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
  194. uint32_t in_sz = out_sz + KEY_BLOB_SIZE + MAC_SIZE;
  195. dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
  196. dma_addr_in = virt_to_phys((void *)enc_blob);
  197. dma_addr_out = virt_to_phys((void *)plain_txt);
  198. init_job_desc(desc, 0);
  199. append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
  200. append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
  201. append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
  202. append_operation(desc, OP_TYPE_DECAP_PROTOCOL | OP_PCLID_BLOB);
  203. }
  204. /*
  205. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  206. * load the JDKEK, TDKEK and TDSK registers
  207. */
  208. void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc)
  209. {
  210. u32 *jump_cmd;
  211. init_job_desc(desc, 0);
  212. /* INIT RNG in non-test mode */
  213. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  214. OP_ALG_AS_INIT);
  215. /* wait for done */
  216. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  217. set_jump_tgt_here(desc, jump_cmd);
  218. /*
  219. * load 1 to clear written reg:
  220. * resets the done interrrupt and returns the RNG to idle.
  221. */
  222. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  223. /* generate secure keys (non-test) */
  224. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  225. OP_ALG_RNG4_SK);
  226. }
  227. /* Change key size to bytes form bits in calling function*/
  228. void inline_cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
  229. struct pk_in_params *pkin, uint8_t *out,
  230. uint32_t out_siz)
  231. {
  232. dma_addr_t dma_addr_e, dma_addr_a, dma_addr_n, dma_addr_out;
  233. dma_addr_e = virt_to_phys((void *)pkin->e);
  234. dma_addr_a = virt_to_phys((void *)pkin->a);
  235. dma_addr_n = virt_to_phys((void *)pkin->n);
  236. dma_addr_out = virt_to_phys((void *)out);
  237. init_job_desc(desc, 0);
  238. append_key(desc, dma_addr_e, pkin->e_siz, KEY_DEST_PKHA_E | CLASS_1);
  239. append_fifo_load(desc, dma_addr_a,
  240. pkin->a_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_A);
  241. append_fifo_load(desc, dma_addr_n,
  242. pkin->n_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_N);
  243. append_operation(desc, OP_TYPE_PK | OP_ALG_PK | OP_ALG_PKMODE_MOD_EXPO);
  244. append_fifo_store(desc, dma_addr_out, out_siz,
  245. LDST_CLASS_1_CCB | FIFOST_TYPE_PKHA_B);
  246. }