Add an internal-representation no-op function.
[u/mdw/catacomb] / desx.c
1 /* -*-c-*-
2 *
3 * $Id: desx.c,v 1.1 2001/04/03 19:36:50 mdw Exp $
4 *
5 * Implementation of DESX
6 *
7 * (c) 2001 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: desx.c,v $
33 * Revision 1.1 2001/04/03 19:36:50 mdw
34 * New block cipher DESX added.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <mLib/bits.h>
46
47 #include "blkc.h"
48 #include "des-base.h"
49 #include "des.h"
50 #include "desx.h"
51 #include "desx-tab.h"
52 #include "gcipher.h"
53
54 /*----- Tables ------------------------------------------------------------*/
55
56 static octet s[256] = DESX_S;
57
58 /*----- Global variables --------------------------------------------------*/
59
60 const octet desx_keysz[] = { KSZ_SET, 7, 8, 15, 16, 23, 24, 0 };
61
62 /*----- Main code ---------------------------------------------------------*/
63
64 /* --- @desx_init@ --- *
65 *
66 * Arguments: @desx_ctx *k@ = pointer to key block
67 * @const void *buf@ = pointer to key buffer
68 * @size_t sz@ = size of key material
69 *
70 * Returns: ---
71 *
72 * Use: Initializes a DESX key buffer. The key buffer contains, in
73 * order, a single-DES key (either 7 or 8 bytes), an optional
74 * 8-byte pre-whitening key, and an optional 8-byte
75 * port-whitening key. If no whitening keys are specified, the
76 * algorithm becomes the same as single-DES.
77 */
78
79 static void mangle(octet *b, const octet *p)
80 {
81 unsigned i;
82
83 for (i = 0; i < 8; i++)
84 b[i] = *p++ ^ s[b[i] ^ b[(i + 1) & 7u]];
85 }
86
87 void desx_init(desx_ctx *k, const void *buf, size_t sz)
88 {
89 const octet *p = buf, *kk = buf;
90 size_t n;
91
92 KSZ_ASSERT(desx, sz);
93
94 n = sz % 8 == 7 ? 7 : 8;
95 des_init(&k->k, p, n);
96 p += n;
97 sz -= n;
98 if (!sz)
99 k->prea = k->preb = k->posta = k->postb = 0;
100 else {
101 const octet *q = p;
102 k->prea = LOAD32(q + 0);
103 k->preb = LOAD32(q + 4);
104 p += 8;
105 sz -= 8;
106 if (sz) {
107 k->posta = LOAD32(p + 0);
108 k->postb = LOAD32(p + 4);
109 } else {
110 octet b[16];
111
112 if (n == 7) {
113
114 /* --- Expand 7 bits to 8 bits --- *
115 *
116 * Cloned and hacked from @des_init@ to set parity.
117 */
118
119 uint32 x, y, z;
120 x = LOAD32(kk + 0);
121 x = (x & 0xfe000000) | ((x & 0x01fffff0) >> 1);
122 x = (x & 0xfffe0000) | ((x & 0x0001fff8) >> 1);
123 x = (x & 0xfffffe00) | ((x & 0x000001fc) >> 1);
124 z = x; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
125 x |= (z & 0x01010101) ^ 0x01010101;
126
127 y = LOAD32(kk + 3) << 1;
128 y = (y & 0x000000fe) | ((y & 0x1fffff00) << 1);
129 y = (y & 0x0000fefe) | ((y & 0x3fff0000) << 1);
130 y = (y & 0x00fefefe) | ((y & 0x7f000000) << 1);
131 z = y; z ^= z >> 4; z ^= z >> 2; z ^= z >> 1;
132 y |= (z & 0x01010101) ^ 0x01010101;
133
134 kk = b + 8;
135 STORE32(kk + 0, x); STORE32(kk + 4, y);
136 }
137
138 memset(b, 0, 8);
139 mangle(b, kk);
140 mangle(b, q);
141 k->posta = LOAD32(b + 0);
142 k->postb = LOAD32(b + 4);
143 }
144 }
145 }
146
147 /* --- @desx_eblk@, @desx_dblk@ --- *
148 *
149 * Arguments: @const desx_ctx *k@ = pointer to key block
150 * @const uint32 s[2]@ = pointer to source block
151 * @uint32 d[2]@ = pointer to destination block
152 *
153 * Returns: ---
154 *
155 * Use: Low-level block encryption and decryption.
156 */
157
158 void desx_eblk(const desx_ctx *k, const uint32 *s, uint32 *d)
159 {
160 uint32 x = s[0], y = s[1];
161 x ^= k->prea; y ^= k->preb;
162 DES_IP(x, y);
163 DES_EBLK(k->k.k, x, y, x, y);
164 DES_IPINV(x, y);
165 x ^= k->posta; y ^= k->postb;
166 d[0] = x, d[1] = y;
167 }
168
169 void desx_dblk(const desx_ctx *k, const uint32 *s, uint32 *d)
170 {
171 uint32 x = s[0], y = s[1];
172 x ^= k->posta; y ^= k->postb;
173 DES_IP(x, y);
174 DES_DBLK(k->k.k, x, y, x, y);
175 DES_IPINV(x, y);
176 x ^= k->prea; y ^= k->preb;
177 d[0] = x, d[1] = y;
178 }
179
180 BLKC_TEST(DESX, desx)
181
182 /*----- That's all, folks -------------------------------------------------*/