2019-05-27 09:55:01 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2005-07-07 00:55:44 +04:00
/*
2005-04-17 02:20:36 +04:00
* Cryptographic API .
*
* DES & Triple DES EDE Cipher Algorithms .
*
2005-07-07 00:55:44 +04:00
* Copyright ( c ) 2005 Dag Arne Osvik < da @ osvik . no >
2005-04-17 02:20:36 +04:00
*/
2005-07-07 00:55:44 +04:00
2005-10-30 13:25:15 +03:00
# include <asm/byteorder.h>
2005-07-07 00:55:44 +04:00
# include <linux/bitops.h>
2005-04-17 02:20:36 +04:00
# include <linux/init.h>
# include <linux/module.h>
# include <linux/errno.h>
# include <linux/crypto.h>
2019-08-15 12:01:09 +03:00
# include <crypto/internal/des.h>
2005-04-17 02:20:36 +04:00
2006-05-16 16:09:29 +04:00
static int des_setkey ( struct crypto_tfm * tfm , const u8 * key ,
2006-08-13 08:16:39 +04:00
unsigned int keylen )
2005-04-17 02:20:36 +04:00
{
2006-05-16 16:09:29 +04:00
struct des_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
2019-08-15 12:01:09 +03:00
int err ;
2005-07-07 00:55:44 +04:00
2019-08-15 12:01:09 +03:00
err = des_expand_key ( dctx , key , keylen ) ;
if ( err = = - ENOKEY ) {
if ( crypto_tfm_get_flags ( tfm ) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS )
err = - EINVAL ;
else
err = 0 ;
2005-07-07 00:55:44 +04:00
}
2019-12-31 06:19:37 +03:00
if ( err )
2019-08-15 12:01:09 +03:00
memset ( dctx , 0 , sizeof ( * dctx ) ) ;
return err ;
2005-04-17 02:20:36 +04:00
}
2019-08-15 12:01:09 +03:00
static void crypto_des_encrypt ( struct crypto_tfm * tfm , u8 * dst , const u8 * src )
2005-04-17 02:20:36 +04:00
{
2019-08-15 12:01:09 +03:00
const struct des_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
2005-07-07 00:55:44 +04:00
2019-08-15 12:01:09 +03:00
des_encrypt ( dctx , dst , src ) ;
2005-04-17 02:20:36 +04:00
}
2019-08-15 12:01:09 +03:00
static void crypto_des_decrypt ( struct crypto_tfm * tfm , u8 * dst , const u8 * src )
2005-04-17 02:20:36 +04:00
{
2019-08-15 12:01:09 +03:00
const struct des_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
2005-07-07 00:55:44 +04:00
2019-08-15 12:01:09 +03:00
des_decrypt ( dctx , dst , src ) ;
2005-04-17 02:20:36 +04:00
}
2014-06-09 21:59:54 +04:00
static int des3_ede_setkey ( struct crypto_tfm * tfm , const u8 * key ,
unsigned int keylen )
{
struct des3_ede_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
2019-08-15 12:01:07 +03:00
int err ;
2019-08-15 12:01:09 +03:00
err = des3_ede_expand_key ( dctx , key , keylen ) ;
if ( err = = - ENOKEY ) {
if ( crypto_tfm_get_flags ( tfm ) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS )
err = - EINVAL ;
else
err = 0 ;
}
2019-12-31 06:19:37 +03:00
if ( err )
2019-08-15 12:01:09 +03:00
memset ( dctx , 0 , sizeof ( * dctx ) ) ;
return err ;
2014-06-09 21:59:54 +04:00
}
2005-04-17 02:20:36 +04:00
2019-08-15 12:01:09 +03:00
static void crypto_des3_ede_encrypt ( struct crypto_tfm * tfm , u8 * dst ,
const u8 * src )
2005-04-17 02:20:36 +04:00
{
2019-08-15 12:01:09 +03:00
const struct des3_ede_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
2005-07-07 00:55:44 +04:00
2019-08-15 12:01:09 +03:00
des3_ede_encrypt ( dctx , dst , src ) ;
2005-04-17 02:20:36 +04:00
}
2019-08-15 12:01:09 +03:00
static void crypto_des3_ede_decrypt ( struct crypto_tfm * tfm , u8 * dst ,
const u8 * src )
2005-04-17 02:20:36 +04:00
{
2019-08-15 12:01:09 +03:00
const struct des3_ede_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
2005-04-17 02:20:36 +04:00
2019-08-15 12:01:09 +03:00
des3_ede_decrypt ( dctx , dst , src ) ;
2005-04-17 02:20:36 +04:00
}
2012-07-11 15:20:05 +04:00
static struct crypto_alg des_algs [ 2 ] = { {
2005-04-17 02:20:36 +04:00
. cra_name = " des " ,
2014-06-09 21:59:54 +04:00
. cra_driver_name = " des-generic " ,
. cra_priority = 100 ,
2005-04-17 02:20:36 +04:00
. cra_flags = CRYPTO_ALG_TYPE_CIPHER ,
. cra_blocksize = DES_BLOCK_SIZE ,
. cra_ctxsize = sizeof ( struct des_ctx ) ,
. cra_module = THIS_MODULE ,
. cra_u = { . cipher = {
. cia_min_keysize = DES_KEY_SIZE ,
. cia_max_keysize = DES_KEY_SIZE ,
2005-07-07 00:55:44 +04:00
. cia_setkey = des_setkey ,
2019-08-15 12:01:09 +03:00
. cia_encrypt = crypto_des_encrypt ,
. cia_decrypt = crypto_des_decrypt } }
2012-07-11 15:20:05 +04:00
} , {
2005-04-17 02:20:36 +04:00
. cra_name = " des3_ede " ,
2014-06-09 21:59:54 +04:00
. cra_driver_name = " des3_ede-generic " ,
. cra_priority = 100 ,
2005-04-17 02:20:36 +04:00
. cra_flags = CRYPTO_ALG_TYPE_CIPHER ,
. cra_blocksize = DES3_EDE_BLOCK_SIZE ,
. cra_ctxsize = sizeof ( struct des3_ede_ctx ) ,
. cra_module = THIS_MODULE ,
. cra_u = { . cipher = {
. cia_min_keysize = DES3_EDE_KEY_SIZE ,
. cia_max_keysize = DES3_EDE_KEY_SIZE ,
2005-07-07 00:55:44 +04:00
. cia_setkey = des3_ede_setkey ,
2019-08-15 12:01:09 +03:00
. cia_encrypt = crypto_des3_ede_encrypt ,
. cia_decrypt = crypto_des3_ede_decrypt } }
2012-07-11 15:20:05 +04:00
} } ;
2005-04-17 02:20:36 +04:00
2008-04-05 17:00:57 +04:00
static int __init des_generic_mod_init ( void )
2005-04-17 02:20:36 +04:00
{
2012-07-11 15:20:05 +04:00
return crypto_register_algs ( des_algs , ARRAY_SIZE ( des_algs ) ) ;
2005-04-17 02:20:36 +04:00
}
2008-04-05 17:00:57 +04:00
static void __exit des_generic_mod_fini ( void )
2005-04-17 02:20:36 +04:00
{
2012-07-11 15:20:05 +04:00
crypto_unregister_algs ( des_algs , ARRAY_SIZE ( des_algs ) ) ;
2005-04-17 02:20:36 +04:00
}
2019-04-12 07:57:42 +03:00
subsys_initcall ( des_generic_mod_init ) ;
2008-04-05 17:00:57 +04:00
module_exit ( des_generic_mod_fini ) ;
2005-04-17 02:20:36 +04:00
MODULE_LICENSE ( " GPL " ) ;
MODULE_DESCRIPTION ( " DES & Triple DES EDE Cipher Algorithms " ) ;
2005-07-07 00:55:44 +04:00
MODULE_AUTHOR ( " Dag Arne Osvik <da@osvik.no> " ) ;
2015-01-11 20:17:42 +03:00
MODULE_ALIAS_CRYPTO ( " des " ) ;
MODULE_ALIAS_CRYPTO ( " des-generic " ) ;
MODULE_ALIAS_CRYPTO ( " des3_ede " ) ;
MODULE_ALIAS_CRYPTO ( " des3_ede-generic " ) ;