xref: /openbmc/u-boot/drivers/crypto/fsl/fsl_blob.c (revision 274bced8)
1 /*
2  * Copyright 2014 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  *
6  */
7 
8 #include <common.h>
9 #include <malloc.h>
10 #include <fsl_sec.h>
11 #include <linux/errno.h>
12 #include "jobdesc.h"
13 #include "desc.h"
14 #include "jr.h"
15 
16 int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
17 {
18 	int ret, i = 0;
19 	u32 *desc;
20 
21 	printf("\nDecapsulating blob to get data\n");
22 	desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
23 	if (!desc) {
24 		debug("Not enough memory for descriptor allocation\n");
25 		return -1;
26 	}
27 
28 	inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
29 
30 	debug("Descriptor dump:\n");
31 	for (i = 0; i < 14; i++)
32 		debug("Word[%d]: %08x\n", i, *(desc + i));
33 	ret = run_descriptor_jr(desc);
34 
35 	if (ret)
36 		printf("Error in Decapsulation %d\n", ret);
37 	else
38 		printf("Decapsulation Success\n");
39 
40 	free(desc);
41 	return ret;
42 }
43 
44 int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
45 {
46 	int ret, i = 0;
47 	u32 *desc;
48 
49 	printf("\nEncapsulating data to form blob\n");
50 	desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
51 	if (!desc) {
52 		debug("Not enough memory for descriptor allocation\n");
53 		return -1;
54 	}
55 
56 	inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
57 
58 	debug("Descriptor dump:\n");
59 	for (i = 0; i < 14; i++)
60 		debug("Word[%d]: %08x\n", i, *(desc + i));
61 	ret = run_descriptor_jr(desc);
62 
63 	if (ret)
64 		printf("Error in Encapsulation %d\n", ret);
65 	else
66 		printf("Encapsulation Success\n");
67 
68 	free(desc);
69 	return ret;
70 }
71 
72 #ifdef CONFIG_CMD_DEKBLOB
73 int blob_dek(const u8 *src, u8 *dst, u8 len)
74 {
75 	int ret, size, i = 0;
76 	u32 *desc;
77 
78 	int out_sz =  WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
79 
80 	puts("\nEncapsulating provided DEK to form blob\n");
81 	desc = memalign(ARCH_DMA_MINALIGN,
82 			sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
83 	if (!desc) {
84 		debug("Not enough memory for descriptor allocation\n");
85 		return -ENOMEM;
86 	}
87 
88 	ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
89 	if (ret) {
90 		debug("Error in Job Descriptor Construction:  %d\n", ret);
91 	} else {
92 		size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
93 			      ARCH_DMA_MINALIGN);
94 		flush_dcache_range((unsigned long)desc,
95 				   (unsigned long)desc + size);
96 		size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
97 		flush_dcache_range((unsigned long)dst,
98 				   (unsigned long)dst + size);
99 
100 		ret = run_descriptor_jr(desc);
101 	}
102 
103 	if (ret) {
104 		debug("Error in Encapsulation %d\n", ret);
105 	   goto err;
106 	}
107 
108 	size = roundup(out_sz, ARCH_DMA_MINALIGN);
109 	invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
110 
111 	puts("DEK Blob\n");
112 	for (i = 0; i < out_sz; i++)
113 		printf("%02X", ((uint8_t *)dst)[i]);
114 	printf("\n");
115 
116 err:
117 	free(desc);
118 	return ret;
119 }
120 #endif
121