hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/drivers/crypto/ccp/ccp-crypto-des3.c
....@@ -1,13 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support
34 *
45 * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
56 *
67 * Author: Gary R Hook <ghook@amd.com>
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
118 */
129
1310 #include <linux/module.h>
....@@ -17,50 +14,35 @@
1714 #include <linux/crypto.h>
1815 #include <crypto/algapi.h>
1916 #include <crypto/scatterwalk.h>
20
-#include <crypto/des.h>
17
+#include <crypto/internal/des.h>
2118
2219 #include "ccp-crypto.h"
2320
2421 static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
2522 {
26
- struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
23
+ struct skcipher_request *req = skcipher_request_cast(async_req);
2724 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
28
- struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
25
+ struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req);
2926
3027 if (ret)
3128 return ret;
3229
3330 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
34
- memcpy(req->info, rctx->iv, DES3_EDE_BLOCK_SIZE);
31
+ memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE);
3532
3633 return 0;
3734 }
3835
39
-static int ccp_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
36
+static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
4037 unsigned int key_len)
4138 {
42
- struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
43
- struct ccp_crypto_ablkcipher_alg *alg =
44
- ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm));
45
- u32 *flags = &tfm->base.crt_flags;
39
+ struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
40
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
41
+ int err;
4642
47
-
48
- /* From des_generic.c:
49
- *
50
- * RFC2451:
51
- * If the first two or last two independent 64-bit keys are
52
- * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
53
- * same as DES. Implementers MUST reject keys that exhibit this
54
- * property.
55
- */
56
- const u32 *K = (const u32 *)key;
57
-
58
- if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
59
- !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
60
- (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
61
- *flags |= CRYPTO_TFM_RES_WEAK_KEY;
62
- return -EINVAL;
63
- }
43
+ err = verify_skcipher_des3_key(tfm, key);
44
+ if (err)
45
+ return err;
6446
6547 /* It's not clear that there is any support for a keysize of 112.
6648 * If needed, the caller should make K1 == K3
....@@ -75,10 +57,11 @@
7557 return 0;
7658 }
7759
78
-static int ccp_des3_crypt(struct ablkcipher_request *req, bool encrypt)
60
+static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt)
7961 {
80
- struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
81
- struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
62
+ struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
63
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
64
+ struct ccp_des3_req_ctx *rctx = skcipher_request_ctx(req);
8265 struct scatterlist *iv_sg = NULL;
8366 unsigned int iv_len = 0;
8467 int ret;
....@@ -88,14 +71,14 @@
8871
8972 if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) ||
9073 (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) &&
91
- (req->nbytes & (DES3_EDE_BLOCK_SIZE - 1)))
74
+ (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1)))
9275 return -EINVAL;
9376
9477 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
95
- if (!req->info)
78
+ if (!req->iv)
9679 return -EINVAL;
9780
98
- memcpy(rctx->iv, req->info, DES3_EDE_BLOCK_SIZE);
81
+ memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE);
9982 iv_sg = &rctx->iv_sg;
10083 iv_len = DES3_EDE_BLOCK_SIZE;
10184 sg_init_one(iv_sg, rctx->iv, iv_len);
....@@ -114,7 +97,7 @@
11497 rctx->cmd.u.des3.iv = iv_sg;
11598 rctx->cmd.u.des3.iv_len = iv_len;
11699 rctx->cmd.u.des3.src = req->src;
117
- rctx->cmd.u.des3.src_len = req->nbytes;
100
+ rctx->cmd.u.des3.src_len = req->cryptlen;
118101 rctx->cmd.u.des3.dst = req->dst;
119102
120103 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
....@@ -122,51 +105,44 @@
122105 return ret;
123106 }
124107
125
-static int ccp_des3_encrypt(struct ablkcipher_request *req)
108
+static int ccp_des3_encrypt(struct skcipher_request *req)
126109 {
127110 return ccp_des3_crypt(req, true);
128111 }
129112
130
-static int ccp_des3_decrypt(struct ablkcipher_request *req)
113
+static int ccp_des3_decrypt(struct skcipher_request *req)
131114 {
132115 return ccp_des3_crypt(req, false);
133116 }
134117
135
-static int ccp_des3_cra_init(struct crypto_tfm *tfm)
118
+static int ccp_des3_init_tfm(struct crypto_skcipher *tfm)
136119 {
137
- struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
120
+ struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
138121
139122 ctx->complete = ccp_des3_complete;
140123 ctx->u.des3.key_len = 0;
141124
142
- tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_des3_req_ctx);
125
+ crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_des3_req_ctx));
143126
144127 return 0;
145128 }
146129
147
-static void ccp_des3_cra_exit(struct crypto_tfm *tfm)
148
-{
149
-}
130
+static const struct skcipher_alg ccp_des3_defaults = {
131
+ .setkey = ccp_des3_setkey,
132
+ .encrypt = ccp_des3_encrypt,
133
+ .decrypt = ccp_des3_decrypt,
134
+ .min_keysize = DES3_EDE_KEY_SIZE,
135
+ .max_keysize = DES3_EDE_KEY_SIZE,
136
+ .init = ccp_des3_init_tfm,
150137
151
-static struct crypto_alg ccp_des3_defaults = {
152
- .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
153
- CRYPTO_ALG_ASYNC |
154
- CRYPTO_ALG_KERN_DRIVER_ONLY |
155
- CRYPTO_ALG_NEED_FALLBACK,
156
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
157
- .cra_ctxsize = sizeof(struct ccp_ctx),
158
- .cra_priority = CCP_CRA_PRIORITY,
159
- .cra_type = &crypto_ablkcipher_type,
160
- .cra_init = ccp_des3_cra_init,
161
- .cra_exit = ccp_des3_cra_exit,
162
- .cra_module = THIS_MODULE,
163
- .cra_ablkcipher = {
164
- .setkey = ccp_des3_setkey,
165
- .encrypt = ccp_des3_encrypt,
166
- .decrypt = ccp_des3_decrypt,
167
- .min_keysize = DES3_EDE_KEY_SIZE,
168
- .max_keysize = DES3_EDE_KEY_SIZE,
169
- },
138
+ .base.cra_flags = CRYPTO_ALG_ASYNC |
139
+ CRYPTO_ALG_ALLOCATES_MEMORY |
140
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
141
+ CRYPTO_ALG_NEED_FALLBACK,
142
+ .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
143
+ .base.cra_ctxsize = sizeof(struct ccp_ctx),
144
+ .base.cra_priority = CCP_CRA_PRIORITY,
145
+ .base.cra_module = THIS_MODULE,
170146 };
171147
172148 struct ccp_des3_def {
....@@ -176,10 +152,10 @@
176152 const char *driver_name;
177153 unsigned int blocksize;
178154 unsigned int ivsize;
179
- struct crypto_alg *alg_defaults;
155
+ const struct skcipher_alg *alg_defaults;
180156 };
181157
182
-static struct ccp_des3_def des3_algs[] = {
158
+static const struct ccp_des3_def des3_algs[] = {
183159 {
184160 .mode = CCP_DES3_MODE_ECB,
185161 .version = CCP_VERSION(5, 0),
....@@ -203,8 +179,8 @@
203179 static int ccp_register_des3_alg(struct list_head *head,
204180 const struct ccp_des3_def *def)
205181 {
206
- struct ccp_crypto_ablkcipher_alg *ccp_alg;
207
- struct crypto_alg *alg;
182
+ struct ccp_crypto_skcipher_alg *ccp_alg;
183
+ struct skcipher_alg *alg;
208184 int ret;
209185
210186 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
....@@ -218,16 +194,16 @@
218194 /* Copy the defaults and override as necessary */
219195 alg = &ccp_alg->alg;
220196 *alg = *def->alg_defaults;
221
- snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
222
- snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
197
+ snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
198
+ snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
223199 def->driver_name);
224
- alg->cra_blocksize = def->blocksize;
225
- alg->cra_ablkcipher.ivsize = def->ivsize;
200
+ alg->base.cra_blocksize = def->blocksize;
201
+ alg->ivsize = def->ivsize;
226202
227
- ret = crypto_register_alg(alg);
203
+ ret = crypto_register_skcipher(alg);
228204 if (ret) {
229
- pr_err("%s ablkcipher algorithm registration error (%d)\n",
230
- alg->cra_name, ret);
205
+ pr_err("%s skcipher algorithm registration error (%d)\n",
206
+ alg->base.cra_name, ret);
231207 kfree(ccp_alg);
232208 return ret;
233209 }