Blame view

crypto/ablkcipher.c 3.07 KB
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   * Asynchronous block chaining cipher operations.
   * 
   * This is the asynchronous version of blkcipher.c indicating completion
   * via a callback.
   *
   * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the Free
   * Software Foundation; either version 2 of the License, or (at your option) 
   * any later version.
   *
   */
  
  #include <crypto/algapi.h>
  #include <linux/errno.h>
  #include <linux/init.h>
791b4d5f7   Herbert Xu   [CRYPTO] api: Add...
19
  #include <linux/kernel.h>
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
20
  #include <linux/module.h>
791b4d5f7   Herbert Xu   [CRYPTO] api: Add...
21
  #include <linux/slab.h>
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
22
  #include <linux/seq_file.h>
791b4d5f7   Herbert Xu   [CRYPTO] api: Add...
23
24
  static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  			    unsigned int keylen)
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  {
  	struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  	unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  	int ret;
  	u8 *buffer, *alignbuffer;
  	unsigned long absize;
  
  	absize = keylen + alignmask;
  	buffer = kmalloc(absize, GFP_ATOMIC);
  	if (!buffer)
  		return -ENOMEM;
  
  	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  	memcpy(alignbuffer, key, keylen);
  	ret = cipher->setkey(tfm, alignbuffer, keylen);
068171767   Sebastian Siewior   [CRYPTO] api: fix...
40
  	memset(alignbuffer, 0, keylen);
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
41
42
43
  	kfree(buffer);
  	return ret;
  }
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
44
45
46
47
  static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  		  unsigned int keylen)
  {
  	struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
48
  	unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
49
50
51
52
53
  
  	if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  		crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  		return -EINVAL;
  	}
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
54
55
  	if ((unsigned long)key & alignmask)
  		return setkey_unaligned(tfm, key, keylen);
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  	return cipher->setkey(tfm, key, keylen);
  }
  
  static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  					      u32 mask)
  {
  	return alg->cra_ctxsize;
  }
  
  static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  				      u32 mask)
  {
  	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  	struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  
  	if (alg->ivsize > PAGE_SIZE / 8)
  		return -EINVAL;
  
  	crt->setkey = setkey;
  	crt->encrypt = alg->encrypt;
  	crt->decrypt = alg->decrypt;
  	crt->ivsize = alg->ivsize;
  
  	return 0;
  }
  
  static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  	__attribute__ ((unused));
  static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  {
  	struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  
  	seq_printf(m, "type         : ablkcipher
  ");
  	seq_printf(m, "blocksize    : %u
  ", alg->cra_blocksize);
  	seq_printf(m, "min keysize  : %u
  ", ablkcipher->min_keysize);
  	seq_printf(m, "max keysize  : %u
  ", ablkcipher->max_keysize);
  	seq_printf(m, "ivsize       : %u
  ", ablkcipher->ivsize);
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
98
99
100
101
102
103
104
105
106
107
108
109
110
  }
  
  const struct crypto_type crypto_ablkcipher_type = {
  	.ctxsize = crypto_ablkcipher_ctxsize,
  	.init = crypto_init_ablkcipher_ops,
  #ifdef CONFIG_PROC_FS
  	.show = crypto_ablkcipher_show,
  #endif
  };
  EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Asynchronous block chaining cipher type");