]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/crypto/fsl/fsl_blob.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
12 #include <asm/cache.h>
13 #include <linux/errno.h>
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
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.
28 * Returns zero on success, negative on error.
30 int blob_decap(u8
*key_mod
, u8
*src
, u8
*dst
, u32 len
)
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");
42 printf("\nDecapsulating blob to get data\n");
43 desc
= malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE
);
45 debug("Not enough memory for descriptor allocation\n");
49 size
= ALIGN(16, ARCH_DMA_MINALIGN
);
50 flush_dcache_range((unsigned long)key_mod
,
51 (unsigned long)key_mod
+ size
);
53 size
= ALIGN(BLOB_SIZE(len
), ARCH_DMA_MINALIGN
);
54 flush_dcache_range((unsigned long)src
,
55 (unsigned long)src
+ size
);
57 inline_cnstr_jobdesc_blob_decap(desc
, key_mod
, src
, dst
, len
);
59 debug("Descriptor dump:\n");
60 for (i
= 0; i
< 14; i
++)
61 debug("Word[%d]: %08x\n", i
, *(desc
+ i
));
63 size
= ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE
, ARCH_DMA_MINALIGN
);
64 flush_dcache_range((unsigned long)desc
,
65 (unsigned long)desc
+ size
);
67 ret
= run_descriptor_jr(desc
);
70 printf("Error in blob decapsulation: %d\n", ret
);
72 size
= ALIGN(len
, ARCH_DMA_MINALIGN
);
73 invalidate_dcache_range((unsigned long)dst
,
74 (unsigned long)dst
+ size
);
76 puts("Blob decapsulation successful.\n");
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
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.
93 * Returns zero on success, negative on error.
95 int blob_encap(u8
*key_mod
, u8
*src
, u8
*dst
, u32 len
)
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");
107 printf("\nEncapsulating data to form blob\n");
108 desc
= malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE
);
110 debug("Not enough memory for descriptor allocation\n");
114 size
= ALIGN(16, ARCH_DMA_MINALIGN
);
115 flush_dcache_range((unsigned long)key_mod
,
116 (unsigned long)key_mod
+ size
);
118 size
= ALIGN(len
, ARCH_DMA_MINALIGN
);
119 flush_dcache_range((unsigned long)src
,
120 (unsigned long)src
+ size
);
122 inline_cnstr_jobdesc_blob_encap(desc
, key_mod
, src
, dst
, len
);
124 debug("Descriptor dump:\n");
125 for (i
= 0; i
< 14; i
++)
126 debug("Word[%d]: %08x\n", i
, *(desc
+ i
));
128 size
= ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE
, ARCH_DMA_MINALIGN
);
129 flush_dcache_range((unsigned long)desc
,
130 (unsigned long)desc
+ size
);
132 ret
= run_descriptor_jr(desc
);
135 printf("Error in blob encapsulation: %d\n", ret
);
137 size
= ALIGN(BLOB_SIZE(len
), ARCH_DMA_MINALIGN
);
138 invalidate_dcache_range((unsigned long)dst
,
139 (unsigned long)dst
+ size
);
141 puts("Blob encapsulation successful.\n");
148 #ifdef CONFIG_CMD_DEKBLOB
149 int blob_dek(const u8
*src
, u8
*dst
, u8 len
)
151 int ret
, size
, i
= 0;
154 int out_sz
= WRP_HDR_SIZE
+ len
+ KEY_BLOB_SIZE
+ MAC_SIZE
;
156 puts("\nEncapsulating provided DEK to form blob\n");
157 desc
= memalign(ARCH_DMA_MINALIGN
,
158 sizeof(uint32_t) * DEK_BLOB_DESCSIZE
);
160 debug("Not enough memory for descriptor allocation\n");
164 ret
= inline_cnstr_jobdesc_blob_dek(desc
, src
, dst
, len
);
166 debug("Error in Job Descriptor Construction: %d\n", ret
);
168 size
= roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE
,
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
);
176 ret
= run_descriptor_jr(desc
);
180 debug("Error in Encapsulation %d\n", ret
);
184 size
= roundup(out_sz
, ARCH_DMA_MINALIGN
);
185 invalidate_dcache_range((unsigned long)dst
, (unsigned long)dst
+size
);
188 for (i
= 0; i
< out_sz
; i
++)
189 printf("%02X", ((uint8_t *)dst
)[i
]);