Blame view

crypto/cbc.c 2.27 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
2
3
4
  /*
   * CBC: Cipher Block Chaining mode
   *
cc868d82a   Herbert Xu   crypto: cbc - Exp...
5
   * Copyright (c) 2006-2016 Herbert Xu <herbert@gondor.apana.org.au>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
6
   */
e6c2e65c7   Marcelo Cerri   crypto: cbc - Pro...
7
  #include <crypto/algapi.h>
cc868d82a   Herbert Xu   crypto: cbc - Exp...
8
  #include <crypto/cbc.h>
79c65d179   Herbert Xu   crypto: cbc - Con...
9
  #include <crypto/internal/skcipher.h>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
10
11
12
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
50b6544e1   Herbert Xu   [CRYPTO] cbc: Req...
13
  #include <linux/log2.h>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
14
  #include <linux/module.h>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
15

79c65d179   Herbert Xu   crypto: cbc - Con...
16
17
18
  static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
  					  const u8 *src, u8 *dst)
  {
a5a84a9db   Eric Biggers   crypto: cbc - con...
19
  	crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src);
79c65d179   Herbert Xu   crypto: cbc - Con...
20
21
22
  }
  
  static int crypto_cbc_encrypt(struct skcipher_request *req)
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
23
  {
79c65d179   Herbert Xu   crypto: cbc - Con...
24
25
  	return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
  }
79c65d179   Herbert Xu   crypto: cbc - Con...
26
27
28
  static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
  					  const u8 *src, u8 *dst)
  {
a5a84a9db   Eric Biggers   crypto: cbc - con...
29
  	crypto_cipher_decrypt_one(skcipher_cipher_simple(tfm), dst, src);
79c65d179   Herbert Xu   crypto: cbc - Con...
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   Herbert Xu   [CRYPTO] cipher: ...
36
  	int err;
79c65d179   Herbert Xu   crypto: cbc - Con...
37
  	err = skcipher_walk_virt(&walk, req, false);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
38

79c65d179   Herbert Xu   crypto: cbc - Con...
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   Herbert Xu   [CRYPTO] cipher: ...
43
44
45
46
  	}
  
  	return err;
  }
79c65d179   Herbert Xu   crypto: cbc - Con...
47
48
49
  static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  {
  	struct skcipher_instance *inst;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
50
  	struct crypto_alg *alg;
ebc610e5b   Herbert Xu   [CRYPTO] template...
51
  	int err;
a5a84a9db   Eric Biggers   crypto: cbc - con...
52
53
54
  	inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
  	if (IS_ERR(inst))
  		return PTR_ERR(inst);
50b6544e1   Herbert Xu   [CRYPTO] cbc: Req...
55

79c65d179   Herbert Xu   crypto: cbc - Con...
56
57
  	err = -EINVAL;
  	if (!is_power_of_2(alg->cra_blocksize))
a5a84a9db   Eric Biggers   crypto: cbc - con...
58
  		goto out_free_inst;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
59

79c65d179   Herbert Xu   crypto: cbc - Con...
60
61
  	inst->alg.encrypt = crypto_cbc_encrypt;
  	inst->alg.decrypt = crypto_cbc_decrypt;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
62

79c65d179   Herbert Xu   crypto: cbc - Con...
63
64
  	err = skcipher_register_instance(tmpl, inst);
  	if (err)
a5a84a9db   Eric Biggers   crypto: cbc - con...
65
66
  		goto out_free_inst;
  	goto out_put_alg;
79c65d179   Herbert Xu   crypto: cbc - Con...
67

a5a84a9db   Eric Biggers   crypto: cbc - con...
68
69
70
  out_free_inst:
  	inst->free(inst);
  out_put_alg:
e5bde04cc   Pan Bian   crypto: do not fr...
71
  	crypto_mod_put(alg);
a5a84a9db   Eric Biggers   crypto: cbc - con...
72
  	return err;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
73
74
75
76
  }
  
  static struct crypto_template crypto_cbc_tmpl = {
  	.name = "cbc",
79c65d179   Herbert Xu   crypto: cbc - Con...
77
  	.create = crypto_cbc_create,
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
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   Eric Biggers   crypto: run initc...
90
  subsys_initcall(crypto_cbc_module_init);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
91
92
93
  module_exit(crypto_cbc_module_exit);
  
  MODULE_LICENSE("GPL");
a5a84a9db   Eric Biggers   crypto: cbc - con...
94
  MODULE_DESCRIPTION("CBC block cipher mode of operation");
4943ba16b   Kees Cook   crypto: include c...
95
  MODULE_ALIAS_CRYPTO("cbc");