blob: 6e13a4a29ecb0b9f158ece39b8a4eddbd615ad96 [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 }
32
Ard Biesheuvel04007b02019-08-15 12:01:09 +030033 if (err) {
34 memset(dctx, 0, sizeof(*dctx));
35 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
36 }
37 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Ard Biesheuvel04007b02019-08-15 12:01:09 +030040static void crypto_des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030042 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070043
Ard Biesheuvel04007b02019-08-15 12:01:09 +030044 des_encrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Ard Biesheuvel04007b02019-08-15 12:01:09 +030047static void crypto_des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030049 const struct des_ctx *dctx = crypto_tfm_ctx(tfm);
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070050
Ard Biesheuvel04007b02019-08-15 12:01:09 +030051 des_decrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030054static 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);
Ard Biesheuvel4fd4be02019-08-15 12:01:07 +030058 int err;
59
Ard Biesheuvel04007b02019-08-15 12:01:09 +030060 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 }
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030067
Ard Biesheuvel04007b02019-08-15 12:01:09 +030068 if (err) {
69 memset(dctx, 0, sizeof(*dctx));
70 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
71 }
72 return err;
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030073}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Ard Biesheuvel04007b02019-08-15 12:01:09 +030075static void crypto_des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst,
76 const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030078 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -070079
Ard Biesheuvel04007b02019-08-15 12:01:09 +030080 des3_ede_encrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Ard Biesheuvel04007b02019-08-15 12:01:09 +030083static void crypto_des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst,
84 const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Ard Biesheuvel04007b02019-08-15 12:01:09 +030086 const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Ard Biesheuvel04007b02019-08-15 12:01:09 +030088 des3_ede_decrypt(dctx, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +030091static struct crypto_alg des_algs[2] = { {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .cra_name = "des",
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +030093 .cra_driver_name = "des-generic",
94 .cra_priority = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
96 .cra_blocksize = DES_BLOCK_SIZE,
97 .cra_ctxsize = sizeof(struct des_ctx),
98 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .cra_u = { .cipher = {
100 .cia_min_keysize = DES_KEY_SIZE,
101 .cia_max_keysize = DES_KEY_SIZE,
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -0700102 .cia_setkey = des_setkey,
Ard Biesheuvel04007b02019-08-15 12:01:09 +0300103 .cia_encrypt = crypto_des_encrypt,
104 .cia_decrypt = crypto_des_decrypt } }
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300105}, {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .cra_name = "des3_ede",
Jussi Kivilinna6574e6c2014-06-09 20:59:54 +0300107 .cra_driver_name = "des3_ede-generic",
108 .cra_priority = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .cra_u = { .cipher = {
114 .cia_min_keysize = DES3_EDE_KEY_SIZE,
115 .cia_max_keysize = DES3_EDE_KEY_SIZE,
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -0700116 .cia_setkey = des3_ede_setkey,
Ard Biesheuvel04007b02019-08-15 12:01:09 +0300117 .cia_encrypt = crypto_des3_ede_encrypt,
118 .cia_decrypt = crypto_des3_ede_decrypt } }
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300119} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800121static int __init des_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300123 return crypto_register_algs(des_algs, ARRAY_SIZE(des_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800126static void __exit des_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Jussi Kivilinna9935e6d2012-07-11 14:20:05 +0300128 crypto_unregister_algs(des_algs, ARRAY_SIZE(des_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Eric Biggersc4741b22019-04-11 21:57:42 -0700131subsys_initcall(des_generic_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800132module_exit(des_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134MODULE_LICENSE("GPL");
135MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms");
Dag Arne Osvike1d5dea2005-07-06 13:55:44 -0700136MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100137MODULE_ALIAS_CRYPTO("des");
138MODULE_ALIAS_CRYPTO("des-generic");
139MODULE_ALIAS_CRYPTO("des3_ede");
140MODULE_ALIAS_CRYPTO("des3_ede-generic");