]> git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/crypto/fsl/fsl_blob.c
common: Drop net.h from common header
[thirdparty/u-boot.git] / drivers / crypto / fsl / fsl_blob.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2014 Freescale Semiconductor, Inc.
4 *
5 */
6
7 #include <common.h>
8 #include <cpu_func.h>
9 #include <malloc.h>
10 #include <memalign.h>
11 #include <fsl_sec.h>
12 #include <asm/cache.h>
13 #include <linux/errno.h>
14 #include "jobdesc.h"
15 #include "desc.h"
16 #include "jr.h"
17
18 /**
19 * blob_decap() - Decapsulate the data from a blob
20 * @key_mod: - Key modifier address
21 * @src: - Source address (blob)
22 * @dst: - Destination address (data)
23 * @len: - Size of decapsulated data
24 *
25 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
26 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
27 *
28 * Returns zero on success, negative on error.
29 */
30 int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
31 {
32 int ret, size, i = 0;
33 u32 *desc;
34
35 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
36 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
37 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
38 puts("Error: blob_decap: Address arguments are not aligned!\n");
39 return -EINVAL;
40 }
41
42 printf("\nDecapsulating blob to get data\n");
43 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
44 if (!desc) {
45 debug("Not enough memory for descriptor allocation\n");
46 return -ENOMEM;
47 }
48
49 size = ALIGN(16, ARCH_DMA_MINALIGN);
50 flush_dcache_range((unsigned long)key_mod,
51 (unsigned long)key_mod + size);
52
53 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
54 flush_dcache_range((unsigned long)src,
55 (unsigned long)src + size);
56
57 inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
58
59 debug("Descriptor dump:\n");
60 for (i = 0; i < 14; i++)
61 debug("Word[%d]: %08x\n", i, *(desc + i));
62
63 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
64 flush_dcache_range((unsigned long)desc,
65 (unsigned long)desc + size);
66
67 ret = run_descriptor_jr(desc);
68
69 if (ret) {
70 printf("Error in blob decapsulation: %d\n", ret);
71 } else {
72 size = ALIGN(len, ARCH_DMA_MINALIGN);
73 invalidate_dcache_range((unsigned long)dst,
74 (unsigned long)dst + size);
75
76 puts("Blob decapsulation successful.\n");
77 }
78
79 free(desc);
80 return ret;
81 }
82
83 /**
84 * blob_encap() - Encapsulate the data as a blob
85 * @key_mod: - Key modifier address
86 * @src: - Source address (data)
87 * @dst: - Destination address (blob)
88 * @len: - Size of data to be encapsulated
89 *
90 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
91 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
92 *
93 * Returns zero on success, negative on error.
94 */
95 int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
96 {
97 int ret, size, i = 0;
98 u32 *desc;
99
100 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
101 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
102 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
103 puts("Error: blob_encap: Address arguments are not aligned!\n");
104 return -EINVAL;
105 }
106
107 printf("\nEncapsulating data to form blob\n");
108 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
109 if (!desc) {
110 debug("Not enough memory for descriptor allocation\n");
111 return -ENOMEM;
112 }
113
114 size = ALIGN(16, ARCH_DMA_MINALIGN);
115 flush_dcache_range((unsigned long)key_mod,
116 (unsigned long)key_mod + size);
117
118 size = ALIGN(len, ARCH_DMA_MINALIGN);
119 flush_dcache_range((unsigned long)src,
120 (unsigned long)src + size);
121
122 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
123
124 debug("Descriptor dump:\n");
125 for (i = 0; i < 14; i++)
126 debug("Word[%d]: %08x\n", i, *(desc + i));
127
128 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
129 flush_dcache_range((unsigned long)desc,
130 (unsigned long)desc + size);
131
132 ret = run_descriptor_jr(desc);
133
134 if (ret) {
135 printf("Error in blob encapsulation: %d\n", ret);
136 } else {
137 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
138 invalidate_dcache_range((unsigned long)dst,
139 (unsigned long)dst + size);
140
141 puts("Blob encapsulation successful.\n");
142 }
143
144 free(desc);
145 return ret;
146 }
147
148 #ifdef CONFIG_CMD_DEKBLOB
149 int blob_dek(const u8 *src, u8 *dst, u8 len)
150 {
151 int ret, size, i = 0;
152 u32 *desc;
153
154 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
155
156 puts("\nEncapsulating provided DEK to form blob\n");
157 desc = memalign(ARCH_DMA_MINALIGN,
158 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
159 if (!desc) {
160 debug("Not enough memory for descriptor allocation\n");
161 return -ENOMEM;
162 }
163
164 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
165 if (ret) {
166 debug("Error in Job Descriptor Construction: %d\n", ret);
167 } else {
168 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
169 ARCH_DMA_MINALIGN);
170 flush_dcache_range((unsigned long)desc,
171 (unsigned long)desc + size);
172 size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
173 flush_dcache_range((unsigned long)dst,
174 (unsigned long)dst + size);
175
176 ret = run_descriptor_jr(desc);
177 }
178
179 if (ret) {
180 debug("Error in Encapsulation %d\n", ret);
181 goto err;
182 }
183
184 size = roundup(out_sz, ARCH_DMA_MINALIGN);
185 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
186
187 puts("DEK Blob\n");
188 for (i = 0; i < out_sz; i++)
189 printf("%02X", ((uint8_t *)dst)[i]);
190 printf("\n");
191
192 err:
193 free(desc);
194 return ret;
195 }
196 #endif