Cryptography Reference
In-Depth Information
Table 2-1 (continued)
INPUT
OUTPUT
0
1
1
1
0
1
1
1
0
If any of the input bits are 1, the output is 1, unless both of the inputs bits
are one. This is equivalent to addition modulo 2 and is referred to that way in
the offi cial specifi cation. One interesting and important property of XOR for
cryptography is that it's reversible. Consider:
0011
0101
0110
However:
0110
0101
0011
This is the same operation as the previous one, but reversed; the output is
the input, but it's XORed against the same set of data. As you can see, you've
recovered the original input this way. You may want to take a moment to look at
the logic of the XOR operation and convince yourself that this is always the case.
To make your implementation match the specifi cation and most public descrip-
tions of the algorithm, you operate on byte arrays rather than taking advantage
(where you can) of the wide integer types of the target hardware. DES is described
using big endian conventions — that is, the most signifi cant bit is bit 1 — whereas
the Intel x86 conventions are little endian — bit 1 is the least-signifi cant bit. To
take full advantage of the hardware, you'd have to reverse quite a few parts of
the specifi cation, which you won't do here.
Instead, you operate on byte (unsigned char) arrays. Because you work at
the bit level — that is, bit 39 of a 64-bit block, for example — you need a few
support macros for fi nding and manipulating bits within such an array. The bit
manipulation support macros are outlined in Listing 2-1.
Listing 2-1: “des.c” bit macros
// This does not return a 1 for a 1 bit; it just returns non-zero
#define GET_BIT( array, bit ) \
( array[ ( int ) ( bit / 8 ) ] & ( 0x80 >> ( bit % 8 ) ) )
#define SET_BIT( array, bit ) \
Search WWH ::




Custom Search