Commit | Line | Data |
---|---|---|
b348397a | 1 | /* -*-c-*- |
2 | * | |
b348397a | 3 | * Implementation of DESX |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
b348397a | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
b348397a | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
b348397a | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
b348397a | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include <assert.h> | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | ||
35 | #include <mLib/bits.h> | |
36 | ||
37 | #include "blkc.h" | |
38 | #include "des-base.h" | |
39 | #include "des.h" | |
40 | #include "desx.h" | |
b348397a | 41 | #include "gcipher.h" |
d3f33b9a | 42 | #include "permute.h" |
b348397a | 43 | |
44 | /*----- Tables ------------------------------------------------------------*/ | |
45 | ||
e5b61a8d | 46 | extern const octet desx_s[256]; |
b348397a | 47 | |
48 | /*----- Global variables --------------------------------------------------*/ | |
49 | ||
986527ae | 50 | const octet desx_keysz[] = { KSZ_SET, 23, 7, 8, 15, 16, 24, 0 }; |
b348397a | 51 | |
52 | /*----- Main code ---------------------------------------------------------*/ | |
53 | ||
54 | /* --- @desx_init@ --- * | |
55 | * | |
56 | * Arguments: @desx_ctx *k@ = pointer to key block | |
57 | * @const void *buf@ = pointer to key buffer | |
58 | * @size_t sz@ = size of key material | |
59 | * | |
60 | * Returns: --- | |
61 | * | |
62 | * Use: Initializes a DESX key buffer. The key buffer contains, in | |
63 | * order, a single-DES key (either 7 or 8 bytes), an optional | |
64 | * 8-byte pre-whitening key, and an optional 8-byte | |
eea8ea58 | 65 | * post-whitening key. If no whitening keys are specified, the |
b348397a | 66 | * algorithm becomes the same as single-DES. |
67 | */ | |
68 | ||
69 | static void mangle(octet *b, const octet *p) | |
70 | { | |
71 | unsigned i; | |
72 | ||
73 | for (i = 0; i < 8; i++) | |
e5b61a8d | 74 | b[i] = *p++ ^ desx_s[b[i] ^ b[(i + 1) & 7u]]; |
b348397a | 75 | } |
76 | ||
77 | void desx_init(desx_ctx *k, const void *buf, size_t sz) | |
78 | { | |
79 | const octet *p = buf, *kk = buf; | |
80 | size_t n; | |
81 | ||
82 | KSZ_ASSERT(desx, sz); | |
83 | ||
84 | n = sz % 8 == 7 ? 7 : 8; | |
85 | des_init(&k->k, p, n); | |
86 | p += n; | |
87 | sz -= n; | |
88 | if (!sz) | |
89 | k->prea = k->preb = k->posta = k->postb = 0; | |
90 | else { | |
91 | const octet *q = p; | |
92 | k->prea = LOAD32(q + 0); | |
93 | k->preb = LOAD32(q + 4); | |
94 | p += 8; | |
95 | sz -= 8; | |
96 | if (sz) { | |
97 | k->posta = LOAD32(p + 0); | |
98 | k->postb = LOAD32(p + 4); | |
99 | } else { | |
100 | octet b[16]; | |
986527ae | 101 | uint32 x, y; |
b348397a | 102 | |
986527ae | 103 | des_expand(kk, n, &x, &y); |
104 | STORE32(b + 8, x); STORE32(b + 12, y); | |
b348397a | 105 | memset(b, 0, 8); |
986527ae | 106 | mangle(b, b + 8); |
b348397a | 107 | mangle(b, q); |
108 | k->posta = LOAD32(b + 0); | |
45c0fd36 | 109 | k->postb = LOAD32(b + 4); |
b348397a | 110 | } |
111 | } | |
112 | } | |
113 | ||
114 | /* --- @desx_eblk@, @desx_dblk@ --- * | |
115 | * | |
116 | * Arguments: @const desx_ctx *k@ = pointer to key block | |
117 | * @const uint32 s[2]@ = pointer to source block | |
118 | * @uint32 d[2]@ = pointer to destination block | |
119 | * | |
120 | * Returns: --- | |
121 | * | |
122 | * Use: Low-level block encryption and decryption. | |
123 | */ | |
124 | ||
125 | void desx_eblk(const desx_ctx *k, const uint32 *s, uint32 *d) | |
126 | { | |
d3f33b9a MW |
127 | #define REGWD 32 |
128 | typedef uint32 regty; | |
129 | ||
b348397a | 130 | uint32 x = s[0], y = s[1]; |
131 | x ^= k->prea; y ^= k->preb; | |
132 | DES_IP(x, y); | |
133 | DES_EBLK(k->k.k, x, y, x, y); | |
134 | DES_IPINV(x, y); | |
135 | x ^= k->posta; y ^= k->postb; | |
136 | d[0] = x, d[1] = y; | |
d3f33b9a MW |
137 | |
138 | #undef REGWD | |
b348397a | 139 | } |
140 | ||
141 | void desx_dblk(const desx_ctx *k, const uint32 *s, uint32 *d) | |
142 | { | |
d3f33b9a MW |
143 | #define REGWD 32 |
144 | typedef uint32 regty; | |
145 | ||
b348397a | 146 | uint32 x = s[0], y = s[1]; |
147 | x ^= k->posta; y ^= k->postb; | |
148 | DES_IP(x, y); | |
149 | DES_DBLK(k->k.k, x, y, x, y); | |
150 | DES_IPINV(x, y); | |
151 | x ^= k->prea; y ^= k->preb; | |
152 | d[0] = x, d[1] = y; | |
d3f33b9a MW |
153 | |
154 | #undef REGWD | |
b348397a | 155 | } |
156 | ||
157 | BLKC_TEST(DESX, desx) | |
158 | ||
159 | /*----- That's all, folks -------------------------------------------------*/ |