3 * The Blowfish block cipher
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
37 /*----- Global variables --------------------------------------------------*/
39 extern const blowfish_ctx blowfish_ikey;
41 const octet blowfish_keysz[] = { KSZ_RANGE, BLOWFISH_KEYSZ, 1, 56, 1 };
43 /*----- Macros ------------------------------------------------------------*/
45 #define ROUND(k, x, y, r) do { \
47 y ^= ((k->s0[U8(x >> 24)] + \
48 k->s1[U8(x >> 16)]) ^ \
49 k->s2[U8(x >> 8)]) + \
53 #define EBLK(k, a, b, c, d) do { \
54 const uint32 *_r = k->p; \
57 ROUND(k, _x, _y, _r++); \
58 ROUND(k, _y, _x, _r++); \
59 ROUND(k, _x, _y, _r++); \
60 ROUND(k, _y, _x, _r++); \
61 ROUND(k, _x, _y, _r++); \
62 ROUND(k, _y, _x, _r++); \
63 ROUND(k, _x, _y, _r++); \
64 ROUND(k, _y, _x, _r++); \
65 ROUND(k, _x, _y, _r++); \
66 ROUND(k, _y, _x, _r++); \
67 ROUND(k, _x, _y, _r++); \
68 ROUND(k, _y, _x, _r++); \
69 ROUND(k, _x, _y, _r++); \
70 ROUND(k, _y, _x, _r++); \
71 ROUND(k, _x, _y, _r++); \
72 ROUND(k, _y, _x, _r++); \
77 #define DBLK(k, a, b, c, d) do { \
78 const uint32 *_r = k->p + 18; \
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); \
87 ROUND(k, _x, _y, --_r); \
88 ROUND(k, _y, _x, --_r); \
89 ROUND(k, _x, _y, --_r); \
90 ROUND(k, _y, _x, --_r); \
91 ROUND(k, _x, _y, --_r); \
92 ROUND(k, _y, _x, --_r); \
93 ROUND(k, _x, _y, --_r); \
94 ROUND(k, _y, _x, --_r); \
95 ROUND(k, _x, _y, --_r); \
96 ROUND(k, _y, _x, --_r); \
101 /*----- Low-level encryption interface ------------------------------------*/
103 /* --- @blowfish_init@ --- *
105 * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in
106 * @const void *buf@ = pointer to buffer of key material
107 * @size_t sz@ = size of key material
111 * Use: Initializes a Blowfish key buffer. Blowfish accepts
112 * a more-or-less arbitrary size key.
115 void blowfish_init(blowfish_ctx *k, const void *buf, size_t sz)
117 KSZ_ASSERT(blowfish, sz);
119 /* --- Copy the initial value over --- */
123 /* --- Initialize the %$P$% array --- */
126 const octet *p = buf;
127 const octet *q = p + sz;
132 x = (x << 8) | U8(*p++);
145 /* --- Now mangle the complete array of keys --- */
153 for (i = 0; i < 18; i += 2) {
154 blowfish_eblk(k, b, b);
155 k->p[i] = b[0]; k->p[i + 1] = b[1];
158 for (i = 0; i < 256; i += 2) {
159 blowfish_eblk(k, b, b);
160 k->s0[i] = b[0]; k->s0[i + 1] = b[1];
163 for (i = 0; i < 256; i += 2) {
164 blowfish_eblk(k, b, b);
165 k->s1[i] = b[0]; k->s1[i + 1] = b[1];
168 for (i = 0; i < 256; i += 2) {
169 blowfish_eblk(k, b, b);
170 k->s2[i] = b[0]; k->s2[i + 1] = b[1];
173 for (i = 0; i < 256; i += 2) {
174 blowfish_eblk(k, b, b);
175 k->s3[i] = b[0]; k->s3[i + 1] = b[1];
182 /* --- @blowfish_eblk@, @blowfish_dblk@ --- *
184 * Arguments: @const blowfish_ctx *k@ = pointer to key block
185 * @const uint32 s[2]@ = pointer to source block
186 * @uint32 d[2]@ = pointer to destination block
190 * Use: Low-level block encryption and decryption.
193 void blowfish_eblk(const blowfish_ctx *k, const uint32 *s, uint32 *d)
195 EBLK(k, s[0], s[1], d[0], d[1]);
198 void blowfish_dblk(const blowfish_ctx *k, const uint32 *s, uint32 *d)
200 DBLK(k, s[0], s[1], d[0], d[1]);
203 BLKC_TEST(BLOWFISH, blowfish)
205 /*----- That's all, folks -------------------------------------------------*/