1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
11 #include <linux/errno.h>
17 * blob_decap() - Decapsulate the data from a blob
18 * @key_mod: - Key modifier address
19 * @src: - Source address (blob)
20 * @dst: - Destination address (data)
21 * @len: - Size of decapsulated data
23 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
24 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
26 * Returns zero on success, negative on error.
28 int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
33 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
34 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
35 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
36 puts("Error: blob_decap: Address arguments are not aligned!\n");
40 printf("\nDecapsulating blob to get data\n");
41 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
43 debug("Not enough memory for descriptor allocation\n");
47 size = ALIGN(16, ARCH_DMA_MINALIGN);
48 flush_dcache_range((unsigned long)key_mod,
49 (unsigned long)key_mod + size);
51 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
52 flush_dcache_range((unsigned long)src,
53 (unsigned long)src + size);
55 inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
57 debug("Descriptor dump:\n");
58 for (i = 0; i < 14; i++)
59 debug("Word[%d]: %08x\n", i, *(desc + i));
61 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
62 flush_dcache_range((unsigned long)desc,
63 (unsigned long)desc + size);
65 ret = run_descriptor_jr(desc);
68 printf("Error in blob decapsulation: %d\n", ret);
70 size = ALIGN(len, ARCH_DMA_MINALIGN);
71 invalidate_dcache_range((unsigned long)dst,
72 (unsigned long)dst + size);
74 puts("Blob decapsulation successful.\n");
82 * blob_encap() - Encapsulate the data as a blob
83 * @key_mod: - Key modifier address
84 * @src: - Source address (data)
85 * @dst: - Destination address (blob)
86 * @len: - Size of data to be encapsulated
88 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
89 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
91 * Returns zero on success, negative on error.
93 int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
98 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
99 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
100 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
101 puts("Error: blob_encap: Address arguments are not aligned!\n");
105 printf("\nEncapsulating data to form blob\n");
106 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
108 debug("Not enough memory for descriptor allocation\n");
112 size = ALIGN(16, ARCH_DMA_MINALIGN);
113 flush_dcache_range((unsigned long)key_mod,
114 (unsigned long)key_mod + size);
116 size = ALIGN(len, ARCH_DMA_MINALIGN);
117 flush_dcache_range((unsigned long)src,
118 (unsigned long)src + size);
120 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
122 debug("Descriptor dump:\n");
123 for (i = 0; i < 14; i++)
124 debug("Word[%d]: %08x\n", i, *(desc + i));
126 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
127 flush_dcache_range((unsigned long)desc,
128 (unsigned long)desc + size);
130 ret = run_descriptor_jr(desc);
133 printf("Error in blob encapsulation: %d\n", ret);
135 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
136 invalidate_dcache_range((unsigned long)dst,
137 (unsigned long)dst + size);
139 puts("Blob encapsulation successful.\n");
146 #ifdef CONFIG_CMD_DEKBLOB
147 int blob_dek(const u8 *src, u8 *dst, u8 len)
149 int ret, size, i = 0;
152 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
154 puts("\nEncapsulating provided DEK to form blob\n");
155 desc = memalign(ARCH_DMA_MINALIGN,
156 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
158 debug("Not enough memory for descriptor allocation\n");
162 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
164 debug("Error in Job Descriptor Construction: %d\n", ret);
166 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
168 flush_dcache_range((unsigned long)desc,
169 (unsigned long)desc + size);
170 size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
171 flush_dcache_range((unsigned long)dst,
172 (unsigned long)dst + size);
174 ret = run_descriptor_jr(desc);
178 debug("Error in Encapsulation %d\n", ret);
182 size = roundup(out_sz, ARCH_DMA_MINALIGN);
183 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
186 for (i = 0; i < out_sz; i++)
187 printf("%02X", ((uint8_t *)dst)[i]);