1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2008-2015 Freescale Semiconductor, Inc.
6 * Command for encapsulating DEK blob
14 #include <asm/byteorder.h>
15 #include <linux/compiler.h>
17 #include <asm/arch/clock.h>
20 #ifdef CONFIG_IMX_SECO_DEK_ENCAP
21 #include <firmware/imx/sci/sci.h>
22 #include <asm/mach-imx/image.h>
24 #ifdef CONFIG_IMX_ELE_DEK_ENCAP
25 #include <asm/mach-imx/ele_api.h>
26 #include <asm/mach-imx/image.h>
32 * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
33 * @src: - Address of data to be encapsulated
34 * @dst: - Desination address of encapsulated data
35 * @len: - Size of data to be encapsulated
37 * Returns zero on success,and negative on error.
39 #ifdef CONFIG_IMX_CAAM_DEK_ENCAP
40 static int blob_encap_dek(u32 src_addr, u32 dst_addr, u32 len)
42 u8 *src_ptr, *dst_ptr;
44 src_ptr = map_sysmem(src_addr, len / 8);
45 dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len / 8));
47 hab_caam_clock_enable(1);
49 u32 out_jr_size = sec_in32(CFG_SYS_FSL_JR0_ADDR +
50 FSL_CAAM_ORSR_JRa_OFFSET);
51 if (out_jr_size != FSL_CAAM_MAX_JR_SIZE)
54 if (!((len == 128) | (len == 192) | (len == 256))) {
55 debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
60 return blob_dek(src_ptr, dst_ptr, len);
62 #endif /* CONFIG_IMX_CAAM_DEK_ENCAP */
64 #ifdef CONFIG_IMX_OPTEE_DEK_ENCAP
66 #define PTA_DEK_BLOB_PTA_UUID {0xef477737, 0x0db1, 0x4a9d, \
67 {0x84, 0x37, 0xf2, 0xf5, 0x35, 0xc0, 0xbd, 0x92} }
69 #define OPTEE_BLOB_HDR_SIZE 8
71 static int blob_encap_dek(u32 src_addr, u32 dst_addr, u32 len)
73 struct udevice *dev = NULL;
74 struct tee_shm *shm_input, *shm_output;
75 struct tee_open_session_arg arg = {0};
76 struct tee_invoke_arg arg_func = {0};
77 const struct tee_optee_ta_uuid uuid = PTA_DEK_BLOB_PTA_UUID;
78 struct tee_param param[4] = {0};
82 dev = tee_find_device(NULL, NULL, NULL, NULL);
84 printf("Cannot get OP-TEE device\n");
89 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
92 ret = tee_open_session(dev, &arg, 0, NULL);
94 printf("Cannot open session with PTA Blob 0x%X\n", ret);
98 /* Allocate shared input and output buffers for TA */
99 ret = tee_shm_register(dev, (void *)(ulong)src_addr, len / 8, 0x0, &shm_input);
101 printf("Cannot register input shared memory 0x%X\n", ret);
105 ret = tee_shm_register(dev, (void *)(ulong)dst_addr,
106 BLOB_SIZE(len / 8) + OPTEE_BLOB_HDR_SIZE,
109 printf("Cannot register output shared memory 0x%X\n", ret);
110 tee_shm_free(shm_input);
114 param[0].u.memref.shm = shm_input;
115 param[0].u.memref.size = shm_input->size;
116 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
117 param[1].u.memref.shm = shm_output;
118 param[1].u.memref.size = shm_output->size;
119 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
120 param[2].attr = TEE_PARAM_ATTR_TYPE_NONE;
121 param[3].attr = TEE_PARAM_ATTR_TYPE_NONE;
124 arg_func.session = arg.session;
126 /* Generate DEK blob */
127 arg_func.session = arg.session;
128 ret = tee_invoke_func(dev, &arg_func, 4, param);
130 printf("Cannot generate Blob with PTA DEK Blob 0x%X\n", ret);
132 /* Free shared memory */
133 tee_shm_free(shm_input);
134 tee_shm_free(shm_output);
138 ret = tee_close_session(dev, arg.session);
140 printf("Cannot close session with PTA DEK Blob 0x%X\n", ret);
144 #endif /* CONFIG_IMX_OPTEE_DEK_ENCAP */
145 #ifdef CONFIG_IMX_SECO_DEK_ENCAP
147 #define DEK_BLOB_KEY_ID 0x0
149 #define AHAB_PRIVATE_KEY 0x81
150 #define AHAB_VERSION 0x00
151 #define AHAB_MODE_CBC 0x67
152 #define AHAB_ALG_AES 0x55
153 #define AHAB_128_AES_KEY 0x10
154 #define AHAB_192_AES_KEY 0x18
155 #define AHAB_256_AES_KEY 0x20
156 #define AHAB_FLAG_KEK 0x80
157 #define AHAB_DEK_BLOB 0x01
159 #define DEK_BLOB_HDR_SIZE 8
162 static int blob_encap_dek(u32 src_addr, u32 dst_addr, u32 len)
165 sc_rm_mr_t mr_input, mr_output;
166 struct generate_key_blob_hdr hdr;
167 u8 in_size, out_size;
168 u8 *src_ptr, *dst_ptr;
173 in_size = sizeof(struct generate_key_blob_hdr) + len / 8;
174 out_size = BLOB_SIZE(len / 8) + DEK_BLOB_HDR_SIZE;
176 /* Get src and dst virtual addresses */
177 src_ptr = map_sysmem(src_addr, in_size);
178 dst_ptr = map_sysmem(dst_addr, out_size);
180 /* Check addr input */
181 if (!(src_ptr && dst_ptr)) {
182 debug("src_addr or dst_addr invalid\n");
186 /* Build key header */
187 hdr.version = AHAB_VERSION;
188 hdr.length_lsb = sizeof(struct generate_key_blob_hdr) + len / 8;
189 hdr.length_msb = 0x00;
190 hdr.tag = AHAB_PRIVATE_KEY;
191 hdr.flags = AHAB_DEK_BLOB;
192 hdr.algorithm = AHAB_ALG_AES;
193 hdr.mode = AHAB_MODE_CBC;
197 hdr.size = AHAB_128_AES_KEY;
200 hdr.size = AHAB_192_AES_KEY;
203 hdr.size = AHAB_256_AES_KEY;
207 debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
211 /* Build input message */
212 memmove((void *)(src_ptr + sizeof(struct generate_key_blob_hdr)),
213 (void *)src_ptr, len / 8);
214 memcpy((void *)src_ptr, (void *)&hdr,
215 sizeof(struct generate_key_blob_hdr));
217 /* Flush the cache before triggering the CAAM DMA */
218 flush_dcache_range(src_addr, src_addr + in_size);
220 /* Find input memory region */
221 err = sc_rm_find_memreg((-1), &mr_input, src_addr & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
222 ALIGN(src_addr + in_size, CONFIG_SYS_CACHELINE_SIZE));
224 printf("Error: find memory region 0x%X\n", src_addr);
228 /* Find output memory region */
229 err = sc_rm_find_memreg((-1), &mr_output, dst_addr & ~(CONFIG_SYS_CACHELINE_SIZE - 1),
230 ALIGN(dst_addr + out_size, CONFIG_SYS_CACHELINE_SIZE));
232 printf("Error: find memory region 0x%X\n", dst_addr);
236 /* Set memory region permissions for SECO */
237 err = sc_rm_set_memreg_permissions(-1, mr_input, SECO_PT,
240 printf("Set permission failed for input memory region\n");
245 err = sc_rm_set_memreg_permissions(-1, mr_output, SECO_PT,
248 printf("Set permission failed for output memory region\n");
253 /* Flush output data before SECO operation */
254 flush_dcache_range((ulong)dst_ptr, (ulong)(dst_ptr +
255 roundup(out_size, ARCH_DMA_MINALIGN)));
257 /* Generate DEK blob */
258 err = sc_seco_gen_key_blob((-1), 0x0, src_addr, dst_addr, out_size);
264 /* Invalidate output buffer */
265 invalidate_dcache_range((ulong)dst_ptr, (ulong)(dst_ptr +
266 roundup(out_size, ARCH_DMA_MINALIGN)));
268 printf("DEK Blob\n");
269 for (i = 0; i < DEK_BLOB_HDR_SIZE + BLOB_SIZE(len / 8); i++)
270 printf("%02X", dst_ptr[i]);
274 /* Remove memory region permission to SECO */
275 err = sc_rm_set_memreg_permissions(-1, mr_input, SECO_PT,
278 printf("Error: remove permission failed for input\n");
282 err = sc_rm_set_memreg_permissions(-1, mr_output, SECO_PT,
285 printf("Error: remove permission failed for output\n");
291 #endif /* CONFIG_IMX_SECO_DEK_ENCAP */
293 #ifdef CONFIG_IMX_ELE_DEK_ENCAP
295 #define DEK_BLOB_HDR_SIZE 8
296 #define AHAB_PRIVATE_KEY 0x81
297 #define AHAB_DEK_BLOB 0x01
298 #define AHAB_ALG_AES 0x03
299 #define AHAB_128_AES_KEY 0x10
300 #define AHAB_192_AES_KEY 0x18
301 #define AHAB_256_AES_KEY 0x20
303 static int blob_encap_dek(u32 src_addr, u32 dst_addr, u32 len)
305 u8 in_size, out_size;
306 u8 *src_ptr, *dst_ptr;
307 struct generate_key_blob_hdr hdr;
310 in_size = sizeof(struct generate_key_blob_hdr) + len / 8;
311 out_size = BLOB_SIZE(len / 8) + DEK_BLOB_HDR_SIZE;
313 /* Get src and dst virtual addresses */
314 src_ptr = map_sysmem(src_addr, in_size);
315 dst_ptr = map_sysmem(dst_addr, out_size);
317 /* Check addr input */
318 if (!(src_ptr && dst_ptr)) {
319 debug("src_addr or dst_addr invalid\n");
323 /* Build key header */
325 hdr.length_lsb = in_size;
326 hdr.length_msb = 0x00;
327 hdr.tag = AHAB_PRIVATE_KEY;
328 hdr.flags = AHAB_DEK_BLOB;
329 hdr.algorithm = AHAB_ALG_AES;
330 hdr.mode = 0x0; /* Not used by the ELE */
334 hdr.size = AHAB_128_AES_KEY;
337 hdr.size = AHAB_192_AES_KEY;
340 hdr.size = AHAB_256_AES_KEY;
344 debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
348 /* Move input key and append blob header */
349 memmove((void *)(src_ptr + sizeof(struct generate_key_blob_hdr)),
350 (void *)src_ptr, len / 8);
351 memcpy((void *)src_ptr, (void *)&hdr,
352 sizeof(struct generate_key_blob_hdr));
354 /* Flush the cache */
355 flush_dcache_range(src_addr, src_addr + in_size);
356 flush_dcache_range((ulong)dst_ptr, (ulong)(dst_ptr +
357 roundup(out_size, ARCH_DMA_MINALIGN)));
360 if (ele_generate_dek_blob(0x00, src_addr, dst_addr, out_size))
363 /* Invalidate output buffer */
364 invalidate_dcache_range((ulong)dst_ptr, (ulong)(dst_ptr +
365 roundup(out_size, ARCH_DMA_MINALIGN)));
369 #endif /* CONFIG_IMX_ELE_DEK_ENCAP */
372 * do_dek_blob() - Handle the "dek_blob" command-line command
373 * @cmdtp: Command data struct pointer
374 * @flag: Command flag
375 * @argc: Command-line argument count
376 * @argv: Array of command-line arguments
378 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
381 static int do_dek_blob(struct cmd_tbl *cmdtp, int flag, int argc,
384 uint32_t src_addr, dst_addr, len;
387 return CMD_RET_USAGE;
389 src_addr = hextoul(argv[1], NULL);
390 dst_addr = hextoul(argv[2], NULL);
391 len = dectoul(argv[3], NULL);
393 return blob_encap_dek(src_addr, dst_addr, len);
396 /***************************************************/
397 static char dek_blob_help_text[] =
398 "src dst len - Encapsulate and create blob of data\n"
399 " $len bits long at address $src and\n"
400 " store the result at address $dst.\n";
403 dek_blob, 4, 1, do_dek_blob,
404 "Data Encryption Key blob encapsulation",