Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Richard Hartmann | 0d8fb0a | 2010-02-16 20:24:30 +0800 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Cryptographic API. |
| 4 | * |
| 5 | * Blowfish Cipher Algorithm, by Bruce Schneier. |
| 6 | * http://www.counterpane.com/blowfish.html |
| 7 | * |
| 8 | * Adapted from Kerneli implementation. |
| 9 | * |
| 10 | * Copyright (c) Herbert Valerio Riedel <hvr@hvrlab.org> |
| 11 | * Copyright (c) Kyle McMartin <kyle@debian.org> |
| 12 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/mm.h> |
Ard Biesheuvel | 50a3a9f | 2021-02-01 19:02:32 +0100 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/crypto.h> |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 19 | #include <linux/types.h> |
Jussi Kivilinna | 52ba867 | 2011-09-02 01:45:07 +0300 | [diff] [blame] | 20 | #include <crypto/blowfish.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Richard Hartmann | 0d8fb0a | 2010-02-16 20:24:30 +0800 | [diff] [blame] | 22 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * Round loop unrolling macros, S is a pointer to a S-Box array |
| 24 | * organized in 4 unsigned longs at a row. |
| 25 | */ |
| 26 | #define GET32_3(x) (((x) & 0xff)) |
| 27 | #define GET32_2(x) (((x) >> (8)) & (0xff)) |
| 28 | #define GET32_1(x) (((x) >> (16)) & (0xff)) |
| 29 | #define GET32_0(x) (((x) >> (24)) & (0xff)) |
| 30 | |
| 31 | #define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \ |
Jussi Kivilinna | 3f2a5d2 | 2011-09-02 01:45:12 +0300 | [diff] [blame] | 32 | S[512 + GET32_2(x)]) + S[768 + GET32_3(x)]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Jussi Kivilinna | 3f2a5d2 | 2011-09-02 01:45:12 +0300 | [diff] [blame] | 34 | #define ROUND(a, b, n) ({ b ^= P[n]; a ^= bf_F(b); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Jussi Kivilinna | 52ba867 | 2011-09-02 01:45:07 +0300 | [diff] [blame] | 36 | static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Jussi Kivilinna | 52ba867 | 2011-09-02 01:45:07 +0300 | [diff] [blame] | 38 | struct bf_ctx *ctx = crypto_tfm_ctx(tfm); |
Jussi Kivilinna | 52ba867 | 2011-09-02 01:45:07 +0300 | [diff] [blame] | 39 | const u32 *P = ctx->p; |
| 40 | const u32 *S = ctx->s; |
Ard Biesheuvel | 50a3a9f | 2021-02-01 19:02:32 +0100 | [diff] [blame] | 41 | u32 yl = get_unaligned_be32(src); |
| 42 | u32 yr = get_unaligned_be32(src + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | ROUND(yr, yl, 0); |
| 45 | ROUND(yl, yr, 1); |
| 46 | ROUND(yr, yl, 2); |
| 47 | ROUND(yl, yr, 3); |
| 48 | ROUND(yr, yl, 4); |
| 49 | ROUND(yl, yr, 5); |
| 50 | ROUND(yr, yl, 6); |
| 51 | ROUND(yl, yr, 7); |
| 52 | ROUND(yr, yl, 8); |
| 53 | ROUND(yl, yr, 9); |
| 54 | ROUND(yr, yl, 10); |
| 55 | ROUND(yl, yr, 11); |
| 56 | ROUND(yr, yl, 12); |
| 57 | ROUND(yl, yr, 13); |
| 58 | ROUND(yr, yl, 14); |
| 59 | ROUND(yl, yr, 15); |
| 60 | |
| 61 | yl ^= P[16]; |
| 62 | yr ^= P[17]; |
| 63 | |
Ard Biesheuvel | 50a3a9f | 2021-02-01 19:02:32 +0100 | [diff] [blame] | 64 | put_unaligned_be32(yr, dst); |
| 65 | put_unaligned_be32(yl, dst + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 68 | static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 70 | struct bf_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 71 | const u32 *P = ctx->p; |
| 72 | const u32 *S = ctx->s; |
Ard Biesheuvel | 50a3a9f | 2021-02-01 19:02:32 +0100 | [diff] [blame] | 73 | u32 yl = get_unaligned_be32(src); |
| 74 | u32 yr = get_unaligned_be32(src + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | ROUND(yr, yl, 17); |
| 77 | ROUND(yl, yr, 16); |
| 78 | ROUND(yr, yl, 15); |
| 79 | ROUND(yl, yr, 14); |
| 80 | ROUND(yr, yl, 13); |
| 81 | ROUND(yl, yr, 12); |
| 82 | ROUND(yr, yl, 11); |
| 83 | ROUND(yl, yr, 10); |
| 84 | ROUND(yr, yl, 9); |
| 85 | ROUND(yl, yr, 8); |
| 86 | ROUND(yr, yl, 7); |
| 87 | ROUND(yl, yr, 6); |
| 88 | ROUND(yr, yl, 5); |
| 89 | ROUND(yl, yr, 4); |
| 90 | ROUND(yr, yl, 3); |
| 91 | ROUND(yl, yr, 2); |
| 92 | |
| 93 | yl ^= P[1]; |
| 94 | yr ^= P[0]; |
| 95 | |
Ard Biesheuvel | 50a3a9f | 2021-02-01 19:02:32 +0100 | [diff] [blame] | 96 | put_unaligned_be32(yr, dst); |
| 97 | put_unaligned_be32(yl, dst + 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static struct crypto_alg alg = { |
| 101 | .cra_name = "blowfish", |
Jussi Kivilinna | 3f2a5d2 | 2011-09-02 01:45:12 +0300 | [diff] [blame] | 102 | .cra_driver_name = "blowfish-generic", |
| 103 | .cra_priority = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 105 | .cra_blocksize = BF_BLOCK_SIZE, |
| 106 | .cra_ctxsize = sizeof(struct bf_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | .cra_module = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .cra_u = { .cipher = { |
| 109 | .cia_min_keysize = BF_MIN_KEY_SIZE, |
| 110 | .cia_max_keysize = BF_MAX_KEY_SIZE, |
Jussi Kivilinna | 52ba867 | 2011-09-02 01:45:07 +0300 | [diff] [blame] | 111 | .cia_setkey = blowfish_setkey, |
Jussi Kivilinna | 3f2a5d2 | 2011-09-02 01:45:12 +0300 | [diff] [blame] | 112 | .cia_encrypt = bf_encrypt, |
| 113 | .cia_decrypt = bf_decrypt } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 116 | static int __init blowfish_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | return crypto_register_alg(&alg); |
| 119 | } |
| 120 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 121 | static void __exit blowfish_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | crypto_unregister_alg(&alg); |
| 124 | } |
| 125 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 126 | subsys_initcall(blowfish_mod_init); |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 127 | module_exit(blowfish_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | MODULE_LICENSE("GPL"); |
| 130 | MODULE_DESCRIPTION("Blowfish Cipher Algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 131 | MODULE_ALIAS_CRYPTO("blowfish"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 132 | MODULE_ALIAS_CRYPTO("blowfish-generic"); |