blob: c85354a5e94c7bd5befc80f754ce8297c1affc14 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Cryptographic API.
4 *
5 * DES & Triple DES EDE Cipher Algorithms.
6 *
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -07007 * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -07009
Herbert Xu06ace7a2005-10-30 21:25:15 +110010#include <asm/byteorder.h>
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070011#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/crypto.h>
16
Ard Biesheuvel04007b02019-08-15 12:01:09 +030017#include <crypto/internal/des.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Herbert Xu6c2bb982006-05-16 22:09:29 +100019static int des_setkey(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +100020 unsigned int keylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Herbert Xu6c2bb982006-05-16 22:09:29 +100022 struct des_ctx *dctx = crypto_tfm_ctx(tfm);
Ard Biesheuvel04007b02019-08-15 12:01:09 +030023 int err;
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070024
Ard Biesheuvel04007b02019-08-15 12:01:09 +030025 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;
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070031 }
Eric Biggersc4c4db02019-12-30 21:19:37 -060032 if (err)
Ard Biesheuvel04007b02019-08-15 12:01:09 +030033 memset(dctx, 0, sizeof(*dctx));
Ard Biesheuvel04007b02019-08-15 12:01:09 +030034 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Ard Biesheuvel04007b02019-08-15 12:01:09 +030037static void crypto_des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030039 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070040
Ard Biesheuvel04007b02019-08-15 12:01:09 +030041 des_encrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Ard Biesheuvel04007b02019-08-15 12:01:09 +030044static void crypto_des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030046 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070047
Ard Biesheuvel04007b02019-08-15 12:01:09 +030048 des_decrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030051static int des3_ede_setkey(struct crypto_tfm *tfm, const u8 *key,
52 unsigned int keylen)
53{
54 struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
Ard Biesheuvel4fd4be02019-08-15 12:01:07 +030055 int err;
56
Ard Biesheuvel04007b02019-08-15 12:01:09 +030057 err = des3_ede_expand_key(dctx, key, keylen);
58 if (err == -ENOKEY) {
59 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
60 err = -EINVAL;
61 else
62 err = 0;
63 }
Eric Biggersc4c4db02019-12-30 21:19:37 -060064 if (err)
Ard Biesheuvel04007b02019-08-15 12:01:09 +030065 memset(dctx, 0, sizeof(*dctx));
Ard Biesheuvel04007b02019-08-15 12:01:09 +030066 return err;
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030067}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Ard Biesheuvel04007b02019-08-15 12:01:09 +030069static void crypto_des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst,
70 const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030072 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070073
Ard Biesheuvel04007b02019-08-15 12:01:09 +030074 des3_ede_encrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Ard Biesheuvel04007b02019-08-15 12:01:09 +030077static void crypto_des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst,
78 const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030080 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Ard Biesheuvel04007b02019-08-15 12:01:09 +030082 des3_ede_decrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +030085static struct crypto_alg des_algs[2] = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .cra_name = "des",
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030087 .cra_driver_name = "des-generic",
88 .cra_priority = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
90 .cra_blocksize = DES_BLOCK_SIZE,
91 .cra_ctxsize = sizeof(struct des_ctx),
92 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .cra_u = { .cipher = {
94 .cia_min_keysize = DES_KEY_SIZE,
95 .cia_max_keysize = DES_KEY_SIZE,
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070096 .cia_setkey = des_setkey,
Ard Biesheuvel04007b02019-08-15 12:01:09 +030097 .cia_encrypt = crypto_des_encrypt,
98 .cia_decrypt = crypto_des_decrypt } }
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +030099}, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .cra_name = "des3_ede",
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +0300101 .cra_driver_name = "des3_ede-generic",
102 .cra_priority = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .cra_u = { .cipher = {
108 .cia_min_keysize = DES3_EDE_KEY_SIZE,
109 .cia_max_keysize = DES3_EDE_KEY_SIZE,
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -0700110 .cia_setkey = des3_ede_setkey,
Ard Biesheuvel04007b02019-08-15 12:01:09 +0300111 .cia_encrypt = crypto_des3_ede_encrypt,
112 .cia_decrypt = crypto_des3_ede_decrypt } }
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300113} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800115static int __init des_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300117 return crypto_register_algs(des_algs, ARRAY_SIZE(des_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800120static void __exit des_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300122 crypto_unregister_algs(des_algs, ARRAY_SIZE(des_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Eric Biggersc4741b22019-04-11 21:57:42 -0700125subsys_initcall(des_generic_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800126module_exit(des_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128MODULE_LICENSE("GPL");
129MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -0700130MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100131MODULE_ALIAS_CRYPTO("des");
132MODULE_ALIAS_CRYPTO("des-generic");
133MODULE_ALIAS_CRYPTO("des3_ede");
134MODULE_ALIAS_CRYPTO("des3_ede-generic");