Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 1 | /* |
| 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 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 16 | #include <crypto/internal/skcipher.h> |
| 17 | #include <linux/err.h> |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 18 | #include <linux/init.h> |
Herbert Xu | 791b4d5 | 2007-08-23 16:23:01 +0800 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 20 | #include <linux/module.h> |
Herbert Xu | 791b4d5 | 2007-08-23 16:23:01 +0800 | [diff] [blame] | 21 | #include <linux/slab.h> |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 22 | #include <linux/seq_file.h> |
| 23 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
Herbert Xu | 791b4d5 | 2007-08-23 16:23:01 +0800 | [diff] [blame] | 26 | static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key, |
| 27 | unsigned int keylen) |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 28 | { |
| 29 | struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm); |
| 30 | unsigned long alignmask = crypto_ablkcipher_alignmask(tfm); |
| 31 | int ret; |
| 32 | u8 *buffer, *alignbuffer; |
| 33 | unsigned long absize; |
| 34 | |
| 35 | absize = keylen + alignmask; |
| 36 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 37 | if (!buffer) |
| 38 | return -ENOMEM; |
| 39 | |
| 40 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 41 | memcpy(alignbuffer, key, keylen); |
| 42 | ret = cipher->setkey(tfm, alignbuffer, keylen); |
Sebastian Siewior | 0681717 | 2007-08-03 20:33:47 +0800 | [diff] [blame] | 43 | memset(alignbuffer, 0, keylen); |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 44 | kfree(buffer); |
| 45 | return ret; |
| 46 | } |
| 47 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 48 | static int setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 49 | unsigned int keylen) |
| 50 | { |
| 51 | struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm); |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 52 | unsigned long alignmask = crypto_ablkcipher_alignmask(tfm); |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 53 | |
| 54 | if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) { |
| 55 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 59 | if ((unsigned long)key & alignmask) |
| 60 | return setkey_unaligned(tfm, key, keylen); |
| 61 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 62 | return cipher->setkey(tfm, key, keylen); |
| 63 | } |
| 64 | |
| 65 | static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type, |
| 66 | u32 mask) |
| 67 | { |
| 68 | return alg->cra_ctxsize; |
| 69 | } |
| 70 | |
| 71 | static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type, |
| 72 | u32 mask) |
| 73 | { |
| 74 | struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher; |
| 75 | struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher; |
| 76 | |
| 77 | if (alg->ivsize > PAGE_SIZE / 8) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | crt->setkey = setkey; |
| 81 | crt->encrypt = alg->encrypt; |
| 82 | crt->decrypt = alg->decrypt; |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame^] | 83 | crt->base = __crypto_ablkcipher_cast(tfm); |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 84 | crt->ivsize = alg->ivsize; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg) |
| 90 | __attribute__ ((unused)); |
| 91 | static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg) |
| 92 | { |
| 93 | struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher; |
| 94 | |
| 95 | seq_printf(m, "type : ablkcipher\n"); |
| 96 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 97 | seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize); |
| 98 | seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize); |
| 99 | seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize); |
Herbert Xu | 23508e1 | 2007-11-27 21:33:24 +0800 | [diff] [blame] | 100 | seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>"); |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | const struct crypto_type crypto_ablkcipher_type = { |
| 104 | .ctxsize = crypto_ablkcipher_ctxsize, |
| 105 | .init = crypto_init_ablkcipher_ops, |
| 106 | #ifdef CONFIG_PROC_FS |
| 107 | .show = crypto_ablkcipher_show, |
| 108 | #endif |
| 109 | }; |
| 110 | EXPORT_SYMBOL_GPL(crypto_ablkcipher_type); |
| 111 | |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 112 | static int no_givdecrypt(struct skcipher_givcrypt_request *req) |
| 113 | { |
| 114 | return -ENOSYS; |
| 115 | } |
| 116 | |
| 117 | static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type, |
| 118 | u32 mask) |
| 119 | { |
| 120 | struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher; |
| 121 | struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher; |
| 122 | |
| 123 | if (alg->ivsize > PAGE_SIZE / 8) |
| 124 | return -EINVAL; |
| 125 | |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame^] | 126 | crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ? |
| 127 | alg->setkey : setkey; |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 128 | crt->encrypt = alg->encrypt; |
| 129 | crt->decrypt = alg->decrypt; |
| 130 | crt->givencrypt = alg->givencrypt; |
| 131 | crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt; |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame^] | 132 | crt->base = __crypto_ablkcipher_cast(tfm); |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 133 | crt->ivsize = alg->ivsize; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg) |
| 139 | __attribute__ ((unused)); |
| 140 | static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg) |
| 141 | { |
| 142 | struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher; |
| 143 | |
| 144 | seq_printf(m, "type : givcipher\n"); |
| 145 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 146 | seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize); |
| 147 | seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize); |
| 148 | seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize); |
Herbert Xu | 23508e1 | 2007-11-27 21:33:24 +0800 | [diff] [blame] | 149 | seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>"); |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | const struct crypto_type crypto_givcipher_type = { |
| 153 | .ctxsize = crypto_ablkcipher_ctxsize, |
| 154 | .init = crypto_init_givcipher_ops, |
| 155 | #ifdef CONFIG_PROC_FS |
| 156 | .show = crypto_givcipher_show, |
| 157 | #endif |
| 158 | }; |
| 159 | EXPORT_SYMBOL_GPL(crypto_givcipher_type); |
| 160 | |
Herbert Xu | ecfc432 | 2007-12-05 21:08:36 +1100 | [diff] [blame^] | 161 | const char *crypto_default_geniv(const struct crypto_alg *alg) |
| 162 | { |
| 163 | return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv"; |
| 164 | } |
| 165 | |
Herbert Xu | 378f4f5 | 2007-12-17 20:07:31 +0800 | [diff] [blame] | 166 | int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name, |
| 167 | u32 type, u32 mask) |
| 168 | { |
| 169 | struct crypto_alg *alg; |
| 170 | int err; |
| 171 | |
| 172 | type = crypto_skcipher_type(type); |
| 173 | mask = crypto_skcipher_mask(mask); |
| 174 | |
| 175 | alg = crypto_alg_mod_lookup(name, type, mask); |
| 176 | if (IS_ERR(alg)) |
| 177 | return PTR_ERR(alg); |
| 178 | |
| 179 | err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask); |
| 180 | crypto_mod_put(alg); |
| 181 | return err; |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(crypto_grab_skcipher); |
| 184 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 185 | MODULE_LICENSE("GPL"); |
| 186 | MODULE_DESCRIPTION("Asynchronous block chaining cipher type"); |