mm: vmscan: do not iterate all mem cgroups for global direct reclaim
[linux/fpc-iii.git] / crypto / cbc.c
blobd12efaac9230b089baa5d137576f6ee1fb7488a4
1 /*
2 * CBC: Cipher Block Chaining mode
4 * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
13 #include <crypto/algapi.h>
14 #include <crypto/cbc.h>
15 #include <crypto/internal/skcipher.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/module.h>
22 static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
23 const u8 *src, u8 *dst)
25 crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
28 static int crypto_cbc_encrypt(struct skcipher_request *req)
30 return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
33 static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
34 const u8 *src, u8 *dst)
36 crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src);
39 static int crypto_cbc_decrypt(struct skcipher_request *req)
41 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
42 struct skcipher_walk walk;
43 int err;
45 err = skcipher_walk_virt(&walk, req, false);
47 while (walk.nbytes) {
48 err = crypto_cbc_decrypt_blocks(&walk, tfm,
49 crypto_cbc_decrypt_one);
50 err = skcipher_walk_done(&walk, err);
53 return err;
56 static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
58 struct skcipher_instance *inst;
59 struct crypto_alg *alg;
60 int err;
62 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
63 if (IS_ERR(inst))
64 return PTR_ERR(inst);
66 err = -EINVAL;
67 if (!is_power_of_2(alg->cra_blocksize))
68 goto out_free_inst;
70 inst->alg.encrypt = crypto_cbc_encrypt;
71 inst->alg.decrypt = crypto_cbc_decrypt;
73 err = skcipher_register_instance(tmpl, inst);
74 if (err)
75 goto out_free_inst;
76 goto out_put_alg;
78 out_free_inst:
79 inst->free(inst);
80 out_put_alg:
81 crypto_mod_put(alg);
82 return err;
85 static struct crypto_template crypto_cbc_tmpl = {
86 .name = "cbc",
87 .create = crypto_cbc_create,
88 .module = THIS_MODULE,
91 static int __init crypto_cbc_module_init(void)
93 return crypto_register_template(&crypto_cbc_tmpl);
96 static void __exit crypto_cbc_module_exit(void)
98 crypto_unregister_template(&crypto_cbc_tmpl);
101 module_init(crypto_cbc_module_init);
102 module_exit(crypto_cbc_module_exit);
104 MODULE_LICENSE("GPL");
105 MODULE_DESCRIPTION("CBC block cipher mode of operation");
106 MODULE_ALIAS_CRYPTO("cbc");