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