net/mlx5e: Added BW check for DIM decision mechanism
[linux/fpc-iii.git] / crypto / skcipher.c
blob93110d70c1d3061bf1640989addca4b8a5a2cb48
1 /*
2 * Symmetric key cipher operations.
4 * Generic encrypt/decrypt wrapper for ciphers, handles operations across
5 * multiple page boundaries by using temporary blocks. In user context,
6 * the kernel is given a chance to schedule us once per page.
8 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <crypto/internal/skcipher.h>
18 #include <linux/bug.h>
19 #include <linux/cryptouser.h>
20 #include <linux/module.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/seq_file.h>
23 #include <net/netlink.h>
25 #include "internal.h"
27 static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
29 if (alg->cra_type == &crypto_blkcipher_type)
30 return sizeof(struct crypto_blkcipher *);
32 if (alg->cra_type == &crypto_ablkcipher_type ||
33 alg->cra_type == &crypto_givcipher_type)
34 return sizeof(struct crypto_ablkcipher *);
36 return crypto_alg_extsize(alg);
39 static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
40 const u8 *key, unsigned int keylen)
42 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
43 struct crypto_blkcipher *blkcipher = *ctx;
44 int err;
46 crypto_blkcipher_clear_flags(blkcipher, ~0);
47 crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
48 CRYPTO_TFM_REQ_MASK);
49 err = crypto_blkcipher_setkey(blkcipher, key, keylen);
50 crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
51 CRYPTO_TFM_RES_MASK);
53 return err;
56 static int skcipher_crypt_blkcipher(struct skcipher_request *req,
57 int (*crypt)(struct blkcipher_desc *,
58 struct scatterlist *,
59 struct scatterlist *,
60 unsigned int))
62 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
63 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
64 struct blkcipher_desc desc = {
65 .tfm = *ctx,
66 .info = req->iv,
67 .flags = req->base.flags,
71 return crypt(&desc, req->dst, req->src, req->cryptlen);
74 static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
76 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
77 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
78 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
80 return skcipher_crypt_blkcipher(req, alg->encrypt);
83 static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
85 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
86 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
87 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
89 return skcipher_crypt_blkcipher(req, alg->decrypt);
92 static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
94 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
96 crypto_free_blkcipher(*ctx);
99 static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
101 struct crypto_alg *calg = tfm->__crt_alg;
102 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
103 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
104 struct crypto_blkcipher *blkcipher;
105 struct crypto_tfm *btfm;
107 if (!crypto_mod_get(calg))
108 return -EAGAIN;
110 btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
111 CRYPTO_ALG_TYPE_MASK);
112 if (IS_ERR(btfm)) {
113 crypto_mod_put(calg);
114 return PTR_ERR(btfm);
117 blkcipher = __crypto_blkcipher_cast(btfm);
118 *ctx = blkcipher;
119 tfm->exit = crypto_exit_skcipher_ops_blkcipher;
121 skcipher->setkey = skcipher_setkey_blkcipher;
122 skcipher->encrypt = skcipher_encrypt_blkcipher;
123 skcipher->decrypt = skcipher_decrypt_blkcipher;
125 skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
126 skcipher->keysize = calg->cra_blkcipher.max_keysize;
128 return 0;
131 static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
132 const u8 *key, unsigned int keylen)
134 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
135 struct crypto_ablkcipher *ablkcipher = *ctx;
136 int err;
138 crypto_ablkcipher_clear_flags(ablkcipher, ~0);
139 crypto_ablkcipher_set_flags(ablkcipher,
140 crypto_skcipher_get_flags(tfm) &
141 CRYPTO_TFM_REQ_MASK);
142 err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
143 crypto_skcipher_set_flags(tfm,
144 crypto_ablkcipher_get_flags(ablkcipher) &
145 CRYPTO_TFM_RES_MASK);
147 return err;
150 static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
151 int (*crypt)(struct ablkcipher_request *))
153 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
154 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
155 struct ablkcipher_request *subreq = skcipher_request_ctx(req);
157 ablkcipher_request_set_tfm(subreq, *ctx);
158 ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
159 req->base.complete, req->base.data);
160 ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
161 req->iv);
163 return crypt(subreq);
166 static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
168 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
169 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
170 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
172 return skcipher_crypt_ablkcipher(req, alg->encrypt);
175 static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
177 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
178 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
179 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
181 return skcipher_crypt_ablkcipher(req, alg->decrypt);
184 static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
186 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
188 crypto_free_ablkcipher(*ctx);
191 static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
193 struct crypto_alg *calg = tfm->__crt_alg;
194 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
195 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
196 struct crypto_ablkcipher *ablkcipher;
197 struct crypto_tfm *abtfm;
199 if (!crypto_mod_get(calg))
200 return -EAGAIN;
202 abtfm = __crypto_alloc_tfm(calg, 0, 0);
203 if (IS_ERR(abtfm)) {
204 crypto_mod_put(calg);
205 return PTR_ERR(abtfm);
208 ablkcipher = __crypto_ablkcipher_cast(abtfm);
209 *ctx = ablkcipher;
210 tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
212 skcipher->setkey = skcipher_setkey_ablkcipher;
213 skcipher->encrypt = skcipher_encrypt_ablkcipher;
214 skcipher->decrypt = skcipher_decrypt_ablkcipher;
216 skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
217 skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
218 sizeof(struct ablkcipher_request);
219 skcipher->keysize = calg->cra_ablkcipher.max_keysize;
221 return 0;
224 static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
225 const u8 *key, unsigned int keylen)
227 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
228 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
229 u8 *buffer, *alignbuffer;
230 unsigned long absize;
231 int ret;
233 absize = keylen + alignmask;
234 buffer = kmalloc(absize, GFP_ATOMIC);
235 if (!buffer)
236 return -ENOMEM;
238 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
239 memcpy(alignbuffer, key, keylen);
240 ret = cipher->setkey(tfm, alignbuffer, keylen);
241 kzfree(buffer);
242 return ret;
245 static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
246 unsigned int keylen)
248 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
249 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
251 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
252 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
253 return -EINVAL;
256 if ((unsigned long)key & alignmask)
257 return skcipher_setkey_unaligned(tfm, key, keylen);
259 return cipher->setkey(tfm, key, keylen);
262 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
264 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
265 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
267 alg->exit(skcipher);
270 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
272 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
273 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
275 if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
276 return crypto_init_skcipher_ops_blkcipher(tfm);
278 if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
279 tfm->__crt_alg->cra_type == &crypto_givcipher_type)
280 return crypto_init_skcipher_ops_ablkcipher(tfm);
282 skcipher->setkey = skcipher_setkey;
283 skcipher->encrypt = alg->encrypt;
284 skcipher->decrypt = alg->decrypt;
285 skcipher->ivsize = alg->ivsize;
286 skcipher->keysize = alg->max_keysize;
288 if (alg->exit)
289 skcipher->base.exit = crypto_skcipher_exit_tfm;
291 if (alg->init)
292 return alg->init(skcipher);
294 return 0;
297 static void crypto_skcipher_free_instance(struct crypto_instance *inst)
299 struct skcipher_instance *skcipher =
300 container_of(inst, struct skcipher_instance, s.base);
302 skcipher->free(skcipher);
305 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
306 __attribute__ ((unused));
307 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
309 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
310 base);
312 seq_printf(m, "type : skcipher\n");
313 seq_printf(m, "async : %s\n",
314 alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
315 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
316 seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
317 seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
318 seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
319 seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
322 #ifdef CONFIG_NET
323 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
325 struct crypto_report_blkcipher rblkcipher;
326 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
327 base);
329 strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
330 strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
332 rblkcipher.blocksize = alg->cra_blocksize;
333 rblkcipher.min_keysize = skcipher->min_keysize;
334 rblkcipher.max_keysize = skcipher->max_keysize;
335 rblkcipher.ivsize = skcipher->ivsize;
337 if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
338 sizeof(struct crypto_report_blkcipher), &rblkcipher))
339 goto nla_put_failure;
340 return 0;
342 nla_put_failure:
343 return -EMSGSIZE;
345 #else
346 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
348 return -ENOSYS;
350 #endif
352 static const struct crypto_type crypto_skcipher_type2 = {
353 .extsize = crypto_skcipher_extsize,
354 .init_tfm = crypto_skcipher_init_tfm,
355 .free = crypto_skcipher_free_instance,
356 #ifdef CONFIG_PROC_FS
357 .show = crypto_skcipher_show,
358 #endif
359 .report = crypto_skcipher_report,
360 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
361 .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
362 .type = CRYPTO_ALG_TYPE_SKCIPHER,
363 .tfmsize = offsetof(struct crypto_skcipher, base),
366 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
367 const char *name, u32 type, u32 mask)
369 spawn->base.frontend = &crypto_skcipher_type2;
370 return crypto_grab_spawn(&spawn->base, name, type, mask);
372 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
374 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
375 u32 type, u32 mask)
377 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
379 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
381 int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
383 return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
384 type, mask);
386 EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
388 static int skcipher_prepare_alg(struct skcipher_alg *alg)
390 struct crypto_alg *base = &alg->base;
392 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
393 return -EINVAL;
395 if (!alg->chunksize)
396 alg->chunksize = base->cra_blocksize;
398 base->cra_type = &crypto_skcipher_type2;
399 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
400 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
402 return 0;
405 int crypto_register_skcipher(struct skcipher_alg *alg)
407 struct crypto_alg *base = &alg->base;
408 int err;
410 err = skcipher_prepare_alg(alg);
411 if (err)
412 return err;
414 return crypto_register_alg(base);
416 EXPORT_SYMBOL_GPL(crypto_register_skcipher);
418 void crypto_unregister_skcipher(struct skcipher_alg *alg)
420 crypto_unregister_alg(&alg->base);
422 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
424 int crypto_register_skciphers(struct skcipher_alg *algs, int count)
426 int i, ret;
428 for (i = 0; i < count; i++) {
429 ret = crypto_register_skcipher(&algs[i]);
430 if (ret)
431 goto err;
434 return 0;
436 err:
437 for (--i; i >= 0; --i)
438 crypto_unregister_skcipher(&algs[i]);
440 return ret;
442 EXPORT_SYMBOL_GPL(crypto_register_skciphers);
444 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
446 int i;
448 for (i = count - 1; i >= 0; --i)
449 crypto_unregister_skcipher(&algs[i]);
451 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
453 int skcipher_register_instance(struct crypto_template *tmpl,
454 struct skcipher_instance *inst)
456 int err;
458 err = skcipher_prepare_alg(&inst->alg);
459 if (err)
460 return err;
462 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
464 EXPORT_SYMBOL_GPL(skcipher_register_instance);
466 MODULE_LICENSE("GPL");
467 MODULE_DESCRIPTION("Symmetric key cipher type");