Blame view

crypto/blowfish_generic.c 3.23 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
0d8fb0a13   Richard Hartmann   crypto: blowfish ...
2
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
   * Cryptographic API.
   *
   * Blowfish Cipher Algorithm, by Bruce Schneier.
   * http://www.counterpane.com/blowfish.html
   *
   * Adapted from Kerneli implementation.
   *
   * Copyright (c) Herbert Valerio Riedel <hvr@hvrlab.org>
   * Copyright (c) Kyle McMartin <kyle@debian.org>
   * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
   */
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/mm.h>
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
17
  #include <asm/byteorder.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/crypto.h>
06ace7a9b   Herbert Xu   [CRYPTO] Use stan...
19
  #include <linux/types.h>
52ba867c8   Jussi Kivilinna   crypto: blowfish ...
20
  #include <crypto/blowfish.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21

0d8fb0a13   Richard Hartmann   crypto: blowfish ...
22
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
27
28
29
30
31
   * Round loop unrolling macros, S is a pointer to a S-Box array
   * organized in 4 unsigned longs at a row.
   */
  #define GET32_3(x) (((x) & 0xff))
  #define GET32_2(x) (((x) >> (8)) & (0xff))
  #define GET32_1(x) (((x) >> (16)) & (0xff))
  #define GET32_0(x) (((x) >> (24)) & (0xff))
  
  #define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \
3f2a5d2d4   Jussi Kivilinna   crypto: blowfish ...
32
  		S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33

3f2a5d2d4   Jussi Kivilinna   crypto: blowfish ...
34
  #define ROUND(a, b, n) ({ b ^= P[n]; a ^= bf_F(b); })
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35

52ba867c8   Jussi Kivilinna   crypto: blowfish ...
36
  static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  {
52ba867c8   Jussi Kivilinna   crypto: blowfish ...
38
39
40
41
42
43
44
  	struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
  	const __be32 *in_blk = (const __be32 *)src;
  	__be32 *const out_blk = (__be32 *)dst;
  	const u32 *P = ctx->p;
  	const u32 *S = ctx->s;
  	u32 yl = be32_to_cpu(in_blk[0]);
  	u32 yr = be32_to_cpu(in_blk[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  
  	ROUND(yr, yl, 0);
  	ROUND(yl, yr, 1);
  	ROUND(yr, yl, 2);
  	ROUND(yl, yr, 3);
  	ROUND(yr, yl, 4);
  	ROUND(yl, yr, 5);
  	ROUND(yr, yl, 6);
  	ROUND(yl, yr, 7);
  	ROUND(yr, yl, 8);
  	ROUND(yl, yr, 9);
  	ROUND(yr, yl, 10);
  	ROUND(yl, yr, 11);
  	ROUND(yr, yl, 12);
  	ROUND(yl, yr, 13);
  	ROUND(yr, yl, 14);
  	ROUND(yl, yr, 15);
  
  	yl ^= P[16];
  	yr ^= P[17];
52ba867c8   Jussi Kivilinna   crypto: blowfish ...
65
66
  	out_blk[0] = cpu_to_be32(yr);
  	out_blk[1] = cpu_to_be32(yl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  }
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
68
  static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
  {
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
70
  	struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
  	const __be32 *in_blk = (const __be32 *)src;
  	__be32 *const out_blk = (__be32 *)dst;
6c2bb98bc   Herbert Xu   [CRYPTO] all: Pas...
73
74
  	const u32 *P = ctx->p;
  	const u32 *S = ctx->s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  	u32 yl = be32_to_cpu(in_blk[0]);
  	u32 yr = be32_to_cpu(in_blk[1]);
  
  	ROUND(yr, yl, 17);
  	ROUND(yl, yr, 16);
  	ROUND(yr, yl, 15);
  	ROUND(yl, yr, 14);
  	ROUND(yr, yl, 13);
  	ROUND(yl, yr, 12);
  	ROUND(yr, yl, 11);
  	ROUND(yl, yr, 10);
  	ROUND(yr, yl, 9);
  	ROUND(yl, yr, 8);
  	ROUND(yr, yl, 7);
  	ROUND(yl, yr, 6);
  	ROUND(yr, yl, 5);
  	ROUND(yl, yr, 4);
  	ROUND(yr, yl, 3);
  	ROUND(yl, yr, 2);
  
  	yl ^= P[1];
  	yr ^= P[0];
  
  	out_blk[0] = cpu_to_be32(yr);
  	out_blk[1] = cpu_to_be32(yl);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  static struct crypto_alg alg = {
  	.cra_name		=	"blowfish",
3f2a5d2d4   Jussi Kivilinna   crypto: blowfish ...
103
104
  	.cra_driver_name	=	"blowfish-generic",
  	.cra_priority		=	100,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
  	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
  	.cra_blocksize		=	BF_BLOCK_SIZE,
  	.cra_ctxsize		=	sizeof(struct bf_ctx),
a429d2609   Herbert Xu   [CRYPTO] cipher: ...
108
  	.cra_alignmask		=	3,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  	.cra_module		=	THIS_MODULE,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
  	.cra_u			=	{ .cipher = {
  	.cia_min_keysize	=	BF_MIN_KEY_SIZE,
  	.cia_max_keysize	=	BF_MAX_KEY_SIZE,
52ba867c8   Jussi Kivilinna   crypto: blowfish ...
113
  	.cia_setkey		=	blowfish_setkey,
3f2a5d2d4   Jussi Kivilinna   crypto: blowfish ...
114
115
  	.cia_encrypt		=	bf_encrypt,
  	.cia_decrypt		=	bf_decrypt } }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  };
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
117
  static int __init blowfish_mod_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
  {
  	return crypto_register_alg(&alg);
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
121
  static void __exit blowfish_mod_fini(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
  {
  	crypto_unregister_alg(&alg);
  }
c4741b230   Eric Biggers   crypto: run initc...
125
  subsys_initcall(blowfish_mod_init);
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
126
  module_exit(blowfish_mod_fini);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
  
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Blowfish Cipher Algorithm");
5d26a105b   Kees Cook   crypto: prefix mo...
130
  MODULE_ALIAS_CRYPTO("blowfish");
3e14dcf7c   Mathias Krause   crypto: add missi...
131
  MODULE_ALIAS_CRYPTO("blowfish-generic");