Miscellaneous constification.
[u/mdw/catacomb] / des-base.h
1 /* -*-c-*-
2 *
3 * $Id: des-base.h,v 1.3 2004/04/02 01:03:49 mdw Exp $
4 *
5 * Common features for DES implementation
6 *
7 * (c) 1999 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: des-base.h,v $
33 * Revision 1.3 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.2 1999/12/10 23:29:48 mdw
37 * Change header file guard names.
38 *
39 * Revision 1.1 1999/09/03 08:41:11 mdw
40 * Initial import.
41 *
42 */
43
44 #ifndef CATACOMB_DES_BASE_H
45 #define CATACOMB_DES_BASE_H
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /*----- Header files ------------------------------------------------------*/
52
53 #include <mLib/bits.h>
54
55 /*----- External data -----------------------------------------------------*/
56
57 extern const uint32 des_sp[8][64];
58
59 /*----- Macros ------------------------------------------------------------*/
60
61 /* --- @DES_ROUND@ --- *
62 *
63 * This is the basic DES round function. The inputs are the two subkey
64 * halves, and the left and right block halves. Note that the block halves
65 * are rotated left one place at this point. This wraps what's meant to be
66 * the top bit around to the bottom, so I get a clear run at the S-boxes.
67 */
68
69 #define DES_ROUND(ka, kb, x, y) do { \
70 uint32 _t = (y) ^ (ka); \
71 (x) ^= des_sp[7][(_t >> 0) & 0x3f] ^ \
72 des_sp[5][(_t >> 8) & 0x3f] ^ \
73 des_sp[3][(_t >> 16) & 0x3f] ^ \
74 des_sp[1][(_t >> 24) & 0x3f]; \
75 _t = ROR32((y), 4) ^ (kb); \
76 (x) ^= des_sp[6][(_t >> 0) & 0x3f] ^ \
77 des_sp[4][(_t >> 8) & 0x3f] ^ \
78 des_sp[2][(_t >> 16) & 0x3f] ^ \
79 des_sp[0][(_t >> 24) & 0x3f]; \
80 } while (0)
81
82 /* --- @DES_IP@, @DES_IPINV@ --- *
83 *
84 * The cryptographically useless initial and final permutations. The initial
85 * permutation also rotates the two block halves left by one place. This is
86 * undone by the inverse permutation at the end.
87 */
88
89 #define DES_IP(x, y) do { \
90 uint32 _t; \
91 _t = (y ^ (x >> 4)) & 0x0f0f0f0f; y ^= _t; x ^= _t << 4; \
92 _t = (x ^ (x >> 18)) & 0x00003333; x ^= _t; x ^= _t << 18; \
93 _t = (y ^ (y >> 18)) & 0x00003333; y ^= _t; y ^= _t << 18; \
94 _t = (x ^ (x >> 9)) & 0x00550055; x ^= _t; x ^= _t << 9; \
95 _t = (y ^ (y >> 9)) & 0x00550055; y ^= _t; y ^= _t << 9; \
96 _t = (x ^ (x >> 24)) & 0x000000ff; x ^= _t; x ^= _t << 24; \
97 _t = (y ^ (y >> 24)) & 0x000000ff; y ^= _t; y ^= _t << 24; \
98 _t = (y ^ (x >> 16)) & 0x0000ffff; y ^= _t; x ^= _t << 16; \
99 x = ROL32(x, 1); y = ROL32(y, 1); \
100 } while (0)
101
102 #define DES_IPINV(x, y) do { \
103 uint32 _t; \
104 x = ROR32(x, 1); y = ROR32(y, 1); \
105 _t = (y ^ (x >> 16)) & 0x0000ffff; y ^= _t; x ^= _t << 16; \
106 _t = (x ^ (x >> 24)) & 0x000000ff; x ^= _t; x ^= _t << 24; \
107 _t = (y ^ (y >> 24)) & 0x000000ff; y ^= _t; y ^= _t << 24; \
108 _t = (y ^ (x >> 4)) & 0x0f0f0f0f; y ^= _t; x ^= _t << 4; \
109 _t = (x ^ (x >> 18)) & 0x00003333; x ^= _t; x ^= _t << 18; \
110 _t = (y ^ (y >> 18)) & 0x00003333; y ^= _t; y ^= _t << 18; \
111 _t = (x ^ (x >> 9)) & 0x00550055; x ^= _t; x ^= _t << 9; \
112 _t = (y ^ (y >> 9)) & 0x00550055; y ^= _t; y ^= _t << 9; \
113 } while (0)
114
115 /* --- @DES_EBLK@, @DES_DBLK@ --- *
116 *
117 * Whole block encryption and decryption.
118 */
119
120 #define DES_EBLK(k, a, b, c, d) do { \
121 const uint32 *_k = (k); \
122 uint32 _x = (a), _y = (b); \
123 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
124 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
125 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
126 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
127 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
128 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
129 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
130 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
131 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
132 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
133 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
134 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
135 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
136 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
137 DES_ROUND(_k[0], _k[1], _x, _y); _k += 2; \
138 DES_ROUND(_k[0], _k[1], _y, _x); _k += 2; \
139 (c) = _y; \
140 (d) = _x; \
141 } while (0)
142
143 #define DES_DBLK(k, a, b, c, d) do { \
144 const uint32 *_k = (k) + 32; \
145 uint32 _x = (a), _y = (b); \
146 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
147 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
148 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
149 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
150 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
151 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
152 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
153 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
154 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
155 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
156 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
157 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
158 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
159 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
160 _k -= 2; DES_ROUND(_k[0], _k[1], _x, _y); \
161 _k -= 2; DES_ROUND(_k[0], _k[1], _y, _x); \
162 (c) = _y; \
163 (d) = _x; \
164 } while (0)
165
166 /*----- That's all, folks -------------------------------------------------*/
167
168 #ifdef __cplusplus
169 }
170 #endif
171
172 #endif