1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * DES & Triple DES EDE Cipher Algorithms.
7 * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no>
10 #include <asm/byteorder.h>
11 #include <linux/bitops.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/crypto.h>
17 #include <crypto/internal/des.h>
19 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
22 struct des_ctx *dctx = crypto_tfm_ctx(tfm);
25 err = des_expand_key(dctx, key, keylen);
27 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
33 memset(dctx, 0, sizeof(*dctx));
37 static void crypto_des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
39 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
41 des_encrypt(dctx, dst, src);
44 static void crypto_des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
46 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
48 des_decrypt(dctx, dst, src);
51 static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
54 struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
57 err = des3_ede_expand_key(dctx, key, keylen);
59 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
65 memset(dctx, 0, sizeof(*dctx));
69 static void crypto_des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst,
72 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
74 des3_ede_encrypt(dctx, dst, src);
77 static void crypto_des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst,
80 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
82 des3_ede_decrypt(dctx, dst, src);
85 static struct crypto_alg des_algs[2] = { {
87 .cra_driver_name = "des-generic",
89 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
90 .cra_blocksize = DES_BLOCK_SIZE,
91 .cra_ctxsize = sizeof(struct des_ctx),
92 .cra_module = THIS_MODULE,
93 .cra_u = { .cipher = {
94 .cia_min_keysize = DES_KEY_SIZE,
95 .cia_max_keysize = DES_KEY_SIZE,
96 .cia_setkey = des_setkey,
97 .cia_encrypt = crypto_des_encrypt,
98 .cia_decrypt = crypto_des_decrypt } }
100 .cra_name = "des3_ede",
101 .cra_driver_name = "des3_ede-generic",
103 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
104 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
105 .cra_ctxsize = sizeof(struct des3_ede_ctx),
106 .cra_module = THIS_MODULE,
107 .cra_u = { .cipher = {
108 .cia_min_keysize = DES3_EDE_KEY_SIZE,
109 .cia_max_keysize = DES3_EDE_KEY_SIZE,
110 .cia_setkey = des3_ede_setkey,
111 .cia_encrypt = crypto_des3_ede_encrypt,
112 .cia_decrypt = crypto_des3_ede_decrypt } }
115 static int __init des_generic_mod_init(void)
117 return crypto_register_algs(des_algs, ARRAY_SIZE(des_algs));
120 static void __exit des_generic_mod_fini(void)
122 crypto_unregister_algs(des_algs, ARRAY_SIZE(des_algs));
125 subsys_initcall(des_generic_mod_init);
126 module_exit(des_generic_mod_fini);
128 MODULE_LICENSE("GPL");
129 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
130 MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>");
131 MODULE_ALIAS_CRYPTO("des");
132 MODULE_ALIAS_CRYPTO("des-generic");
133 MODULE_ALIAS_CRYPTO("des3_ede");
134 MODULE_ALIAS_CRYPTO("des3_ede-generic");