Commit 3f2a5d2d42e0784b6aa96c2e5dcdd64e28e838d6

Authored by Jussi Kivilinna
Committed by Herbert Xu
1 parent 52ba867c8c

crypto: blowfish - rename C-version to blowfish_generic

Rename blowfish to blowfish_generic so that assembler versions of blowfish
cipher can autoload. Module alias 'blowfish' is added.

Also fix checkpatch warnings.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 3 changed files with 143 additions and 140 deletions Side-by-side Diff

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