Blame view
crypto/cbc.c
2.27 KB
2874c5fd2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
db131ef90
|
2 3 4 |
/* * CBC: Cipher Block Chaining mode * |
cc868d82a
|
5 |
* Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au> |
db131ef90
|
6 |
*/ |
e6c2e65c7
|
7 |
#include <crypto/algapi.h> |
cc868d82a
|
8 |
#include <crypto/cbc.h> |
79c65d179
|
9 |
#include <crypto/internal/skcipher.h> |
db131ef90
|
10 11 12 |
#include <linux/err.h> #include <linux/init.h> #include <linux/kernel.h> |
50b6544e1
|
13 |
#include <linux/log2.h> |
db131ef90
|
14 |
#include <linux/module.h> |
db131ef90
|
15 |
|
79c65d179
|
16 17 18 |
static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) { |
a5a84a9db
|
19 |
crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src); |
79c65d179
|
20 21 22 |
} static int crypto_cbc_encrypt(struct skcipher_request *req) |
db131ef90
|
23 |
{ |
79c65d179
|
24 25 |
return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one); } |
79c65d179
|
26 27 28 |
static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) { |
a5a84a9db
|
29 |
crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src); |
79c65d179
|
30 31 32 33 34 35 |
} static int crypto_cbc_decrypt(struct skcipher_request *req) { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct skcipher_walk walk; |
db131ef90
|
36 |
int err; |
79c65d179
|
37 |
err = skcipher_walk_virt(&walk, req, false); |
db131ef90
|
38 |
|
79c65d179
|
39 40 41 42 |
while (walk.nbytes) { err = crypto_cbc_decrypt_blocks(&walk, tfm, crypto_cbc_decrypt_one); err = skcipher_walk_done(&walk, err); |
db131ef90
|
43 44 45 46 |
} return err; } |
79c65d179
|
47 48 49 |
static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb) { struct skcipher_instance *inst; |
db131ef90
|
50 |
struct crypto_alg *alg; |
ebc610e5b
|
51 |
int err; |
a5a84a9db
|
52 53 54 |
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); if (IS_ERR(inst)) return PTR_ERR(inst); |
50b6544e1
|
55 |
|
79c65d179
|
56 57 |
err = -EINVAL; if (!is_power_of_2(alg->cra_blocksize)) |
a5a84a9db
|
58 |
goto out_free_inst; |
db131ef90
|
59 |
|
79c65d179
|
60 61 |
inst->alg.encrypt = crypto_cbc_encrypt; inst->alg.decrypt = crypto_cbc_decrypt; |
db131ef90
|
62 |
|
79c65d179
|
63 64 |
err = skcipher_register_instance(tmpl, inst); if (err) |
a5a84a9db
|
65 66 |
goto out_free_inst; goto out_put_alg; |
79c65d179
|
67 |
|
a5a84a9db
|
68 69 70 |
out_free_inst: inst->free(inst); out_put_alg: |
e5bde04cc
|
71 |
crypto_mod_put(alg); |
a5a84a9db
|
72 |
return err; |
db131ef90
|
73 74 75 76 |
} static struct crypto_template crypto_cbc_tmpl = { .name = "cbc", |
79c65d179
|
77 |
.create = crypto_cbc_create, |
db131ef90
|
78 79 80 81 82 83 84 85 86 87 88 89 |
.module = THIS_MODULE, }; static int __init crypto_cbc_module_init(void) { return crypto_register_template(&crypto_cbc_tmpl); } static void __exit crypto_cbc_module_exit(void) { crypto_unregister_template(&crypto_cbc_tmpl); } |
c4741b230
|
90 |
subsys_initcall(crypto_cbc_module_init); |
db131ef90
|
91 92 93 |
module_exit(crypto_cbc_module_exit); MODULE_LICENSE("GPL"); |
a5a84a9db
|
94 |
MODULE_DESCRIPTION("CBC block cipher mode of operation"); |
4943ba16b
|
95 |
MODULE_ALIAS_CRYPTO("cbc"); |