This source file includes following definitions.
- ccp_des3_complete
- ccp_des3_setkey
- ccp_des3_crypt
- ccp_des3_encrypt
- ccp_des3_decrypt
- ccp_des3_cra_init
- ccp_des3_cra_exit
- ccp_register_des3_alg
- ccp_register_des3_algs
1
2
3
4
5
6
7
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
21 static int ccp_des3_complete(struct crypto_async_request *async_req, int ret)
22 {
23 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
24 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
25 struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
26
27 if (ret)
28 return ret;
29
30 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB)
31 memcpy(req->info, rctx->iv, DES3_EDE_BLOCK_SIZE);
32
33 return 0;
34 }
35
36 static int ccp_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
37 unsigned int key_len)
38 {
39 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
40 struct ccp_crypto_ablkcipher_alg *alg =
41 ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm));
42 int err;
43
44 err = verify_ablkcipher_des3_key(tfm, key);
45 if (err)
46 return err;
47
48
49
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
61 static int ccp_des3_crypt(struct ablkcipher_request *req, bool encrypt)
62 {
63 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
64 struct ccp_des3_req_ctx *rctx = ablkcipher_request_ctx(req);
65 struct scatterlist *iv_sg = NULL;
66 unsigned int iv_len = 0;
67 int ret;
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->nbytes & (DES3_EDE_BLOCK_SIZE - 1)))
75 return -EINVAL;
76
77 if (ctx->u.des3.mode != CCP_DES3_MODE_ECB) {
78 if (!req->info)
79 return -EINVAL;
80
81 memcpy(rctx->iv, req->info, 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->nbytes;
101 rctx->cmd.u.des3.dst = req->dst;
102
103 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
104
105 return ret;
106 }
107
108 static int ccp_des3_encrypt(struct ablkcipher_request *req)
109 {
110 return ccp_des3_crypt(req, true);
111 }
112
113 static int ccp_des3_decrypt(struct ablkcipher_request *req)
114 {
115 return ccp_des3_crypt(req, false);
116 }
117
118 static int ccp_des3_cra_init(struct crypto_tfm *tfm)
119 {
120 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
121
122 ctx->complete = ccp_des3_complete;
123 ctx->u.des3.key_len = 0;
124
125 tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_des3_req_ctx);
126
127 return 0;
128 }
129
130 static void ccp_des3_cra_exit(struct crypto_tfm *tfm)
131 {
132 }
133
134 static struct crypto_alg ccp_des3_defaults = {
135 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
136 CRYPTO_ALG_ASYNC |
137 CRYPTO_ALG_KERN_DRIVER_ONLY |
138 CRYPTO_ALG_NEED_FALLBACK,
139 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
140 .cra_ctxsize = sizeof(struct ccp_ctx),
141 .cra_priority = CCP_CRA_PRIORITY,
142 .cra_type = &crypto_ablkcipher_type,
143 .cra_init = ccp_des3_cra_init,
144 .cra_exit = ccp_des3_cra_exit,
145 .cra_module = THIS_MODULE,
146 .cra_ablkcipher = {
147 .setkey = ccp_des3_setkey,
148 .encrypt = ccp_des3_encrypt,
149 .decrypt = ccp_des3_decrypt,
150 .min_keysize = DES3_EDE_KEY_SIZE,
151 .max_keysize = DES3_EDE_KEY_SIZE,
152 },
153 };
154
155 struct ccp_des3_def {
156 enum ccp_des3_mode mode;
157 unsigned int version;
158 const char *name;
159 const char *driver_name;
160 unsigned int blocksize;
161 unsigned int ivsize;
162 struct crypto_alg *alg_defaults;
163 };
164
165 static struct ccp_des3_def des3_algs[] = {
166 {
167 .mode = CCP_DES3_MODE_ECB,
168 .version = CCP_VERSION(5, 0),
169 .name = "ecb(des3_ede)",
170 .driver_name = "ecb-des3-ccp",
171 .blocksize = DES3_EDE_BLOCK_SIZE,
172 .ivsize = 0,
173 .alg_defaults = &ccp_des3_defaults,
174 },
175 {
176 .mode = CCP_DES3_MODE_CBC,
177 .version = CCP_VERSION(5, 0),
178 .name = "cbc(des3_ede)",
179 .driver_name = "cbc-des3-ccp",
180 .blocksize = DES3_EDE_BLOCK_SIZE,
181 .ivsize = DES3_EDE_BLOCK_SIZE,
182 .alg_defaults = &ccp_des3_defaults,
183 },
184 };
185
186 static int ccp_register_des3_alg(struct list_head *head,
187 const struct ccp_des3_def *def)
188 {
189 struct ccp_crypto_ablkcipher_alg *ccp_alg;
190 struct crypto_alg *alg;
191 int ret;
192
193 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
194 if (!ccp_alg)
195 return -ENOMEM;
196
197 INIT_LIST_HEAD(&ccp_alg->entry);
198
199 ccp_alg->mode = def->mode;
200
201
202 alg = &ccp_alg->alg;
203 *alg = *def->alg_defaults;
204 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
205 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
206 def->driver_name);
207 alg->cra_blocksize = def->blocksize;
208 alg->cra_ablkcipher.ivsize = def->ivsize;
209
210 ret = crypto_register_alg(alg);
211 if (ret) {
212 pr_err("%s ablkcipher algorithm registration error (%d)\n",
213 alg->cra_name, ret);
214 kfree(ccp_alg);
215 return ret;
216 }
217
218 list_add(&ccp_alg->entry, head);
219
220 return 0;
221 }
222
223 int ccp_register_des3_algs(struct list_head *head)
224 {
225 int i, ret;
226 unsigned int ccpversion = ccp_version();
227
228 for (i = 0; i < ARRAY_SIZE(des3_algs); i++) {
229 if (des3_algs[i].version > ccpversion)
230 continue;
231 ret = ccp_register_des3_alg(head, &des3_algs[i]);
232 if (ret)
233 return ret;
234 }
235
236 return 0;
237 }