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