blob: 2f35e0c90b84f3b3dec7e2b219aa05aba87aac8c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptaac1b2692014-10-15 11:35:30 +05302/*
3 * SEC Descriptor Construction Library
4 * Basic job descriptor construction
5 *
6 * Copyright 2014 Freescale Semiconductor, Inc.
7 *
Ruchika Guptaac1b2692014-10-15 11:35:30 +05308 */
9
10#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Raul Cardenasb5a36d82015-02-27 11:22:06 -060012#include <fsl_sec.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053013#include "desc_constr.h"
14#include "jobdesc.h"
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +053015#include "rsa_caam.h"
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Ruchika Guptaac1b2692014-10-15 11:35:30 +053017
Ulises Cardenas2f736a92016-02-02 04:39:39 -060018#if defined(CONFIG_MX6) || defined(CONFIG_MX7)
Raul Cardenasb5a36d82015-02-27 11:22:06 -060019/*!
20 * Secure memory run command
21 *
22 * @param sec_mem_cmd Secure memory command register
23 * @return cmd_status Secure memory command status register
24 */
25uint32_t secmem_set_cmd(uint32_t sec_mem_cmd)
26{
27 uint32_t temp_reg;
28
Ulises Cardenas2f736a92016-02-02 04:39:39 -060029 ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
30 uint32_t sm_vid = SM_VERSION(sec_in32(&sec->smvid));
31 uint32_t jr_id = 0;
32
33 sec_out32(CAAM_SMCJR(sm_vid, jr_id), sec_mem_cmd);
Raul Cardenasb5a36d82015-02-27 11:22:06 -060034
35 do {
Ulises Cardenas2f736a92016-02-02 04:39:39 -060036 temp_reg = sec_in32(CAAM_SMCSJR(sm_vid, jr_id));
Raul Cardenasb5a36d82015-02-27 11:22:06 -060037 } while (temp_reg & CMD_COMPLETE);
38
39 return temp_reg;
40}
41
42/*!
43 * CAAM page allocation:
44 * Allocates a partition from secure memory, with the id
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -040045 * equal to partition_num. This will de-allocate the page
Raul Cardenasb5a36d82015-02-27 11:22:06 -060046 * if it is already allocated. The partition will have
47 * full access permissions. The permissions are set before,
48 * running a job descriptor. A memory page of secure RAM
49 * is allocated for the partition.
50 *
51 * @param page Number of the page to allocate.
52 * @param partition Number of the partition to allocate.
53 * @return 0 on success, ERROR_IN_PAGE_ALLOC otherwise
54 */
55int caam_page_alloc(uint8_t page_num, uint8_t partition_num)
56{
57 uint32_t temp_reg;
58
Ulises Cardenas2f736a92016-02-02 04:39:39 -060059 ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
60 uint32_t sm_vid = SM_VERSION(sec_in32(&sec->smvid));
61 uint32_t jr_id = 0;
62
Raul Cardenasb5a36d82015-02-27 11:22:06 -060063 /*
64 * De-Allocate partition_num if already allocated to ARM core
65 */
66 if (sec_in32(CAAM_SMPO_0) & PARTITION_OWNER(partition_num)) {
67 temp_reg = secmem_set_cmd(PARTITION(partition_num) |
68 CMD_PART_DEALLOC);
69 if (temp_reg & SMCSJR_AERR) {
70 printf("Error: De-allocation status 0x%X\n", temp_reg);
71 return ERROR_IN_PAGE_ALLOC;
72 }
73 }
74
75 /* set the access rights to allow full access */
Ulises Cardenas2f736a92016-02-02 04:39:39 -060076 sec_out32(CAAM_SMAG1JR(sm_vid, jr_id, partition_num), 0xF);
77 sec_out32(CAAM_SMAG2JR(sm_vid, jr_id, partition_num), 0xF);
78 sec_out32(CAAM_SMAPJR(sm_vid, jr_id, partition_num), 0xFF);
Raul Cardenasb5a36d82015-02-27 11:22:06 -060079
80 /* Now need to allocate partition_num of secure RAM. */
81 /* De-Allocate page_num by starting with a page inquiry command */
82 temp_reg = secmem_set_cmd(PAGE(page_num) | CMD_INQUIRY);
83
84 /* if the page is owned, de-allocate it */
85 if ((temp_reg & SMCSJR_PO) == PAGE_OWNED) {
86 temp_reg = secmem_set_cmd(PAGE(page_num) | CMD_PAGE_DEALLOC);
87 if (temp_reg & SMCSJR_AERR) {
88 printf("Error: Allocation status 0x%X\n", temp_reg);
89 return ERROR_IN_PAGE_ALLOC;
90 }
91 }
92
93 /* Allocate page_num to partition_num */
94 temp_reg = secmem_set_cmd(PAGE(page_num) | PARTITION(partition_num)
95 | CMD_PAGE_ALLOC);
96 if (temp_reg & SMCSJR_AERR) {
97 printf("Error: Allocation status 0x%X\n", temp_reg);
98 return ERROR_IN_PAGE_ALLOC;
99 }
100 /* page inquiry command to ensure that the page was allocated */
101 temp_reg = secmem_set_cmd(PAGE(page_num) | CMD_INQUIRY);
102
103 /* if the page is not owned => problem */
104 if ((temp_reg & SMCSJR_PO) != PAGE_OWNED) {
105 printf("Allocation of page %d in partition %d failed 0x%X\n",
106 temp_reg, page_num, partition_num);
107
108 return ERROR_IN_PAGE_ALLOC;
109 }
110
111 return 0;
112}
113
114int inline_cnstr_jobdesc_blob_dek(uint32_t *desc, const uint8_t *plain_txt,
115 uint8_t *dek_blob, uint32_t in_sz)
116{
Ulises Cardenas2f736a92016-02-02 04:39:39 -0600117 ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
118 uint32_t sm_vid = SM_VERSION(sec_in32(&sec->smvid));
119 uint32_t jr_id = 0;
120
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600121 uint32_t ret = 0;
122 u32 aad_w1, aad_w2;
123 /* output blob will have 32 bytes key blob in beginning and
124 * 16 byte HMAC identifier at end of data blob */
125 uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
126 /* Setting HDR for blob */
127 uint8_t wrapped_key_hdr[8] = {HDR_TAG, 0x00, WRP_HDR_SIZE + out_sz,
128 HDR_PAR, HAB_MOD, HAB_ALG, in_sz, HAB_FLG};
129
130 /* initialize the blob array */
131 memset(dek_blob, 0, out_sz + 8);
132 /* Copy the header into the DEK blob buffer */
133 memcpy(dek_blob, wrapped_key_hdr, sizeof(wrapped_key_hdr));
134
135 /* allocating secure memory */
136 ret = caam_page_alloc(PAGE_1, PARTITION_1);
137 if (ret)
138 return ret;
139
140 /* Write DEK to secure memory */
141 memcpy((uint32_t *)SEC_MEM_PAGE1, (uint32_t *)plain_txt, in_sz);
142
143 unsigned long start = (unsigned long)SEC_MEM_PAGE1 &
144 ~(ARCH_DMA_MINALIGN - 1);
145 unsigned long end = ALIGN(start + 0x1000, ARCH_DMA_MINALIGN);
146 flush_dcache_range(start, end);
147
148 /* Now configure the access rights of the partition */
Ulises Cardenas2f736a92016-02-02 04:39:39 -0600149 sec_out32(CAAM_SMAG1JR(sm_vid, jr_id, PARTITION_1), KS_G1);
150 sec_out32(CAAM_SMAG2JR(sm_vid, jr_id, PARTITION_1), 0);
151 sec_out32(CAAM_SMAPJR(sm_vid, jr_id, PARTITION_1), PERM);
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600152
153 /* construct aad for AES */
154 aad_w1 = (in_sz << OP_ALG_ALGSEL_SHIFT) | KEY_AES_SRC | LD_CCM_MODE;
155 aad_w2 = 0x0;
156
157 init_job_desc(desc, 0);
158
159 append_cmd(desc, CMD_LOAD | CLASS_2 | KEY_IMM | KEY_ENC |
160 (0x0c << LDST_OFFSET_SHIFT) | 0x08);
161
162 append_u32(desc, aad_w1);
163
164 append_u32(desc, aad_w2);
165
166 append_cmd_ptr(desc, (dma_addr_t)SEC_MEM_PAGE1, in_sz, CMD_SEQ_IN_PTR);
167
168 append_cmd_ptr(desc, (dma_addr_t)dek_blob + 8, out_sz, CMD_SEQ_OUT_PTR);
169
170 append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB |
171 OP_PCLID_SECMEM);
172
173 return ret;
174}
175#endif
Ruchika Gupta4345a572014-10-07 15:46:20 +0530176
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530177void inline_cnstr_jobdesc_hash(uint32_t *desc,
178 const uint8_t *msg, uint32_t msgsz, uint8_t *digest,
179 u32 alg_type, uint32_t alg_size, int sg_tbl)
180{
181 /* SHA 256 , output is of length 32 words */
182 uint32_t storelen = alg_size;
183 u32 options;
184 dma_addr_t dma_addr_in, dma_addr_out;
185
186 dma_addr_in = virt_to_phys((void *)msg);
187 dma_addr_out = virt_to_phys((void *)digest);
188
189 init_job_desc(desc, 0);
190 append_operation(desc, OP_TYPE_CLASS2_ALG |
191 OP_ALG_AAI_HASH | OP_ALG_AS_INITFINAL |
192 OP_ALG_ENCRYPT | OP_ALG_ICV_OFF | alg_type);
193
194 options = LDST_CLASS_2_CCB | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2;
195 if (sg_tbl)
196 options |= FIFOLDST_SGF;
197 if (msgsz > 0xffff) {
198 options |= FIFOLDST_EXT;
199 append_fifo_load(desc, dma_addr_in, 0, options);
200 append_cmd(desc, msgsz);
201 } else {
202 append_fifo_load(desc, dma_addr_in, msgsz, options);
203 }
204
205 append_store(desc, dma_addr_out, storelen,
206 LDST_CLASS_2_CCB | LDST_SRCDST_BYTE_CONTEXT);
207}
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530208#ifndef CONFIG_SPL_BUILD
Ruchika Gupta4345a572014-10-07 15:46:20 +0530209void inline_cnstr_jobdesc_blob_encap(uint32_t *desc, uint8_t *key_idnfr,
210 uint8_t *plain_txt, uint8_t *enc_blob,
211 uint32_t in_sz)
212{
213 dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
214 uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
215 /* output blob will have 32 bytes key blob in beginning and
216 * 16 byte HMAC identifier at end of data blob */
217 uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
218
219 dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
220 dma_addr_in = virt_to_phys((void *)plain_txt);
221 dma_addr_out = virt_to_phys((void *)enc_blob);
222
223 init_job_desc(desc, 0);
224
225 append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
226
227 append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
228
229 append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
230
231 append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB);
232}
233
234void inline_cnstr_jobdesc_blob_decap(uint32_t *desc, uint8_t *key_idnfr,
235 uint8_t *enc_blob, uint8_t *plain_txt,
236 uint32_t out_sz)
237{
238 dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
239 uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
240 uint32_t in_sz = out_sz + KEY_BLOB_SIZE + MAC_SIZE;
241
242 dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
243 dma_addr_in = virt_to_phys((void *)enc_blob);
244 dma_addr_out = virt_to_phys((void *)plain_txt);
245
246 init_job_desc(desc, 0);
247
248 append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
249
250 append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
251
252 append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
253
254 append_operation(desc, OP_TYPE_DECAP_PROTOCOL | OP_PCLID_BLOB);
255}
Ruchika Gupta0009c8f2017-04-17 18:07:19 +0530256#endif
Ruchika Gupta4345a572014-10-07 15:46:20 +0530257/*
258 * Descriptor to instantiate RNG State Handle 0 in normal mode and
259 * load the JDKEK, TDKEK and TDSK registers
260 */
Lukas Aueraed8eac2018-01-25 14:11:17 +0100261void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc, int handle)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530262{
263 u32 *jump_cmd;
264
265 init_job_desc(desc, 0);
266
267 /* INIT RNG in non-test mode */
268 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
Lukas Aueraed8eac2018-01-25 14:11:17 +0100269 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530270
Lukas Aueraed8eac2018-01-25 14:11:17 +0100271 /* For SH0, Secure Keys must be generated as well */
272 if (handle == 0) {
273 /* wait for done */
274 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
275 set_jump_tgt_here(desc, jump_cmd);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530276
Lukas Aueraed8eac2018-01-25 14:11:17 +0100277 /*
278 * load 1 to clear written reg:
279 * resets the done interrupt and returns the RNG to idle.
280 */
281 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530282
Lukas Aueraed8eac2018-01-25 14:11:17 +0100283 /* generate secure keys (non-test) */
284 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
285 OP_ALG_RNG4_SK);
286 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530287}
Ruchika Gupta29c1a6b2015-01-23 16:01:55 +0530288
289/* Change key size to bytes form bits in calling function*/
290void inline_cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
291 struct pk_in_params *pkin, uint8_t *out,
292 uint32_t out_siz)
293{
294 dma_addr_t dma_addr_e, dma_addr_a, dma_addr_n, dma_addr_out;
295
296 dma_addr_e = virt_to_phys((void *)pkin->e);
297 dma_addr_a = virt_to_phys((void *)pkin->a);
298 dma_addr_n = virt_to_phys((void *)pkin->n);
299 dma_addr_out = virt_to_phys((void *)out);
300
301 init_job_desc(desc, 0);
302 append_key(desc, dma_addr_e, pkin->e_siz, KEY_DEST_PKHA_E | CLASS_1);
303
304 append_fifo_load(desc, dma_addr_a,
305 pkin->a_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_A);
306
307 append_fifo_load(desc, dma_addr_n,
308 pkin->n_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_N);
309
310 append_operation(desc, OP_TYPE_PK | OP_ALG_PK | OP_ALG_PKMODE_MOD_EXPO);
311
312 append_fifo_store(desc, dma_addr_out, out_siz,
313 LDST_CLASS_1_CCB | FIFOST_TYPE_PKHA_B);
314}