The Data Encryption Standard is described in the US Government Federal
Information Processing Standards (FIPS) 46-3 published by the National
Institute of Standards and Technology. The DES has been very thoroughly
analyzed since it was developed in the late 1970s, and no new
significant flaws have been found.

However, the DES uses only a 56-bit key (plus 8 parity bits), and a
machine has been built in 1998 which can search through all possible
keys in about 6 days, which cost about US$200000; faster searches would
be possible with more money. This makes simple DES insecure for most
purposes, and NIST no longer permits new US government systems
to use simple DES.

For serious encryption functionality, it is recommended that one of the
many free encryption libraries be used instead of these routines.

The DES is a reversible operation which takes a 64-bit block and a
64-bit key, and produces another 64-bit block. Usually the bits are
numbered so that the most-significant bit, the first bit, of each block
is numbered 1.

Under that numbering, every 8th bit of the key (the 8th, 16th, and so
on) is not used by the encryption algorithm itself. But the key must
have odd parity; that is, out of bits 1 through 8, and 9 through 16, and
so on, there must be an odd number of `1' bits, and this completely
specifies the unused bits.

— Function: void setkey (const char *key)

The setkey function sets an internal data structure to be an
expanded form of key. key is specified as an array of 64
bits each stored in a char, the first bit is key[0] and
the 64th bit is key[63]. The key should have the correct
parity.

— Function: void encrypt (char *block, int edflag)

The encrypt function encrypts block if
edflag is 0, otherwise it decrypts block, using a key
previously set by setkey. The result is
placed in block.

Like setkey, block is specified as an array of 64 bits each
stored in a char, but there are no parity bits in block.

These are reentrant versions of setkey and encrypt. The
only difference is the extra parameter, which stores the expanded
version of key. Before calling setkey_r the first time,
data->initialized must be cleared to zero.

The setkey_r and encrypt_r functions are GNU extensions.
setkey, encrypt, setkey_r, and encrypt_r are
defined in crypt.h.

The function ecb_crypt encrypts or decrypts one or more blocks
using DES. Each block is encrypted independently.

The blocks and the key are stored packed in 8-bit bytes, so
that the first bit of the key is the most-significant bit of
key[0] and the 63rd bit of the key is stored as the
least-significant bit of key[7]. The key should have the
correct parity.

len is the number of bytes in blocks. It should be a
multiple of 8 (so that there is a whole number of blocks to encrypt).
len is limited to a maximum of DES_MAXDATA bytes.

The result of the encryption replaces the input in blocks.

The mode parameter is the bitwise OR of two of the following:

DES_ENCRYPT

This constant, used in the mode parameter, specifies that
blocks is to be encrypted.

DES_DECRYPT

This constant, used in the mode parameter, specifies that
blocks is to be decrypted.

DES_HW

This constant, used in the mode parameter, asks to use a hardware
device. If no hardware device is available, encryption happens anyway,
but in software.

DES_SW

This constant, used in the mode parameter, specifies that no
hardware device is to be used.

The result of the function will be one of these values:

DESERR_NONE

The encryption succeeded.

DESERR_NOHWDEVICE

The encryption succeeded, but there was no hardware device available.

DESERR_HWERROR

The encryption failed because of a hardware problem.

DESERR_BADPARAM

The encryption failed because of a bad parameter, for instance len
is not a multiple of 8 or len is larger than DES_MAXDATA.

— Function: int DES_FAILED (int err)

This macro returns 1 if err is a `success' result code from
ecb_crypt or cbc_crypt, and 0 otherwise.

The function cbc_crypt encrypts or decrypts one or more blocks
using DES in Cipher Block Chaining mode.

For encryption in CBC mode, each block is exclusive-ored with ivec
before being encrypted, then ivec is replaced with the result of
the encryption, then the next block is processed. Decryption is the
reverse of this process.

This has the advantage that blocks which are the same before being
encrypted are very unlikely to be the same after being encrypted, making
it much harder to detect patterns in the data.

Usually, ivec is set to 8 random bytes before encryption starts.
Then the 8 random bytes are transmitted along with the encrypted data
(without themselves being encrypted), and passed back in as ivec
for decryption. Another possibility is to set ivec to 8 zeroes
initially, and have the first the block encrypted consist of 8 random
bytes.

Otherwise, all the parameters are similar to those for ecb_crypt.

— Function: void des_setparity (char *key)

The function des_setparity changes the 64-bit key, stored
packed in 8-bit bytes, to have odd parity by altering the low bits of
each byte.

The ecb_crypt, cbc_crypt, and des_setparity
functions and their accompanying macros are all defined in the header
rpc/des_crypt.h.

Published under the terms of the GNU General Public License