2006-09-21 05:44:08 +04:00
/*
* CBC : Cipher Block Chaining mode
*
2016-11-22 15:08:42 +03:00
* Copyright ( c ) 2006 - 2016 Herbert Xu < herbert @ gondor . apana . org . au >
2006-09-21 05:44:08 +04:00
*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation ; either version 2 of the License , or ( at your option )
* any later version .
*
*/
2016-11-22 15:08:42 +03:00
# include <crypto/cbc.h>
2016-11-22 15:08:39 +03:00
# include <crypto/internal/skcipher.h>
2006-09-21 05:44:08 +04:00
# include <linux/err.h>
# include <linux/init.h>
# include <linux/kernel.h>
2007-11-20 12:36:00 +03:00
# include <linux/log2.h>
2006-09-21 05:44:08 +04:00
# include <linux/module.h>
# include <linux/slab.h>
struct crypto_cbc_ctx {
struct crypto_cipher * child ;
} ;
2016-11-22 15:08:39 +03:00
static int crypto_cbc_setkey ( struct crypto_skcipher * parent , const u8 * key ,
2006-09-21 05:44:08 +04:00
unsigned int keylen )
{
2016-11-22 15:08:39 +03:00
struct crypto_cbc_ctx * ctx = crypto_skcipher_ctx ( parent ) ;
2006-09-21 05:44:08 +04:00
struct crypto_cipher * child = ctx - > child ;
int err ;
crypto_cipher_clear_flags ( child , CRYPTO_TFM_REQ_MASK ) ;
2016-11-22 15:08:39 +03:00
crypto_cipher_set_flags ( child , crypto_skcipher_get_flags ( parent ) &
2006-09-21 05:44:08 +04:00
CRYPTO_TFM_REQ_MASK ) ;
err = crypto_cipher_setkey ( child , key , keylen ) ;
2016-11-22 15:08:39 +03:00
crypto_skcipher_set_flags ( parent , crypto_cipher_get_flags ( child ) &
CRYPTO_TFM_RES_MASK ) ;
2006-09-21 05:44:08 +04:00
return err ;
}
2016-11-22 15:08:39 +03:00
static inline void crypto_cbc_encrypt_one ( struct crypto_skcipher * tfm ,
const u8 * src , u8 * dst )
{
struct crypto_cbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
crypto_cipher_encrypt_one ( ctx - > child , dst , src ) ;
}
static int crypto_cbc_encrypt ( struct skcipher_request * req )
2006-09-21 05:44:08 +04:00
{
2016-11-22 15:08:39 +03:00
return crypto_cbc_encrypt_walk ( req , crypto_cbc_encrypt_one ) ;
}
static inline void crypto_cbc_decrypt_one ( struct crypto_skcipher * tfm ,
const u8 * src , u8 * dst )
{
struct crypto_cbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
crypto_cipher_decrypt_one ( ctx - > child , dst , src ) ;
}
static int crypto_cbc_decrypt ( struct skcipher_request * req )
{
struct crypto_skcipher * tfm = crypto_skcipher_reqtfm ( req ) ;
struct skcipher_walk walk ;
2006-09-21 05:44:08 +04:00
int err ;
2016-11-22 15:08:39 +03:00
err = skcipher_walk_virt ( & walk , req , false ) ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
while ( walk . nbytes ) {
err = crypto_cbc_decrypt_blocks ( & walk , tfm ,
crypto_cbc_decrypt_one ) ;
err = skcipher_walk_done ( & walk , err ) ;
2006-09-21 05:44:08 +04:00
}
return err ;
}
2016-11-22 15:08:39 +03:00
static int crypto_cbc_init_tfm ( struct crypto_skcipher * tfm )
2006-09-21 05:44:08 +04:00
{
2016-11-22 15:08:39 +03:00
struct skcipher_instance * inst = skcipher_alg_instance ( tfm ) ;
struct crypto_spawn * spawn = skcipher_instance_ctx ( inst ) ;
struct crypto_cbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2006-12-17 02:05:58 +03:00
struct crypto_cipher * cipher ;
2006-09-21 05:44:08 +04:00
2006-12-17 02:05:58 +03:00
cipher = crypto_spawn_cipher ( spawn ) ;
if ( IS_ERR ( cipher ) )
return PTR_ERR ( cipher ) ;
2006-09-21 05:44:08 +04:00
2006-12-17 02:05:58 +03:00
ctx - > child = cipher ;
2006-09-21 05:44:08 +04:00
return 0 ;
}
2016-11-22 15:08:39 +03:00
static void crypto_cbc_exit_tfm ( struct crypto_skcipher * tfm )
2006-09-21 05:44:08 +04:00
{
2016-11-22 15:08:39 +03:00
struct crypto_cbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2006-09-21 05:44:08 +04:00
crypto_free_cipher ( ctx - > child ) ;
}
2016-11-22 15:08:39 +03:00
static void crypto_cbc_free ( struct skcipher_instance * inst )
2006-09-21 05:44:08 +04:00
{
2016-11-22 15:08:39 +03:00
crypto_drop_skcipher ( skcipher_instance_ctx ( inst ) ) ;
kfree ( inst ) ;
}
static int crypto_cbc_create ( struct crypto_template * tmpl , struct rtattr * * tb )
{
struct skcipher_instance * inst ;
struct crypto_spawn * spawn ;
2006-09-21 05:44:08 +04:00
struct crypto_alg * alg ;
2007-01-01 10:37:02 +03:00
int err ;
2016-11-22 15:08:39 +03:00
err = crypto_check_attr_type ( tb , CRYPTO_ALG_TYPE_SKCIPHER ) ;
2007-01-01 10:37:02 +03:00
if ( err )
2016-11-22 15:08:39 +03:00
return err ;
inst = kzalloc ( sizeof ( * inst ) + sizeof ( * spawn ) , GFP_KERNEL ) ;
if ( ! inst )
return - ENOMEM ;
2006-09-21 05:44:08 +04:00
2007-01-01 10:37:02 +03:00
alg = crypto_get_attr_alg ( tb , CRYPTO_ALG_TYPE_CIPHER ,
CRYPTO_ALG_TYPE_MASK ) ;
2016-11-22 15:08:39 +03:00
err = PTR_ERR ( alg ) ;
2006-09-21 05:44:08 +04:00
if ( IS_ERR ( alg ) )
2016-11-22 15:08:39 +03:00
goto err_free_inst ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
spawn = skcipher_instance_ctx ( inst ) ;
err = crypto_init_spawn ( spawn , alg , skcipher_crypto_instance ( inst ) ,
CRYPTO_ALG_TYPE_MASK ) ;
crypto_mod_put ( alg ) ;
if ( err )
goto err_free_inst ;
err = crypto_inst_setname ( skcipher_crypto_instance ( inst ) , " cbc " , alg ) ;
if ( err )
goto err_drop_spawn ;
2007-11-20 12:36:00 +03:00
2016-11-22 15:08:39 +03:00
err = - EINVAL ;
if ( ! is_power_of_2 ( alg - > cra_blocksize ) )
goto err_drop_spawn ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
inst - > alg . base . cra_priority = alg - > cra_priority ;
inst - > alg . base . cra_blocksize = alg - > cra_blocksize ;
inst - > alg . base . cra_alignmask = alg - > cra_alignmask ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
inst - > alg . ivsize = alg - > cra_blocksize ;
inst - > alg . min_keysize = alg - > cra_cipher . cia_min_keysize ;
inst - > alg . max_keysize = alg - > cra_cipher . cia_max_keysize ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
inst - > alg . base . cra_ctxsize = sizeof ( struct crypto_cbc_ctx ) ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
inst - > alg . init = crypto_cbc_init_tfm ;
inst - > alg . exit = crypto_cbc_exit_tfm ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
inst - > alg . setkey = crypto_cbc_setkey ;
inst - > alg . encrypt = crypto_cbc_encrypt ;
inst - > alg . decrypt = crypto_cbc_decrypt ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
inst - > free = crypto_cbc_free ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
err = skcipher_register_instance ( tmpl , inst ) ;
if ( err )
goto err_drop_spawn ;
out :
return err ;
err_drop_spawn :
crypto_drop_spawn ( spawn ) ;
err_free_inst :
2006-09-21 05:44:08 +04:00
kfree ( inst ) ;
2016-11-22 15:08:39 +03:00
goto out ;
2006-09-21 05:44:08 +04:00
}
static struct crypto_template crypto_cbc_tmpl = {
. name = " cbc " ,
2016-11-22 15:08:39 +03:00
. create = crypto_cbc_create ,
2006-09-21 05:44:08 +04:00
. module = THIS_MODULE ,
} ;
static int __init crypto_cbc_module_init ( void )
{
return crypto_register_template ( & crypto_cbc_tmpl ) ;
}
static void __exit crypto_cbc_module_exit ( void )
{
crypto_unregister_template ( & crypto_cbc_tmpl ) ;
}
module_init ( crypto_cbc_module_init ) ;
module_exit ( crypto_cbc_module_exit ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_DESCRIPTION ( " CBC block cipher algorithm " ) ;
2014-11-25 03:32:38 +03:00
MODULE_ALIAS_CRYPTO ( " cbc " ) ;