blob: c2059b8b0a39a6d169987b44ad27a21c5484f2cb [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>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.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
Ruchika Gupta4345a572014-10-07 15:46:20 +053067 ret = run_descriptor_jr(desc);
68
Clemens Gruber58b13962018-01-07 20:26:29 +010069 if (ret) {
70 printf("Error in blob decapsulation: %d\n", ret);
71 } else {
72 size = ALIGN(len, ARCH_DMA_MINALIGN);
73 invalidate_dcache_range((unsigned long)dst,
74 (unsigned long)dst + size);
75
76 puts("Blob decapsulation successful.\n");
77 }
Ruchika Gupta4345a572014-10-07 15:46:20 +053078
79 free(desc);
80 return ret;
81}
82
Clemens Gruber58b13962018-01-07 20:26:29 +010083/**
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
89 *
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.
92 *
93 * Returns zero on success, negative on error.
94 */
gaurav ranab64308e2015-02-25 09:37:09 +053095int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +053096{
Clemens Gruber58b13962018-01-07 20:26:29 +010097 int ret, size, i = 0;
Ruchika Gupta4345a572014-10-07 15:46:20 +053098 u32 *desc;
99
Clemens Gruber58b13962018-01-07 20:26:29 +0100100 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");
104 return -EINVAL;
105 }
106
Ruchika Gupta4345a572014-10-07 15:46:20 +0530107 printf("\nEncapsulating data to form blob\n");
Clemens Gruber58b13962018-01-07 20:26:29 +0100108 desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
Ruchika Gupta4345a572014-10-07 15:46:20 +0530109 if (!desc) {
110 debug("Not enough memory for descriptor allocation\n");
Clemens Gruber58b13962018-01-07 20:26:29 +0100111 return -ENOMEM;
Ruchika Gupta4345a572014-10-07 15:46:20 +0530112 }
113
Clemens Gruber58b13962018-01-07 20:26:29 +0100114 size = ALIGN(16, ARCH_DMA_MINALIGN);
115 flush_dcache_range((unsigned long)key_mod,
116 (unsigned long)key_mod + size);
117
118 size = ALIGN(len, ARCH_DMA_MINALIGN);
119 flush_dcache_range((unsigned long)src,
120 (unsigned long)src + size);
121
Ruchika Gupta4345a572014-10-07 15:46:20 +0530122 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
Sumit Gargff9eb052016-07-14 13:27:50 -0400123
124 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +0530125 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -0400126 debug("Word[%d]: %08x\n", i, *(desc + i));
Clemens Gruber58b13962018-01-07 20:26:29 +0100127
128 size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
129 flush_dcache_range((unsigned long)desc,
130 (unsigned long)desc + size);
131
Ruchika Gupta4345a572014-10-07 15:46:20 +0530132 ret = run_descriptor_jr(desc);
133
Clemens Gruber58b13962018-01-07 20:26:29 +0100134 if (ret) {
135 printf("Error in blob encapsulation: %d\n", ret);
136 } else {
137 size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
138 invalidate_dcache_range((unsigned long)dst,
139 (unsigned long)dst + size);
140
141 puts("Blob encapsulation successful.\n");
142 }
Ruchika Gupta4345a572014-10-07 15:46:20 +0530143
144 free(desc);
145 return ret;
146}
Raul Cardenasb5a36d82015-02-27 11:22:06 -0600147
148#ifdef CONFIG_CMD_DEKBLOB
149int blob_dek(const u8 *src, u8 *dst, u8 len)
150{
151 int ret, size, i = 0;
152 u32 *desc;
153
154 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
155
156 puts("\nEncapsulating provided DEK to form blob\n");
157 desc = memalign(ARCH_DMA_MINALIGN,
158 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
159 if (!desc) {
160 debug("Not enough memory for descriptor allocation\n");
161 return -ENOMEM;
162 }
163
164 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
165 if (ret) {
166 debug("Error in Job Descriptor Construction: %d\n", ret);
167 } else {
168 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
169 ARCH_DMA_MINALIGN);
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);
175
176 ret = run_descriptor_jr(desc);
177 }
178
179 if (ret) {
180 debug("Error in Encapsulation %d\n", ret);
181 goto err;
182 }
183
184 size = roundup(out_sz, ARCH_DMA_MINALIGN);
185 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
186
187 puts("DEK Blob\n");
188 for (i = 0; i < out_sz; i++)
189 printf("%02X", ((uint8_t *)dst)[i]);
190 printf("\n");
191
192err:
193 free(desc);
194 return ret;
195}
196#endif