blob: 0ecd6befd2591dc48afc901ef1c100896cc27884 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Gupta4345a572014-10-07 15:46:20 +05302/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
4 *
Ruchika Gupta4345a572014-10-07 15:46:20 +05305 */
6
Simon Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Ruchika Gupta4345a572014-10-07 15:46:20 +05309#include <malloc.h>
Clemens Gruber58b13962018-01-07 20:26:29 +010010#include <memalign.h>
Raul Cardenasb5a36d82015-02-27 11:22:06 -060011#include <fsl_sec.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <asm/cache.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090013#include <linux/errno.h>
Ruchika Gupta4345a572014-10-07 15:46:20 +053014#include "jobdesc.h"
15#include "desc.h"
16#include "jr.h"
17
Clemens Gruber58b13962018-01-07 20:26:29 +010018/**
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 */
gaurav ranab64308e2015-02-25 09:37:09 +053030int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +053031{
Clemens Gruber58b13962018-01-07 20:26:29 +010032 int ret, size, i = 0;
Ruchika Gupta4345a572014-10-07 15:46:20 +053033 u32 *desc;
34
Clemens Gruber58b13962018-01-07 20:26:29 +010035 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
Sumit Gargff9eb052016-07-14 13:27:50 -040042 printf("\nDecapsulating blob to get data\n");
Clemens Gruber58b13962018-01-07 20:26:29 +010043 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Gupta4345a572014-10-07 15:46:20 +053044 if (!desc) {
45 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber58b13962018-01-07 20:26:29 +010046 return -ENOMEM;
Ruchika Gupta4345a572014-10-07 15:46:20 +053047 }
48
Clemens Gruber58b13962018-01-07 20:26:29 +010049 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
Ruchika Gupta4345a572014-10-07 15:46:20 +053057 inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
58
Sumit Gargff9eb052016-07-14 13:27:50 -040059 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053060 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -040061 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber58b13962018-01-07 20:26:29 +010062
63 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
64 flush_dcache_range((unsigned long)desc,
65 (unsigned long)desc + size);
66
Breno Limaa422c212021-03-25 17:30:16 +080067 flush_dcache_range((unsigned long)dst,
68 (unsigned long)dst + size);
69
Ruchika Gupta4345a572014-10-07 15:46:20 +053070 ret = run_descriptor_jr(desc);
71
Clemens Gruber58b13962018-01-07 20:26:29 +010072 if (ret) {
Gaurav Jain2b0f0592022-04-15 16:52:34 +053073 /* clear the blob data output buffer */
74 memset(dst, 0x00, len);
75 size = ALIGN(len, ARCH_DMA_MINALIGN);
76 flush_dcache_range((unsigned long)dst, (unsigned long)dst + size);
Clemens Gruber58b13962018-01-07 20:26:29 +010077 printf("Error in blob decapsulation: %d\n", ret);
78 } else {
79 size = ALIGN(len, ARCH_DMA_MINALIGN);
80 invalidate_dcache_range((unsigned long)dst,
81 (unsigned long)dst + size);
82
83 puts("Blob decapsulation successful.\n");
84 }
Ruchika Gupta4345a572014-10-07 15:46:20 +053085
86 free(desc);
87 return ret;
88}
89
Clemens Gruber58b13962018-01-07 20:26:29 +010090/**
91 * blob_encap() - Encapsulate the data as a blob
92 * @key_mod: - Key modifier address
93 * @src: - Source address (data)
94 * @dst: - Destination address (blob)
95 * @len: - Size of data to be encapsulated
96 *
97 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
98 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
99 *
100 * Returns zero on success, negative on error.
101 */
gaurav ranab64308e2015-02-25 09:37:09 +0530102int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +0530103{
Clemens Gruber58b13962018-01-07 20:26:29 +0100104 int ret, size, i = 0;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530105 u32 *desc;
106
Clemens Gruber58b13962018-01-07 20:26:29 +0100107 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
108 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
109 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
110 puts("Error: blob_encap: Address arguments are not aligned!\n");
111 return -EINVAL;
112 }
113
Ruchika Gupta4345a572014-10-07 15:46:20 +0530114 printf("\nEncapsulating data to form blob\n");
Clemens Gruber58b13962018-01-07 20:26:29 +0100115 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530116 if (!desc) {
117 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber58b13962018-01-07 20:26:29 +0100118 return -ENOMEM;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530119 }
120
Clemens Gruber58b13962018-01-07 20:26:29 +0100121 size = ALIGN(16, ARCH_DMA_MINALIGN);
122 flush_dcache_range((unsigned long)key_mod,
123 (unsigned long)key_mod + size);
124
125 size = ALIGN(len, ARCH_DMA_MINALIGN);
126 flush_dcache_range((unsigned long)src,
127 (unsigned long)src + size);
128
Ruchika Gupta4345a572014-10-07 15:46:20 +0530129 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
Sumit Gargff9eb052016-07-14 13:27:50 -0400130
131 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +0530132 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -0400133 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber58b13962018-01-07 20:26:29 +0100134
135 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
136 flush_dcache_range((unsigned long)desc,
137 (unsigned long)desc + size);
138
Breno Limaa422c212021-03-25 17:30:16 +0800139 flush_dcache_range((unsigned long)dst,
140 (unsigned long)dst + size);
141
Ruchika Gupta4345a572014-10-07 15:46:20 +0530142 ret = run_descriptor_jr(desc);
143
Clemens Gruber58b13962018-01-07 20:26:29 +0100144 if (ret) {
145 printf("Error in blob encapsulation: %d\n", ret);
146 } else {
147 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
148 invalidate_dcache_range((unsigned long)dst,
149 (unsigned long)dst + size);
150
151 puts("Blob encapsulation successful.\n");
152 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530153
154 free(desc);
155 return ret;
156}
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600157
158#ifdef CONFIG_CMD_DEKBLOB
159int blob_dek(const u8 *src, u8 *dst, u8 len)
160{
161 int ret, size, i = 0;
162 u32 *desc;
163
164 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
165
166 puts("\nEncapsulating provided DEK to form blob\n");
167 desc = memalign(ARCH_DMA_MINALIGN,
168 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
169 if (!desc) {
170 debug("Not enough memory for descriptor allocation\n");
171 return -ENOMEM;
172 }
173
174 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
175 if (ret) {
176 debug("Error in Job Descriptor Construction: %d\n", ret);
177 } else {
178 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
179 ARCH_DMA_MINALIGN);
180 flush_dcache_range((unsigned long)desc,
181 (unsigned long)desc + size);
182 size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
183 flush_dcache_range((unsigned long)dst,
184 (unsigned long)dst + size);
185
186 ret = run_descriptor_jr(desc);
187 }
188
189 if (ret) {
190 debug("Error in Encapsulation %d\n", ret);
191 goto err;
192 }
193
194 size = roundup(out_sz, ARCH_DMA_MINALIGN);
195 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
196
197 puts("DEK Blob\n");
198 for (i = 0; i < out_sz; i++)
199 printf("%02X", ((uint8_t *)dst)[i]);
200 printf("\n");
201
202err:
203 free(desc);
204 return ret;
205}
206#endif