3 * $Id: blowfish.c,v 1.1 1999/09/03 08:41:11 mdw Exp $
5 * The Blowfish block cipher
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: blowfish.c,v $
33 * Revision 1.1 1999/09/03 08:41:11 mdw
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
47 /*----- Global variables --------------------------------------------------*/
49 static blowfish_ctx ikey
= BLOWFISH_IKEY
;
51 /*----- Macros ------------------------------------------------------------*/
53 #define ROUND(k, x, y, r) \
54 ((x) ^= (k)->p[(r)], \
55 (y) ^= (((((k)->s0[((x) >> 24) & MASK8]) + \
56 ((k)->s1[((x) >> 16) & MASK8])) ^ \
57 ((k)->s2[((x) >> 8) & MASK8])) + \
58 ((k)->s3[((x) >> 0) & MASK8])))
60 #define EBLK(k, a, b, c, d) do { \
63 ROUND((k), _x, _y, 0); \
64 ROUND((k), _y, _x, 1); \
65 ROUND((k), _x, _y, 2); \
66 ROUND((k), _y, _x, 3); \
67 ROUND((k), _x, _y, 4); \
68 ROUND((k), _y, _x, 5); \
69 ROUND((k), _x, _y, 6); \
70 ROUND((k), _y, _x, 7); \
71 ROUND((k), _x, _y, 8); \
72 ROUND((k), _y, _x, 9); \
73 ROUND((k), _x, _y, 10); \
74 ROUND((k), _y, _x, 11); \
75 ROUND((k), _x, _y, 12); \
76 ROUND((k), _y, _x, 13); \
77 ROUND((k), _x, _y, 14); \
78 ROUND((k), _y, _x, 15); \
79 (c) = _y ^ (k)->p[17]; \
80 (d) = _x ^ (k)->p[16]; \
83 #define DBLK(k, a, b, c, d) do { \
86 ROUND((k), _x, _y, 17); \
87 ROUND((k), _y, _x, 16); \
88 ROUND((k), _x, _y, 15); \
89 ROUND((k), _y, _x, 14); \
90 ROUND((k), _x, _y, 13); \
91 ROUND((k), _y, _x, 12); \
92 ROUND((k), _x, _y, 11); \
93 ROUND((k), _y, _x, 10); \
94 ROUND((k), _x, _y, 9); \
95 ROUND((k), _y, _x, 8); \
96 ROUND((k), _x, _y, 7); \
97 ROUND((k), _y, _x, 6); \
98 ROUND((k), _x, _y, 5); \
99 ROUND((k), _y, _x, 4); \
100 ROUND((k), _x, _y, 3); \
101 ROUND((k), _y, _x, 2); \
102 (c) = _y ^ (k)->p[0]; \
103 (d) = _x ^ (k)->p[1]; \
106 /*----- Low-level encryption interface ------------------------------------*/
108 /* --- @blowfish_init@ --- *
110 * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in
111 * @const void *buf@ = pointer to buffer of key material
112 * @size_t sz@ = size of key material
116 * Use: Initializes a Blowfish key buffer. Blowfish accepts
117 * a more-or-less arbitrary size key.
120 void blowfish_init(blowfish_ctx
*k
, const void *buf
, size_t sz
)
122 /* --- Copy the initial value over --- */
124 memcpy(k
, &ikey
, sizeof(ikey
));
126 /* --- Initialize the %$P$% array --- */
129 const octet
*p
= buf
;
130 const octet
*q
= p
+ sz
;
135 x
= (x
<< 8) | U8(*p
++);
148 /* --- Now mangle the complete array of keys --- */
156 for (i
= 0; i
< 18; i
+= 2) {
157 blowfish_eblk(k
, b
, b
);
158 k
->p
[i
] = b
[0]; k
->p
[i
+ 1] = b
[1];
161 for (i
= 0; i
< 256; i
+= 2) {
162 blowfish_eblk(k
, b
, b
);
163 k
->s0
[i
] = b
[0]; k
->s0
[i
+ 1] = b
[1];
166 for (i
= 0; i
< 256; i
+= 2) {
167 blowfish_eblk(k
, b
, b
);
168 k
->s1
[i
] = b
[0]; k
->s1
[i
+ 1] = b
[1];
171 for (i
= 0; i
< 256; i
+= 2) {
172 blowfish_eblk(k
, b
, b
);
173 k
->s2
[i
] = b
[0]; k
->s2
[i
+ 1] = b
[1];
176 for (i
= 0; i
< 256; i
+= 2) {
177 blowfish_eblk(k
, b
, b
);
178 k
->s3
[i
] = b
[0]; k
->s3
[i
+ 1] = b
[1];
185 /* --- @blowfish_eblk@, @blowfish_dblk@ --- *
187 * Arguments: @const blowfish_ctx *k@ = pointer to key block
188 * @const uint32 s[2]@ = pointer to source block
189 * @uint32 d[2]@ = pointer to destination block
193 * Use: Low-level block encryption and decryption.
196 void blowfish_eblk(const blowfish_ctx
*k
, const uint32
*s
, uint32
*d
)
198 EBLK(k
, s
[0], s
[1], d
[0], d
[1]);
201 void blowfish_dblk(const blowfish_ctx
*k
, const uint32
*s
, uint32
*d
)
203 DBLK(k
, s
[0], s
[1], d
[0], d
[1]);
206 BLKC_TEST(BLOWFISH
, blowfish
)
208 /*----- That's all, folks -------------------------------------------------*/