Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * CBC: Cipher Block Chaining 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 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 13 | #include <crypto/internal/skcipher.h> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 17 | #include <linux/log2.h> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 18 | #include <linux/module.h> |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | |
| 21 | struct crypto_cbc_ctx { |
| 22 | struct crypto_cipher *child; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 23 | }; |
| 24 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 25 | static int crypto_cbc_setkey(struct crypto_skcipher *parent, const u8 *key, |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 26 | unsigned int keylen) |
| 27 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 28 | struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(parent); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 29 | struct crypto_cipher *child = ctx->child; |
| 30 | int err; |
| 31 | |
| 32 | crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 33 | crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 34 | CRYPTO_TFM_REQ_MASK); |
| 35 | err = crypto_cipher_setkey(child, key, keylen); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 36 | crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) & |
| 37 | CRYPTO_TFM_RES_MASK); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 38 | return err; |
| 39 | } |
| 40 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 41 | static inline int crypto_cbc_encrypt_segment( |
| 42 | struct skcipher_walk *walk, struct crypto_skcipher *tfm, |
| 43 | void (*fn)(struct crypto_skcipher *, const u8 *, u8 *)) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 44 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 45 | unsigned int bsize = crypto_skcipher_blocksize(tfm); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 46 | unsigned int nbytes = walk->nbytes; |
| 47 | u8 *src = walk->src.virt.addr; |
| 48 | u8 *dst = walk->dst.virt.addr; |
| 49 | u8 *iv = walk->iv; |
| 50 | |
| 51 | do { |
Herbert Xu | 3c7f076 | 2007-11-20 17:33:39 +0800 | [diff] [blame] | 52 | crypto_xor(iv, src, bsize); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 53 | fn(tfm, iv, dst); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 54 | memcpy(iv, dst, bsize); |
| 55 | |
| 56 | src += bsize; |
| 57 | dst += bsize; |
| 58 | } while ((nbytes -= bsize) >= bsize); |
| 59 | |
| 60 | return nbytes; |
| 61 | } |
| 62 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 63 | static inline int crypto_cbc_encrypt_inplace( |
| 64 | struct skcipher_walk *walk, struct crypto_skcipher *tfm, |
| 65 | void (*fn)(struct crypto_skcipher *, const u8 *, u8 *)) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 66 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 67 | unsigned int bsize = crypto_skcipher_blocksize(tfm); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 68 | unsigned int nbytes = walk->nbytes; |
| 69 | u8 *src = walk->src.virt.addr; |
| 70 | u8 *iv = walk->iv; |
| 71 | |
| 72 | do { |
Herbert Xu | 3c7f076 | 2007-11-20 17:33:39 +0800 | [diff] [blame] | 73 | crypto_xor(src, iv, bsize); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 74 | fn(tfm, src, src); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 75 | iv = src; |
| 76 | |
| 77 | src += bsize; |
| 78 | } while ((nbytes -= bsize) >= bsize); |
| 79 | |
| 80 | memcpy(walk->iv, iv, bsize); |
| 81 | |
| 82 | return nbytes; |
| 83 | } |
| 84 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 85 | static inline int crypto_cbc_encrypt_walk(struct skcipher_request *req, |
| 86 | void (*fn)(struct crypto_skcipher *, |
| 87 | const u8 *, u8 *)) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 88 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 89 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 90 | struct skcipher_walk walk; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 91 | int err; |
| 92 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 93 | err = skcipher_walk_virt(&walk, req, false); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 94 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 95 | while (walk.nbytes) { |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 96 | if (walk.src.virt.addr == walk.dst.virt.addr) |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 97 | err = crypto_cbc_encrypt_inplace(&walk, tfm, fn); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 98 | else |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 99 | err = crypto_cbc_encrypt_segment(&walk, tfm, fn); |
| 100 | err = skcipher_walk_done(&walk, err); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return err; |
| 104 | } |
| 105 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 106 | static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm, |
| 107 | const u8 *src, u8 *dst) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 108 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 109 | struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 110 | |
| 111 | crypto_cipher_encrypt_one(ctx->child, dst, src); |
| 112 | } |
| 113 | |
| 114 | static int crypto_cbc_encrypt(struct skcipher_request *req) |
| 115 | { |
| 116 | return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one); |
| 117 | } |
| 118 | |
| 119 | static inline int crypto_cbc_decrypt_segment( |
| 120 | struct skcipher_walk *walk, struct crypto_skcipher *tfm, |
| 121 | void (*fn)(struct crypto_skcipher *, const u8 *, u8 *)) |
| 122 | { |
| 123 | unsigned int bsize = crypto_skcipher_blocksize(tfm); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 124 | unsigned int nbytes = walk->nbytes; |
| 125 | u8 *src = walk->src.virt.addr; |
| 126 | u8 *dst = walk->dst.virt.addr; |
| 127 | u8 *iv = walk->iv; |
| 128 | |
| 129 | do { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 130 | fn(tfm, src, dst); |
Herbert Xu | 3c7f076 | 2007-11-20 17:33:39 +0800 | [diff] [blame] | 131 | crypto_xor(dst, iv, bsize); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 132 | iv = src; |
| 133 | |
| 134 | src += bsize; |
| 135 | dst += bsize; |
| 136 | } while ((nbytes -= bsize) >= bsize); |
| 137 | |
| 138 | memcpy(walk->iv, iv, bsize); |
| 139 | |
| 140 | return nbytes; |
| 141 | } |
| 142 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 143 | static inline int crypto_cbc_decrypt_inplace( |
| 144 | struct skcipher_walk *walk, struct crypto_skcipher *tfm, |
| 145 | void (*fn)(struct crypto_skcipher *, const u8 *, u8 *)) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 146 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 147 | unsigned int bsize = crypto_skcipher_blocksize(tfm); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 148 | unsigned int nbytes = walk->nbytes; |
| 149 | u8 *src = walk->src.virt.addr; |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 150 | u8 last_iv[bsize]; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 151 | |
| 152 | /* Start of the last block. */ |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 153 | src += nbytes - (nbytes & (bsize - 1)) - bsize; |
| 154 | memcpy(last_iv, src, bsize); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 155 | |
| 156 | for (;;) { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 157 | fn(tfm, src, src); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 158 | if ((nbytes -= bsize) < bsize) |
| 159 | break; |
Herbert Xu | 3c7f076 | 2007-11-20 17:33:39 +0800 | [diff] [blame] | 160 | crypto_xor(src, src - bsize, bsize); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 161 | src -= bsize; |
| 162 | } |
| 163 | |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 164 | crypto_xor(src, walk->iv, bsize); |
| 165 | memcpy(walk->iv, last_iv, bsize); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 166 | |
| 167 | return nbytes; |
| 168 | } |
| 169 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 170 | static inline int crypto_cbc_decrypt_blocks( |
| 171 | struct skcipher_walk *walk, struct crypto_skcipher *tfm, |
| 172 | void (*fn)(struct crypto_skcipher *, const u8 *, u8 *)) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 173 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 174 | if (walk->src.virt.addr == walk->dst.virt.addr) |
| 175 | return crypto_cbc_decrypt_inplace(walk, tfm, fn); |
| 176 | else |
| 177 | return crypto_cbc_decrypt_segment(walk, tfm, fn); |
| 178 | } |
| 179 | |
| 180 | static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm, |
| 181 | const u8 *src, u8 *dst) |
| 182 | { |
| 183 | struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 184 | |
| 185 | crypto_cipher_decrypt_one(ctx->child, dst, src); |
| 186 | } |
| 187 | |
| 188 | static int crypto_cbc_decrypt(struct skcipher_request *req) |
| 189 | { |
| 190 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 191 | struct skcipher_walk walk; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 192 | int err; |
| 193 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 194 | err = skcipher_walk_virt(&walk, req, false); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 195 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 196 | while (walk.nbytes) { |
| 197 | err = crypto_cbc_decrypt_blocks(&walk, tfm, |
| 198 | crypto_cbc_decrypt_one); |
| 199 | err = skcipher_walk_done(&walk, err); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return err; |
| 203 | } |
| 204 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 205 | static int crypto_cbc_init_tfm(struct crypto_skcipher *tfm) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 206 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 207 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
| 208 | struct crypto_spawn *spawn = skcipher_instance_ctx(inst); |
| 209 | struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 210 | struct crypto_cipher *cipher; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 211 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 212 | cipher = crypto_spawn_cipher(spawn); |
| 213 | if (IS_ERR(cipher)) |
| 214 | return PTR_ERR(cipher); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 215 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 216 | ctx->child = cipher; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 220 | static void crypto_cbc_exit_tfm(struct crypto_skcipher *tfm) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 221 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 222 | struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 223 | |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 224 | crypto_free_cipher(ctx->child); |
| 225 | } |
| 226 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 227 | static void crypto_cbc_free(struct skcipher_instance *inst) |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 228 | { |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 229 | crypto_drop_skcipher(skcipher_instance_ctx(inst)); |
| 230 | kfree(inst); |
| 231 | } |
| 232 | |
| 233 | static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 234 | { |
| 235 | struct skcipher_instance *inst; |
| 236 | struct crypto_spawn *spawn; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 237 | struct crypto_alg *alg; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 238 | int err; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 239 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 240 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 241 | if (err) |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 242 | return err; |
| 243 | |
| 244 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 245 | if (!inst) |
| 246 | return -ENOMEM; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 247 | |
| 248 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, |
| 249 | CRYPTO_ALG_TYPE_MASK); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 250 | err = PTR_ERR(alg); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 251 | if (IS_ERR(alg)) |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 252 | goto err_free_inst; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 253 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 254 | spawn = skcipher_instance_ctx(inst); |
| 255 | err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst), |
| 256 | CRYPTO_ALG_TYPE_MASK); |
| 257 | crypto_mod_put(alg); |
| 258 | if (err) |
| 259 | goto err_free_inst; |
| 260 | |
| 261 | err = crypto_inst_setname(skcipher_crypto_instance(inst), "cbc", alg); |
| 262 | if (err) |
| 263 | goto err_drop_spawn; |
| 264 | |
| 265 | err = -EINVAL; |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 266 | if (!is_power_of_2(alg->cra_blocksize)) |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 267 | goto err_drop_spawn; |
Herbert Xu | 50b6544 | 2007-11-20 17:36:00 +0800 | [diff] [blame] | 268 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 269 | inst->alg.base.cra_priority = alg->cra_priority; |
| 270 | inst->alg.base.cra_blocksize = alg->cra_blocksize; |
| 271 | inst->alg.base.cra_alignmask = alg->cra_alignmask; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 272 | |
Herbert Xu | 3c7f076 | 2007-11-20 17:33:39 +0800 | [diff] [blame] | 273 | /* We access the data as u32s when xoring. */ |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 274 | inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; |
Herbert Xu | 3c7f076 | 2007-11-20 17:33:39 +0800 | [diff] [blame] | 275 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 276 | inst->alg.ivsize = alg->cra_blocksize; |
| 277 | inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize; |
| 278 | inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 279 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 280 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_cbc_ctx); |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 281 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 282 | inst->alg.init = crypto_cbc_init_tfm; |
| 283 | inst->alg.exit = crypto_cbc_exit_tfm; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 284 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 285 | inst->alg.setkey = crypto_cbc_setkey; |
| 286 | inst->alg.encrypt = crypto_cbc_encrypt; |
| 287 | inst->alg.decrypt = crypto_cbc_decrypt; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 288 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 289 | inst->free = crypto_cbc_free; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 290 | |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 291 | err = skcipher_register_instance(tmpl, inst); |
| 292 | if (err) |
| 293 | goto err_drop_spawn; |
| 294 | |
| 295 | out: |
| 296 | return err; |
| 297 | |
| 298 | err_drop_spawn: |
| 299 | crypto_drop_spawn(spawn); |
| 300 | err_free_inst: |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 301 | kfree(inst); |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 302 | goto out; |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static struct crypto_template crypto_cbc_tmpl = { |
| 306 | .name = "cbc", |
Herbert Xu | 79c65d1 | 2016-11-22 20:08:39 +0800 | [diff] [blame^] | 307 | .create = crypto_cbc_create, |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 308 | .module = THIS_MODULE, |
| 309 | }; |
| 310 | |
| 311 | static int __init crypto_cbc_module_init(void) |
| 312 | { |
| 313 | return crypto_register_template(&crypto_cbc_tmpl); |
| 314 | } |
| 315 | |
| 316 | static void __exit crypto_cbc_module_exit(void) |
| 317 | { |
| 318 | crypto_unregister_template(&crypto_cbc_tmpl); |
| 319 | } |
| 320 | |
| 321 | module_init(crypto_cbc_module_init); |
| 322 | module_exit(crypto_cbc_module_exit); |
| 323 | |
| 324 | MODULE_LICENSE("GPL"); |
| 325 | MODULE_DESCRIPTION("CBC block cipher algorithm"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 326 | MODULE_ALIAS_CRYPTO("cbc"); |