hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/crypto/ccp/ccp-crypto-aes-xts.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
34 *
....@@ -5,10 +6,6 @@
56 *
67 * Author: Gary R Hook <gary.hook@amd.com>
78 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License version 2 as
11
- * published by the Free Software Foundation.
129 */
1310
1411 #include <linux/module.h>
....@@ -27,7 +24,7 @@
2724 const char *drv_name;
2825 };
2926
30
-static struct ccp_aes_xts_def aes_xts_algs[] = {
27
+static const struct ccp_aes_xts_def aes_xts_algs[] = {
3128 {
3229 .name = "xts(aes)",
3330 .drv_name = "xts-aes-ccp",
....@@ -64,26 +61,25 @@
6461
6562 static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
6663 {
67
- struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
68
- struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
64
+ struct skcipher_request *req = skcipher_request_cast(async_req);
65
+ struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
6966
7067 if (ret)
7168 return ret;
7269
73
- memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
70
+ memcpy(req->iv, rctx->iv, AES_BLOCK_SIZE);
7471
7572 return 0;
7673 }
7774
78
-static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
75
+static int ccp_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
7976 unsigned int key_len)
8077 {
81
- struct crypto_tfm *xfm = crypto_ablkcipher_tfm(tfm);
82
- struct ccp_ctx *ctx = crypto_tfm_ctx(xfm);
78
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
8379 unsigned int ccpversion = ccp_version();
8480 int ret;
8581
86
- ret = xts_check_key(xfm, key, key_len);
82
+ ret = xts_verify_key(tfm, key, key_len);
8783 if (ret)
8884 return ret;
8985
....@@ -105,11 +101,12 @@
105101 return crypto_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
106102 }
107103
108
-static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
104
+static int ccp_aes_xts_crypt(struct skcipher_request *req,
109105 unsigned int encrypt)
110106 {
111
- struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
112
- struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
107
+ struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
108
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
109
+ struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
113110 unsigned int ccpversion = ccp_version();
114111 unsigned int fallback = 0;
115112 unsigned int unit;
....@@ -119,10 +116,7 @@
119116 if (!ctx->u.aes.key_len)
120117 return -EINVAL;
121118
122
- if (req->nbytes & (AES_BLOCK_SIZE - 1))
123
- return -EINVAL;
124
-
125
- if (!req->info)
119
+ if (!req->iv)
126120 return -EINVAL;
127121
128122 /* Check conditions under which the CCP can fulfill a request. The
....@@ -133,7 +127,7 @@
133127 */
134128 unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
135129 for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) {
136
- if (req->nbytes == xts_unit_sizes[unit].size) {
130
+ if (req->cryptlen == xts_unit_sizes[unit].size) {
137131 unit_size = unit;
138132 break;
139133 }
....@@ -151,23 +145,23 @@
151145 (ctx->u.aes.key_len != AES_KEYSIZE_256))
152146 fallback = 1;
153147 if (fallback) {
154
- SKCIPHER_REQUEST_ON_STACK(subreq, ctx->u.aes.tfm_skcipher);
155
-
156148 /* Use the fallback to process the request for any
157149 * unsupported unit sizes or key sizes
158150 */
159
- skcipher_request_set_tfm(subreq, ctx->u.aes.tfm_skcipher);
160
- skcipher_request_set_callback(subreq, req->base.flags,
161
- NULL, NULL);
162
- skcipher_request_set_crypt(subreq, req->src, req->dst,
163
- req->nbytes, req->info);
164
- ret = encrypt ? crypto_skcipher_encrypt(subreq) :
165
- crypto_skcipher_decrypt(subreq);
166
- skcipher_request_zero(subreq);
151
+ skcipher_request_set_tfm(&rctx->fallback_req,
152
+ ctx->u.aes.tfm_skcipher);
153
+ skcipher_request_set_callback(&rctx->fallback_req,
154
+ req->base.flags,
155
+ req->base.complete,
156
+ req->base.data);
157
+ skcipher_request_set_crypt(&rctx->fallback_req, req->src,
158
+ req->dst, req->cryptlen, req->iv);
159
+ ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
160
+ crypto_skcipher_decrypt(&rctx->fallback_req);
167161 return ret;
168162 }
169163
170
- memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
164
+ memcpy(rctx->iv, req->iv, AES_BLOCK_SIZE);
171165 sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
172166
173167 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
....@@ -182,7 +176,7 @@
182176 rctx->cmd.u.xts.iv = &rctx->iv_sg;
183177 rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
184178 rctx->cmd.u.xts.src = req->src;
185
- rctx->cmd.u.xts.src_len = req->nbytes;
179
+ rctx->cmd.u.xts.src_len = req->cryptlen;
186180 rctx->cmd.u.xts.dst = req->dst;
187181
188182 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
....@@ -190,26 +184,25 @@
190184 return ret;
191185 }
192186
193
-static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
187
+static int ccp_aes_xts_encrypt(struct skcipher_request *req)
194188 {
195189 return ccp_aes_xts_crypt(req, 1);
196190 }
197191
198
-static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
192
+static int ccp_aes_xts_decrypt(struct skcipher_request *req)
199193 {
200194 return ccp_aes_xts_crypt(req, 0);
201195 }
202196
203
-static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
197
+static int ccp_aes_xts_init_tfm(struct crypto_skcipher *tfm)
204198 {
205
- struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
199
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
206200 struct crypto_skcipher *fallback_tfm;
207201
208202 ctx->complete = ccp_aes_xts_complete;
209203 ctx->u.aes.key_len = 0;
210204
211205 fallback_tfm = crypto_alloc_skcipher("xts(aes)", 0,
212
- CRYPTO_ALG_ASYNC |
213206 CRYPTO_ALG_NEED_FALLBACK);
214207 if (IS_ERR(fallback_tfm)) {
215208 pr_warn("could not load fallback driver xts(aes)\n");
....@@ -217,14 +210,15 @@
217210 }
218211 ctx->u.aes.tfm_skcipher = fallback_tfm;
219212
220
- tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
213
+ crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx) +
214
+ crypto_skcipher_reqsize(fallback_tfm));
221215
222216 return 0;
223217 }
224218
225
-static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
219
+static void ccp_aes_xts_exit_tfm(struct crypto_skcipher *tfm)
226220 {
227
- struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
221
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
228222
229223 crypto_free_skcipher(ctx->u.aes.tfm_skcipher);
230224 }
....@@ -232,8 +226,8 @@
232226 static int ccp_register_aes_xts_alg(struct list_head *head,
233227 const struct ccp_aes_xts_def *def)
234228 {
235
- struct ccp_crypto_ablkcipher_alg *ccp_alg;
236
- struct crypto_alg *alg;
229
+ struct ccp_crypto_skcipher_alg *ccp_alg;
230
+ struct skcipher_alg *alg;
237231 int ret;
238232
239233 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
....@@ -244,30 +238,31 @@
244238
245239 alg = &ccp_alg->alg;
246240
247
- snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
248
- snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
241
+ snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
242
+ snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
249243 def->drv_name);
250
- alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
251
- CRYPTO_ALG_KERN_DRIVER_ONLY |
252
- CRYPTO_ALG_NEED_FALLBACK;
253
- alg->cra_blocksize = AES_BLOCK_SIZE;
254
- alg->cra_ctxsize = sizeof(struct ccp_ctx);
255
- alg->cra_priority = CCP_CRA_PRIORITY;
256
- alg->cra_type = &crypto_ablkcipher_type;
257
- alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
258
- alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
259
- alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
260
- alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
261
- alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
262
- alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
263
- alg->cra_init = ccp_aes_xts_cra_init;
264
- alg->cra_exit = ccp_aes_xts_cra_exit;
265
- alg->cra_module = THIS_MODULE;
244
+ alg->base.cra_flags = CRYPTO_ALG_ASYNC |
245
+ CRYPTO_ALG_ALLOCATES_MEMORY |
246
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
247
+ CRYPTO_ALG_NEED_FALLBACK;
248
+ alg->base.cra_blocksize = AES_BLOCK_SIZE;
249
+ alg->base.cra_ctxsize = sizeof(struct ccp_ctx);
250
+ alg->base.cra_priority = CCP_CRA_PRIORITY;
251
+ alg->base.cra_module = THIS_MODULE;
266252
267
- ret = crypto_register_alg(alg);
253
+ alg->setkey = ccp_aes_xts_setkey;
254
+ alg->encrypt = ccp_aes_xts_encrypt;
255
+ alg->decrypt = ccp_aes_xts_decrypt;
256
+ alg->min_keysize = AES_MIN_KEY_SIZE * 2;
257
+ alg->max_keysize = AES_MAX_KEY_SIZE * 2;
258
+ alg->ivsize = AES_BLOCK_SIZE;
259
+ alg->init = ccp_aes_xts_init_tfm;
260
+ alg->exit = ccp_aes_xts_exit_tfm;
261
+
262
+ ret = crypto_register_skcipher(alg);
268263 if (ret) {
269
- pr_err("%s ablkcipher algorithm registration error (%d)\n",
270
- alg->cra_name, ret);
264
+ pr_err("%s skcipher algorithm registration error (%d)\n",
265
+ alg->base.cra_name, ret);
271266 kfree(ccp_alg);
272267 return ret;
273268 }