3 * $Id: blowfish.c,v 1.2 2000/06/17 10:47:56 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.2 2000/06/17 10:47:56 mdw
34 * Tidy round function a little. Support new key size interface.
36 * Revision 1.1 1999/09/03 08:41:11 mdw
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
46 #include "blowfish-tab.h"
51 /*----- Global variables --------------------------------------------------*/
53 static blowfish_ctx ikey
= BLOWFISH_IKEY
;
55 const octet blowfish_keysz
[] = { KSZ_RANGE
, BLOWFISH_KEYSZ
, 1, 56, 1 };
57 /*----- Macros ------------------------------------------------------------*/
59 #define ROUND(k, x, y, r) do { \
61 y ^= ((k->s0[U8(x >> 24)] + \
62 k->s1[U8(x >> 16)]) ^ \
63 k->s2[U8(x >> 8)]) + \
67 #define EBLK(k, a, b, c, d) do { \
68 const uint32 *_r = k->p; \
71 ROUND(k, _x, _y, _r++); \
72 ROUND(k, _y, _x, _r++); \
73 ROUND(k, _x, _y, _r++); \
74 ROUND(k, _y, _x, _r++); \
75 ROUND(k, _x, _y, _r++); \
76 ROUND(k, _y, _x, _r++); \
77 ROUND(k, _x, _y, _r++); \
78 ROUND(k, _y, _x, _r++); \
79 ROUND(k, _x, _y, _r++); \
80 ROUND(k, _y, _x, _r++); \
81 ROUND(k, _x, _y, _r++); \
82 ROUND(k, _y, _x, _r++); \
83 ROUND(k, _x, _y, _r++); \
84 ROUND(k, _y, _x, _r++); \
85 ROUND(k, _x, _y, _r++); \
86 ROUND(k, _y, _x, _r++); \
91 #define DBLK(k, a, b, c, d) do { \
92 const uint32 *_r = k->p + 18; \
95 ROUND(k, _x, _y, --_r); \
96 ROUND(k, _y, _x, --_r); \
97 ROUND(k, _x, _y, --_r); \
98 ROUND(k, _y, _x, --_r); \
99 ROUND(k, _x, _y, --_r); \
100 ROUND(k, _y, _x, --_r); \
101 ROUND(k, _x, _y, --_r); \
102 ROUND(k, _y, _x, --_r); \
103 ROUND(k, _x, _y, --_r); \
104 ROUND(k, _y, _x, --_r); \
105 ROUND(k, _x, _y, --_r); \
106 ROUND(k, _y, _x, --_r); \
107 ROUND(k, _x, _y, --_r); \
108 ROUND(k, _y, _x, --_r); \
109 ROUND(k, _x, _y, --_r); \
110 ROUND(k, _y, _x, --_r); \
115 /*----- Low-level encryption interface ------------------------------------*/
117 /* --- @blowfish_init@ --- *
119 * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in
120 * @const void *buf@ = pointer to buffer of key material
121 * @size_t sz@ = size of key material
125 * Use: Initializes a Blowfish key buffer. Blowfish accepts
126 * a more-or-less arbitrary size key.
129 void blowfish_init(blowfish_ctx
*k
, const void *buf
, size_t sz
)
131 KSZ_ASSERT(blowfish
, sz
);
133 /* --- Copy the initial value over --- */
135 memcpy(k
, &ikey
, sizeof(ikey
));
137 /* --- Initialize the %$P$% array --- */
140 const octet
*p
= buf
;
141 const octet
*q
= p
+ sz
;
146 x
= (x
<< 8) | U8(*p
++);
159 /* --- Now mangle the complete array of keys --- */
167 for (i
= 0; i
< 18; i
+= 2) {
168 blowfish_eblk(k
, b
, b
);
169 k
->p
[i
] = b
[0]; k
->p
[i
+ 1] = b
[1];
172 for (i
= 0; i
< 256; i
+= 2) {
173 blowfish_eblk(k
, b
, b
);
174 k
->s0
[i
] = b
[0]; k
->s0
[i
+ 1] = b
[1];
177 for (i
= 0; i
< 256; i
+= 2) {
178 blowfish_eblk(k
, b
, b
);
179 k
->s1
[i
] = b
[0]; k
->s1
[i
+ 1] = b
[1];
182 for (i
= 0; i
< 256; i
+= 2) {
183 blowfish_eblk(k
, b
, b
);
184 k
->s2
[i
] = b
[0]; k
->s2
[i
+ 1] = b
[1];
187 for (i
= 0; i
< 256; i
+= 2) {
188 blowfish_eblk(k
, b
, b
);
189 k
->s3
[i
] = b
[0]; k
->s3
[i
+ 1] = b
[1];
196 /* --- @blowfish_eblk@, @blowfish_dblk@ --- *
198 * Arguments: @const blowfish_ctx *k@ = pointer to key block
199 * @const uint32 s[2]@ = pointer to source block
200 * @uint32 d[2]@ = pointer to destination block
204 * Use: Low-level block encryption and decryption.
207 void blowfish_eblk(const blowfish_ctx
*k
, const uint32
*s
, uint32
*d
)
209 EBLK(k
, s
[0], s
[1], d
[0], d
[1]);
212 void blowfish_dblk(const blowfish_ctx
*k
, const uint32
*s
, uint32
*d
)
214 DBLK(k
, s
[0], s
[1], d
[0], d
[1]);
217 BLKC_TEST(BLOWFISH
, blowfish
)
219 /*----- That's all, folks -------------------------------------------------*/