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