blob: 0e74c7242e77e409ebfa7b71d1759a5f3f10081e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Richard Hartmann0d8fb0a2010-02-16 20:24:30 +08002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Herbert Xu14386d42022-11-25 12:36:28 +080014
15#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/mm.h>
Ard Biesheuvel50a3a9f2021-02-01 19:02:32 +010019#include <asm/unaligned.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110020#include <linux/types.h>
Jussi Kivilinna52ba8672011-09-02 01:45:07 +030021#include <crypto/blowfish.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Richard Hartmann0d8fb0a2010-02-16 20:24:30 +080023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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)]) ^ \
Jussi Kivilinna3f2a5d22011-09-02 01:45:12 +030033 S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jussi Kivilinna3f2a5d22011-09-02 01:45:12 +030035#define ROUND(a, b, n) ({ b ^= P[n]; a ^= bf_F(b); })
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Jussi Kivilinna52ba8672011-09-02 01:45:07 +030037static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Jussi Kivilinna52ba8672011-09-02 01:45:07 +030039 struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
Jussi Kivilinna52ba8672011-09-02 01:45:07 +030040 const u32 *P = ctx->p;
41 const u32 *S = ctx->s;
Ard Biesheuvel50a3a9f2021-02-01 19:02:32 +010042 u32 yl = get_unaligned_be32(src);
43 u32 yr = get_unaligned_be32(src + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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
Ard Biesheuvel50a3a9f2021-02-01 19:02:32 +010065 put_unaligned_be32(yr, dst);
66 put_unaligned_be32(yl, dst + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Herbert Xu6c2bb982006-05-16 22:09:29 +100069static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Herbert Xu6c2bb982006-05-16 22:09:29 +100071 struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu6c2bb982006-05-16 22:09:29 +100072 const u32 *P = ctx->p;
73 const u32 *S = ctx->s;
Ard Biesheuvel50a3a9f2021-02-01 19:02:32 +010074 u32 yl = get_unaligned_be32(src);
75 u32 yr = get_unaligned_be32(src + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
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
Ard Biesheuvel50a3a9f2021-02-01 19:02:32 +010097 put_unaligned_be32(yr, dst);
98 put_unaligned_be32(yl, dst + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct crypto_alg alg = {
102 .cra_name = "blowfish",
Jussi Kivilinna3f2a5d22011-09-02 01:45:12 +0300103 .cra_driver_name = "blowfish-generic",
104 .cra_priority = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
106 .cra_blocksize = BF_BLOCK_SIZE,
107 .cra_ctxsize = sizeof(struct bf_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .cra_module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .cra_u = { .cipher = {
110 .cia_min_keysize = BF_MIN_KEY_SIZE,
111 .cia_max_keysize = BF_MAX_KEY_SIZE,
Jussi Kivilinna52ba8672011-09-02 01:45:07 +0300112 .cia_setkey = blowfish_setkey,
Jussi Kivilinna3f2a5d22011-09-02 01:45:12 +0300113 .cia_encrypt = bf_encrypt,
114 .cia_decrypt = bf_decrypt } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800117static int __init blowfish_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 return crypto_register_alg(&alg);
120}
121
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800122static void __exit blowfish_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 crypto_unregister_alg(&alg);
125}
126
Eric Biggersc4741b22019-04-11 21:57:42 -0700127subsys_initcall(blowfish_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800128module_exit(blowfish_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130MODULE_LICENSE("GPL");
131MODULE_DESCRIPTION("Blowfish Cipher Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800132MODULE_ALIAS_CRYPTO("blowfish");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100133MODULE_ALIAS_CRYPTO("blowfish-generic");