1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API.
4 *
5 * DES & Triple DES EDE Cipher Algorithms.
6 *
7 * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no>
8 */
9
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>
16
17 #include <crypto/internal/des.h>
18
des_setkey(struct crypto_tfm * tfm,const u8 * key,unsigned int keylen)19 static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
20 unsigned int keylen)
21 {
22 struct des_ctx *dctx = crypto_tfm_ctx(tfm);
23 int err;
24
25 err = des_expand_key(dctx, key, keylen);
26 if (err == -ENOKEY) {
27 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
28 err = -EINVAL;
29 else
30 err = 0;
31 }
32
33 if (err) {
34 memset(dctx, 0, sizeof(*dctx));
35 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
36 }
37 return err;
38 }
39
crypto_des_encrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)40 static void crypto_des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
41 {
42 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
43
44 des_encrypt(dctx, dst, src);
45 }
46
crypto_des_decrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)47 static void crypto_des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
48 {
49 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
50
51 des_decrypt(dctx, dst, src);
52 }
53
des3_ede_setkey(struct crypto_tfm * tfm,const u8 * key,unsigned int keylen)54 static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
55 unsigned int keylen)
56 {
57 struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
58 int err;
59
60 err = des3_ede_expand_key(dctx, key, keylen);
61 if (err == -ENOKEY) {
62 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
63 err = -EINVAL;
64 else
65 err = 0;
66 }
67
68 if (err) {
69 memset(dctx, 0, sizeof(*dctx));
70 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
71 }
72 return err;
73 }
74
crypto_des3_ede_encrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)75 static void crypto_des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst,
76 const u8 *src)
77 {
78 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
79
80 des3_ede_encrypt(dctx, dst, src);
81 }
82
crypto_des3_ede_decrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)83 static void crypto_des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst,
84 const u8 *src)
85 {
86 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
87
88 des3_ede_decrypt(dctx, dst, src);
89 }
90
91 static struct crypto_alg des_algs[2] = { {
92 .cra_name = "des",
93 .cra_driver_name = "des-generic",
94 .cra_priority = 100,
95 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
96 .cra_blocksize = DES_BLOCK_SIZE,
97 .cra_ctxsize = sizeof(struct des_ctx),
98 .cra_module = THIS_MODULE,
99 .cra_u = { .cipher = {
100 .cia_min_keysize = DES_KEY_SIZE,
101 .cia_max_keysize = DES_KEY_SIZE,
102 .cia_setkey = des_setkey,
103 .cia_encrypt = crypto_des_encrypt,
104 .cia_decrypt = crypto_des_decrypt } }
105 }, {
106 .cra_name = "des3_ede",
107 .cra_driver_name = "des3_ede-generic",
108 .cra_priority = 100,
109 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
110 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
111 .cra_ctxsize = sizeof(struct des3_ede_ctx),
112 .cra_module = THIS_MODULE,
113 .cra_u = { .cipher = {
114 .cia_min_keysize = DES3_EDE_KEY_SIZE,
115 .cia_max_keysize = DES3_EDE_KEY_SIZE,
116 .cia_setkey = des3_ede_setkey,
117 .cia_encrypt = crypto_des3_ede_encrypt,
118 .cia_decrypt = crypto_des3_ede_decrypt } }
119 } };
120
des_generic_mod_init(void)121 static int __init des_generic_mod_init(void)
122 {
123 return crypto_register_algs(des_algs, ARRAY_SIZE(des_algs));
124 }
125
des_generic_mod_fini(void)126 static void __exit des_generic_mod_fini(void)
127 {
128 crypto_unregister_algs(des_algs, ARRAY_SIZE(des_algs));
129 }
130
131 subsys_initcall(des_generic_mod_init);
132 module_exit(des_generic_mod_fini);
133
134 MODULE_LICENSE("GPL");
135 MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
136 MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>");
137 MODULE_ALIAS_CRYPTO("des");
138 MODULE_ALIAS_CRYPTO("des-generic");
139 MODULE_ALIAS_CRYPTO("des3_ede");
140 MODULE_ALIAS_CRYPTO("des3_ede-generic");
141