Generic interface.
[u/mdw/catacomb] / blowfish.c
1 /* -*-c-*-
2 *
3 * $Id: blowfish.c,v 1.1 1999/09/03 08:41:11 mdw Exp $
4 *
5 * The Blowfish block cipher
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: blowfish.c,v $
33 * Revision 1.1 1999/09/03 08:41:11 mdw
34 * Initial import.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <mLib/bits.h>
41
42 #include "blowfish.h"
43 #include "bf_ikey.h"
44 #include "blkc.h"
45 #include "paranoia.h"
46
47 /*----- Global variables --------------------------------------------------*/
48
49 static blowfish_ctx ikey = BLOWFISH_IKEY;
50
51 /*----- Macros ------------------------------------------------------------*/
52
53 #define ROUND(k, x, y, r) \
54 ((x) ^= (k)->p[(r)], \
55 (y) ^= (((((k)->s0[((x) >> 24) & MASK8]) + \
56 ((k)->s1[((x) >> 16) & MASK8])) ^ \
57 ((k)->s2[((x) >> 8) & MASK8])) + \
58 ((k)->s3[((x) >> 0) & MASK8])))
59
60 #define EBLK(k, a, b, c, d) do { \
61 uint32 _x = (a); \
62 uint32 _y = (b); \
63 ROUND((k), _x, _y, 0); \
64 ROUND((k), _y, _x, 1); \
65 ROUND((k), _x, _y, 2); \
66 ROUND((k), _y, _x, 3); \
67 ROUND((k), _x, _y, 4); \
68 ROUND((k), _y, _x, 5); \
69 ROUND((k), _x, _y, 6); \
70 ROUND((k), _y, _x, 7); \
71 ROUND((k), _x, _y, 8); \
72 ROUND((k), _y, _x, 9); \
73 ROUND((k), _x, _y, 10); \
74 ROUND((k), _y, _x, 11); \
75 ROUND((k), _x, _y, 12); \
76 ROUND((k), _y, _x, 13); \
77 ROUND((k), _x, _y, 14); \
78 ROUND((k), _y, _x, 15); \
79 (c) = _y ^ (k)->p[17]; \
80 (d) = _x ^ (k)->p[16]; \
81 } while (0)
82
83 #define DBLK(k, a, b, c, d) do { \
84 uint32 _x = (a); \
85 uint32 _y = (b); \
86 ROUND((k), _x, _y, 17); \
87 ROUND((k), _y, _x, 16); \
88 ROUND((k), _x, _y, 15); \
89 ROUND((k), _y, _x, 14); \
90 ROUND((k), _x, _y, 13); \
91 ROUND((k), _y, _x, 12); \
92 ROUND((k), _x, _y, 11); \
93 ROUND((k), _y, _x, 10); \
94 ROUND((k), _x, _y, 9); \
95 ROUND((k), _y, _x, 8); \
96 ROUND((k), _x, _y, 7); \
97 ROUND((k), _y, _x, 6); \
98 ROUND((k), _x, _y, 5); \
99 ROUND((k), _y, _x, 4); \
100 ROUND((k), _x, _y, 3); \
101 ROUND((k), _y, _x, 2); \
102 (c) = _y ^ (k)->p[0]; \
103 (d) = _x ^ (k)->p[1]; \
104 } while (0)
105
106 /*----- Low-level encryption interface ------------------------------------*/
107
108 /* --- @blowfish_init@ --- *
109 *
110 * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in
111 * @const void *buf@ = pointer to buffer of key material
112 * @size_t sz@ = size of key material
113 *
114 * Returns: ---
115 *
116 * Use: Initializes a Blowfish key buffer. Blowfish accepts
117 * a more-or-less arbitrary size key.
118 */
119
120 void blowfish_init(blowfish_ctx *k, const void *buf, size_t sz)
121 {
122 /* --- Copy the initial value over --- */
123
124 memcpy(k, &ikey, sizeof(ikey));
125
126 /* --- Initialize the %$P$% array --- */
127
128 {
129 const octet *p = buf;
130 const octet *q = p + sz;
131 int i = 0, j = 0;
132 uint32 x = 0;
133
134 while (i < 18) {
135 x = (x << 8) | U8(*p++);
136 if (p >= q)
137 p = buf;
138 if (++j >= 4) {
139 k->p[i++] ^= x;
140 x = 0;
141 j = 0;
142 }
143 }
144
145 x = 0;
146 }
147
148 /* --- Now mangle the complete array of keys --- */
149
150 {
151 uint32 b[2];
152 int i;
153
154 b[0] = b[1] = 0;
155
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];
159 }
160
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];
164 }
165
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];
169 }
170
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];
174 }
175
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];
179 }
180
181 BURN(b);
182 }
183 }
184
185 /* --- @blowfish_eblk@, @blowfish_dblk@ --- *
186 *
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
190 *
191 * Returns: ---
192 *
193 * Use: Low-level block encryption and decryption.
194 */
195
196 void blowfish_eblk(const blowfish_ctx *k, const uint32 *s, uint32 *d)
197 {
198 EBLK(k, s[0], s[1], d[0], d[1]);
199 }
200
201 void blowfish_dblk(const blowfish_ctx *k, const uint32 *s, uint32 *d)
202 {
203 DBLK(k, s[0], s[1], d[0], d[1]);
204 }
205
206 BLKC_TEST(BLOWFISH, blowfish)
207
208 /*----- That's all, folks -------------------------------------------------*/