2007-04-16 14:48:54 +04:00
/*
* Asynchronous block chaining cipher operations .
*
* This is the asynchronous version of blkcipher . c indicating completion
* via a callback .
*
* 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 .
*
*/
2007-12-17 15:07:31 +03:00
# include <crypto/internal/skcipher.h>
# include <linux/err.h>
2007-04-16 14:48:54 +04:00
# include <linux/init.h>
2007-08-23 12:23:01 +04:00
# include <linux/kernel.h>
2007-04-16 14:48:54 +04:00
# include <linux/module.h>
2007-08-23 12:23:01 +04:00
# include <linux/slab.h>
2007-04-16 14:48:54 +04:00
# include <linux/seq_file.h>
2007-12-17 15:07:31 +03:00
# include "internal.h"
2007-08-23 12:23:01 +04:00
static int setkey_unaligned ( struct crypto_ablkcipher * tfm , const u8 * key ,
unsigned int keylen )
2007-05-19 13:51:21 +04:00
{
struct ablkcipher_alg * cipher = crypto_ablkcipher_alg ( tfm ) ;
unsigned long alignmask = crypto_ablkcipher_alignmask ( tfm ) ;
int ret ;
u8 * buffer , * alignbuffer ;
unsigned long absize ;
absize = keylen + alignmask ;
buffer = kmalloc ( absize , GFP_ATOMIC ) ;
if ( ! buffer )
return - ENOMEM ;
alignbuffer = ( u8 * ) ALIGN ( ( unsigned long ) buffer , alignmask + 1 ) ;
memcpy ( alignbuffer , key , keylen ) ;
ret = cipher - > setkey ( tfm , alignbuffer , keylen ) ;
2007-08-03 16:33:47 +04:00
memset ( alignbuffer , 0 , keylen ) ;
2007-05-19 13:51:21 +04:00
kfree ( buffer ) ;
return ret ;
}
2007-04-16 14:48:54 +04:00
static int setkey ( struct crypto_ablkcipher * tfm , const u8 * key ,
unsigned int keylen )
{
struct ablkcipher_alg * cipher = crypto_ablkcipher_alg ( tfm ) ;
2007-05-19 13:51:21 +04:00
unsigned long alignmask = crypto_ablkcipher_alignmask ( tfm ) ;
2007-04-16 14:48:54 +04:00
if ( keylen < cipher - > min_keysize | | keylen > cipher - > max_keysize ) {
crypto_ablkcipher_set_flags ( tfm , CRYPTO_TFM_RES_BAD_KEY_LEN ) ;
return - EINVAL ;
}
2007-05-19 13:51:21 +04:00
if ( ( unsigned long ) key & alignmask )
return setkey_unaligned ( tfm , key , keylen ) ;
2007-04-16 14:48:54 +04:00
return cipher - > setkey ( tfm , key , keylen ) ;
}
static unsigned int crypto_ablkcipher_ctxsize ( struct crypto_alg * alg , u32 type ,
u32 mask )
{
return alg - > cra_ctxsize ;
}
static int crypto_init_ablkcipher_ops ( struct crypto_tfm * tfm , u32 type ,
u32 mask )
{
struct ablkcipher_alg * alg = & tfm - > __crt_alg - > cra_ablkcipher ;
struct ablkcipher_tfm * crt = & tfm - > crt_ablkcipher ;
if ( alg - > ivsize > PAGE_SIZE / 8 )
return - EINVAL ;
crt - > setkey = setkey ;
crt - > encrypt = alg - > encrypt ;
crt - > decrypt = alg - > decrypt ;
2007-12-05 13:08:36 +03:00
crt - > base = __crypto_ablkcipher_cast ( tfm ) ;
2007-04-16 14:48:54 +04:00
crt - > ivsize = alg - > ivsize ;
return 0 ;
}
static void crypto_ablkcipher_show ( struct seq_file * m , struct crypto_alg * alg )
__attribute__ ( ( unused ) ) ;
static void crypto_ablkcipher_show ( struct seq_file * m , struct crypto_alg * alg )
{
struct ablkcipher_alg * ablkcipher = & alg - > cra_ablkcipher ;
seq_printf ( m , " type : ablkcipher \n " ) ;
seq_printf ( m , " blocksize : %u \n " , alg - > cra_blocksize ) ;
seq_printf ( m , " min keysize : %u \n " , ablkcipher - > min_keysize ) ;
seq_printf ( m , " max keysize : %u \n " , ablkcipher - > max_keysize ) ;
seq_printf ( m , " ivsize : %u \n " , ablkcipher - > ivsize ) ;
2007-11-27 16:33:24 +03:00
seq_printf ( m , " geniv : %s \n " , ablkcipher - > geniv ? : " <default> " ) ;
2007-04-16 14:48:54 +04:00
}
const struct crypto_type crypto_ablkcipher_type = {
. ctxsize = crypto_ablkcipher_ctxsize ,
. init = crypto_init_ablkcipher_ops ,
# ifdef CONFIG_PROC_FS
. show = crypto_ablkcipher_show ,
# endif
} ;
EXPORT_SYMBOL_GPL ( crypto_ablkcipher_type ) ;
2007-12-17 16:51:27 +03:00
static int no_givdecrypt ( struct skcipher_givcrypt_request * req )
{
return - ENOSYS ;
}
static int crypto_init_givcipher_ops ( struct crypto_tfm * tfm , u32 type ,
u32 mask )
{
struct ablkcipher_alg * alg = & tfm - > __crt_alg - > cra_ablkcipher ;
struct ablkcipher_tfm * crt = & tfm - > crt_ablkcipher ;
if ( alg - > ivsize > PAGE_SIZE / 8 )
return - EINVAL ;
2007-12-05 13:08:36 +03:00
crt - > setkey = tfm - > __crt_alg - > cra_flags & CRYPTO_ALG_GENIV ?
alg - > setkey : setkey ;
2007-12-17 16:51:27 +03:00
crt - > encrypt = alg - > encrypt ;
crt - > decrypt = alg - > decrypt ;
crt - > givencrypt = alg - > givencrypt ;
crt - > givdecrypt = alg - > givdecrypt ? : no_givdecrypt ;
2007-12-05 13:08:36 +03:00
crt - > base = __crypto_ablkcipher_cast ( tfm ) ;
2007-12-17 16:51:27 +03:00
crt - > ivsize = alg - > ivsize ;
return 0 ;
}
static void crypto_givcipher_show ( struct seq_file * m , struct crypto_alg * alg )
__attribute__ ( ( unused ) ) ;
static void crypto_givcipher_show ( struct seq_file * m , struct crypto_alg * alg )
{
struct ablkcipher_alg * ablkcipher = & alg - > cra_ablkcipher ;
seq_printf ( m , " type : givcipher \n " ) ;
seq_printf ( m , " blocksize : %u \n " , alg - > cra_blocksize ) ;
seq_printf ( m , " min keysize : %u \n " , ablkcipher - > min_keysize ) ;
seq_printf ( m , " max keysize : %u \n " , ablkcipher - > max_keysize ) ;
seq_printf ( m , " ivsize : %u \n " , ablkcipher - > ivsize ) ;
2007-11-27 16:33:24 +03:00
seq_printf ( m , " geniv : %s \n " , ablkcipher - > geniv ? : " <built-in> " ) ;
2007-12-17 16:51:27 +03:00
}
const struct crypto_type crypto_givcipher_type = {
. ctxsize = crypto_ablkcipher_ctxsize ,
. init = crypto_init_givcipher_ops ,
# ifdef CONFIG_PROC_FS
. show = crypto_givcipher_show ,
# endif
} ;
EXPORT_SYMBOL_GPL ( crypto_givcipher_type ) ;
2007-12-05 13:08:36 +03:00
const char * crypto_default_geniv ( const struct crypto_alg * alg )
{
return alg - > cra_flags & CRYPTO_ALG_ASYNC ? " eseqiv " : " chainiv " ;
}
2007-12-17 15:07:31 +03:00
int crypto_grab_skcipher ( struct crypto_skcipher_spawn * spawn , const char * name ,
u32 type , u32 mask )
{
struct crypto_alg * alg ;
int err ;
type = crypto_skcipher_type ( type ) ;
mask = crypto_skcipher_mask ( mask ) ;
alg = crypto_alg_mod_lookup ( name , type , mask ) ;
if ( IS_ERR ( alg ) )
return PTR_ERR ( alg ) ;
err = crypto_init_spawn ( & spawn - > base , alg , spawn - > base . inst , mask ) ;
crypto_mod_put ( alg ) ;
return err ;
}
EXPORT_SYMBOL_GPL ( crypto_grab_skcipher ) ;
2007-04-16 14:48:54 +04:00
MODULE_LICENSE ( " GPL " ) ;
MODULE_DESCRIPTION ( " Asynchronous block chaining cipher type " ) ;