2006-12-16 04:09:02 +03:00
/*
* PCBC : Propagating Cipher Block Chaining mode
*
* Copyright ( C ) 2006 Red Hat , Inc . All Rights Reserved .
* Written by David Howells ( dhowells @ redhat . com )
*
* Derived from cbc . c
* - Copyright ( c ) 2006 Herbert Xu < herbert @ gondor . apana . org . au >
*
* 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 .
*
*/
2018-04-09 16:54:47 +03:00
# include <crypto/algapi.h>
2016-11-22 15:08:27 +03:00
# include <crypto/internal/skcipher.h>
2006-12-16 04:09:02 +03:00
# include <linux/err.h>
# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/slab.h>
2016-12-31 18:56:23 +03:00
# include <linux/compiler.h>
2006-12-16 04:09:02 +03:00
struct crypto_pcbc_ctx {
struct crypto_cipher * child ;
} ;
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_setkey ( struct crypto_skcipher * parent , const u8 * key ,
2006-12-16 04:09:02 +03:00
unsigned int keylen )
{
2016-11-22 15:08:27 +03:00
struct crypto_pcbc_ctx * ctx = crypto_skcipher_ctx ( parent ) ;
2006-12-16 04:09:02 +03:00
struct crypto_cipher * child = ctx - > child ;
int err ;
crypto_cipher_clear_flags ( child , CRYPTO_TFM_REQ_MASK ) ;
2016-11-22 15:08:27 +03:00
crypto_cipher_set_flags ( child , crypto_skcipher_get_flags ( parent ) &
CRYPTO_TFM_REQ_MASK ) ;
2006-12-16 04:09:02 +03:00
err = crypto_cipher_setkey ( child , key , keylen ) ;
2016-11-22 15:08:27 +03:00
crypto_skcipher_set_flags ( parent , crypto_cipher_get_flags ( child ) &
CRYPTO_TFM_RES_MASK ) ;
2006-12-16 04:09:02 +03:00
return err ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_encrypt_segment ( struct skcipher_request * req ,
struct skcipher_walk * walk ,
2007-11-20 12:49:49 +03:00
struct crypto_cipher * tfm )
2006-12-16 04:09:02 +03:00
{
int bsize = crypto_cipher_blocksize ( tfm ) ;
unsigned int nbytes = walk - > nbytes ;
u8 * src = walk - > src . virt . addr ;
u8 * dst = walk - > dst . virt . addr ;
u8 * iv = walk - > iv ;
do {
2007-11-20 12:49:49 +03:00
crypto_xor ( iv , src , bsize ) ;
2016-11-22 15:08:27 +03:00
crypto_cipher_encrypt_one ( tfm , dst , iv ) ;
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 13:28:04 +03:00
crypto_xor_cpy ( iv , dst , src , bsize ) ;
2006-12-16 04:09:02 +03:00
src + = bsize ;
dst + = bsize ;
} while ( ( nbytes - = bsize ) > = bsize ) ;
return nbytes ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_encrypt_inplace ( struct skcipher_request * req ,
struct skcipher_walk * walk ,
2007-11-20 12:49:49 +03:00
struct crypto_cipher * tfm )
2006-12-16 04:09:02 +03:00
{
int bsize = crypto_cipher_blocksize ( tfm ) ;
unsigned int nbytes = walk - > nbytes ;
u8 * src = walk - > src . virt . addr ;
u8 * iv = walk - > iv ;
2018-04-09 16:54:47 +03:00
u8 tmpbuf [ MAX_CIPHER_BLOCKSIZE ] ;
2006-12-16 04:09:02 +03:00
do {
memcpy ( tmpbuf , src , bsize ) ;
2007-11-20 12:49:49 +03:00
crypto_xor ( iv , src , bsize ) ;
2016-11-22 15:08:27 +03:00
crypto_cipher_encrypt_one ( tfm , src , iv ) ;
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 13:28:04 +03:00
crypto_xor_cpy ( iv , tmpbuf , src , bsize ) ;
2006-12-16 04:09:02 +03:00
src + = bsize ;
} while ( ( nbytes - = bsize ) > = bsize ) ;
memcpy ( walk - > iv , iv , bsize ) ;
return nbytes ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_encrypt ( struct skcipher_request * req )
2006-12-16 04:09:02 +03:00
{
2016-11-22 15:08:27 +03:00
struct crypto_skcipher * tfm = crypto_skcipher_reqtfm ( req ) ;
struct crypto_pcbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2006-12-16 04:09:02 +03:00
struct crypto_cipher * child = ctx - > child ;
2016-11-22 15:08:27 +03:00
struct skcipher_walk walk ;
unsigned int nbytes ;
2006-12-16 04:09:02 +03:00
int err ;
2016-11-22 15:08:27 +03:00
err = skcipher_walk_virt ( & walk , req , false ) ;
2006-12-16 04:09:02 +03:00
while ( ( nbytes = walk . nbytes ) ) {
if ( walk . src . virt . addr = = walk . dst . virt . addr )
2016-11-22 15:08:27 +03:00
nbytes = crypto_pcbc_encrypt_inplace ( req , & walk ,
2007-11-20 12:49:49 +03:00
child ) ;
2006-12-16 04:09:02 +03:00
else
2016-11-22 15:08:27 +03:00
nbytes = crypto_pcbc_encrypt_segment ( req , & walk ,
2007-11-20 12:49:49 +03:00
child ) ;
2016-11-22 15:08:27 +03:00
err = skcipher_walk_done ( & walk , nbytes ) ;
2006-12-16 04:09:02 +03:00
}
return err ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_decrypt_segment ( struct skcipher_request * req ,
struct skcipher_walk * walk ,
2007-11-20 12:49:49 +03:00
struct crypto_cipher * tfm )
2006-12-16 04:09:02 +03:00
{
int bsize = crypto_cipher_blocksize ( tfm ) ;
unsigned int nbytes = walk - > nbytes ;
u8 * src = walk - > src . virt . addr ;
u8 * dst = walk - > dst . virt . addr ;
u8 * iv = walk - > iv ;
do {
2016-11-22 15:08:27 +03:00
crypto_cipher_decrypt_one ( tfm , dst , src ) ;
2007-11-20 12:49:49 +03:00
crypto_xor ( dst , iv , bsize ) ;
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 13:28:04 +03:00
crypto_xor_cpy ( iv , dst , src , bsize ) ;
2006-12-16 04:09:02 +03:00
src + = bsize ;
dst + = bsize ;
} while ( ( nbytes - = bsize ) > = bsize ) ;
memcpy ( walk - > iv , iv , bsize ) ;
return nbytes ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_decrypt_inplace ( struct skcipher_request * req ,
struct skcipher_walk * walk ,
2007-11-20 12:49:49 +03:00
struct crypto_cipher * tfm )
2006-12-16 04:09:02 +03:00
{
int bsize = crypto_cipher_blocksize ( tfm ) ;
unsigned int nbytes = walk - > nbytes ;
u8 * src = walk - > src . virt . addr ;
u8 * iv = walk - > iv ;
2018-04-09 16:54:47 +03:00
u8 tmpbuf [ MAX_CIPHER_BLOCKSIZE ] __aligned ( __alignof__ ( u32 ) ) ;
2006-12-16 04:09:02 +03:00
do {
memcpy ( tmpbuf , src , bsize ) ;
2016-11-22 15:08:27 +03:00
crypto_cipher_decrypt_one ( tfm , src , src ) ;
2007-11-20 12:49:49 +03:00
crypto_xor ( src , iv , bsize ) ;
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 13:28:04 +03:00
crypto_xor_cpy ( iv , src , tmpbuf , bsize ) ;
2006-12-16 04:09:02 +03:00
src + = bsize ;
} while ( ( nbytes - = bsize ) > = bsize ) ;
memcpy ( walk - > iv , iv , bsize ) ;
return nbytes ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_decrypt ( struct skcipher_request * req )
2006-12-16 04:09:02 +03:00
{
2016-11-22 15:08:27 +03:00
struct crypto_skcipher * tfm = crypto_skcipher_reqtfm ( req ) ;
struct crypto_pcbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2006-12-16 04:09:02 +03:00
struct crypto_cipher * child = ctx - > child ;
2016-11-22 15:08:27 +03:00
struct skcipher_walk walk ;
unsigned int nbytes ;
2006-12-16 04:09:02 +03:00
int err ;
2016-11-22 15:08:27 +03:00
err = skcipher_walk_virt ( & walk , req , false ) ;
2006-12-16 04:09:02 +03:00
while ( ( nbytes = walk . nbytes ) ) {
if ( walk . src . virt . addr = = walk . dst . virt . addr )
2016-11-22 15:08:27 +03:00
nbytes = crypto_pcbc_decrypt_inplace ( req , & walk ,
2007-11-20 12:49:49 +03:00
child ) ;
2006-12-16 04:09:02 +03:00
else
2016-11-22 15:08:27 +03:00
nbytes = crypto_pcbc_decrypt_segment ( req , & walk ,
2007-11-20 12:49:49 +03:00
child ) ;
2016-11-22 15:08:27 +03:00
err = skcipher_walk_done ( & walk , nbytes ) ;
2006-12-16 04:09:02 +03:00
}
return err ;
}
2016-11-22 15:08:27 +03:00
static int crypto_pcbc_init_tfm ( struct crypto_skcipher * tfm )
2006-12-16 04:09:02 +03:00
{
2016-11-22 15:08:27 +03:00
struct skcipher_instance * inst = skcipher_alg_instance ( tfm ) ;
struct crypto_spawn * spawn = skcipher_instance_ctx ( inst ) ;
struct crypto_pcbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2006-12-17 02:05:58 +03:00
struct crypto_cipher * cipher ;
2006-12-16 04:09:02 +03:00
2006-12-17 02:05:58 +03:00
cipher = crypto_spawn_cipher ( spawn ) ;
if ( IS_ERR ( cipher ) )
return PTR_ERR ( cipher ) ;
2006-12-16 04:09:02 +03:00
2006-12-17 02:05:58 +03:00
ctx - > child = cipher ;
2006-12-16 04:09:02 +03:00
return 0 ;
}
2016-11-22 15:08:27 +03:00
static void crypto_pcbc_exit_tfm ( struct crypto_skcipher * tfm )
2006-12-16 04:09:02 +03:00
{
2016-11-22 15:08:27 +03:00
struct crypto_pcbc_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2006-12-16 04:09:02 +03:00
crypto_free_cipher ( ctx - > child ) ;
}
2016-11-22 15:08:27 +03:00
static void crypto_pcbc_free ( struct skcipher_instance * inst )
{
crypto_drop_skcipher ( skcipher_instance_ctx ( inst ) ) ;
kfree ( inst ) ;
}
static int crypto_pcbc_create ( struct crypto_template * tmpl , struct rtattr * * tb )
2006-12-16 04:09:02 +03:00
{
2016-11-22 15:08:27 +03:00
struct skcipher_instance * inst ;
struct crypto_attr_type * algt ;
struct crypto_spawn * spawn ;
2006-12-16 04:09:02 +03:00
struct crypto_alg * alg ;
2007-01-01 10:37:02 +03:00
int err ;
2016-11-22 15:08:27 +03:00
algt = crypto_get_attr_type ( tb ) ;
if ( IS_ERR ( algt ) )
return PTR_ERR ( algt ) ;
if ( ( ( algt - > type ^ CRYPTO_ALG_TYPE_SKCIPHER ) & algt - > mask ) &
~ CRYPTO_ALG_INTERNAL )
return - EINVAL ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
inst = kzalloc ( sizeof ( * inst ) + sizeof ( * spawn ) , GFP_KERNEL ) ;
if ( ! inst )
return - ENOMEM ;
alg = crypto_get_attr_alg ( tb , CRYPTO_ALG_TYPE_CIPHER |
( algt - > type & CRYPTO_ALG_INTERNAL ) ,
CRYPTO_ALG_TYPE_MASK |
( algt - > mask & CRYPTO_ALG_INTERNAL ) ) ;
err = PTR_ERR ( alg ) ;
2006-12-16 04:09:02 +03:00
if ( IS_ERR ( alg ) )
2016-11-22 15:08:27 +03:00
goto err_free_inst ;
spawn = skcipher_instance_ctx ( inst ) ;
err = crypto_init_spawn ( spawn , alg , skcipher_crypto_instance ( inst ) ,
CRYPTO_ALG_TYPE_MASK ) ;
if ( err )
2018-11-22 13:00:16 +03:00
goto err_put_alg ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
err = crypto_inst_setname ( skcipher_crypto_instance ( inst ) , " pcbc " , alg ) ;
if ( err )
goto err_drop_spawn ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
inst - > alg . base . cra_flags = alg - > cra_flags & CRYPTO_ALG_INTERNAL ;
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-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +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-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
inst - > alg . base . cra_ctxsize = sizeof ( struct crypto_pcbc_ctx ) ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
inst - > alg . init = crypto_pcbc_init_tfm ;
inst - > alg . exit = crypto_pcbc_exit_tfm ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
inst - > alg . setkey = crypto_pcbc_setkey ;
inst - > alg . encrypt = crypto_pcbc_encrypt ;
inst - > alg . decrypt = crypto_pcbc_decrypt ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
inst - > free = crypto_pcbc_free ;
2006-12-16 04:09:02 +03:00
2016-11-22 15:08:27 +03:00
err = skcipher_register_instance ( tmpl , inst ) ;
if ( err )
goto err_drop_spawn ;
2018-11-22 13:00:16 +03:00
crypto_mod_put ( alg ) ;
2016-11-22 15:08:27 +03:00
out :
return err ;
err_drop_spawn :
crypto_drop_spawn ( spawn ) ;
2018-11-22 13:00:16 +03:00
err_put_alg :
crypto_mod_put ( alg ) ;
2016-11-22 15:08:27 +03:00
err_free_inst :
2006-12-16 04:09:02 +03:00
kfree ( inst ) ;
2016-11-22 15:08:27 +03:00
goto out ;
2006-12-16 04:09:02 +03:00
}
static struct crypto_template crypto_pcbc_tmpl = {
. name = " pcbc " ,
2016-11-22 15:08:27 +03:00
. create = crypto_pcbc_create ,
2006-12-16 04:09:02 +03:00
. module = THIS_MODULE ,
} ;
static int __init crypto_pcbc_module_init ( void )
{
return crypto_register_template ( & crypto_pcbc_tmpl ) ;
}
static void __exit crypto_pcbc_module_exit ( void )
{
crypto_unregister_template ( & crypto_pcbc_tmpl ) ;
}
module_init ( crypto_pcbc_module_init ) ;
module_exit ( crypto_pcbc_module_exit ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_DESCRIPTION ( " PCBC block cipher algorithm " ) ;
2014-11-25 03:32:38 +03:00
MODULE_ALIAS_CRYPTO ( " pcbc " ) ;