4bfb84d37e999a65fade30405d643897c351c058
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>
33 #include "blowfish-tab.h"
38 /*----- Global variables --------------------------------------------------*/
40 static const blowfish_ctx ikey
= BLOWFISH_IKEY
;
42 const octet blowfish_keysz
[] = { KSZ_RANGE
, BLOWFISH_KEYSZ
, 1, 56, 1 };
44 /*----- Macros ------------------------------------------------------------*/
46 #define ROUND(k, x, y, r) do { \
48 y ^= ((k->s0[U8(x >> 24)] + \
49 k->s1[U8(x >> 16)]) ^ \
50 k->s2[U8(x >> 8)]) + \
54 #define EBLK(k, a, b, c, d) do { \
55 const uint32 *_r = k->p; \
58 ROUND(k, _x, _y, _r++); \
59 ROUND(k, _y, _x, _r++); \
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++); \
78 #define DBLK(k, a, b, c, d) do { \
79 const uint32 *_r = k->p + 18; \
82 ROUND(k, _x, _y, --_r); \
83 ROUND(k, _y, _x, --_r); \
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); \
102 /*----- Low-level encryption interface ------------------------------------*/
104 /* --- @blowfish_init@ --- *
106 * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in
107 * @const void *buf@ = pointer to buffer of key material
108 * @size_t sz@ = size of key material
112 * Use: Initializes a Blowfish key buffer. Blowfish accepts
113 * a more-or-less arbitrary size key.
116 void blowfish_init(blowfish_ctx
*k
, const void *buf
, size_t sz
)
118 KSZ_ASSERT(blowfish
, sz
);
120 /* --- Copy the initial value over --- */
122 memcpy(k
, &ikey
, sizeof(ikey
));
124 /* --- Initialize the %$P$% array --- */
127 const octet
*p
= buf
;
128 const octet
*q
= p
+ sz
;
133 x
= (x
<< 8) | U8(*p
++);
146 /* --- Now mangle the complete array of keys --- */
154 for (i
= 0; i
< 18; i
+= 2) {
155 blowfish_eblk(k
, b
, b
);
156 k
->p
[i
] = b
[0]; k
->p
[i
+ 1] = b
[1];
159 for (i
= 0; i
< 256; i
+= 2) {
160 blowfish_eblk(k
, b
, b
);
161 k
->s0
[i
] = b
[0]; k
->s0
[i
+ 1] = b
[1];
164 for (i
= 0; i
< 256; i
+= 2) {
165 blowfish_eblk(k
, b
, b
);
166 k
->s1
[i
] = b
[0]; k
->s1
[i
+ 1] = b
[1];
169 for (i
= 0; i
< 256; i
+= 2) {
170 blowfish_eblk(k
, b
, b
);
171 k
->s2
[i
] = b
[0]; k
->s2
[i
+ 1] = b
[1];
174 for (i
= 0; i
< 256; i
+= 2) {
175 blowfish_eblk(k
, b
, b
);
176 k
->s3
[i
] = b
[0]; k
->s3
[i
+ 1] = b
[1];
183 /* --- @blowfish_eblk@, @blowfish_dblk@ --- *
185 * Arguments: @const blowfish_ctx *k@ = pointer to key block
186 * @const uint32 s[2]@ = pointer to source block
187 * @uint32 d[2]@ = pointer to destination block
191 * Use: Low-level block encryption and decryption.
194 void blowfish_eblk(const blowfish_ctx
*k
, const uint32
*s
, uint32
*d
)
196 EBLK(k
, s
[0], s
[1], d
[0], d
[1]);
199 void blowfish_dblk(const blowfish_ctx
*k
, const uint32
*s
, uint32
*d
)
201 DBLK(k
, s
[0], s
[1], d
[0], d
[1]);
204 BLKC_TEST(BLOWFISH
, blowfish
)
206 /*----- That's all, folks -------------------------------------------------*/