b10a0accee
The functions encrypt, setkey, encrypt_r, setkey_r, cbc_crypt, ecb_crypt, and des_setparity should not be used in new programs, because they use the DES block cipher, which is unacceptably weak by modern standards. Demote all of them to compatibility symbols, and remove their prototypes from installed headers. cbc_crypt, ecb_crypt, and des_setparity were already compat symbols when glibc was configured with --disable-obsolete-rpc. POSIX requires encrypt and setkey to be available when _XOPEN_CRYPT is defined, so this change also removes the definition of X_OPEN_CRYPT from <unistd.h>. The entire "DES Encryption" section is dropped from the manual, as is the mention of AUTH_DES and FIPS 140-2 in the introduction to crypt.texi. The documentation of 'memfrob' cross-referenced the DES Encryption section, which is replaced by a hyperlink to libgcrypt, and while I was in there I spruced up the actual documentation of 'memfrob' and 'strfry' a little. It's still fairly jokey, because those functions _are_ jokes, but they do also have real use cases, so people trying to use them for real should have all the information they need. DES-based authentication for Sun RPC is also insecure and should be deprecated or even removed, but maybe that can be left as TI-RPC's problem.
323 lines
14 KiB
Plaintext
323 lines
14 KiB
Plaintext
@c This node must have no pointers.
|
|
@node Cryptographic Functions
|
|
@c @node Cryptographic Functions, Debugging Support, System Configuration, Top
|
|
@chapter DES Encryption and Password Handling
|
|
@c %MENU% 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 (@pxref{Users and Groups}). One traditional way of
|
|
doing this is for each user to choose a secret @dfn{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 @dfn{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 @code{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.
|
|
|
|
@menu
|
|
* Legal Problems:: This software can get you locked up, or worse.
|
|
* getpass:: Prompting the user for a password.
|
|
* crypt:: A one-way function for passwords.
|
|
* Unpredictable Bytes:: Randomness for cryptography purposes.
|
|
@end menu
|
|
|
|
@node Legal Problems
|
|
@section 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 license 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. @xref{Reporting Bugs}.
|
|
|
|
@node getpass
|
|
@section 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.
|
|
|
|
@deftypefun {char *} getpass (const char *@var{prompt})
|
|
@standards{BSD, unistd.h}
|
|
@safety{@prelim{}@mtunsafe{@mtasuterm{}}@asunsafe{@ascuheap{} @asulock{} @asucorrupt{}}@acunsafe{@acuterm{} @aculock{} @acucorrupt{}}}
|
|
@c This function will attempt to create a stream for terminal I/O, but
|
|
@c will fallback to stdio/stderr. It attempts to change the terminal
|
|
@c mode in a thread-unsafe way, write out the prompt, read the password,
|
|
@c then restore the terminal mode. It has a cleanup to close the stream
|
|
@c in case of (synchronous) cancellation, but not to restore the
|
|
@c terminal mode.
|
|
|
|
@code{getpass} outputs @var{prompt}, then reads a string in from the
|
|
terminal without echoing it. It tries to connect to the real terminal,
|
|
@file{/dev/tty}, if possible, to encourage users not to put plaintext
|
|
passwords in files; otherwise, it uses @code{stdin} and @code{stderr}.
|
|
@code{getpass} also disables the INTR, QUIT, and SUSP characters on the
|
|
terminal using the @code{ISIG} terminal attribute (@pxref{Local Modes}).
|
|
The terminal is flushed before and after @code{getpass}, so that
|
|
characters of a mistyped password are not accidentally visible.
|
|
|
|
In other C libraries, @code{getpass} may only return the first
|
|
@code{PASS_MAX} bytes of a password. @Theglibc{} has no limit, so
|
|
@code{PASS_MAX} is undefined.
|
|
|
|
The prototype for this function is in @file{unistd.h}. @code{PASS_MAX}
|
|
would be defined in @file{limits.h}.
|
|
@end deftypefun
|
|
|
|
This precise set of operations may not suit all possible situations. In
|
|
this case, it is recommended that users write their own @code{getpass}
|
|
substitute. For instance, a very simple substitute is as follows:
|
|
|
|
@smallexample
|
|
@include mygetpass.c.texi
|
|
@end smallexample
|
|
|
|
The substitute takes the same parameters as @code{getline}
|
|
(@pxref{Line Input}); the user must print any prompt desired.
|
|
|
|
@node crypt
|
|
@section Encrypting Passwords
|
|
|
|
@deftypefun {char *} crypt (const char *@var{key}, const char *@var{salt})
|
|
@standards{BSD, crypt.h}
|
|
@standards{SVID, crypt.h}
|
|
@safety{@prelim{}@mtunsafe{@mtasurace{:crypt}}@asunsafe{@asucorrupt{} @asulock{} @ascuheap{} @ascudlopen{}}@acunsafe{@aculock{} @acsmem{}}}
|
|
@c Besides the obvious problem of returning a pointer into static
|
|
@c storage, the DES initializer takes an internal lock with the usual
|
|
@c set of problems for AS- and AC-Safety. The FIPS mode checker and the
|
|
@c NSS implementations of may leak file descriptors if canceled. The
|
|
@c The MD5, SHA256 and SHA512 implementations will malloc on long keys,
|
|
@c and NSS relies on dlopening, which brings about another can of worms.
|
|
|
|
The @code{crypt} function takes a password, @var{key}, as a string, and
|
|
a @var{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 @var{key}
|
|
that will produce that output is to guess values of @var{key} until the
|
|
original value of @var{key} is found.
|
|
|
|
The @var{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 @var{salt}, an intruder can make a
|
|
guess, run @code{crypt} on it once, and compare the result with all the
|
|
passwords. With a @var{salt}, the intruder must run @code{crypt} once
|
|
for each different salt.
|
|
|
|
For the MD5-based algorithm, the @var{salt} should consist of the string
|
|
@code{$1$}, followed by up to 8 characters, terminated by either
|
|
another @code{$} or the end of the string. The result of @code{crypt}
|
|
will be the @var{salt}, followed by a @code{$} if the salt didn't end
|
|
with one, followed by 22 characters from the alphabet
|
|
@code{./0-9A-Za-z}, up to 34 characters total. Every character in the
|
|
@var{key} is significant.
|
|
|
|
For the DES-based algorithm, the @var{salt} should consist of two
|
|
characters from the alphabet @code{./0-9A-Za-z}, and the result of
|
|
@code{crypt} will be those two characters followed by 11 more from the
|
|
same alphabet, 13 in total. Only the first 8 characters in the
|
|
@var{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 @var{salt}
|
|
should be set to a new string which is reasonably random. To verify a
|
|
password against the result of a previous call to @code{crypt}, pass
|
|
the result of the previous call as the @var{salt}.
|
|
@end deftypefun
|
|
|
|
The following short program is an example of how to use @code{crypt} the
|
|
first time a password is entered. Note that the @var{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.
|
|
|
|
@smallexample
|
|
@include genpass.c.texi
|
|
@end smallexample
|
|
|
|
The next program shows how to verify a password. It prompts the user
|
|
for a password and prints ``Access granted.'' if the user types
|
|
@code{GNU libc manual}.
|
|
|
|
@smallexample
|
|
@include testpass.c.texi
|
|
@end smallexample
|
|
|
|
@deftypefun {char *} crypt_r (const char *@var{key}, const char *@var{salt}, {struct crypt_data *} @var{data})
|
|
@standards{GNU, crypt.h}
|
|
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @asulock{} @ascuheap{} @ascudlopen{}}@acunsafe{@aculock{} @acsmem{}}}
|
|
@c Compared with crypt, this function fixes the @mtasurace:crypt
|
|
@c problem, but nothing else.
|
|
|
|
The @code{crypt_r} function does the same thing as @code{crypt}, but
|
|
takes an extra parameter which includes space for its result (among
|
|
other things), so it can be reentrant. @code{data@w{->}initialized} must be
|
|
cleared to zero before the first time @code{crypt_r} is called.
|
|
|
|
The @code{crypt_r} function is a GNU extension.
|
|
@end deftypefun
|
|
|
|
The @code{crypt} and @code{crypt_r} functions are prototyped in the
|
|
header @file{crypt.h}.
|
|
|
|
@node Unpredictable Bytes
|
|
@section Generating Unpredictable Bytes
|
|
|
|
Some cryptographic applications (such as session key generation) need
|
|
unpredictable bytes.
|
|
|
|
In general, application code should use a deterministic random bit
|
|
generator, which could call the @code{getentropy} function described
|
|
below internally to obtain randomness to seed the generator. The
|
|
@code{getrandom} function is intended for low-level applications which
|
|
need additional control over the blocking behavior.
|
|
|
|
@deftypefun int getentropy (void *@var{buffer}, size_t @var{length})
|
|
@standards{GNU, sys/random.h}
|
|
@safety{@mtsafe{}@assafe{}@acsafe{}}
|
|
|
|
This function writes @var{length} bytes of random data to the array
|
|
starting at @var{buffer}, which must be at most 256 bytes long. The
|
|
function returns zero on success. On failure, it returns @code{-1} and
|
|
@code{errno} is updated accordingly.
|
|
|
|
The @code{getentropy} function is declared in the header file
|
|
@file{sys/random.h}. It is derived from OpenBSD.
|
|
|
|
The @code{getentropy} function is not a cancellation point. A call to
|
|
@code{getentropy} can block if the system has just booted and the kernel
|
|
entropy pool has not yet been initialized. In this case, the function
|
|
will keep blocking even if a signal arrives, and return only after the
|
|
entropy pool has been initialized.
|
|
|
|
The @code{getentropy} function can fail with several errors, some of
|
|
which are listed below.
|
|
|
|
@table @code
|
|
@item ENOSYS
|
|
The kernel does not implement the required system call.
|
|
|
|
@item EFAULT
|
|
The combination of @var{buffer} and @var{length} arguments specifies
|
|
an invalid memory range.
|
|
|
|
@item EIO
|
|
More than 256 bytes of randomness have been requested, or the buffer
|
|
could not be overwritten with random data for an unspecified reason.
|
|
|
|
@end table
|
|
|
|
@end deftypefun
|
|
|
|
@deftypefun ssize_t getrandom (void *@var{buffer}, size_t @var{length}, unsigned int @var{flags})
|
|
@standards{GNU, sys/random.h}
|
|
@safety{@mtsafe{}@assafe{}@acsafe{}}
|
|
|
|
This function writes @var{length} bytes of random data to the array
|
|
starting at @var{buffer}. On success, this function returns the number
|
|
of bytes which have been written to the buffer (which can be less than
|
|
@var{length}). On error, @code{-1} is returned, and @code{errno} is
|
|
updated accordingly.
|
|
|
|
The @code{getrandom} function is declared in the header file
|
|
@file{sys/random.h}. It is a GNU extension.
|
|
|
|
The following flags are defined for the @var{flags} argument:
|
|
|
|
@table @code
|
|
@item GRND_RANDOM
|
|
Use the @file{/dev/random} (blocking) pool instead of the
|
|
@file{/dev/urandom} (non-blocking) pool to obtain randomness. If the
|
|
@code{GRND_RANDOM} flag is specified, the @code{getrandom} function can
|
|
block even after the randomness source has been initialized.
|
|
|
|
@item GRND_NONBLOCK
|
|
Instead of blocking, return to the caller immediately if no data is
|
|
available.
|
|
@end table
|
|
|
|
The @code{getrandom} function is a cancellation point.
|
|
|
|
Obtaining randomness from the @file{/dev/urandom} pool (i.e., a call
|
|
without the @code{GRND_RANDOM} flag) can block if the system has just
|
|
booted and the pool has not yet been initialized.
|
|
|
|
The @code{getrandom} function can fail with several errors, some of
|
|
which are listed below. In addition, the function may not fill the
|
|
buffer completely and return a value less than @var{length}.
|
|
|
|
@table @code
|
|
@item ENOSYS
|
|
The kernel does not implement the @code{getrandom} system call.
|
|
|
|
@item EAGAIN
|
|
No random data was available and @code{GRND_NONBLOCK} was specified in
|
|
@var{flags}.
|
|
|
|
@item EFAULT
|
|
The combination of @var{buffer} and @var{length} arguments specifies
|
|
an invalid memory range.
|
|
|
|
@item EINTR
|
|
The system call was interrupted. During the system boot process, before
|
|
the kernel randomness pool is initialized, this can happen even if
|
|
@var{flags} is zero.
|
|
|
|
@item EINVAL
|
|
The @var{flags} argument contains an invalid combination of flags.
|
|
@end table
|
|
|
|
@end deftypefun
|