File: libc.info, Node: DES Encryption, Prev: crypt, Up: Cryptographic Functions
33.4 DES Encryption
===================
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)
Preliminary: | MT-Unsafe race:crypt | AS-Unsafe corrupt lock |
AC-Unsafe lock | *Note POSIX Safety Concepts::.
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)
Preliminary: | MT-Unsafe race:crypt | AS-Unsafe corrupt lock |
AC-Unsafe lock | *Note POSIX Safety Concepts::.
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.
-- Function: void setkey_r (const char *KEY, struct crypt_data * DATA)
-- Function: void encrypt_r (char *BLOCK, int EDFLAG, struct crypt_data
* DATA)
Preliminary: | MT-Safe | AS-Unsafe corrupt lock | AC-Unsafe lock |
*Note POSIX Safety Concepts::.
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'.
-- Function: int ecb_crypt (char *KEY, char *BLOCKS, unsigned LEN,
unsigned MODE)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | *Note POSIX Safety
Concepts::.
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)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | *Note POSIX Safety
Concepts::.
This macro returns 1 if ERR is a 'success' result code from
'ecb_crypt' or 'cbc_crypt', and 0 otherwise.
-- Function: int cbc_crypt (char *KEY, char *BLOCKS, unsigned LEN,
unsigned MODE, char *IVEC)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | *Note POSIX Safety
Concepts::.
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)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | *Note POSIX Safety
Concepts::.
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'.