Linux 5.1.15
[linux/fpc-iii.git] / drivers / crypto / ccp / ccp-crypto-des3.c
blobc2ff551d215b55f3e9b40a0eeec95db3c36e4ac8
1 /*
2 * AMD Cryptographic Coprocessor (CCP) DES3 crypto API support
4 * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
6 * Author: Gary R Hook <ghook@amd.com>
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.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/scatterlist.h>
17 #include <linux/crypto.h>
18 #include <crypto/algapi.h>
19 #include <crypto/scatterwalk.h>
20 #include <crypto/des.h>
22 #include "ccp-crypto.h"
24 static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
26 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
27 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
28 struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
30 if (ret)
31 return ret;
33 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
34 memcpy(req->info, rctx->iv, DES3_EDE_BLOCK_SIZE);
36 return 0;
39 static int ccp_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
40 unsigned int key_len)
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;
48 /* From des_generic.c:
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.
56 const u32 *K = (const u32 *)key;
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_FORBID_WEAK_KEYS)) {
61 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
62 return -EINVAL;
65 /* It's not clear that there is any support for a keysize of 112.
66 * If needed, the caller should make K1 == K3
68 ctx->u.des3.type = CCP_DES3_TYPE_168;
69 ctx->u.des3.mode = alg->mode;
70 ctx->u.des3.key_len = key_len;
72 memcpy(ctx->u.des3.key, key, key_len);
73 sg_init_one(&ctx->u.des3.key_sg, ctx->u.des3.key, key_len);
75 return 0;
78 static int ccp_des3_crypt(struct ablkcipher_request *req, bool encrypt)
80 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
81 struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
82 struct scatterlist *iv_sg = NULL;
83 unsigned int iv_len = 0;
84 int ret;
86 if (!ctx->u.des3.key_len)
87 return -EINVAL;
89 if (((ctx->u.des3.mode == CCP_DES3_MODE_ECB) ||
90 (ctx->u.des3.mode == CCP_DES3_MODE_CBC)) &&
91 (req->nbytes & (DES3_EDE_BLOCK_SIZE - 1)))
92 return -EINVAL;
94 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
95 if (!req->info)
96 return -EINVAL;
98 memcpy(rctx->iv, req->info, DES3_EDE_BLOCK_SIZE);
99 iv_sg = &rctx->iv_sg;
100 iv_len = DES3_EDE_BLOCK_SIZE;
101 sg_init_one(iv_sg, rctx->iv, iv_len);
104 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
105 INIT_LIST_HEAD(&rctx->cmd.entry);
106 rctx->cmd.engine = CCP_ENGINE_DES3;
107 rctx->cmd.u.des3.type = ctx->u.des3.type;
108 rctx->cmd.u.des3.mode = ctx->u.des3.mode;
109 rctx->cmd.u.des3.action = (encrypt)
110 ? CCP_DES3_ACTION_ENCRYPT
111 : CCP_DES3_ACTION_DECRYPT;
112 rctx->cmd.u.des3.key = &ctx->u.des3.key_sg;
113 rctx->cmd.u.des3.key_len = ctx->u.des3.key_len;
114 rctx->cmd.u.des3.iv = iv_sg;
115 rctx->cmd.u.des3.iv_len = iv_len;
116 rctx->cmd.u.des3.src = req->src;
117 rctx->cmd.u.des3.src_len = req->nbytes;
118 rctx->cmd.u.des3.dst = req->dst;
120 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
122 return ret;
125 static int ccp_des3_encrypt(struct ablkcipher_request *req)
127 return ccp_des3_crypt(req, true);
130 static int ccp_des3_decrypt(struct ablkcipher_request *req)
132 return ccp_des3_crypt(req, false);
135 static int ccp_des3_cra_init(struct crypto_tfm *tfm)
137 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
139 ctx->complete = ccp_des3_complete;
140 ctx->u.des3.key_len = 0;
142 tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_des3_req_ctx);
144 return 0;
147 static void ccp_des3_cra_exit(struct crypto_tfm *tfm)
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,
172 struct ccp_des3_def {
173 enum ccp_des3_mode mode;
174 unsigned int version;
175 const char *name;
176 const char *driver_name;
177 unsigned int blocksize;
178 unsigned int ivsize;
179 struct crypto_alg *alg_defaults;
182 static struct ccp_des3_def des3_algs[] = {
184 .mode = CCP_DES3_MODE_ECB,
185 .version = CCP_VERSION(5, 0),
186 .name = "ecb(des3_ede)",
187 .driver_name = "ecb-des3-ccp",
188 .blocksize = DES3_EDE_BLOCK_SIZE,
189 .ivsize = 0,
190 .alg_defaults = &ccp_des3_defaults,
193 .mode = CCP_DES3_MODE_CBC,
194 .version = CCP_VERSION(5, 0),
195 .name = "cbc(des3_ede)",
196 .driver_name = "cbc-des3-ccp",
197 .blocksize = DES3_EDE_BLOCK_SIZE,
198 .ivsize = DES3_EDE_BLOCK_SIZE,
199 .alg_defaults = &ccp_des3_defaults,
203 static int ccp_register_des3_alg(struct list_head *head,
204 const struct ccp_des3_def *def)
206 struct ccp_crypto_ablkcipher_alg *ccp_alg;
207 struct crypto_alg *alg;
208 int ret;
210 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
211 if (!ccp_alg)
212 return -ENOMEM;
214 INIT_LIST_HEAD(&ccp_alg->entry);
216 ccp_alg->mode = def->mode;
218 /* Copy the defaults and override as necessary */
219 alg = &ccp_alg->alg;
220 *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",
223 def->driver_name);
224 alg->cra_blocksize = def->blocksize;
225 alg->cra_ablkcipher.ivsize = def->ivsize;
227 ret = crypto_register_alg(alg);
228 if (ret) {
229 pr_err("%s ablkcipher algorithm registration error (%d)\n",
230 alg->cra_name, ret);
231 kfree(ccp_alg);
232 return ret;
235 list_add(&ccp_alg->entry, head);
237 return 0;
240 int ccp_register_des3_algs(struct list_head *head)
242 int i, ret;
243 unsigned int ccpversion = ccp_version();
245 for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
246 if (des3_algs[i].version > ccpversion)
247 continue;
248 ret = ccp_register_des3_alg(head, &des3_algs[i]);
249 if (ret)
250 return ret;
253 return 0;