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