blob: d6bd861251e0464e834ec5344d139b5e288ea713 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Guptac5de15c2014-10-07 15:46:20 +05302/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
4 *
Ruchika Guptac5de15c2014-10-07 15:46:20 +05305 */
6
7#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Ruchika Guptac5de15c2014-10-07 15:46:20 +053010#include <malloc.h>
Clemens Gruber598e9dc2018-01-07 20:26:29 +010011#include <memalign.h>
Raul Cardenas02000202015-02-27 11:22:06 -060012#include <fsl_sec.h>
Simon Glass90526e92020-05-10 11:39:56 -060013#include <asm/cache.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090014#include <linux/errno.h>
Ruchika Guptac5de15c2014-10-07 15:46:20 +053015#include "jobdesc.h"
16#include "desc.h"
17#include "jr.h"
18
Clemens Gruber598e9dc2018-01-07 20:26:29 +010019/**
20 * blob_decap() - Decapsulate the data from a blob
21 * @key_mod: - Key modifier address
22 * @src: - Source address (blob)
23 * @dst: - Destination address (data)
24 * @len: - Size of decapsulated data
25 *
26 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
27 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
28 *
29 * Returns zero on success, negative on error.
30 */
gaurav rana7ee8c472015-02-25 09:37:09 +053031int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Guptac5de15c2014-10-07 15:46:20 +053032{
Clemens Gruber598e9dc2018-01-07 20:26:29 +010033 int ret, size, i = 0;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053034 u32 *desc;
35
Clemens Gruber598e9dc2018-01-07 20:26:29 +010036 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
37 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
38 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
39 puts("Error: blob_decap: Address arguments are not aligned!\n");
40 return -EINVAL;
41 }
42
Sumit Garg7fe1d6a2016-07-14 13:27:50 -040043 printf("\nDecapsulating blob to get data\n");
Clemens Gruber598e9dc2018-01-07 20:26:29 +010044 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Guptac5de15c2014-10-07 15:46:20 +053045 if (!desc) {
46 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber598e9dc2018-01-07 20:26:29 +010047 return -ENOMEM;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053048 }
49
Clemens Gruber598e9dc2018-01-07 20:26:29 +010050 size = ALIGN(16, ARCH_DMA_MINALIGN);
51 flush_dcache_range((unsigned long)key_mod,
52 (unsigned long)key_mod + size);
53
54 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
55 flush_dcache_range((unsigned long)src,
56 (unsigned long)src + size);
57
Ruchika Guptac5de15c2014-10-07 15:46:20 +053058 inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
59
Sumit Garg7fe1d6a2016-07-14 13:27:50 -040060 debug("Descriptor dump:\n");
Ruchika Guptac5de15c2014-10-07 15:46:20 +053061 for (i = 0; i < 14; i++)
Sumit Garg7fe1d6a2016-07-14 13:27:50 -040062 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber598e9dc2018-01-07 20:26:29 +010063
64 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
65 flush_dcache_range((unsigned long)desc,
66 (unsigned long)desc + size);
67
Ruchika Guptac5de15c2014-10-07 15:46:20 +053068 ret = run_descriptor_jr(desc);
69
Clemens Gruber598e9dc2018-01-07 20:26:29 +010070 if (ret) {
71 printf("Error in blob decapsulation: %d\n", ret);
72 } else {
73 size = ALIGN(len, ARCH_DMA_MINALIGN);
74 invalidate_dcache_range((unsigned long)dst,
75 (unsigned long)dst + size);
76
77 puts("Blob decapsulation successful.\n");
78 }
Ruchika Guptac5de15c2014-10-07 15:46:20 +053079
80 free(desc);
81 return ret;
82}
83
Clemens Gruber598e9dc2018-01-07 20:26:29 +010084/**
85 * blob_encap() - Encapsulate the data as a blob
86 * @key_mod: - Key modifier address
87 * @src: - Source address (data)
88 * @dst: - Destination address (blob)
89 * @len: - Size of data to be encapsulated
90 *
91 * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
92 * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
93 *
94 * Returns zero on success, negative on error.
95 */
gaurav rana7ee8c472015-02-25 09:37:09 +053096int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Guptac5de15c2014-10-07 15:46:20 +053097{
Clemens Gruber598e9dc2018-01-07 20:26:29 +010098 int ret, size, i = 0;
Ruchika Guptac5de15c2014-10-07 15:46:20 +053099 u32 *desc;
100
Clemens Gruber598e9dc2018-01-07 20:26:29 +0100101 if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
102 !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
103 !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
104 puts("Error: blob_encap: Address arguments are not aligned!\n");
105 return -EINVAL;
106 }
107
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530108 printf("\nEncapsulating data to form blob\n");
Clemens Gruber598e9dc2018-01-07 20:26:29 +0100109 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530110 if (!desc) {
111 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber598e9dc2018-01-07 20:26:29 +0100112 return -ENOMEM;
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530113 }
114
Clemens Gruber598e9dc2018-01-07 20:26:29 +0100115 size = ALIGN(16, ARCH_DMA_MINALIGN);
116 flush_dcache_range((unsigned long)key_mod,
117 (unsigned long)key_mod + size);
118
119 size = ALIGN(len, ARCH_DMA_MINALIGN);
120 flush_dcache_range((unsigned long)src,
121 (unsigned long)src + size);
122
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530123 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
Sumit Garg7fe1d6a2016-07-14 13:27:50 -0400124
125 debug("Descriptor dump:\n");
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530126 for (i = 0; i < 14; i++)
Sumit Garg7fe1d6a2016-07-14 13:27:50 -0400127 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber598e9dc2018-01-07 20:26:29 +0100128
129 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
130 flush_dcache_range((unsigned long)desc,
131 (unsigned long)desc + size);
132
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530133 ret = run_descriptor_jr(desc);
134
Clemens Gruber598e9dc2018-01-07 20:26:29 +0100135 if (ret) {
136 printf("Error in blob encapsulation: %d\n", ret);
137 } else {
138 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
139 invalidate_dcache_range((unsigned long)dst,
140 (unsigned long)dst + size);
141
142 puts("Blob encapsulation successful.\n");
143 }
Ruchika Guptac5de15c2014-10-07 15:46:20 +0530144
145 free(desc);
146 return ret;
147}
Raul Cardenas02000202015-02-27 11:22:06 -0600148
149#ifdef CONFIG_CMD_DEKBLOB
150int blob_dek(const u8 *src, u8 *dst, u8 len)
151{
152 int ret, size, i = 0;
153 u32 *desc;
154
155 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
156
157 puts("\nEncapsulating provided DEK to form blob\n");
158 desc = memalign(ARCH_DMA_MINALIGN,
159 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
160 if (!desc) {
161 debug("Not enough memory for descriptor allocation\n");
162 return -ENOMEM;
163 }
164
165 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
166 if (ret) {
167 debug("Error in Job Descriptor Construction: %d\n", ret);
168 } else {
169 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
170 ARCH_DMA_MINALIGN);
171 flush_dcache_range((unsigned long)desc,
172 (unsigned long)desc + size);
173 size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
174 flush_dcache_range((unsigned long)dst,
175 (unsigned long)dst + size);
176
177 ret = run_descriptor_jr(desc);
178 }
179
180 if (ret) {
181 debug("Error in Encapsulation %d\n", ret);
182 goto err;
183 }
184
185 size = roundup(out_sz, ARCH_DMA_MINALIGN);
186 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
187
188 puts("DEK Blob\n");
189 for (i = 0; i < out_sz; i++)
190 printf("%02X", ((uint8_t *)dst)[i]);
191 printf("\n");
192
193err:
194 free(desc);
195 return ret;
196}
197#endif