^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) * Blowfish Cipher Algorithm, by Bruce Schneier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * http://www.counterpane.com/blowfish.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Adapted from Kerneli implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (c) Herbert Valerio Riedel <hvr@hvrlab.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (c) Kyle McMartin <kyle@debian.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <crypto/blowfish.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Round loop unrolling macros, S is a pointer to a S-Box array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * organized in 4 unsigned longs at a row.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define GET32_3(x) (((x) & 0xff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define GET32_2(x) (((x) >> (8)) & (0xff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define GET32_1(x) (((x) >> (16)) & (0xff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define GET32_0(x) (((x) >> (24)) & (0xff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define ROUND(a, b, n) ({ b ^= P[n]; a ^= bf_F(b); })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const __be32 *in_blk = (const __be32 *)src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __be32 *const out_blk = (__be32 *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) const u32 *P = ctx->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) const u32 *S = ctx->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u32 yl = be32_to_cpu(in_blk[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u32 yr = be32_to_cpu(in_blk[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ROUND(yr, yl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ROUND(yl, yr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ROUND(yr, yl, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ROUND(yl, yr, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ROUND(yr, yl, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ROUND(yl, yr, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ROUND(yr, yl, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ROUND(yl, yr, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ROUND(yr, yl, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ROUND(yl, yr, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ROUND(yr, yl, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ROUND(yl, yr, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ROUND(yr, yl, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ROUND(yl, yr, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ROUND(yr, yl, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ROUND(yl, yr, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) yl ^= P[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) yr ^= P[17];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) out_blk[0] = cpu_to_be32(yr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) out_blk[1] = cpu_to_be32(yl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) const __be32 *in_blk = (const __be32 *)src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) __be32 *const out_blk = (__be32 *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const u32 *P = ctx->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const u32 *S = ctx->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u32 yl = be32_to_cpu(in_blk[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u32 yr = be32_to_cpu(in_blk[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ROUND(yr, yl, 17);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ROUND(yl, yr, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) ROUND(yr, yl, 15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ROUND(yl, yr, 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ROUND(yr, yl, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ROUND(yl, yr, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ROUND(yr, yl, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ROUND(yl, yr, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ROUND(yr, yl, 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ROUND(yl, yr, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ROUND(yr, yl, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ROUND(yl, yr, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ROUND(yr, yl, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ROUND(yl, yr, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) ROUND(yr, yl, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ROUND(yl, yr, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) yl ^= P[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) yr ^= P[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) out_blk[0] = cpu_to_be32(yr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) out_blk[1] = cpu_to_be32(yl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static struct crypto_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .cra_name = "blowfish",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .cra_driver_name = "blowfish-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .cra_blocksize = BF_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .cra_ctxsize = sizeof(struct bf_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .cra_alignmask = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .cra_u = { .cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .cia_min_keysize = BF_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .cia_max_keysize = BF_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .cia_setkey = blowfish_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .cia_encrypt = bf_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .cia_decrypt = bf_decrypt } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int __init blowfish_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return crypto_register_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static void __exit blowfish_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) crypto_unregister_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) subsys_initcall(blowfish_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) module_exit(blowfish_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) MODULE_DESCRIPTION("Blowfish Cipher Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) MODULE_ALIAS_CRYPTO("blowfish");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) MODULE_ALIAS_CRYPTO("blowfish-generic");