1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support
4 *
5 * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
6 *
7 * Author: Gary R Hook <ghook@amd.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/delay.h>
13 #include <linux/scatterlist.h>
14 #include <linux/crypto.h>
15 #include <crypto/algapi.h>
16 #include <crypto/scatterwalk.h>
17 #include <crypto/internal/des.h>
18
19 #include "ccp-crypto.h"
20
ccp_des3_complete(struct crypto_async_request * async_req,int ret)21 static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
22 {
23 struct skcipher_request *req = skcipher_request_cast(async_req);
24 struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(
25 crypto_skcipher_reqtfm(req));
26 struct ccp_des3_req_ctx *rctx = skcipher_request_ctx_dma(req);
27
28 if (ret)
29 return ret;
30
31 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
32 memcpy(req->iv, rctx->iv, DES3_EDE_BLOCK_SIZE);
33
34 return 0;
35 }
36
ccp_des3_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int key_len)37 static int ccp_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
38 unsigned int key_len)
39 {
40 struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
41 struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
42 int err;
43
44 err = verify_skcipher_des3_key(tfm, key);
45 if (err)
46 return err;
47
48 /* It's not clear that there is any support for a keysize of 112.
49 * If needed, the caller should make K1 == K3
50 */
51 ctx->u.des3.type = CCP_DES3_TYPE_168;
52 ctx->u.des3.mode = alg->mode;
53 ctx->u.des3.key_len = key_len;
54
55 memcpy(ctx->u.des3.key, key, key_len);
56 sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len);
57
58 return 0;
59 }
60
ccp_des3_crypt(struct skcipher_request * req,bool encrypt)61 static int ccp_des3_crypt(struct skcipher_request *req, bool encrypt)
62 {
63 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
64 struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
65 struct ccp_des3_req_ctx *rctx = skcipher_request_ctx_dma(req);
66 struct scatterlist *iv_sg = NULL;
67 unsigned int iv_len = 0;
68
69 if (!ctx->u.des3.key_len)
70 return -EINVAL;
71
72 if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) ||
73 (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) &&
74 (req->cryptlen & (DES3_EDE_BLOCK_SIZE - 1)))
75 return -EINVAL;
76
77 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
78 if (!req->iv)
79 return -EINVAL;
80
81 memcpy(rctx->iv, req->iv, DES3_EDE_BLOCK_SIZE);
82 iv_sg = &rctx->iv_sg;
83 iv_len = DES3_EDE_BLOCK_SIZE;
84 sg_init_one(iv_sg, rctx->iv, iv_len);
85 }
86
87 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
88 INIT_LIST_HEAD(&rctx->cmd.entry);
89 rctx->cmd.engine = CCP_ENGINE_DES3;
90 rctx->cmd.u.des3.type = ctx->u.des3.type;
91 rctx->cmd.u.des3.mode = ctx->u.des3.mode;
92 rctx->cmd.u.des3.action = (encrypt)
93 ? CCP_DES3_ACTION_ENCRYPT
94 : CCP_DES3_ACTION_DECRYPT;
95 rctx->cmd.u.des3.key = &ctx->u.des3.key_sg;
96 rctx->cmd.u.des3.key_len = ctx->u.des3.key_len;
97 rctx->cmd.u.des3.iv = iv_sg;
98 rctx->cmd.u.des3.iv_len = iv_len;
99 rctx->cmd.u.des3.src = req->src;
100 rctx->cmd.u.des3.src_len = req->cryptlen;
101 rctx->cmd.u.des3.dst = req->dst;
102
103 return ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
104 }
105
ccp_des3_encrypt(struct skcipher_request * req)106 static int ccp_des3_encrypt(struct skcipher_request *req)
107 {
108 return ccp_des3_crypt(req, true);
109 }
110
ccp_des3_decrypt(struct skcipher_request * req)111 static int ccp_des3_decrypt(struct skcipher_request *req)
112 {
113 return ccp_des3_crypt(req, false);
114 }
115
ccp_des3_init_tfm(struct crypto_skcipher * tfm)116 static int ccp_des3_init_tfm(struct crypto_skcipher *tfm)
117 {
118 struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
119
120 ctx->complete = ccp_des3_complete;
121 ctx->u.des3.key_len = 0;
122
123 crypto_skcipher_set_reqsize_dma(tfm, sizeof(struct ccp_des3_req_ctx));
124
125 return 0;
126 }
127
128 static const struct skcipher_alg ccp_des3_defaults = {
129 .setkey = ccp_des3_setkey,
130 .encrypt = ccp_des3_encrypt,
131 .decrypt = ccp_des3_decrypt,
132 .min_keysize = DES3_EDE_KEY_SIZE,
133 .max_keysize = DES3_EDE_KEY_SIZE,
134 .init = ccp_des3_init_tfm,
135
136 .base.cra_flags = CRYPTO_ALG_ASYNC |
137 CRYPTO_ALG_ALLOCATES_MEMORY |
138 CRYPTO_ALG_KERN_DRIVER_ONLY |
139 CRYPTO_ALG_NEED_FALLBACK,
140 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
141 .base.cra_ctxsize = sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
142 .base.cra_priority = CCP_CRA_PRIORITY,
143 .base.cra_module = THIS_MODULE,
144 };
145
146 struct ccp_des3_def {
147 enum ccp_des3_mode mode;
148 unsigned int version;
149 const char *name;
150 const char *driver_name;
151 unsigned int blocksize;
152 unsigned int ivsize;
153 const struct skcipher_alg *alg_defaults;
154 };
155
156 static const struct ccp_des3_def des3_algs[] = {
157 {
158 .mode = CCP_DES3_MODE_ECB,
159 .version = CCP_VERSION(5, 0),
160 .name = "ecb(des3_ede)",
161 .driver_name = "ecb-des3-ccp",
162 .blocksize = DES3_EDE_BLOCK_SIZE,
163 .ivsize = 0,
164 .alg_defaults = &ccp_des3_defaults,
165 },
166 {
167 .mode = CCP_DES3_MODE_CBC,
168 .version = CCP_VERSION(5, 0),
169 .name = "cbc(des3_ede)",
170 .driver_name = "cbc-des3-ccp",
171 .blocksize = DES3_EDE_BLOCK_SIZE,
172 .ivsize = DES3_EDE_BLOCK_SIZE,
173 .alg_defaults = &ccp_des3_defaults,
174 },
175 };
176
ccp_register_des3_alg(struct list_head * head,const struct ccp_des3_def * def)177 static int ccp_register_des3_alg(struct list_head *head,
178 const struct ccp_des3_def *def)
179 {
180 struct ccp_crypto_skcipher_alg *ccp_alg;
181 struct skcipher_alg *alg;
182 int ret;
183
184 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
185 if (!ccp_alg)
186 return -ENOMEM;
187
188 INIT_LIST_HEAD(&ccp_alg->entry);
189
190 ccp_alg->mode = def->mode;
191
192 /* Copy the defaults and override as necessary */
193 alg = &ccp_alg->alg;
194 *alg = *def->alg_defaults;
195 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
196 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
197 def->driver_name);
198 alg->base.cra_blocksize = def->blocksize;
199 alg->ivsize = def->ivsize;
200
201 ret = crypto_register_skcipher(alg);
202 if (ret) {
203 pr_err("%s skcipher algorithm registration error (%d)\n",
204 alg->base.cra_name, ret);
205 kfree(ccp_alg);
206 return ret;
207 }
208
209 list_add(&ccp_alg->entry, head);
210
211 return 0;
212 }
213
ccp_register_des3_algs(struct list_head * head)214 int ccp_register_des3_algs(struct list_head *head)
215 {
216 int i, ret;
217 unsigned int ccpversion = ccp_version();
218
219 for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
220 if (des3_algs[i].version > ccpversion)
221 continue;
222 ret = ccp_register_des3_alg(head, &des3_algs[i]);
223 if (ret)
224 return ret;
225 }
226
227 return 0;
228 }
229