Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Cryptographic API. |
| 4 | * |
| 5 | * DES & Triple DES EDE Cipher Algorithms. |
| 6 | * |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 7 | * Copyright (c) 2005 Dag Arne Osvik <da@osvik.no> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 9 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 10 | #include <asm/byteorder.h> |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/crypto.h> |
| 16 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 17 | #include <crypto/internal/des.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 19 | static int des_setkey(struct crypto_tfm *tfm, const u8 *key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 20 | unsigned int keylen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 22 | struct des_ctx *dctx = crypto_tfm_ctx(tfm); |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 23 | int err; |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 24 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 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; |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 31 | } |
Eric Biggers | c4c4db0 | 2019-12-30 21:19:37 -0600 | [diff] [blame] | 32 | if (err) |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 33 | memset(dctx, 0, sizeof(*dctx)); |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 34 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 37 | static void crypto_des_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 39 | const struct des_ctx *dctx = crypto_tfm_ctx(tfm); |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 40 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 41 | des_encrypt(dctx, dst, src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 44 | static void crypto_des_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 46 | const struct des_ctx *dctx = crypto_tfm_ctx(tfm); |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 47 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 48 | des_decrypt(dctx, dst, src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jussi Kivilinna | 6574e6c | 2014-06-09 20:59:54 +0300 | [diff] [blame] | 51 | static 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 Biesheuvel | 4fd4be0 | 2019-08-15 12:01:07 +0300 | [diff] [blame] | 55 | int err; |
| 56 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 57 | 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 Biggers | c4c4db0 | 2019-12-30 21:19:37 -0600 | [diff] [blame] | 64 | if (err) |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 65 | memset(dctx, 0, sizeof(*dctx)); |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 66 | return err; |
Jussi Kivilinna | 6574e6c | 2014-06-09 20:59:54 +0300 | [diff] [blame] | 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 69 | static void crypto_des3_ede_encrypt(struct crypto_tfm *tfm, u8 *dst, |
| 70 | const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 72 | const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm); |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 73 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 74 | des3_ede_encrypt(dctx, dst, src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 77 | static void crypto_des3_ede_decrypt(struct crypto_tfm *tfm, u8 *dst, |
| 78 | const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 80 | const struct des3_ede_ctx *dctx = crypto_tfm_ctx(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 82 | des3_ede_decrypt(dctx, dst, src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jussi Kivilinna | 9935e6d | 2012-07-11 14:20:05 +0300 | [diff] [blame] | 85 | static struct crypto_alg des_algs[2] = { { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | .cra_name = "des", |
Jussi Kivilinna | 6574e6c | 2014-06-09 20:59:54 +0300 | [diff] [blame] | 87 | .cra_driver_name = "des-generic", |
| 88 | .cra_priority = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .cra_u = { .cipher = { |
| 94 | .cia_min_keysize = DES_KEY_SIZE, |
| 95 | .cia_max_keysize = DES_KEY_SIZE, |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 96 | .cia_setkey = des_setkey, |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 97 | .cia_encrypt = crypto_des_encrypt, |
| 98 | .cia_decrypt = crypto_des_decrypt } } |
Jussi Kivilinna | 9935e6d | 2012-07-11 14:20:05 +0300 | [diff] [blame] | 99 | }, { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .cra_name = "des3_ede", |
Jussi Kivilinna | 6574e6c | 2014-06-09 20:59:54 +0300 | [diff] [blame] | 101 | .cra_driver_name = "des3_ede-generic", |
| 102 | .cra_priority = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .cra_u = { .cipher = { |
| 108 | .cia_min_keysize = DES3_EDE_KEY_SIZE, |
| 109 | .cia_max_keysize = DES3_EDE_KEY_SIZE, |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 110 | .cia_setkey = des3_ede_setkey, |
Ard Biesheuvel | 04007b0 | 2019-08-15 12:01:09 +0300 | [diff] [blame] | 111 | .cia_encrypt = crypto_des3_ede_encrypt, |
| 112 | .cia_decrypt = crypto_des3_ede_decrypt } } |
Jussi Kivilinna | 9935e6d | 2012-07-11 14:20:05 +0300 | [diff] [blame] | 113 | } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 115 | static int __init des_generic_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Jussi Kivilinna | 9935e6d | 2012-07-11 14:20:05 +0300 | [diff] [blame] | 117 | return crypto_register_algs(des_algs, ARRAY_SIZE(des_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 120 | static void __exit des_generic_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Jussi Kivilinna | 9935e6d | 2012-07-11 14:20:05 +0300 | [diff] [blame] | 122 | crypto_unregister_algs(des_algs, ARRAY_SIZE(des_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 125 | subsys_initcall(des_generic_mod_init); |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 126 | module_exit(des_generic_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | MODULE_LICENSE("GPL"); |
| 129 | MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); |
Dag Arne Osvik | e1d5dea | 2005-07-06 13:55:44 -0700 | [diff] [blame] | 130 | MODULE_AUTHOR("Dag Arne Osvik <da@osvik.no>"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 131 | MODULE_ALIAS_CRYPTO("des"); |
| 132 | MODULE_ALIAS_CRYPTO("des-generic"); |
| 133 | MODULE_ALIAS_CRYPTO("des3_ede"); |
| 134 | MODULE_ALIAS_CRYPTO("des3_ede-generic"); |