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