]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/crypto/ccp/ccp-crypto-aes-xts.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[mirror_ubuntu-hirsute-kernel.git] / drivers / crypto / ccp / ccp-crypto-aes-xts.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1d6b8a6f
TL
2/*
3 * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
4 *
e652399e 5 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
1d6b8a6f 6 *
e652399e 7 * Author: Gary R Hook <gary.hook@amd.com>
1d6b8a6f 8 * Author: Tom Lendacky <thomas.lendacky@amd.com>
1d6b8a6f
TL
9 */
10
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/delay.h>
14#include <linux/scatterlist.h>
1d6b8a6f 15#include <crypto/aes.h>
47f27f16 16#include <crypto/xts.h>
241118de 17#include <crypto/internal/skcipher.h>
1d6b8a6f
TL
18#include <crypto/scatterwalk.h>
19
20#include "ccp-crypto.h"
21
1d6b8a6f
TL
22struct ccp_aes_xts_def {
23 const char *name;
24 const char *drv_name;
25};
26
27static struct ccp_aes_xts_def aes_xts_algs[] = {
28 {
29 .name = "xts(aes)",
30 .drv_name = "xts-aes-ccp",
31 },
32};
33
34struct ccp_unit_size_map {
35 unsigned int size;
36 u32 value;
37};
38
7f7216cf 39static struct ccp_unit_size_map xts_unit_sizes[] = {
1d6b8a6f 40 {
7f7216cf
GH
41 .size = 16,
42 .value = CCP_XTS_AES_UNIT_SIZE_16,
1d6b8a6f
TL
43 },
44 {
7f7216cf 45 .size = 512,
1d6b8a6f
TL
46 .value = CCP_XTS_AES_UNIT_SIZE_512,
47 },
48 {
7f7216cf
GH
49 .size = 1024,
50 .value = CCP_XTS_AES_UNIT_SIZE_1024,
1d6b8a6f
TL
51 },
52 {
7f7216cf
GH
53 .size = 2048,
54 .value = CCP_XTS_AES_UNIT_SIZE_2048,
1d6b8a6f
TL
55 },
56 {
7f7216cf
GH
57 .size = 4096,
58 .value = CCP_XTS_AES_UNIT_SIZE_4096,
1d6b8a6f
TL
59 },
60};
61
62static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
63{
64 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
65 struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
66
67 if (ret)
68 return ret;
69
70 memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
71
72 return 0;
73}
74
75static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
76 unsigned int key_len)
77{
47f27f16
GH
78 struct crypto_tfm *xfm = crypto_ablkcipher_tfm(tfm);
79 struct ccp_ctx *ctx = crypto_tfm_ctx(xfm);
5060ffc9 80 unsigned int ccpversion = ccp_version();
47f27f16
GH
81 int ret;
82
83 ret = xts_check_key(xfm, key, key_len);
84 if (ret)
85 return ret;
1d6b8a6f 86
5060ffc9
GH
87 /* Version 3 devices support 128-bit keys; version 5 devices can
88 * accommodate 128- and 256-bit keys.
1d6b8a6f
TL
89 */
90 switch (key_len) {
91 case AES_KEYSIZE_128 * 2:
92 memcpy(ctx->u.aes.key, key, key_len);
93 break;
5060ffc9
GH
94 case AES_KEYSIZE_256 * 2:
95 if (ccpversion > CCP_VERSION(3, 0))
96 memcpy(ctx->u.aes.key, key, key_len);
97 break;
1d6b8a6f
TL
98 }
99 ctx->u.aes.key_len = key_len / 2;
100 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
101
7f28615d 102 return crypto_sync_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
1d6b8a6f
TL
103}
104
105static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
106 unsigned int encrypt)
107{
1d6b8a6f
TL
108 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
109 struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
5060ffc9
GH
110 unsigned int ccpversion = ccp_version();
111 unsigned int fallback = 0;
1d6b8a6f 112 unsigned int unit;
ab6a11a7 113 u32 unit_size;
1d6b8a6f
TL
114 int ret;
115
369f3dab 116 if (!ctx->u.aes.key_len)
1d6b8a6f 117 return -EINVAL;
1d6b8a6f 118
369f3dab 119 if (req->nbytes & (AES_BLOCK_SIZE - 1))
1d6b8a6f 120 return -EINVAL;
1d6b8a6f 121
369f3dab 122 if (!req->info)
1d6b8a6f 123 return -EINVAL;
1d6b8a6f 124
7f7216cf
GH
125 /* Check conditions under which the CCP can fulfill a request. The
126 * device can handle input plaintext of a length that is a multiple
127 * of the unit_size, bug the crypto implementation only supports
128 * the unit_size being equal to the input length. This limits the
129 * number of scenarios we can handle.
130 */
ab6a11a7 131 unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
7f7216cf
GH
132 for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) {
133 if (req->nbytes == xts_unit_sizes[unit].size) {
134 unit_size = unit;
135 break;
ab6a11a7
TL
136 }
137 }
5060ffc9
GH
138 /* The CCP has restrictions on block sizes. Also, a version 3 device
139 * only supports AES-128 operations; version 5 CCPs support both
140 * AES-128 and -256 operations.
141 */
142 if (unit_size == CCP_XTS_AES_UNIT_SIZE__LAST)
143 fallback = 1;
144 if ((ccpversion < CCP_VERSION(5, 0)) &&
145 (ctx->u.aes.key_len != AES_KEYSIZE_128))
146 fallback = 1;
147 if ((ctx->u.aes.key_len != AES_KEYSIZE_128) &&
148 (ctx->u.aes.key_len != AES_KEYSIZE_256))
149 fallback = 1;
150 if (fallback) {
7f28615d
KC
151 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq,
152 ctx->u.aes.tfm_skcipher);
241118de 153
1d6b8a6f
TL
154 /* Use the fallback to process the request for any
155 * unsupported unit sizes or key sizes
156 */
7f28615d 157 skcipher_request_set_sync_tfm(subreq, ctx->u.aes.tfm_skcipher);
241118de
HX
158 skcipher_request_set_callback(subreq, req->base.flags,
159 NULL, NULL);
160 skcipher_request_set_crypt(subreq, req->src, req->dst,
161 req->nbytes, req->info);
162 ret = encrypt ? crypto_skcipher_encrypt(subreq) :
163 crypto_skcipher_decrypt(subreq);
164 skcipher_request_zero(subreq);
1d6b8a6f
TL
165 return ret;
166 }
167
168 memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
169 sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
170
171 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
172 INIT_LIST_HEAD(&rctx->cmd.entry);
173 rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
e652399e 174 rctx->cmd.u.xts.type = CCP_AES_TYPE_128;
1d6b8a6f
TL
175 rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
176 : CCP_AES_ACTION_DECRYPT;
ab6a11a7 177 rctx->cmd.u.xts.unit_size = unit_size;
1d6b8a6f
TL
178 rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
179 rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
180 rctx->cmd.u.xts.iv = &rctx->iv_sg;
181 rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
182 rctx->cmd.u.xts.src = req->src;
183 rctx->cmd.u.xts.src_len = req->nbytes;
184 rctx->cmd.u.xts.dst = req->dst;
185
186 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
187
188 return ret;
189}
190
191static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
192{
193 return ccp_aes_xts_crypt(req, 1);
194}
195
196static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
197{
198 return ccp_aes_xts_crypt(req, 0);
199}
200
201static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
202{
203 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
7f28615d 204 struct crypto_sync_skcipher *fallback_tfm;
1d6b8a6f
TL
205
206 ctx->complete = ccp_aes_xts_complete;
207 ctx->u.aes.key_len = 0;
208
7f28615d 209 fallback_tfm = crypto_alloc_sync_skcipher("xts(aes)", 0,
241118de
HX
210 CRYPTO_ALG_ASYNC |
211 CRYPTO_ALG_NEED_FALLBACK);
1d6b8a6f 212 if (IS_ERR(fallback_tfm)) {
241118de 213 pr_warn("could not load fallback driver xts(aes)\n");
1d6b8a6f
TL
214 return PTR_ERR(fallback_tfm);
215 }
241118de 216 ctx->u.aes.tfm_skcipher = fallback_tfm;
1d6b8a6f 217
241118de 218 tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
1d6b8a6f
TL
219
220 return 0;
221}
222
223static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
224{
225 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
226
7f28615d 227 crypto_free_sync_skcipher(ctx->u.aes.tfm_skcipher);
1d6b8a6f
TL
228}
229
1d6b8a6f
TL
230static int ccp_register_aes_xts_alg(struct list_head *head,
231 const struct ccp_aes_xts_def *def)
232{
233 struct ccp_crypto_ablkcipher_alg *ccp_alg;
234 struct crypto_alg *alg;
235 int ret;
236
237 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
238 if (!ccp_alg)
239 return -ENOMEM;
240
241 INIT_LIST_HEAD(&ccp_alg->entry);
242
243 alg = &ccp_alg->alg;
244
245 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
246 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
247 def->drv_name);
248 alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
249 CRYPTO_ALG_KERN_DRIVER_ONLY |
250 CRYPTO_ALG_NEED_FALLBACK;
251 alg->cra_blocksize = AES_BLOCK_SIZE;
252 alg->cra_ctxsize = sizeof(struct ccp_ctx);
253 alg->cra_priority = CCP_CRA_PRIORITY;
254 alg->cra_type = &crypto_ablkcipher_type;
255 alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
256 alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
257 alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
258 alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
259 alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
260 alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
261 alg->cra_init = ccp_aes_xts_cra_init;
262 alg->cra_exit = ccp_aes_xts_cra_exit;
263 alg->cra_module = THIS_MODULE;
264
265 ret = crypto_register_alg(alg);
266 if (ret) {
267 pr_err("%s ablkcipher algorithm registration error (%d)\n",
8db88467 268 alg->cra_name, ret);
1d6b8a6f
TL
269 kfree(ccp_alg);
270 return ret;
271 }
272
273 list_add(&ccp_alg->entry, head);
274
275 return 0;
276}
277
278int ccp_register_aes_xts_algs(struct list_head *head)
279{
280 int i, ret;
281
282 for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
283 ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
284 if (ret)
285 return ret;
286 }
287
288 return 0;
289}