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