NAME | SYNOPSIS | LIBRARY | DESCRIPTION | RETURN VALUES | ATTRIBUTES | BUGS
$(OS_DIR)/lib/libc.a #include <unistd.h>char *crypt(const char *key, const char *salt);
Crypt Library (libcrypt, -lcrypt)
The crypt() function performs password hashing with additional code to deter key search attempts. Different algorithms can be used for the hash. These include the NBS Data Encryption Standard (DES) and MD5. The algorithm used depends upon the format of the salt (following the Modular Crypt Format (MCF)) and whether DES is installed.
The first argument to crypt() is the data to hash (usually a password), in a null-terminated string. The second argument is the salt, in one of three forms:
Extended |
If the salt begins with an underscore (``_"), the DES Extended Format is used to interpret both the the key and the salt, as outlined below. |
Modular |
If it begins with the string $digit$, the Modular Crypt Format is used, as outlined below. |
Traditional |
If neither of the above is true, it assumes the Traditional Format, using the entire string as the salt (or the first portion). |
All routines are designed to be time-consuming. A brief test on Pentium 166/MMX shows the DES crypt to do approximately 2640 crypts a CPU second and MD5 to do about 62 crypts a CPU second.
The key is divided into groups of 8 characters (the last group is null- padded) and the low-order 7 bits of each character (56 bits per group) are used to form the DES key as follows: the first group of 56 bits becomes the initial DES key. For each additional group, the XOR of the encryption of the current DES key with itself and the group bits becomes the next DES key.
The salt is a 9-character array consisting of an underscore followed by 4 bytes of iteration count and 4 bytes of salt. These are encoded as printable characters, 6 bits per character, least significant character first. The values 0 to 63 are encoded as ``./0-9A-Za-z''. This allows 24 bits for both count and salt.
The salt introduces disorder in the DES algorithm in one of 16777216 or 4096 possible ways (that is, with 24 or 12 bits: if bit i of the salt is set, then bits i and i+24 are swapped in the DES E-box output).
The DES key is used to encrypt a 64-bit constant using count iterations of DES. The value returned is a null-terminated string, 20 or 13 bytes (plus null) in length, consisting of the salt followed by the encoded 64-bit encryption.
If the salt begins with the string $digit$ then the Modular Crypt Format is used. The digit represents which algorithm is used in encryption. Following the token is the actual salt to use in the encryption. The length of the salt is limited to 16 characters because the length of the returned output is also limited (_PASSWORD_LEN). The salt must be terminated with the end of the string (NULL) or a dollar sign. Any characters after the dollar sign are ignored.
Currently supported algorithms are:
1 |
MD5 |
Other crypt formats may be easily added. An example salt would be:
$3$thesalt$rest
The algorithm used will depend upon whether DES is installed. If it is, DES will be used. Otherwise, the best algorithm is used, which is currently MD5.
How the salt is used will depend upon the algorithm for the hash. For best results, specify at least two characters of the salt.
crypt() returns a pointer to the encrypted value on success, and NULL on failure.
See attributes(5) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
---|---|
Interface Stability | Evolving |
MT-Level | Not MT safe |
The crypt() function returns a pointer to static data, and subsequent calls to crypt() will modify the same data.
NAME | SYNOPSIS | LIBRARY | DESCRIPTION | RETURN VALUES | ATTRIBUTES | BUGS