3 * $Id: blowfish.c,v 1.4 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/bits.h>
35 #include "blowfish-tab.h"
40 /*----- Global variables --------------------------------------------------*/
42 static const blowfish_ctx ikey
= BLOWFISH_IKEY
;
44 const octet blowfish_keysz
[] = { KSZ_RANGE
, BLOWFISH_KEYSZ
, 1, 56, 1 };
46 /*----- Macros ------------------------------------------------------------*/
48 #define ROUND(k, x, y, r) do { \
50 y ^= ((k->s0[U8(x >> 24)] + \
51 k->s1[U8(x >> 16)]) ^ \
52 k->s2[U8(x >> 8)]) + \
56 #define EBLK(k, a, b, c, d) do { \
57 const uint32 *_r = k->p; \
60 ROUND(k, _x, _y, _r++); \
61 ROUND(k, _y, _x, _r++); \
62 ROUND(k, _x, _y, _r++); \
63 ROUND(k, _y, _x, _r++); \
64 ROUND(k, _x, _y, _r++); \
65 ROUND(k, _y, _x, _r++); \
66 ROUND(k, _x, _y, _r++); \
67 ROUND(k, _y, _x, _r++); \
68 ROUND(k, _x, _y, _r++); \
69 ROUND(k, _y, _x, _r++); \
70 ROUND(k, _x, _y, _r++); \
71 ROUND(k, _y, _x, _r++); \
72 ROUND(k, _x, _y, _r++); \
73 ROUND(k, _y, _x, _r++); \
74 ROUND(k, _x, _y, _r++); \
75 ROUND(k, _y, _x, _r++); \
80 #define DBLK(k, a, b, c, d) do { \
81 const uint32 *_r = k->p + 18; \
84 ROUND(k, _x, _y, --_r); \
85 ROUND(k, _y, _x, --_r); \
86 ROUND(k, _x, _y, --_r); \
87 ROUND(k, _y, _x, --_r); \
88 ROUND(k, _x, _y, --_r); \
89 ROUND(k, _y, _x, --_r); \
90 ROUND(k, _x, _y, --_r); \
91 ROUND(k, _y, _x, --_r); \
92 ROUND(k, _x, _y, --_r); \
93 ROUND(k, _y, _x, --_r); \
94 ROUND(k, _x, _y, --_r); \
95 ROUND(k, _y, _x, --_r); \
96 ROUND(k, _x, _y, --_r); \
97 ROUND(k, _y, _x, --_r); \
98 ROUND(k, _x, _y, --_r); \
99 ROUND(k, _y, _x, --_r); \
104 /*----- Low-level encryption interface ------------------------------------*/
106 /* --- @blowfish_init@ --- *
108 * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in
109 * @const void *buf@ = pointer to buffer of key material
110 * @size_t sz@ = size of key material
114 * Use: Initializes a Blowfish key buffer. Blowfish accepts
115 * a more-or-less arbitrary size key.
118 void blowfish_init(blowfish_ctx
*k
, const void *buf
, size_t sz
)
120 KSZ_ASSERT(blowfish
, 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 -------------------------------------------------*/