Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * ECB: Electronic CodeBook mode |
| 3 | * |
| 4 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <crypto/algapi.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/scatterlist.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
| 21 | struct crypto_ecb_ctx { |
| 22 | struct crypto_cipher *child; |
| 23 | }; |
| 24 | |
| 25 | static int crypto_ecb_setkey(struct crypto_tfm *parent, const u8 *key, |
| 26 | unsigned int keylen) |
| 27 | { |
| 28 | struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(parent); |
| 29 | struct crypto_cipher *child = ctx->child; |
| 30 | int err; |
| 31 | |
| 32 | crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 33 | crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & |
| 34 | CRYPTO_TFM_REQ_MASK); |
| 35 | err = crypto_cipher_setkey(child, key, keylen); |
| 36 | crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & |
| 37 | CRYPTO_TFM_RES_MASK); |
| 38 | return err; |
| 39 | } |
| 40 | |
| 41 | static int crypto_ecb_crypt(struct blkcipher_desc *desc, |
| 42 | struct blkcipher_walk *walk, |
| 43 | struct crypto_cipher *tfm, |
| 44 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *)) |
| 45 | { |
| 46 | int bsize = crypto_cipher_blocksize(tfm); |
| 47 | unsigned int nbytes; |
| 48 | int err; |
| 49 | |
| 50 | err = blkcipher_walk_virt(desc, walk); |
| 51 | |
| 52 | while ((nbytes = walk->nbytes)) { |
| 53 | u8 *wsrc = walk->src.virt.addr; |
| 54 | u8 *wdst = walk->dst.virt.addr; |
| 55 | |
| 56 | do { |
| 57 | fn(crypto_cipher_tfm(tfm), wdst, wsrc); |
| 58 | |
| 59 | wsrc += bsize; |
| 60 | wdst += bsize; |
| 61 | } while ((nbytes -= bsize) >= bsize); |
| 62 | |
| 63 | err = blkcipher_walk_done(desc, walk, nbytes); |
| 64 | } |
| 65 | |
| 66 | return err; |
| 67 | } |
| 68 | |
| 69 | static int crypto_ecb_encrypt(struct blkcipher_desc *desc, |
| 70 | struct scatterlist *dst, struct scatterlist *src, |
| 71 | unsigned int nbytes) |
| 72 | { |
| 73 | struct blkcipher_walk walk; |
| 74 | struct crypto_blkcipher *tfm = desc->tfm; |
| 75 | struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm); |
| 76 | struct crypto_cipher *child = ctx->child; |
| 77 | |
| 78 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 79 | return crypto_ecb_crypt(desc, &walk, child, |
| 80 | crypto_cipher_alg(child)->cia_encrypt); |
| 81 | } |
| 82 | |
| 83 | static int crypto_ecb_decrypt(struct blkcipher_desc *desc, |
| 84 | struct scatterlist *dst, struct scatterlist *src, |
| 85 | unsigned int nbytes) |
| 86 | { |
| 87 | struct blkcipher_walk walk; |
| 88 | struct crypto_blkcipher *tfm = desc->tfm; |
| 89 | struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm); |
| 90 | struct crypto_cipher *child = ctx->child; |
| 91 | |
| 92 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 93 | return crypto_ecb_crypt(desc, &walk, child, |
| 94 | crypto_cipher_alg(child)->cia_decrypt); |
| 95 | } |
| 96 | |
| 97 | static int crypto_ecb_init_tfm(struct crypto_tfm *tfm) |
| 98 | { |
| 99 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 100 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); |
| 101 | struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 102 | struct crypto_cipher *cipher; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 103 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 104 | cipher = crypto_spawn_cipher(spawn); |
| 105 | if (IS_ERR(cipher)) |
| 106 | return PTR_ERR(cipher); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 107 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 108 | ctx->child = cipher; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm) |
| 113 | { |
| 114 | struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm); |
| 115 | crypto_free_cipher(ctx->child); |
| 116 | } |
| 117 | |
| 118 | static struct crypto_instance *crypto_ecb_alloc(void *param, unsigned int len) |
| 119 | { |
| 120 | struct crypto_instance *inst; |
| 121 | struct crypto_alg *alg; |
| 122 | |
| 123 | alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, |
| 124 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
| 125 | if (IS_ERR(alg)) |
| 126 | return ERR_PTR(PTR_ERR(alg)); |
| 127 | |
| 128 | inst = crypto_alloc_instance("ecb", alg); |
| 129 | if (IS_ERR(inst)) |
| 130 | goto out_put_alg; |
| 131 | |
| 132 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; |
| 133 | inst->alg.cra_priority = alg->cra_priority; |
| 134 | inst->alg.cra_blocksize = alg->cra_blocksize; |
| 135 | inst->alg.cra_alignmask = alg->cra_alignmask; |
| 136 | inst->alg.cra_type = &crypto_blkcipher_type; |
| 137 | |
| 138 | inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize; |
| 139 | inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize; |
| 140 | |
| 141 | inst->alg.cra_ctxsize = sizeof(struct crypto_ecb_ctx); |
| 142 | |
| 143 | inst->alg.cra_init = crypto_ecb_init_tfm; |
| 144 | inst->alg.cra_exit = crypto_ecb_exit_tfm; |
| 145 | |
| 146 | inst->alg.cra_blkcipher.setkey = crypto_ecb_setkey; |
| 147 | inst->alg.cra_blkcipher.encrypt = crypto_ecb_encrypt; |
| 148 | inst->alg.cra_blkcipher.decrypt = crypto_ecb_decrypt; |
| 149 | |
| 150 | out_put_alg: |
| 151 | crypto_mod_put(alg); |
| 152 | return inst; |
| 153 | } |
| 154 | |
| 155 | static void crypto_ecb_free(struct crypto_instance *inst) |
| 156 | { |
| 157 | crypto_drop_spawn(crypto_instance_ctx(inst)); |
| 158 | kfree(inst); |
| 159 | } |
| 160 | |
| 161 | static struct crypto_template crypto_ecb_tmpl = { |
| 162 | .name = "ecb", |
| 163 | .alloc = crypto_ecb_alloc, |
| 164 | .free = crypto_ecb_free, |
| 165 | .module = THIS_MODULE, |
| 166 | }; |
| 167 | |
| 168 | static int __init crypto_ecb_module_init(void) |
| 169 | { |
| 170 | return crypto_register_template(&crypto_ecb_tmpl); |
| 171 | } |
| 172 | |
| 173 | static void __exit crypto_ecb_module_exit(void) |
| 174 | { |
| 175 | crypto_unregister_template(&crypto_ecb_tmpl); |
| 176 | } |
| 177 | |
| 178 | module_init(crypto_ecb_module_init); |
| 179 | module_exit(crypto_ecb_module_exit); |
| 180 | |
| 181 | MODULE_LICENSE("GPL"); |
| 182 | MODULE_DESCRIPTION("ECB block cipher algorithm"); |