Rizin
unix-like reverse engineering framework and cli tools
rz_des.h File Reference
#include <rz_types.h>

Go to the source code of this file.

Macros

#define DES_KEY_SIZE   8
 
#define DES_BLOCK_SIZE   8
 

Functions

RZ_API void rz_des_permute_key (ut32 *keylo, ut32 *keyhi)
 Apply PC-1. More...
 
RZ_API void rz_des_permute_key_inv (ut32 *keylo, ut32 *keyhi)
 Inverse of rz_des_permute_key (PC-1) More...
 
RZ_API void rz_des_permute_block0 (ut32 *blocklo, ut32 *blockhi)
 first permutation of the input block More...
 
RZ_API void rz_des_permute_block1 (ut32 *blocklo, ut32 *blockhi)
 last permutation of the block More...
 
RZ_API void rz_des_shift_key (int i, bool decrypt, ut32 *deskeylo, ut32 *deskeyhi)
 
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. More...
 
RZ_API void rz_des_round_key (int i, ut32 *keylo, ut32 *keyhi, ut32 *deskeylo, ut32 *deskeyhi)
 
RZ_API void rz_des_round (ut32 *buflo, ut32 *bufhi, ut32 *roundkeylo, ut32 *roundkeyhi)
 

Macro Definition Documentation

◆ DES_BLOCK_SIZE

#define DES_BLOCK_SIZE   8

Definition at line 7 of file rz_des.h.

◆ DES_KEY_SIZE

#define DES_KEY_SIZE   8

Definition at line 6 of file rz_des.h.

Function Documentation

◆ rz_des_pc2()

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 at line 247 of file des.c.

247  {
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 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References rz_return_if_fail.

Referenced by avr_custom_des(), and rz_des_round_key().

◆ rz_des_permute_block0()

RZ_API void rz_des_permute_block0 ( ut32 blocklo,
ut32 blockhi 
)

first permutation of the input block

Definition at line 171 of file des.c.

171  {
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 }
uint32_t ut32
#define ROTL(rs, sh)
Definition: des.c:11
hi(addr) 0x03

References hi(), lo, ROTL, and rz_return_if_fail.

Referenced by avr_custom_des(), des_decrypt(), and des_encrypt().

◆ rz_des_permute_block1()

RZ_API void rz_des_permute_block1 ( ut32 blocklo,
ut32 blockhi 
)

last permutation of the block

Definition at line 195 of file des.c.

195  {
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 }
#define ROTR(rs, sh)
Definition: des.c:12

References hi(), lo, ROTR, and rz_return_if_fail.

Referenced by avr_custom_des(), des_decrypt(), and des_encrypt().

◆ rz_des_permute_key()

RZ_API void rz_des_permute_key ( ut32 keylo,
ut32 keyhi 
)

Apply PC-1.

Definition at line 115 of file des.c.

115  {
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 }

References rz_return_if_fail.

Referenced by avr_custom_des(), and des_set_key().

◆ rz_des_permute_key_inv()

RZ_API void rz_des_permute_key_inv ( ut32 keylo,
ut32 keyhi 
)

Inverse of rz_des_permute_key (PC-1)

This is usually not necessary when executing DES. Keep in mind that PC-1 is not injective on arbitrary values, as it drops the parity bits. This inverse function simply sets the positions of those to 0.

Definition at line 153 of file des.c.

153  {
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 }
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
static const st8 pc1_inv[64]
Definition: des.c:103
void * p
Definition: libc.cpp:67
#define st8
Definition: rz_types_base.h:16
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References i, in, out, p, pc1_inv, rz_return_if_fail, st8, and ut64().

Referenced by avr_custom_des().

◆ rz_des_round()

RZ_API void rz_des_round ( ut32 buflo,
ut32 bufhi,
ut32 roundkeylo,
ut32 roundkeyhi 
)

◆ rz_des_round_key()

RZ_API void rz_des_round_key ( int  i,
ut32 keylo,
ut32 keyhi,
ut32 deskeylo,
ut32 deskeyhi 
)

◆ rz_des_shift_key()

RZ_API void rz_des_shift_key ( int  i,
bool  decrypt,
ut32 deskeylo,
ut32 deskeyhi 
)