blob: ce6aa05fe7f82771943653099ed9a59dee1d9f10 [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
7#include <common.h>
8#include <malloc.h>
Clemens Gruber58b13962018-01-07 20:26:29 +01009#include <memalign.h>
Raul Cardenasb5a36d82015-02-27 11:22:06 -060010#include <fsl_sec.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090011#include <linux/errno.h>
Ruchika Gupta4345a572014-10-07 15:46:20 +053012#include "jobdesc.h"
13#include "desc.h"
14#include "jr.h"
15
Clemens Gruber58b13962018-01-07 20:26:29 +010016/**
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
22 *
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.
25 *
26 * Returns zero on success, negative on error.
27 */
gaurav ranab64308e2015-02-25 09:37:09 +053028int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +053029{
Clemens Gruber58b13962018-01-07 20:26:29 +010030 int ret, size, i = 0;
Ruchika Gupta4345a572014-10-07 15:46:20 +053031 u32 *desc;
32
Clemens Gruber58b13962018-01-07 20:26:29 +010033 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");
37 return -EINVAL;
38 }
39
Sumit Gargff9eb052016-07-14 13:27:50 -040040 printf("\nDecapsulating blob to get data\n");
Clemens Gruber58b13962018-01-07 20:26:29 +010041 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Gupta4345a572014-10-07 15:46:20 +053042 if (!desc) {
43 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber58b13962018-01-07 20:26:29 +010044 return -ENOMEM;
Ruchika Gupta4345a572014-10-07 15:46:20 +053045 }
46
Clemens Gruber58b13962018-01-07 20:26:29 +010047 size = ALIGN(16, ARCH_DMA_MINALIGN);
48 flush_dcache_range((unsigned long)key_mod,
49 (unsigned long)key_mod + size);
50
51 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
52 flush_dcache_range((unsigned long)src,
53 (unsigned long)src + size);
54
Ruchika Gupta4345a572014-10-07 15:46:20 +053055 inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
56
Sumit Gargff9eb052016-07-14 13:27:50 -040057 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053058 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -040059 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber58b13962018-01-07 20:26:29 +010060
61 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
62 flush_dcache_range((unsigned long)desc,
63 (unsigned long)desc + size);
64
Ruchika Gupta4345a572014-10-07 15:46:20 +053065 ret = run_descriptor_jr(desc);
66
Clemens Gruber58b13962018-01-07 20:26:29 +010067 if (ret) {
68 printf("Error in blob decapsulation: %d\n", ret);
69 } else {
70 size = ALIGN(len, ARCH_DMA_MINALIGN);
71 invalidate_dcache_range((unsigned long)dst,
72 (unsigned long)dst + size);
73
74 puts("Blob decapsulation successful.\n");
75 }
Ruchika Gupta4345a572014-10-07 15:46:20 +053076
77 free(desc);
78 return ret;
79}
80
Clemens Gruber58b13962018-01-07 20:26:29 +010081/**
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
87 *
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.
90 *
91 * Returns zero on success, negative on error.
92 */
gaurav ranab64308e2015-02-25 09:37:09 +053093int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +053094{
Clemens Gruber58b13962018-01-07 20:26:29 +010095 int ret, size, i = 0;
Ruchika Gupta4345a572014-10-07 15:46:20 +053096 u32 *desc;
97
Clemens Gruber58b13962018-01-07 20:26:29 +010098 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");
102 return -EINVAL;
103 }
104
Ruchika Gupta4345a572014-10-07 15:46:20 +0530105 printf("\nEncapsulating data to form blob\n");
Clemens Gruber58b13962018-01-07 20:26:29 +0100106 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530107 if (!desc) {
108 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber58b13962018-01-07 20:26:29 +0100109 return -ENOMEM;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530110 }
111
Clemens Gruber58b13962018-01-07 20:26:29 +0100112 size = ALIGN(16, ARCH_DMA_MINALIGN);
113 flush_dcache_range((unsigned long)key_mod,
114 (unsigned long)key_mod + size);
115
116 size = ALIGN(len, ARCH_DMA_MINALIGN);
117 flush_dcache_range((unsigned long)src,
118 (unsigned long)src + size);
119
Ruchika Gupta4345a572014-10-07 15:46:20 +0530120 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
Sumit Gargff9eb052016-07-14 13:27:50 -0400121
122 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +0530123 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -0400124 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber58b13962018-01-07 20:26:29 +0100125
126 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
127 flush_dcache_range((unsigned long)desc,
128 (unsigned long)desc + size);
129
Ruchika Gupta4345a572014-10-07 15:46:20 +0530130 ret = run_descriptor_jr(desc);
131
Clemens Gruber58b13962018-01-07 20:26:29 +0100132 if (ret) {
133 printf("Error in blob encapsulation: %d\n", ret);
134 } else {
135 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
136 invalidate_dcache_range((unsigned long)dst,
137 (unsigned long)dst + size);
138
139 puts("Blob encapsulation successful.\n");
140 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530141
142 free(desc);
143 return ret;
144}
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600145
146#ifdef CONFIG_CMD_DEKBLOB
147int blob_dek(const u8 *src, u8 *dst, u8 len)
148{
149 int ret, size, i = 0;
150 u32 *desc;
151
152 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
153
154 puts("\nEncapsulating provided DEK to form blob\n");
155 desc = memalign(ARCH_DMA_MINALIGN,
156 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
157 if (!desc) {
158 debug("Not enough memory for descriptor allocation\n");
159 return -ENOMEM;
160 }
161
162 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
163 if (ret) {
164 debug("Error in Job Descriptor Construction: %d\n", ret);
165 } else {
166 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
167 ARCH_DMA_MINALIGN);
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);
173
174 ret = run_descriptor_jr(desc);
175 }
176
177 if (ret) {
178 debug("Error in Encapsulation %d\n", ret);
179 goto err;
180 }
181
182 size = roundup(out_sz, ARCH_DMA_MINALIGN);
183 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
184
185 puts("DEK Blob\n");
186 for (i = 0; i < out_sz; i++)
187 printf("%02X", ((uint8_t *)dst)[i]);
188 printf("\n");
189
190err:
191 free(desc);
192 return ret;
193}
194#endif