chiark / gitweb /
Rearrange the file tree.
[catacomb] / symm / blowfish.c
1 /* -*-c-*-
2  *
3  * The Blowfish block cipher
4  *
5  * (c) 1998 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/bits.h>
31
32 #include "blowfish.h"
33 #include "blowfish-tab.h"
34 #include "blkc.h"
35 #include "gcipher.h"
36 #include "paranoia.h"
37
38 /*----- Global variables --------------------------------------------------*/
39
40 static const blowfish_ctx ikey = BLOWFISH_IKEY;
41
42 const octet blowfish_keysz[] = { KSZ_RANGE, BLOWFISH_KEYSZ, 1, 56, 1 };
43
44 /*----- Macros ------------------------------------------------------------*/
45
46 #define ROUND(k, x, y, r) do {                                          \
47   x ^= *r;                                                              \
48   y ^= ((k->s0[U8(x >> 24)] +                                           \
49          k->s1[U8(x >> 16)]) ^                                          \
50          k->s2[U8(x >>  8)]) +                                          \
51          k->s3[U8(x >>  0)];                                            \
52 } while (0)
53
54 #define EBLK(k, a, b, c, d) do {                                        \
55   const uint32 *_r = k->p;                                              \
56   uint32 _x = a;                                                        \
57   uint32 _y = b;                                                        \
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++);                                               \
74   c = _y ^ k->p[17];                                                    \
75   d = _x ^ k->p[16];                                                    \
76 } while (0)
77
78 #define DBLK(k, a, b, c, d) do {                                        \
79   const uint32 *_r = k->p + 18;                                         \
80   uint32 _x = a;                                                        \
81   uint32 _y = b;                                                        \
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);                                               \
98   c = _y ^ k->p[0];                                                     \
99   d = _x ^ k->p[1];                                                     \
100 } while (0)
101
102 /*----- Low-level encryption interface ------------------------------------*/
103
104 /* --- @blowfish_init@ --- *
105  *
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
109  *
110  * Returns:     ---
111  *
112  * Use:         Initializes a Blowfish key buffer.  Blowfish accepts
113  *              a more-or-less arbitrary size key.
114  */
115
116 void blowfish_init(blowfish_ctx *k, const void *buf, size_t sz)
117 {
118   KSZ_ASSERT(blowfish, sz);
119
120   /* --- Copy the initial value over --- */
121
122   memcpy(k, &ikey, sizeof(ikey));
123
124   /* --- Initialize the %$P$% array --- */
125
126   {
127     const octet *p = buf;
128     const octet *q = p + sz;
129     int i = 0, j = 0;
130     uint32 x = 0;
131
132     while (i < 18) {
133       x = (x << 8) | U8(*p++);
134       if (p >= q)
135         p = buf;
136       if (++j >= 4) {
137         k->p[i++] ^= x;
138         x = 0;
139         j = 0;
140       }
141     }
142
143     x = 0;
144   }
145
146   /* --- Now mangle the complete array of keys --- */
147
148   {
149     uint32 b[2];
150     int i;
151
152     b[0] = b[1] = 0;
153
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];
157     }
158
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];
162     }
163
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];
167     }
168
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];
172     }
173
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];
177     }
178
179     BURN(b);
180   }
181 }
182
183 /* --- @blowfish_eblk@, @blowfish_dblk@ --- *
184  *
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
188  *
189  * Returns:     ---
190  *
191  * Use:         Low-level block encryption and decryption.
192  */
193
194 void blowfish_eblk(const blowfish_ctx *k, const uint32 *s, uint32 *d)
195 {
196   EBLK(k, s[0], s[1], d[0], d[1]);
197 }
198
199 void blowfish_dblk(const blowfish_ctx *k, const uint32 *s, uint32 *d)
200 {
201   DBLK(k, s[0], s[1], d[0], d[1]);
202 }
203
204 BLKC_TEST(BLOWFISH, blowfish)
205
206 /*----- That's all, folks -------------------------------------------------*/