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