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 .
*
*/
2017-02-27 15:38:25 +03:00
# include <crypto/algapi.h>
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>
2016-11-22 15:08:39 +03:00
static inline void crypto_cbc_encrypt_one ( struct crypto_skcipher * tfm ,
const u8 * src , u8 * dst )
{
2019-01-04 07:16:15 +03:00
crypto_cipher_encrypt_one ( skcipher_cipher_simple ( tfm ) , dst , src ) ;
2016-11-22 15:08:39 +03:00
}
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 )
{
2019-01-04 07:16:15 +03:00
crypto_cipher_decrypt_one ( skcipher_cipher_simple ( tfm ) , dst , src ) ;
2016-11-22 15:08:39 +03:00
}
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_create ( struct crypto_template * tmpl , struct rtattr * * tb )
{
struct skcipher_instance * inst ;
2006-09-21 05:44:08 +04:00
struct crypto_alg * alg ;
2007-01-01 10:37:02 +03:00
int err ;
2019-01-04 07:16:15 +03:00
inst = skcipher_alloc_instance_simple ( tmpl , tb , & alg ) ;
if ( IS_ERR ( inst ) )
return PTR_ERR ( inst ) ;
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 ) )
2019-01-04 07:16:15 +03:00
goto out_free_inst ;
2006-09-21 05:44:08 +04:00
2016-11-22 15:08:39 +03:00
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
err = skcipher_register_instance ( tmpl , inst ) ;
if ( err )
2019-01-04 07:16:15 +03:00
goto out_free_inst ;
goto out_put_alg ;
2016-11-22 15:08:39 +03:00
2019-01-04 07:16:15 +03:00
out_free_inst :
inst - > free ( inst ) ;
out_put_alg :
2018-11-22 13:00:16 +03:00
crypto_mod_put ( alg ) ;
2019-01-04 07:16:15 +03:00
return err ;
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 ) ;
}
2019-04-12 07:57:42 +03:00
subsys_initcall ( crypto_cbc_module_init ) ;
2006-09-21 05:44:08 +04:00
module_exit ( crypto_cbc_module_exit ) ;
MODULE_LICENSE ( " GPL " ) ;
2019-01-04 07:16:15 +03:00
MODULE_DESCRIPTION ( " CBC block cipher mode of operation " ) ;
2014-11-25 03:32:38 +03:00
MODULE_ALIAS_CRYPTO ( " cbc " ) ;