.\" FreeSec: libcrypt for NetBSD
.\" Copyright (c) 1994 David Burren
.\" All rights reserved.
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the author nor the names of other contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.Dd May 26, 2019
.Dt CRYPT 3
.Nd Trapdoor encryption
.Ft char *
.Fn crypt "const char *key" "const char *salt"
.Ft char *
.Fn crypt_r "const char *key" "const char *salt" "struct crypt_data *data"
.Ft const char *
.Fn crypt_get_format "void"
.Fn crypt_set_format "const char *string"
function performs password hashing with additional code added to
deter key search attempts.
Different algorithms can be used to
in the hash.
.\" If you add more algorithms, make sure to update this list
.\" and the default used for the Traditional format, below.
Currently these include the
.Tn Data Encryption Standard (DES) ,
.Pq compatible with Microsoft's NT scheme
.Tn Blowfish .
The algorithm used will depend upon the format of the Salt
the Modular Crypt Format
is installed or not, and whether
has been called to change the default.
The first argument to
is the data to hash
.Pq usually a password ,
.Dv NUL Ns -terminated
The second is the salt, in one of three forms:
.Bl -tag -width Traditional -compact -offset indent
If it begins with an underscore
.Pq Dq _
is used in interpreting both the key and the salt, as outlined below.
If it begins with the string
then the Modular Crypt Format is used, as outlined below.
If neither of the above is true, it assumes the Traditional Format,
using the entire string as the salt
.Pq or the first portion .
All routines are designed to be time-consuming.
.Ss DES Extended Format:
is divided into groups of 8 characters
.Pq the last group is NUL-padded
and the low-order 7 bits of each character
.Pq 56 bits per group
are used to form the
key as follows:
the first group of 56 bits becomes the initial
For each additional group, the XOR of the encryption of the current
key with itself and the group bits becomes the next
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
.Dq ./0-9A-Za-z .
This allows 24 bits for both
.Fa salt .
introduces disorder in the
algorithm in one of 16777216 or 4096 possible ways
i.e., with 24 or 12 bits: if bit
is set, then bits
are swapped in the
key is used to encrypt a 64-bit constant using
.Tn DES .
The value returned is a
.Dv NUL Ns -terminated
string, 20 or 13 bytes
.Pq plus NUL
in length, consisting of the
followed by the encoded 64-bit encryption.
.Ss Modular crypt:
If the salt begins with the string
then the Modular Crypt Format is used.
represents which algorithm is used in encryption.
Following the token is
the actual salt to use in the encryption.
The maximum length of the salt used depends upon the module.
The salt must be terminated with the end of the string character
or a dollar sign.
Any characters after the dollar sign are ignored.
Currently supported algorithms are:
.Bl -enum -compact -offset indent
Other crypt formats may be easily added.
An example salt would be:
.Bl -tag -width 6n -offset indent
.It Cm "$4$thesalt$rest"
.Ss Traditional crypt:
The algorithm used will depend upon whether
has been called and whether a global default format has been specified.
Unless a global default has been specified or
has set the format to something else, the built-in default format is
This is currently
.\" NOTICE: Also make sure to update this
if it is available, or SHA-512 if not.
How the salt is used will depend upon the algorithm for the hash.
best results, specify at least eight characters of salt.
function returns a constant string that represents the name of the
algorithm currently used.
Valid values are
.\" NOTICE: Also make sure to update this, too, as well
.Ql des ,
.Ql blf ,
.Ql md5 ,
.Ql sha256 ,
.Ql nth .
function sets the default encoding format according to the supplied
.Fa string .
function behaves identically to
.Fn crypt ,
except that the resulting string is stored in
.Fa data ,
making it thread-safe.
.Sh RETURN VALUES
functions return a pointer to the encrypted value on success, and NULL on
Note: this is not a standard behaviour, AT&T
will always return a pointer to a string.
function will return 1 if the supplied encoding format was valid.
Otherwise, a value of 0 is returned.
.Sh SEE ALSO
.Xr login 1 ,
.Xr passwd 1 ,
.Xr getpass 3 ,
.Xr passwd 5
function appeared in
.At v6 .
The current style
first appeared in
.At v7 .
section of the code (FreeSec 1.0) was developed outside the United
States of America as an unencumbered replacement for the U.S.-only
libcrypt encryption library.
function was added in
.Fx 12.0 .
Originally written by
.An David Burren Aq Mt firstname.lastname@example.org ,
later additions and changes by
.An Poul-Henning Kamp ,
.An Mark R V Murray ,
.An Michael Bretterklieber ,
.An Kris Kennaway ,
.An Brian Feldman ,
.An Paul Herman
.An Niels Provos .
function returns a pointer to static data, and subsequent calls to
will modify the same data.
modifies static data.
The NT-hash scheme does not use a salt,
and is not hard
for a competent attacker
Its use is not recommended.