Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Cryptographic API.
0004  *
0005  * Blowfish Cipher Algorithm, by Bruce Schneier.
0006  * http://www.counterpane.com/blowfish.html
0007  *
0008  * Adapted from Kerneli implementation.
0009  *
0010  * Copyright (c) Herbert Valerio Riedel <hvr@hvrlab.org>
0011  * Copyright (c) Kyle McMartin <kyle@debian.org>
0012  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0013  */
0014 #include <linux/init.h>
0015 #include <linux/module.h>
0016 #include <linux/mm.h>
0017 #include <asm/unaligned.h>
0018 #include <linux/crypto.h>
0019 #include <linux/types.h>
0020 #include <crypto/blowfish.h>
0021 
0022 /*
0023  * Round loop unrolling macros, S is a pointer to a S-Box array
0024  * organized in 4 unsigned longs at a row.
0025  */
0026 #define GET32_3(x) (((x) & 0xff))
0027 #define GET32_2(x) (((x) >> (8)) & (0xff))
0028 #define GET32_1(x) (((x) >> (16)) & (0xff))
0029 #define GET32_0(x) (((x) >> (24)) & (0xff))
0030 
0031 #define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \
0032         S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])
0033 
0034 #define ROUND(a, b, n) ({ b ^= P[n]; a ^= bf_F(b); })
0035 
0036 static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0037 {
0038     struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
0039     const u32 *P = ctx->p;
0040     const u32 *S = ctx->s;
0041     u32 yl = get_unaligned_be32(src);
0042     u32 yr = get_unaligned_be32(src + 4);
0043 
0044     ROUND(yr, yl, 0);
0045     ROUND(yl, yr, 1);
0046     ROUND(yr, yl, 2);
0047     ROUND(yl, yr, 3);
0048     ROUND(yr, yl, 4);
0049     ROUND(yl, yr, 5);
0050     ROUND(yr, yl, 6);
0051     ROUND(yl, yr, 7);
0052     ROUND(yr, yl, 8);
0053     ROUND(yl, yr, 9);
0054     ROUND(yr, yl, 10);
0055     ROUND(yl, yr, 11);
0056     ROUND(yr, yl, 12);
0057     ROUND(yl, yr, 13);
0058     ROUND(yr, yl, 14);
0059     ROUND(yl, yr, 15);
0060 
0061     yl ^= P[16];
0062     yr ^= P[17];
0063 
0064     put_unaligned_be32(yr, dst);
0065     put_unaligned_be32(yl, dst + 4);
0066 }
0067 
0068 static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
0069 {
0070     struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
0071     const u32 *P = ctx->p;
0072     const u32 *S = ctx->s;
0073     u32 yl = get_unaligned_be32(src);
0074     u32 yr = get_unaligned_be32(src + 4);
0075 
0076     ROUND(yr, yl, 17);
0077     ROUND(yl, yr, 16);
0078     ROUND(yr, yl, 15);
0079     ROUND(yl, yr, 14);
0080     ROUND(yr, yl, 13);
0081     ROUND(yl, yr, 12);
0082     ROUND(yr, yl, 11);
0083     ROUND(yl, yr, 10);
0084     ROUND(yr, yl, 9);
0085     ROUND(yl, yr, 8);
0086     ROUND(yr, yl, 7);
0087     ROUND(yl, yr, 6);
0088     ROUND(yr, yl, 5);
0089     ROUND(yl, yr, 4);
0090     ROUND(yr, yl, 3);
0091     ROUND(yl, yr, 2);
0092 
0093     yl ^= P[1];
0094     yr ^= P[0];
0095 
0096     put_unaligned_be32(yr, dst);
0097     put_unaligned_be32(yl, dst + 4);
0098 }
0099 
0100 static struct crypto_alg alg = {
0101     .cra_name       =   "blowfish",
0102     .cra_driver_name    =   "blowfish-generic",
0103     .cra_priority       =   100,
0104     .cra_flags      =   CRYPTO_ALG_TYPE_CIPHER,
0105     .cra_blocksize      =   BF_BLOCK_SIZE,
0106     .cra_ctxsize        =   sizeof(struct bf_ctx),
0107     .cra_module     =   THIS_MODULE,
0108     .cra_u          =   { .cipher = {
0109     .cia_min_keysize    =   BF_MIN_KEY_SIZE,
0110     .cia_max_keysize    =   BF_MAX_KEY_SIZE,
0111     .cia_setkey     =   blowfish_setkey,
0112     .cia_encrypt        =   bf_encrypt,
0113     .cia_decrypt        =   bf_decrypt } }
0114 };
0115 
0116 static int __init blowfish_mod_init(void)
0117 {
0118     return crypto_register_alg(&alg);
0119 }
0120 
0121 static void __exit blowfish_mod_fini(void)
0122 {
0123     crypto_unregister_alg(&alg);
0124 }
0125 
0126 subsys_initcall(blowfish_mod_init);
0127 module_exit(blowfish_mod_fini);
0128 
0129 MODULE_LICENSE("GPL");
0130 MODULE_DESCRIPTION("Blowfish Cipher Algorithm");
0131 MODULE_ALIAS_CRYPTO("blowfish");
0132 MODULE_ALIAS_CRYPTO("blowfish-generic");