Back to home page

LXR

 
 

    


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