| .. | .. |
|---|
| 20 | 20 | */ |
|---|
| 21 | 21 | |
|---|
| 22 | 22 | #include <crypto/algapi.h> |
|---|
| 23 | +#include <crypto/internal/cipher.h> |
|---|
| 23 | 24 | #include <crypto/internal/skcipher.h> |
|---|
| 24 | 25 | #include <linux/err.h> |
|---|
| 25 | 26 | #include <linux/init.h> |
|---|
| 26 | 27 | #include <linux/kernel.h> |
|---|
| 27 | 28 | #include <linux/module.h> |
|---|
| 28 | | -#include <linux/slab.h> |
|---|
| 29 | 29 | #include <linux/string.h> |
|---|
| 30 | | -#include <linux/types.h> |
|---|
| 31 | | - |
|---|
| 32 | | -struct crypto_cfb_ctx { |
|---|
| 33 | | - struct crypto_cipher *child; |
|---|
| 34 | | -}; |
|---|
| 35 | 30 | |
|---|
| 36 | 31 | static unsigned int crypto_cfb_bsize(struct crypto_skcipher *tfm) |
|---|
| 37 | 32 | { |
|---|
| 38 | | - struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 39 | | - struct crypto_cipher *child = ctx->child; |
|---|
| 40 | | - |
|---|
| 41 | | - return crypto_cipher_blocksize(child); |
|---|
| 33 | + return crypto_cipher_blocksize(skcipher_cipher_simple(tfm)); |
|---|
| 42 | 34 | } |
|---|
| 43 | 35 | |
|---|
| 44 | 36 | static void crypto_cfb_encrypt_one(struct crypto_skcipher *tfm, |
|---|
| 45 | 37 | const u8 *src, u8 *dst) |
|---|
| 46 | 38 | { |
|---|
| 47 | | - struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 48 | | - |
|---|
| 49 | | - crypto_cipher_encrypt_one(ctx->child, dst, src); |
|---|
| 39 | + crypto_cipher_encrypt_one(skcipher_cipher_simple(tfm), dst, src); |
|---|
| 50 | 40 | } |
|---|
| 51 | 41 | |
|---|
| 52 | 42 | /* final encrypt and decrypt is the same */ |
|---|
| .. | .. |
|---|
| 186 | 176 | return crypto_cfb_decrypt_segment(walk, tfm); |
|---|
| 187 | 177 | } |
|---|
| 188 | 178 | |
|---|
| 189 | | -static int crypto_cfb_setkey(struct crypto_skcipher *parent, const u8 *key, |
|---|
| 190 | | - unsigned int keylen) |
|---|
| 191 | | -{ |
|---|
| 192 | | - struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(parent); |
|---|
| 193 | | - struct crypto_cipher *child = ctx->child; |
|---|
| 194 | | - int err; |
|---|
| 195 | | - |
|---|
| 196 | | - crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
|---|
| 197 | | - crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) & |
|---|
| 198 | | - CRYPTO_TFM_REQ_MASK); |
|---|
| 199 | | - err = crypto_cipher_setkey(child, key, keylen); |
|---|
| 200 | | - crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) & |
|---|
| 201 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 202 | | - return err; |
|---|
| 203 | | -} |
|---|
| 204 | | - |
|---|
| 205 | 179 | static int crypto_cfb_decrypt(struct skcipher_request *req) |
|---|
| 206 | 180 | { |
|---|
| 207 | 181 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| .. | .. |
|---|
| 224 | 198 | return err; |
|---|
| 225 | 199 | } |
|---|
| 226 | 200 | |
|---|
| 227 | | -static int crypto_cfb_init_tfm(struct crypto_skcipher *tfm) |
|---|
| 228 | | -{ |
|---|
| 229 | | - struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
|---|
| 230 | | - struct crypto_spawn *spawn = skcipher_instance_ctx(inst); |
|---|
| 231 | | - struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 232 | | - struct crypto_cipher *cipher; |
|---|
| 233 | | - |
|---|
| 234 | | - cipher = crypto_spawn_cipher(spawn); |
|---|
| 235 | | - if (IS_ERR(cipher)) |
|---|
| 236 | | - return PTR_ERR(cipher); |
|---|
| 237 | | - |
|---|
| 238 | | - ctx->child = cipher; |
|---|
| 239 | | - return 0; |
|---|
| 240 | | -} |
|---|
| 241 | | - |
|---|
| 242 | | -static void crypto_cfb_exit_tfm(struct crypto_skcipher *tfm) |
|---|
| 243 | | -{ |
|---|
| 244 | | - struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 245 | | - |
|---|
| 246 | | - crypto_free_cipher(ctx->child); |
|---|
| 247 | | -} |
|---|
| 248 | | - |
|---|
| 249 | | -static void crypto_cfb_free(struct skcipher_instance *inst) |
|---|
| 250 | | -{ |
|---|
| 251 | | - crypto_drop_skcipher(skcipher_instance_ctx(inst)); |
|---|
| 252 | | - kfree(inst); |
|---|
| 253 | | -} |
|---|
| 254 | | - |
|---|
| 255 | 201 | static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb) |
|---|
| 256 | 202 | { |
|---|
| 257 | 203 | struct skcipher_instance *inst; |
|---|
| 258 | | - struct crypto_attr_type *algt; |
|---|
| 259 | | - struct crypto_spawn *spawn; |
|---|
| 260 | 204 | struct crypto_alg *alg; |
|---|
| 261 | | - u32 mask; |
|---|
| 262 | 205 | int err; |
|---|
| 263 | 206 | |
|---|
| 264 | | - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER); |
|---|
| 265 | | - if (err) |
|---|
| 266 | | - return err; |
|---|
| 207 | + inst = skcipher_alloc_instance_simple(tmpl, tb); |
|---|
| 208 | + if (IS_ERR(inst)) |
|---|
| 209 | + return PTR_ERR(inst); |
|---|
| 267 | 210 | |
|---|
| 268 | | - inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
|---|
| 269 | | - if (!inst) |
|---|
| 270 | | - return -ENOMEM; |
|---|
| 211 | + alg = skcipher_ialg_simple(inst); |
|---|
| 271 | 212 | |
|---|
| 272 | | - algt = crypto_get_attr_type(tb); |
|---|
| 273 | | - err = PTR_ERR(algt); |
|---|
| 274 | | - if (IS_ERR(algt)) |
|---|
| 275 | | - goto err_free_inst; |
|---|
| 276 | | - |
|---|
| 277 | | - mask = CRYPTO_ALG_TYPE_MASK | |
|---|
| 278 | | - crypto_requires_off(algt->type, algt->mask, |
|---|
| 279 | | - CRYPTO_ALG_NEED_FALLBACK); |
|---|
| 280 | | - |
|---|
| 281 | | - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask); |
|---|
| 282 | | - err = PTR_ERR(alg); |
|---|
| 283 | | - if (IS_ERR(alg)) |
|---|
| 284 | | - goto err_free_inst; |
|---|
| 285 | | - |
|---|
| 286 | | - spawn = skcipher_instance_ctx(inst); |
|---|
| 287 | | - err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst), |
|---|
| 288 | | - CRYPTO_ALG_TYPE_MASK); |
|---|
| 289 | | - if (err) |
|---|
| 290 | | - goto err_put_alg; |
|---|
| 291 | | - |
|---|
| 292 | | - err = crypto_inst_setname(skcipher_crypto_instance(inst), "cfb", alg); |
|---|
| 293 | | - if (err) |
|---|
| 294 | | - goto err_drop_spawn; |
|---|
| 295 | | - |
|---|
| 296 | | - inst->alg.base.cra_priority = alg->cra_priority; |
|---|
| 297 | | - /* we're a stream cipher independend of the crypto cra_blocksize */ |
|---|
| 213 | + /* CFB mode is a stream cipher. */ |
|---|
| 298 | 214 | inst->alg.base.cra_blocksize = 1; |
|---|
| 299 | | - inst->alg.base.cra_alignmask = alg->cra_alignmask; |
|---|
| 300 | 215 | |
|---|
| 301 | 216 | /* |
|---|
| 302 | 217 | * To simplify the implementation, configure the skcipher walk to only |
|---|
| .. | .. |
|---|
| 304 | 219 | */ |
|---|
| 305 | 220 | inst->alg.chunksize = alg->cra_blocksize; |
|---|
| 306 | 221 | |
|---|
| 307 | | - inst->alg.ivsize = alg->cra_blocksize; |
|---|
| 308 | | - inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize; |
|---|
| 309 | | - inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize; |
|---|
| 310 | | - |
|---|
| 311 | | - inst->alg.base.cra_ctxsize = sizeof(struct crypto_cfb_ctx); |
|---|
| 312 | | - |
|---|
| 313 | | - inst->alg.init = crypto_cfb_init_tfm; |
|---|
| 314 | | - inst->alg.exit = crypto_cfb_exit_tfm; |
|---|
| 315 | | - |
|---|
| 316 | | - inst->alg.setkey = crypto_cfb_setkey; |
|---|
| 317 | 222 | inst->alg.encrypt = crypto_cfb_encrypt; |
|---|
| 318 | 223 | inst->alg.decrypt = crypto_cfb_decrypt; |
|---|
| 319 | 224 | |
|---|
| 320 | | - inst->free = crypto_cfb_free; |
|---|
| 321 | | - |
|---|
| 322 | 225 | err = skcipher_register_instance(tmpl, inst); |
|---|
| 323 | 226 | if (err) |
|---|
| 324 | | - goto err_drop_spawn; |
|---|
| 325 | | - crypto_mod_put(alg); |
|---|
| 227 | + inst->free(inst); |
|---|
| 326 | 228 | |
|---|
| 327 | | -out: |
|---|
| 328 | 229 | return err; |
|---|
| 329 | | - |
|---|
| 330 | | -err_drop_spawn: |
|---|
| 331 | | - crypto_drop_spawn(spawn); |
|---|
| 332 | | -err_put_alg: |
|---|
| 333 | | - crypto_mod_put(alg); |
|---|
| 334 | | -err_free_inst: |
|---|
| 335 | | - kfree(inst); |
|---|
| 336 | | - goto out; |
|---|
| 337 | 230 | } |
|---|
| 338 | 231 | |
|---|
| 339 | 232 | static struct crypto_template crypto_cfb_tmpl = { |
|---|
| .. | .. |
|---|
| 352 | 245 | crypto_unregister_template(&crypto_cfb_tmpl); |
|---|
| 353 | 246 | } |
|---|
| 354 | 247 | |
|---|
| 355 | | -module_init(crypto_cfb_module_init); |
|---|
| 248 | +subsys_initcall(crypto_cfb_module_init); |
|---|
| 356 | 249 | module_exit(crypto_cfb_module_exit); |
|---|
| 357 | 250 | |
|---|
| 358 | 251 | MODULE_LICENSE("GPL"); |
|---|
| 359 | | -MODULE_DESCRIPTION("CFB block cipher algorithm"); |
|---|
| 252 | +MODULE_DESCRIPTION("CFB block cipher mode of operation"); |
|---|
| 360 | 253 | MODULE_ALIAS_CRYPTO("cfb"); |
|---|
| 254 | +MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
|---|