Blame view

crypto/ecb.c 2.39 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
2
3
4
5
  /*
   * ECB: Electronic CodeBook mode
   *
   * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
6
7
8
   */
  
  #include <crypto/algapi.h>
52e9368fe   Eric Biggers   crypto: ecb - con...
9
  #include <crypto/internal/skcipher.h>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
10
11
12
13
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
14

52e9368fe   Eric Biggers   crypto: ecb - con...
15
16
  static int crypto_ecb_crypt(struct skcipher_request *req,
  			    struct crypto_cipher *cipher,
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
17
18
  			    void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  {
52e9368fe   Eric Biggers   crypto: ecb - con...
19
20
  	const unsigned int bsize = crypto_cipher_blocksize(cipher);
  	struct skcipher_walk walk;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
21
22
  	unsigned int nbytes;
  	int err;
52e9368fe   Eric Biggers   crypto: ecb - con...
23
  	err = skcipher_walk_virt(&walk, req, false);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
24

52e9368fe   Eric Biggers   crypto: ecb - con...
25
26
27
  	while ((nbytes = walk.nbytes) != 0) {
  		const u8 *src = walk.src.virt.addr;
  		u8 *dst = walk.dst.virt.addr;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
28
29
  
  		do {
52e9368fe   Eric Biggers   crypto: ecb - con...
30
  			fn(crypto_cipher_tfm(cipher), dst, src);
5b37c19e3   Richard Hartmann   crypto: ecb - Fix...
31

52e9368fe   Eric Biggers   crypto: ecb - con...
32
33
  			src += bsize;
  			dst += bsize;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
34
  		} while ((nbytes -= bsize) >= bsize);
52e9368fe   Eric Biggers   crypto: ecb - con...
35
  		err = skcipher_walk_done(&walk, nbytes);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
36
37
38
39
  	}
  
  	return err;
  }
52e9368fe   Eric Biggers   crypto: ecb - con...
40
  static int crypto_ecb_encrypt(struct skcipher_request *req)
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
41
  {
52e9368fe   Eric Biggers   crypto: ecb - con...
42
43
  	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
44

52e9368fe   Eric Biggers   crypto: ecb - con...
45
46
  	return crypto_ecb_crypt(req, cipher,
  				crypto_cipher_alg(cipher)->cia_encrypt);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
47
  }
52e9368fe   Eric Biggers   crypto: ecb - con...
48
  static int crypto_ecb_decrypt(struct skcipher_request *req)
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
49
  {
52e9368fe   Eric Biggers   crypto: ecb - con...
50
51
  	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
52

52e9368fe   Eric Biggers   crypto: ecb - con...
53
54
  	return crypto_ecb_crypt(req, cipher,
  				crypto_cipher_alg(cipher)->cia_decrypt);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
55
  }
52e9368fe   Eric Biggers   crypto: ecb - con...
56
  static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
57
  {
52e9368fe   Eric Biggers   crypto: ecb - con...
58
  	struct skcipher_instance *inst;
ebc610e5b   Herbert Xu   [CRYPTO] template...
59
  	int err;
b3c16bfc6   Herbert Xu   crypto: skcipher ...
60
  	inst = skcipher_alloc_instance_simple(tmpl, tb);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
61
  	if (IS_ERR(inst))
52e9368fe   Eric Biggers   crypto: ecb - con...
62
  		return PTR_ERR(inst);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
63

52e9368fe   Eric Biggers   crypto: ecb - con...
64
  	inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
65

52e9368fe   Eric Biggers   crypto: ecb - con...
66
67
  	inst->alg.encrypt = crypto_ecb_encrypt;
  	inst->alg.decrypt = crypto_ecb_decrypt;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
68

52e9368fe   Eric Biggers   crypto: ecb - con...
69
70
71
  	err = skcipher_register_instance(tmpl, inst);
  	if (err)
  		inst->free(inst);
b3c16bfc6   Herbert Xu   crypto: skcipher ...
72

52e9368fe   Eric Biggers   crypto: ecb - con...
73
  	return err;
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
74
75
76
77
  }
  
  static struct crypto_template crypto_ecb_tmpl = {
  	.name = "ecb",
52e9368fe   Eric Biggers   crypto: ecb - con...
78
  	.create = crypto_ecb_create,
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
79
80
81
82
83
84
85
86
87
88
89
90
  	.module = THIS_MODULE,
  };
  
  static int __init crypto_ecb_module_init(void)
  {
  	return crypto_register_template(&crypto_ecb_tmpl);
  }
  
  static void __exit crypto_ecb_module_exit(void)
  {
  	crypto_unregister_template(&crypto_ecb_tmpl);
  }
c4741b230   Eric Biggers   crypto: run initc...
91
  subsys_initcall(crypto_ecb_module_init);
db131ef90   Herbert Xu   [CRYPTO] cipher: ...
92
93
94
  module_exit(crypto_ecb_module_exit);
  
  MODULE_LICENSE("GPL");
52e9368fe   Eric Biggers   crypto: ecb - con...
95
  MODULE_DESCRIPTION("ECB block cipher mode of operation");
4943ba16b   Kees Cook   crypto: include c...
96
  MODULE_ALIAS_CRYPTO("ecb");