Blame manual/crypt.texi

Packit 6c4009
@node Cryptographic Functions, Debugging Support, System Configuration, Top
Packit 6c4009
@chapter Cryptographic Functions
Packit 6c4009
@c %MENU% Passphrase storage and strongly unpredictable bytes.
Packit 6c4009
Packit 6c4009
@Theglibc{} includes only a few special-purpose cryptographic
Packit 6c4009
functions: one-way hash functions for passphrase storage, and access
Packit 6c4009
to a cryptographic randomness source, if one is provided by the
Packit 6c4009
operating system.  Programs that need general-purpose cryptography
Packit 6c4009
should use a dedicated cryptography library, such as
Packit 6c4009
@uref{https://www.gnu.org/software/libgcrypt/,,libgcrypt}.
Packit 6c4009
Packit 6c4009
Many countries place legal restrictions on the import, export,
Packit 6c4009
possession, or use of cryptographic software.  We deplore these
Packit 6c4009
restrictions, but we must still warn you that @theglibc{} may be
Packit 6c4009
subject to them, even if you do not use the functions in this chapter
Packit 6c4009
yourself.  The restrictions vary from place to place and are changed
Packit 6c4009
often, so we cannot give any more specific advice than this warning.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Passphrase Storage::          One-way hashing for passphrases.
Packit 6c4009
* Unpredictable Bytes::         Randomness for cryptographic purposes.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Passphrase Storage
Packit 6c4009
@section Passphrase Storage
Packit 6c4009
@cindex passphrase hashing
Packit 6c4009
@cindex one-way hashing
Packit 6c4009
@cindex hashing, passphrase
Packit 6c4009
Packit 6c4009
Sometimes it is necessary to be sure that a user is authorized
Packit 6c4009
to use some service a machine provides---for instance, to log in as a
Packit 6c4009
particular user id (@pxref{Users and Groups}).  One traditional way of
Packit 6c4009
doing this is for each user to choose a secret @dfn{passphrase}; then, the
Packit 6c4009
system can ask someone claiming to be a user what the user's passphrase
Packit 6c4009
is, and if the person gives the correct passphrase then the system can
Packit 6c4009
grant the appropriate privileges.  (Traditionally, these were called
Packit 6c4009
``passwords,'' but nowadays a single word is too easy to guess.)
Packit 6c4009
Packit 6c4009
Programs that handle passphrases must take special care not to reveal
Packit 6c4009
them to anyone, no matter what.  It is not enough to keep them in a
Packit 6c4009
file that is only accessible with special privileges.  The file might
Packit 6c4009
be ``leaked'' via a bug or misconfiguration, and system administrators
Packit 6c4009
shouldn't learn everyone's passphrase even if they have to edit that
Packit 6c4009
file for some reason.  To avoid this, passphrases should also be
Packit 6c4009
converted into @dfn{one-way hashes}, using a @dfn{one-way function},
Packit 6c4009
before they are stored.
Packit 6c4009
Packit 6c4009
A one-way function is easy to compute, but there is no known way to
Packit 6c4009
compute its inverse.  This means the system can easily check
Packit 6c4009
passphrases, by hashing them and comparing the result with the stored
Packit 6c4009
hash.  But an attacker who discovers someone's passphrase hash can
Packit 6c4009
only discover the passphrase it corresponds to by guessing and
Packit 6c4009
checking.  The one-way functions are designed to make this process
Packit 6c4009
impractically slow, for all but the most obvious guesses.  (Do not use
Packit 6c4009
a word from the dictionary as your passphrase.)
Packit 6c4009
Packit 6c4009
@Theglibc{} provides an interface to four one-way functions, based on
Packit 6c4009
the SHA-2-512, SHA-2-256, MD5, and DES cryptographic primitives.  New
Packit 6c4009
passphrases should be hashed with either of the SHA-based functions.
Packit 6c4009
The others are too weak for newly set passphrases, but we continue to
Packit 6c4009
support them for verifying old passphrases.  The DES-based hash is
Packit 6c4009
especially weak, because it ignores all but the first eight characters
Packit 6c4009
of its input.
Packit 6c4009
Packit 6c4009
@deftypefun {char *} crypt (const char *@var{phrase}, const char *@var{salt})
Packit 6c4009
@standards{X/Open, unistd.h}
Packit 6c4009
@standards{GNU, crypt.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:crypt}}@asunsafe{@asucorrupt{} @asulock{} @ascuheap{} @ascudlopen{}}@acunsafe{@aculock{} @acsmem{}}}
Packit 6c4009
@c Besides the obvious problem of returning a pointer into static
Packit 6c4009
@c storage, the DES initializer takes an internal lock with the usual
Packit 6c4009
@c set of problems for AS- and AC-Safety.
Packit 6c4009
@c The NSS implementations may leak file descriptors if cancelled.
Packit 6c4009
@c The MD5, SHA256 and SHA512 implementations will malloc on long keys,
Packit 6c4009
@c and NSS relies on dlopening, which brings about another can of worms.
Packit 6c4009
Packit 6c4009
The function @code{crypt} converts a passphrase string, @var{phrase},
Packit 6c4009
into a one-way hash suitable for storage in the user database.  The
Packit 6c4009
string that it returns will consist entirely of printable ASCII
Packit 6c4009
characters.  It will not contain whitespace, nor any of the characters
Packit 6c4009
@samp{:}, @samp{;}, @samp{*}, @samp{!}, or @samp{\}.
Packit 6c4009
Packit 6c4009
The @var{salt} parameter controls which one-way function is used, and
Packit 6c4009
it also ensures that the output of the one-way function is different
Packit 6c4009
for every user, even if they have the same passphrase.  This makes it
Packit 6c4009
harder to guess passphrases from a large user database.  Without salt,
Packit 6c4009
the attacker could make a guess, run @code{crypt} on it once, and
Packit 6c4009
compare the result with all the hashes.  Salt forces the attacker to
Packit 6c4009
make separate calls to @code{crypt} for each user.
Packit 6c4009
Packit 6c4009
To verify a passphrase, pass the previously hashed passphrase as the
Packit 6c4009
@var{salt}.  To hash a new passphrase for storage, set @var{salt} to a
Packit 6c4009
string consisting of a prefix plus a sequence of randomly chosen
Packit 6c4009
characters, according to this table:
Packit 6c4009
Packit 6c4009
@multitable @columnfractions .2 .1 .3
Packit 6c4009
@headitem One-way function @tab Prefix @tab Random sequence
Packit 6c4009
@item SHA-2-512
Packit 6c4009
@tab @samp{$6$}
Packit 6c4009
@tab 16 characters
Packit 6c4009
@item SHA-2-256
Packit 6c4009
@tab @samp{$5$}
Packit 6c4009
@tab 16 characters
Packit 6c4009
@item MD5
Packit 6c4009
@tab @samp{$1$}
Packit 6c4009
@tab 8 characters
Packit 6c4009
@item DES
Packit 6c4009
@tab @samp{}
Packit 6c4009
@tab 2 characters
Packit 6c4009
@end multitable
Packit 6c4009
Packit 6c4009
In all cases, the random characters should be chosen from the alphabet
Packit 6c4009
@code{./0-9A-Za-z}.
Packit 6c4009
Packit 6c4009
With all of the hash functions @emph{except} DES, @var{phrase} can be
Packit 6c4009
arbitrarily long, and all eight bits of each byte are significant.
Packit 6c4009
With DES, only the first eight characters of @var{phrase} affect the
Packit 6c4009
output, and the eighth bit of each byte is also ignored.
Packit 6c4009
Packit 6c4009
@code{crypt} can fail.  Some implementations return @code{NULL} on
Packit 6c4009
failure, and others return an @emph{invalid} hashed passphrase, which
Packit 6c4009
will begin with a @samp{*} and will not be the same as @var{salt}.  In
Packit 6c4009
either case, @code{errno} will be set to indicate the problem.  Some
Packit 6c4009
of the possible error codes are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item EINVAL
Packit 6c4009
@var{salt} is invalid; neither a previously hashed passphrase, nor a
Packit 6c4009
well-formed new salt for any of the supported hash functions.
Packit 6c4009
Packit 6c4009
@item EPERM
Packit 6c4009
The system configuration forbids use of the hash function selected by
Packit 6c4009
@var{salt}.
Packit 6c4009
Packit 6c4009
@item ENOMEM
Packit 6c4009
Failed to allocate internal scratch storage.
Packit 6c4009
Packit 6c4009
@item ENOSYS
Packit 6c4009
@itemx EOPNOTSUPP
Packit 6c4009
Hashing passphrases is not supported at all, or the hash function
Packit 6c4009
selected by @var{salt} is not supported.  @Theglibc{} does not use
Packit 6c4009
these error codes, but they may be encountered on other operating
Packit 6c4009
systems.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@code{crypt} uses static storage for both internal scratchwork and the
Packit 6c4009
string it returns.  It is not safe to call @code{crypt} from multiple
Packit 6c4009
threads simultaneously, and the string it returns will be overwritten
Packit 6c4009
by any subsequent call to @code{crypt}.
Packit 6c4009
Packit 6c4009
@code{crypt} is specified in the X/Open Portability Guide and is
Packit 6c4009
present on nearly all historical Unix systems.  However, the XPG does
Packit 6c4009
not specify any one-way functions.
Packit 6c4009
Packit 6c4009
@code{crypt} is declared in @file{unistd.h}.  @Theglibc{} also
Packit 6c4009
declares this function in @file{crypt.h}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun {char *} crypt_r (const char *@var{phrase}, const char *@var{salt}, struct crypt_data *@var{data})
Packit 6c4009
@standards{GNU, crypt.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @asulock{} @ascuheap{} @ascudlopen{}}@acunsafe{@aculock{} @acsmem{}}}
Packit 6c4009
@tindex struct crypt_data
Packit 6c4009
@c Compared with crypt, this function fixes the @mtasurace:crypt
Packit 6c4009
@c problem, but nothing else.
Packit 6c4009
Packit 6c4009
The function @code{crypt_r} is a thread-safe version of @code{crypt}.
Packit 6c4009
Instead of static storage, it uses the memory pointed to by its
Packit 6c4009
@var{data} argument for both scratchwork and the string it returns.
Packit 6c4009
It can safely be used from multiple threads, as long as different
Packit 6c4009
@var{data} objects are used in each thread.  The string it returns
Packit 6c4009
will still be overwritten by another call with the same @var{data}.
Packit 6c4009
Packit 6c4009
@var{data} must point to a @code{struct crypt_data} object allocated
Packit 6c4009
by the caller.  All of the fields of @code{struct crypt_data} are
Packit 6c4009
private, but before one of these objects is used for the first time,
Packit 6c4009
it must be initialized to all zeroes, using @code{memset} or similar.
Packit 6c4009
After that, it can be reused for many calls to @code{crypt_r} without
Packit 6c4009
erasing it again.  @code{struct crypt_data} is very large, so it is
Packit 6c4009
best to allocate it with @code{malloc} rather than as a local
Packit 6c4009
variable.  @xref{Memory Allocation}.
Packit 6c4009
Packit 6c4009
@code{crypt_r} is a GNU extension.  It is declared in @file{crypt.h},
Packit 6c4009
as is @code{struct crypt_data}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
The following program shows how to use @code{crypt} the first time a
Packit 6c4009
passphrase is entered.  It uses @code{getentropy} to make the salt as
Packit 6c4009
unpredictable as possible; @pxref{Unpredictable Bytes}.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include genpass.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The next program demonstrates how to verify a passphrase.  It checks a
Packit 6c4009
hash hardcoded into the program, because looking up real users' hashed
Packit 6c4009
passphrases may require special privileges (@pxref{User Database}).
Packit 6c4009
It also shows that different one-way functions produce different
Packit 6c4009
hashes for the same passphrase.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include testpass.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Unpredictable Bytes
Packit 6c4009
@section Generating Unpredictable Bytes
Packit 6c4009
@cindex randomness source
Packit 6c4009
@cindex random numbers, cryptographic
Packit 6c4009
@cindex pseudo-random numbers, cryptographic
Packit 6c4009
@cindex cryptographic random number generator
Packit 6c4009
@cindex deterministic random bit generator
Packit 6c4009
@cindex CRNG
Packit 6c4009
@cindex CSPRNG
Packit 6c4009
@cindex DRBG
Packit 6c4009
Packit 6c4009
Cryptographic applications often need some random data that will be as
Packit 6c4009
difficult as possible for a hostile eavesdropper to guess.  For
Packit 6c4009
instance, encryption keys should be chosen at random, and the ``salt''
Packit 6c4009
strings used by @code{crypt} (@pxref{Passphrase Storage}) should also
Packit 6c4009
be chosen at random.
Packit 6c4009
Packit 6c4009
Some pseudo-random number generators do not provide unpredictable-enough
Packit 6c4009
output for cryptographic applications; @pxref{Pseudo-Random Numbers}.
Packit 6c4009
Such applications need to use a @dfn{cryptographic random number
Packit 6c4009
generator} (CRNG), also sometimes called a @dfn{cryptographically strong
Packit 6c4009
pseudo-random number generator} (CSPRNG) or @dfn{deterministic random
Packit 6c4009
bit generator} (DRBG).
Packit 6c4009
Packit 6c4009
Currently, @theglibc{} does not provide a cryptographic random number
Packit 6c4009
generator, but it does provide functions that read random data from a
Packit 6c4009
@dfn{randomness source} supplied by the operating system.  The
Packit 6c4009
randomness source is a CRNG at heart, but it also continually
Packit 6c4009
``re-seeds'' itself from physical sources of randomness, such as
Packit 6c4009
electronic noise and clock jitter.  This means applications do not need
Packit 6c4009
to do anything to ensure that the random numbers it produces are
Packit 6c4009
different on each run.
Packit 6c4009
Packit 6c4009
The catch, however, is that these functions will only produce
Packit 6c4009
relatively short random strings in any one call.  Often this is not a
Packit 6c4009
problem, but applications that need more than a few kilobytes of
Packit 6c4009
cryptographically strong random data should call these functions once
Packit 6c4009
and use their output to seed a CRNG.
Packit 6c4009
Packit 6c4009
Most applications should use @code{getentropy}.  The @code{getrandom}
Packit 6c4009
function is intended for low-level applications which need additional
Packit 6c4009
control over blocking behavior.
Packit 6c4009
Packit 6c4009
@deftypefun int getentropy (void *@var{buffer}, size_t @var{length})
Packit 6c4009
@standards{GNU, sys/random.h}
Packit 6c4009
@safety{@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Packit 6c4009
This function writes exactly @var{length} bytes of random data to the
Packit 6c4009
array starting at @var{buffer}.  @var{length} can be no more than 256.
Packit 6c4009
On success, it returns zero.  On failure, it returns @math{-1}, and
Packit 6c4009
@code{errno} is set to indicate the problem.  Some of the possible
Packit 6c4009
errors are listed below.
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ENOSYS
Packit 6c4009
The operating system does not implement a randomness source, or does
Packit 6c4009
not support this way of accessing it.  (For instance, the system call
Packit 6c4009
used by this function was added to the Linux kernel in version 3.17.)
Packit 6c4009
Packit 6c4009
@item EFAULT
Packit 6c4009
The combination of @var{buffer} and @var{length} arguments specifies
Packit 6c4009
an invalid memory range.
Packit 6c4009
Packit 6c4009
@item EIO
Packit 6c4009
@var{length} is larger than 256, or the kernel entropy pool has
Packit 6c4009
suffered a catastrophic failure.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
A call to @code{getentropy} can only block when the system has just
Packit 6c4009
booted and the randomness source has not yet been initialized.
Packit 6c4009
However, if it does block, it cannot be interrupted by signals or
Packit 6c4009
thread cancellation.  Programs intended to run in very early stages of
Packit 6c4009
the boot process may need to use @code{getrandom} in non-blocking mode
Packit 6c4009
instead, and be prepared to cope with random data not being available
Packit 6c4009
at all.
Packit 6c4009
Packit 6c4009
The @code{getentropy} function is declared in the header file
Packit 6c4009
@file{sys/random.h}.  It is derived from OpenBSD.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun ssize_t getrandom (void *@var{buffer}, size_t @var{length}, unsigned int @var{flags})
Packit 6c4009
@standards{GNU, sys/random.h}
Packit 6c4009
@safety{@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
Packit 6c4009
This function writes up to @var{length} bytes of random data to the
Packit 6c4009
array starting at @var{buffer}.  The @var{flags} argument should be
Packit 6c4009
either zero, or the bitwise OR of some of the following flags:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item GRND_RANDOM
Packit 6c4009
Use the @file{/dev/random} (blocking) source instead of the
Packit 6c4009
@file{/dev/urandom} (non-blocking) source to obtain randomness.
Packit 6c4009
Packit 6c4009
If this flag is specified, the call may block, potentially for quite
Packit 6c4009
some time, even after the randomness source has been initialized.  If it
Packit 6c4009
is not specified, the call can only block when the system has just
Packit 6c4009
booted and the randomness source has not yet been initialized.
Packit 6c4009
Packit 6c4009
@item GRND_NONBLOCK
Packit 6c4009
Instead of blocking, return to the caller immediately if no data is
Packit 6c4009
available.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
Unlike @code{getentropy}, the @code{getrandom} function is a
Packit 6c4009
cancellation point, and if it blocks, it can be interrupted by
Packit 6c4009
signals.
Packit 6c4009
Packit 6c4009
On success, @code{getrandom} returns the number of bytes which have
Packit 6c4009
been written to the buffer, which may be less than @var{length}.  On
Packit 6c4009
error, it returns @math{-1}, and @code{errno} is set to indicate the
Packit 6c4009
problem.  Some of the possible errors are:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item ENOSYS
Packit 6c4009
The operating system does not implement a randomness source, or does
Packit 6c4009
not support this way of accessing it.  (For instance, the system call
Packit 6c4009
used by this function was added to the Linux kernel in version 3.17.)
Packit 6c4009
Packit 6c4009
@item EAGAIN
Packit 6c4009
No random data was available and @code{GRND_NONBLOCK} was specified in
Packit 6c4009
@var{flags}.
Packit 6c4009
Packit 6c4009
@item EFAULT
Packit 6c4009
The combination of @var{buffer} and @var{length} arguments specifies
Packit 6c4009
an invalid memory range.
Packit 6c4009
Packit 6c4009
@item EINTR
Packit 6c4009
The system call was interrupted.  During the system boot process, before
Packit 6c4009
the kernel randomness pool is initialized, this can happen even if
Packit 6c4009
@var{flags} is zero.
Packit 6c4009
Packit 6c4009
@item EINVAL
Packit 6c4009
The @var{flags} argument contains an invalid combination of flags.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
The @code{getrandom} function is declared in the header file
Packit 6c4009
@file{sys/random.h}.  It is a GNU extension.
Packit 6c4009
Packit 6c4009
@end deftypefun