[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 section 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.
The GNU C library 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.
32.1 Legal Problems | This software can get you locked up, or worse. | |
32.2 Reading Passwords | Prompting the user for a password. | |
32.3 Encrypting Passwords | A one-way function for passwords. | |
32.4 DES Encryption | Routines for DES encryption. |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 section Reporting Bugs.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
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 section 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. The GNU C library 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:
#include <termios.h> #include <stdio.h> ssize_t my_getpass (char **lineptr, size_t *n, FILE *stream) { struct termios old, new; int nread; /* Turn echoing off and fail if we can't. */ if (tcgetattr (fileno (stream), &old) != 0) return -1; new = old; new.c_lflag &= ~ECHO; if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0) return -1; /* Read the password. */ nread = getline (lineptr, n, stream); /* Restore terminal. */ (void) tcsetattr (fileno (stream), TCSAFLUSH, &old); return nread; } |
The substitute takes the same parameters as getline
(see section Line-Oriented Input); the user must print any prompt desired.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
#include <stdio.h> #include <time.h> #include <unistd.h> #include <crypt.h> int main(void) { unsigned long seed[2]; char salt[] = "$1$........"; const char *const seedchars = "./0123456789ABCDEFGHIJKLMNOPQRST" "UVWXYZabcdefghijklmnopqrstuvwxyz"; char *password; int i; /* Generate a (not very) random seed. You should do it better than this... */ seed[0] = time(NULL); seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000); /* Turn it into printable characters from `seedchars'. */ for (i = 0; i < 8; i++) salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; /* Read in the user's password and encrypt it. */ password = crypt(getpass("Password:"), salt); /* Print the results. */ puts(password); return 0; } |
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
.
#include <stdio.h> #include <string.h> #include <unistd.h> #include <crypt.h> int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; char *result; int ok; /* Read in the user's password and encrypt it, passing the expected password in as the salt. */ result = crypt(getpass("Password:"), pass); /* Test the result. */ ok = strcmp (result, pass) == 0; puts(ok ? "Access granted." : "Access denied."); return ok ? 0 : 1; } |
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’.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
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.
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
.
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
.
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’.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by root on January, 9 2009 using texi2html 1.78.