Rizin
unix-like reverse engineering framework and cli tools
des.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 seu <seu@panopticon.re>
2 // SPDX-FileCopyrightText: 2015 condret <condret@runas-racer.com>
3 // SPDX-FileCopyrightText: 2017 deroad <wargio@libero.it>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include <rz_types.h>
7 #include <rz_util.h>
8 
9 #define ROTL28(rs, sh) ((((rs) << (sh)) | ((rs) >> (28 - (sh)))) & 0x0FFFFFFF) // left 28
10 #define ROTR28(rs, sh) ((((rs) >> (sh)) | ((rs) << (28 - (sh)))) & 0x0FFFFFFF) // right 28
11 #define ROTL(rs, sh) (((rs) << (sh)) | ((rs) >> (32 - (sh)))) // left 32
12 #define ROTR(rs, sh) (((rs) >> (sh)) | ((rs) << (32 - (sh)))) // right 32
13 
14 /* des sboxes */
15 static const ut32 sbox1[64] = {
16  0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
17  0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
18  0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
19  0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
20  0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400,
21  0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
22  0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400,
23  0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004
24 };
25 
26 static const ut32 sbox2[64] = {
27  0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
28  0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
29  0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
30  0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
31  0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000,
32  0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
33  0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020,
34  0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000
35 };
36 
37 static const ut32 sbox3[64] = {
38  0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
39  0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
40  0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
41  0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
42  0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000,
43  0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
44  0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008,
45  0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200
46 };
47 
48 static const ut32 sbox4[64] = {
49  0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
50  0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
51  0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
52  0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
53  0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000,
54  0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
55  0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081,
56  0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080
57 };
58 
59 static const ut32 sbox5[64] = {
60  0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
61  0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
62  0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
63  0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
64  0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100,
65  0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
66  0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000,
67  0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100
68 };
69 
70 static const ut32 sbox6[64] = {
71  0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
72  0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010,
73  0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
74  0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000,
75  0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010,
76  0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
77  0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010,
78  0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010
79 };
80 
81 static const ut32 sbox7[64] = {
82  0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
83  0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802,
84  0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
85  0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800,
86  0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002,
87  0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
88  0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802,
89  0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002
90 };
91 
92 static const ut32 sbox8[64] = {
93  0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
94  0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040,
95  0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
96  0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000,
97  0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040,
98  0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
99  0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000,
100  0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000
101 };
102 
103 static const st8 pc1_inv[64] = {
104  -1, 0x3b, 0x33, 0x2b, 0x03, 0x0b, 0x13, 0x1b,
105  -1, 0x3a, 0x32, 0x2a, 0x02, 0x0a, 0x12, 0x1a,
106  -1, 0x39, 0x31, 0x29, 0x01, 0x09, 0x11, 0x19,
107  -1, 0x38, 0x30, 0x28, 0x00, 0x08, 0x10, 0x18,
108  -1, 0x37, 0x2f, 0x27, 0x23, 0x07, 0x0f, 0x17,
109  -1, 0x36, 0x2e, 0x26, 0x22, 0x06, 0x0e, 0x16,
110  -1, 0x35, 0x2d, 0x25, 0x21, 0x05, 0x0d, 0x15,
111  -1, 0x34, 0x2c, 0x24, 0x20, 0x04, 0x0c, 0x14
112 };
113 
115 RZ_API void rz_des_permute_key(ut32 *keylo, ut32 *keyhi) {
116  rz_return_if_fail(keylo && keyhi);
117  ut32 perm = ((*keylo >> 4) ^ *keyhi) & 0x0F0F0F0F;
118  *keyhi ^= perm;
119  *keylo ^= (perm << 4);
120  perm = ((*keyhi >> 16) ^ *keylo) & 0x0000FFFF;
121  *keylo ^= perm;
122  *keyhi ^= (perm << 16);
123  perm = ((*keylo >> 2) ^ *keyhi) & 0x33333333;
124  *keyhi ^= perm;
125  *keylo ^= (perm << 2);
126  perm = ((*keyhi >> 16) ^ *keylo) & 0x0000FFFF;
127  *keylo ^= perm;
128  *keyhi ^= (perm << 16);
129  perm = ((*keylo >> 1) ^ *keyhi) & 0x55555555;
130  *keyhi ^= perm;
131  *keylo ^= (perm << 1);
132  perm = ((*keyhi >> 8) ^ *keylo) & 0x00FF00FF;
133  *keylo ^= perm;
134  *keyhi ^= (perm << 8);
135  perm = ((*keylo >> 1) ^ *keyhi) & 0x55555555;
136  *keyhi ^= perm;
137  *keylo ^= (perm << 1);
138  perm = (*keylo << 8) | ((*keyhi >> 20) & 0x000000F0);
139  *keylo = ((*keyhi << 20) & 0x0FF00000);
140  *keylo |= ((*keyhi << 4) & 0x000FF000);
141  *keylo |= ((*keyhi >> 12) & 0x00000FF0);
142  *keylo |= ((*keyhi >> 28) & 0x0000000F);
143  *keyhi = perm >> 4;
144 }
145 
153 RZ_API void rz_des_permute_key_inv(ut32 *keylo, ut32 *keyhi) {
154  rz_return_if_fail(keylo && keyhi);
155  ut64 in = *keylo | ((ut64)*keyhi << 32);
156  ut64 out = 0;
157  for (size_t i = 0; i < 64; i++) {
158  st8 p = pc1_inv[i];
159  if (p < 0) {
160  continue;
161  }
162  if (in & ((ut64)1 << p)) {
163  out |= ((ut64)1 << i);
164  }
165  }
166  *keylo = out & 0xffffffff;
167  *keyhi = out >> 32;
168 }
169 
171 RZ_API void rz_des_permute_block0(ut32 *blocklo, ut32 *blockhi) {
172  rz_return_if_fail(blocklo && blockhi);
173  ut32 lo = *blocklo;
174  ut32 hi = *blockhi;
175  ut32 perm = ((lo >> 4) ^ hi) & 0x0F0F0F0F;
176  hi ^= perm;
177  lo ^= perm << 4;
178  perm = ((lo >> 16) ^ hi) & 0x0000FFFF;
179  hi ^= perm;
180  lo ^= perm << 16;
181  perm = ((hi >> 2) ^ lo) & 0x33333333;
182  lo ^= perm;
183  hi ^= perm << 2;
184  perm = ((hi >> 8) ^ lo) & 0x00FF00FF;
185  lo ^= perm;
186  hi ^= perm << 8;
187  perm = ((lo >> 1) ^ hi) & 0x55555555;
188  hi ^= perm;
189  lo ^= perm << 1;
190  *blocklo = ROTL(lo, 1);
191  *blockhi = ROTL(hi, 1);
192 }
193 
195 RZ_API void rz_des_permute_block1(ut32 *blocklo, ut32 *blockhi) {
196  rz_return_if_fail(blocklo && blockhi);
197  ut32 lo = *blocklo;
198  ut32 hi = *blockhi;
199  lo = ROTR(lo, 1);
200  hi = ROTR(hi, 1);
201  ut32 perm = ((lo >> 1) ^ hi) & 0x55555555;
202  hi ^= perm;
203  lo ^= perm << 1;
204  perm = ((hi >> 8) ^ lo) & 0x00FF00FF;
205  lo ^= perm;
206  hi ^= perm << 8;
207  perm = ((hi >> 2) ^ lo) & 0x33333333;
208  lo ^= perm;
209  hi ^= perm << 2;
210  perm = ((lo >> 16) ^ hi) & 0x0000FFFF;
211  hi ^= perm;
212  lo ^= perm << 16;
213  perm = ((lo >> 4) ^ hi) & 0x0F0F0F0F;
214  hi ^= perm;
215  lo ^= perm << 4;
216  *blocklo = lo;
217  *blockhi = hi;
218 }
219 
225 RZ_API void rz_des_shift_key(int i, bool decrypt, RZ_INOUT ut32 *deskeylo, RZ_INOUT ut32 *deskeyhi) {
226  rz_return_if_fail(deskeylo && deskeyhi);
227  if (!decrypt) {
228  if (i == 0 || i == 1 || i == 8 || i == 15) {
229  *deskeylo = ROTL28(*deskeylo, 1);
230  *deskeyhi = ROTL28(*deskeyhi, 1);
231  } else {
232  *deskeylo = ROTL28(*deskeylo, 2);
233  *deskeyhi = ROTL28(*deskeyhi, 2);
234  }
235  } else {
236  if (i == 0 || i == 1 || i == 8 || i == 15) {
237  *deskeylo = ROTR28(*deskeylo, 1);
238  *deskeyhi = ROTR28(*deskeyhi, 1);
239  } else {
240  *deskeylo = ROTR28(*deskeylo, 2);
241  *deskeyhi = ROTR28(*deskeyhi, 2);
242  }
243  }
244 }
245 
247 RZ_API void rz_des_pc2(RZ_OUT ut32 *keylo, RZ_OUT ut32 *keyhi, RZ_IN ut32 deslo, RZ_IN ut32 deshi) {
248  rz_return_if_fail(keylo && keyhi);
249  *keylo = ((deslo << 4) & 0x24000000) | ((deslo << 28) & 0x10000000) |
250  ((deslo << 14) & 0x08000000) | ((deslo << 18) & 0x02080000) |
251  ((deslo << 6) & 0x01000000) | ((deslo << 9) & 0x00200000) |
252  ((deslo >> 1) & 0x00100000) | ((deslo << 10) & 0x00040000) |
253  ((deslo << 2) & 0x00020000) | ((deslo >> 10) & 0x00010000) |
254  ((deshi >> 13) & 0x00002000) | ((deshi >> 4) & 0x00001000) |
255  ((deshi << 6) & 0x00000800) | ((deshi >> 1) & 0x00000400) |
256  ((deshi >> 14) & 0x00000200) | ((deshi)&0x00000100) |
257  ((deshi >> 5) & 0x00000020) | ((deshi >> 10) & 0x00000010) |
258  ((deshi >> 3) & 0x00000008) | ((deshi >> 18) & 0x00000004) |
259  ((deshi >> 26) & 0x00000002) | ((deshi >> 24) & 0x00000001);
260 
261  *keyhi = ((deslo << 15) & 0x20000000) | ((deslo << 17) & 0x10000000) |
262  ((deslo << 10) & 0x08000000) | ((deslo << 22) & 0x04000000) |
263  ((deslo >> 2) & 0x02000000) | ((deslo << 1) & 0x01000000) |
264  ((deslo << 16) & 0x00200000) | ((deslo << 11) & 0x00100000) |
265  ((deslo << 3) & 0x00080000) | ((deslo >> 6) & 0x00040000) |
266  ((deslo << 15) & 0x00020000) | ((deslo >> 4) & 0x00010000) |
267  ((deshi >> 2) & 0x00002000) | ((deshi << 8) & 0x00001000) |
268  ((deshi >> 14) & 0x00000808) | ((deshi >> 9) & 0x00000400) |
269  ((deshi)&0x00000200) | ((deshi << 7) & 0x00000100) |
270  ((deshi >> 7) & 0x00000020) | ((deshi >> 3) & 0x00000011) |
271  ((deshi << 2) & 0x00000004) | ((deshi >> 21) & 0x00000002);
272 }
273 
285 RZ_API void rz_des_round_key(int i, RZ_OUT ut32 *keylo, RZ_OUT ut32 *keyhi, RZ_INOUT ut32 *deskeylo, RZ_INOUT ut32 *deskeyhi) {
286  rz_return_if_fail(keylo && keyhi && deskeylo && deskeyhi);
287  rz_des_shift_key(i, false, deskeylo, deskeyhi);
288  rz_des_pc2(keylo, keyhi, *deskeylo, *deskeyhi);
289 }
290 
292 RZ_API void rz_des_round(RZ_OUT ut32 *buflo, RZ_OUT ut32 *bufhi, RZ_IN ut32 *roundkeylo, RZ_IN ut32 *roundkeyhi) {
293  rz_return_if_fail(buflo && bufhi && roundkeylo && roundkeyhi);
294  ut32 lo = *buflo;
295  ut32 hi = *bufhi;
296  ut32 perm = hi ^ (*roundkeylo);
297  lo ^= sbox2[(perm >> 24) & 0x3F];
298  lo ^= sbox4[(perm >> 16) & 0x3F];
299  lo ^= sbox6[(perm >> 8) & 0x3F];
300  lo ^= sbox8[perm & 0x3F];
301  perm = ROTR(hi, 4) ^ (*roundkeyhi);
302  lo ^= sbox1[(perm >> 24) & 0x3F];
303  lo ^= sbox3[(perm >> 16) & 0x3F];
304  lo ^= sbox5[(perm >> 8) & 0x3F];
305  lo ^= sbox7[perm & 0x3F];
306  perm = hi;
307  *bufhi = lo;
308  *buflo = perm;
309 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
uint32_t ut32
#define ROTR(rs, sh)
Definition: des.c:12
static const ut32 sbox8[64]
Definition: des.c:92
static const ut32 sbox7[64]
Definition: des.c:81
#define ROTL28(rs, sh)
Definition: des.c:9
static const ut32 sbox3[64]
Definition: des.c:37
static const ut32 sbox6[64]
Definition: des.c:70
RZ_API void rz_des_permute_key(ut32 *keylo, ut32 *keyhi)
Apply PC-1.
Definition: des.c:115
#define ROTL(rs, sh)
Definition: des.c:11
#define ROTR28(rs, sh)
Definition: des.c:10
RZ_API void rz_des_pc2(RZ_OUT ut32 *keylo, RZ_OUT ut32 *keyhi, RZ_IN ut32 deslo, RZ_IN ut32 deshi)
PC-2 permutation of a key.
Definition: des.c:247
static const ut32 sbox1[64]
Definition: des.c:15
static const ut32 sbox5[64]
Definition: des.c:59
RZ_API void rz_des_round_key(int i, RZ_OUT ut32 *keylo, RZ_OUT ut32 *keyhi, RZ_INOUT ut32 *deskeylo, RZ_INOUT ut32 *deskeyhi)
Calculate the final key to be used in a given round.
Definition: des.c:285
RZ_API void rz_des_permute_block1(ut32 *blocklo, ut32 *blockhi)
last permutation of the block
Definition: des.c:195
RZ_API void rz_des_shift_key(int i, bool decrypt, RZ_INOUT ut32 *deskeylo, RZ_INOUT ut32 *deskeyhi)
Apply the respective shift to the key for a given round.
Definition: des.c:225
RZ_API void rz_des_round(RZ_OUT ut32 *buflo, RZ_OUT ut32 *bufhi, RZ_IN ut32 *roundkeylo, RZ_IN ut32 *roundkeyhi)
Apply the cipher function (f)
Definition: des.c:292
RZ_API void rz_des_permute_key_inv(ut32 *keylo, ut32 *keyhi)
Inverse of rz_des_permute_key (PC-1)
Definition: des.c:153
RZ_API void rz_des_permute_block0(ut32 *blocklo, ut32 *blockhi)
first permutation of the input block
Definition: des.c:171
static const ut32 sbox4[64]
Definition: des.c:48
static const ut32 sbox2[64]
Definition: des.c:26
static const st8 pc1_inv[64]
Definition: des.c:103
hi(addr) 0x03
void * p
Definition: libc.cpp:67
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define RZ_IN
Definition: rz_types.h:50
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_INOUT
Definition: rz_types.h:52
#define st8
Definition: rz_types_base.h:16
ut64(WINAPI *w32_GetEnabledXStateFeatures)()