d03ab969 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: blowfish.c,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
d03ab969 |
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 | |
d03ab969 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <mLib/bits.h> |
33 | |
34 | #include "blowfish.h" |
2e6a5ab5 |
35 | #include "blowfish-tab.h" |
d03ab969 |
36 | #include "blkc.h" |
2e6a5ab5 |
37 | #include "gcipher.h" |
d03ab969 |
38 | #include "paranoia.h" |
39 | |
40 | /*----- Global variables --------------------------------------------------*/ |
41 | |
4e66da02 |
42 | static const blowfish_ctx ikey = BLOWFISH_IKEY; |
d03ab969 |
43 | |
2e6a5ab5 |
44 | const octet blowfish_keysz[] = { KSZ_RANGE, BLOWFISH_KEYSZ, 1, 56, 1 }; |
45 | |
d03ab969 |
46 | /*----- Macros ------------------------------------------------------------*/ |
47 | |
2e6a5ab5 |
48 | #define ROUND(k, x, y, r) do { \ |
49 | x ^= *r; \ |
50 | y ^= ((k->s0[U8(x >> 24)] + \ |
51 | k->s1[U8(x >> 16)]) ^ \ |
52 | k->s2[U8(x >> 8)]) + \ |
53 | k->s3[U8(x >> 0)]; \ |
54 | } while (0) |
d03ab969 |
55 | |
56 | #define EBLK(k, a, b, c, d) do { \ |
2e6a5ab5 |
57 | const uint32 *_r = k->p; \ |
58 | uint32 _x = a; \ |
59 | uint32 _y = b; \ |
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 | ROUND(k, _x, _y, _r++); \ |
75 | ROUND(k, _y, _x, _r++); \ |
76 | c = _y ^ k->p[17]; \ |
77 | d = _x ^ k->p[16]; \ |
d03ab969 |
78 | } while (0) |
79 | |
80 | #define DBLK(k, a, b, c, d) do { \ |
2e6a5ab5 |
81 | const uint32 *_r = k->p + 18; \ |
82 | uint32 _x = a; \ |
83 | uint32 _y = b; \ |
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 | ROUND(k, _x, _y, --_r); \ |
99 | ROUND(k, _y, _x, --_r); \ |
100 | c = _y ^ k->p[0]; \ |
101 | d = _x ^ k->p[1]; \ |
d03ab969 |
102 | } while (0) |
103 | |
104 | /*----- Low-level encryption interface ------------------------------------*/ |
105 | |
106 | /* --- @blowfish_init@ --- * |
107 | * |
108 | * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in |
109 | * @const void *buf@ = pointer to buffer of key material |
110 | * @size_t sz@ = size of key material |
111 | * |
112 | * Returns: --- |
113 | * |
114 | * Use: Initializes a Blowfish key buffer. Blowfish accepts |
115 | * a more-or-less arbitrary size key. |
116 | */ |
117 | |
118 | void blowfish_init(blowfish_ctx *k, const void *buf, size_t sz) |
119 | { |
2e6a5ab5 |
120 | KSZ_ASSERT(blowfish, sz); |
121 | |
d03ab969 |
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 -------------------------------------------------*/ |