DES Encryption and Password Handling
************************************

On many systems, it is unnecessary to have any kind of user authentication; for instance, a workstation which is not connected to a network probably does not need any user authentication, because to use the machine an intruder must have physical access.

Sometimes, however, it is necessary to be sure that a user is authorized to use some service a machine provides---for instance, to log in as a particular user id (see Users and Groups).  One traditional way of doing this is for each user to choose a secret password; then, the system can ask someone claiming to be a user what the user's password is, and if the person gives the correct password then the system can grant the appropriate privileges.

If all the passwords are just stored in a file somewhere, then this file has to be very carefully protected.  To avoid this, passwords are run through a one-way function, a function which makes it difficult to work out what its input was by looking at its output, before storing in the file.

Theglibc provides a one-way function that is compatible with the behavior of the crypt function introduced in FreeBSD 2.0. It supports two one-way algorithms: one based on the MD5 message-digest algorithm that is compatible with modern BSD systems, and the other based on the Data Encryption Standard (DES) that is compatible with Unix systems.

It also provides support for Secure RPC, and some library functions that can be used to perform normal DES encryption.  The AUTH_DES authentication flavor in Secure RPC, as provided by theglibc, uses DES and does not comply with FIPS 140-2 nor does any other use of DES within theglibc.  It is recommended that Secure RPC should not be used for systems that need to comply with FIPS 140-2 since all flavors of encrypted authentication use normal DES.

Legal Problems
==============

Because of the continuously changing state of the law, it's not possible to provide a definitive survey of the laws affecting cryptography. Instead, this section warns you of some of the known trouble spots; this may help you when you try to find out what the laws of your country are.

Some countries require that you have a licence to use, possess, or import cryptography.  These countries are believed to include Byelorussia, Burma, India, Indonesia, Israel, Kazakhstan, Pakistan, Russia, and Saudi Arabia.

Some countries restrict the transmission of encrypted messages by radio; some telecommunications carriers restrict the transmission of encrypted messages over their network.

Many countries have some form of export control for encryption software. The Wassenaar Arrangement is a multilateral agreement between 33 countries (Argentina, Australia, Austria, Belgium, Bulgaria, Canada, the Czech Republic, Denmark, Finland, France, Germany, Greece, Hungary, Ireland, Italy, Japan, Luxembourg, the Netherlands, New Zealand, Norway, Poland, Portugal, the Republic of Korea, Romania, the Russian Federation, the Slovak Republic, Spain, Sweden, Switzerland, Turkey, Ukraine, the United Kingdom and the United States) which restricts some kinds of encryption exports.  Different countries apply the arrangement in different ways; some do not allow the exception for certain kinds of ``public domain'' software (which would include this library), some only restrict the export of software in tangible form, and others impose significant additional restrictions.

The United States has additional rules.  This software would generally be exportable under 15 CFR 740.13(e), which permits exports of ``encryption source code'' which is ``publicly available'' and which is ``not subject to an express agreement for the payment of a licensing fee or royalty for commercial production or sale of any product developed with the source code'' to most countries.

The rules in this area are continuously changing.  If you know of any information in this manual that is out-of-date, please report it to the bug database.  See Reporting Bugs.

Reading Passwords
=================

When reading in a password, it is desirable to avoid displaying it on the screen, to help keep it secret.  The following function handles this in a convenient way.

 -- Function: char * getpass (const char *PROMPT)

     getpass outputs PROMPT, then reads a string in from the terminal without echoing it.  It tries to connect to the real terminal, /dev/tty, if possible, to encourage users not to put plaintext passwords in files; otherwise, it uses stdin and stderr. getpass also disables the INTR, QUIT, and SUSP characters on the terminal using the ISIG terminal attribute (see Local Modes). The terminal is flushed before and after getpass, so that characters of a mistyped password are not accidentally visible.

     In other C libraries, getpass may only return the first PASS_MAX bytes of a password.  Theglibc has no limit, so PASS_MAX is undefined.

     The prototype for this function is in unistd.h.  PASS_MAX would be defined in limits.h.

This precise set of operations may not suit all possible situations.  In this case, it is recommended that users write their own getpass substitute.  For instance, a very simple substitute is as follows:

          [file mygetpass.c.texi not included]

The substitute takes the same parameters as getline (see Line Input); the user must print any prompt desired.

Encrypting Passwords
====================

 -- Function: char * crypt (const char *KEY, const char *SALT)
     prelim:crypt

     The crypt function takes a password, KEY, as a string, and a SALT character array which is described below, and returns a printable ASCII string which starts with another salt.  It is believed that, given the output of the function, the best way to find a KEY that will produce that output is to guess values of KEY until the original value of KEY is found.

     The SALT parameter does two things.  Firstly, it selects which algorithm is used, the MD5-based one or the DES-based one.  Secondly, it makes life harder for someone trying to guess passwords against a file containing many passwords; without a SALT, an intruder can make a guess, run crypt on it once, and compare the result with all the passwords.  With a SALT, the intruder must run crypt once for each different salt.

     For the MD5-based algorithm, the SALT should consist of the string $1$, followed by up to 8 characters, terminated by either another $ or the end of the string.  The result of crypt will be the SALT, followed by a $ if the salt didn't end with one, followed by 22 characters from the alphabet ./0-9A-Za-z, up to 34 characters total.  Every character in the KEY is significant.

     For the DES-based algorithm, the SALT should consist of two characters from the alphabet ./0-9A-Za-z, and the result of crypt will be those two characters followed by 11 more from the same alphabet, 13 in total.  Only the first 8 characters in the KEY are significant.

     The MD5-based algorithm has no limit on the useful length of the password used, and is slightly more secure.  It is therefore preferred over the DES-based algorithm.

     When the user enters their password for the first time, the SALT should be set to a new string which is reasonably random.  To verify a password against the result of a previous call to crypt, pass the result of the previous call as the SALT.

The following short program is an example of how to use crypt the first time a password is entered.  Note that the SALT generation is just barely acceptable; in particular, it is not unique between machines, and in many applications it would not be acceptable to let an attacker know what time the user's password was last set.

          [file genpass.c.texi not included]

The next program shows how to verify a password.  It prompts the user for a password and prints ``Access granted.'' if the user types GNU libc manual.

          [file testpass.c.texi not included]

 -- Function: char * crypt_r (const char *KEY, const char *SALT, struct crypt_data * DATA)

     The crypt_r function does the same thing as crypt, but takes an extra parameter which includes space for its result (among other things), so it can be reentrant.  data->initialized must be cleared to zero before the first time crypt_r is called.

     The crypt_r function is a GNU extension.

The crypt and crypt_r functions are prototyped in the header crypt.h.

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)
     prelim:crypt

     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)
     prelim:crypt

     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)

     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)

     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.

 -- Function: int cbc_crypt (char *KEY, char *BLOCKS, unsigned LEN, unsigned MODE, char *IVEC)

     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.