xref: /freebsd/lib/libcrypt/crypt.3 (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1.\" FreeSec: libcrypt for NetBSD
2.\"
3.\" Copyright (c) 1994 David Burren
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 4. Neither the name of the author nor the names of other contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"	$FreeBSD$
31.\"
32.\" Manual page, using -mandoc macros
33.\"
34.Dd January 19, 1997
35.Dt CRYPT 3
36.Os "FreeSec 1.0"
37.Sh NAME
38.Nm crypt
39.Nd Trapdoor encryption
40.Sh SYNOPSIS
41.Ft char
42.Fn *crypt "const char *key" "const char *salt"
43.Sh DESCRIPTION
44The
45.Fn crypt
46function performs password hashing with additional code added to
47deter key search attempts.  Different algorithms can be used to
48in the hash.
49.\"
50.\" NOTICE:
51.\" If you add more algorithms, make sure to update this list
52.\" and the default used for the Traditional format, below.
53.\"
54Currently these include the
55.Tn NBS
56Data Encryption Standard (DES), MD5 or SHS.  The algorithm
57used will depend upon the format of the Salt--following the Modular
58Crypt Format (MCF)--and if DES is installed or not.
59.Pp
60The first argument to
61.Nm crypt
62is the data to hash (usually a password), in a
63.Dv null Ns -terminated
64string.
65The second is the salt, in one of three forms:
66.Pp
67.Bl -tag -width Traditional -compact -offset indent
68.It Extended
69If it begins with an underscore (``_'') then the DES Extended Format
70is used in interpreting both the the key and the salt, as outlined below.
71.It Modular
72If it begins with the string ``$digit$'' then the Modular Crypt Format
73is used, as outlined below.
74.It Traditional
75If neither of the above is true, it assumes the Traditional Format,
76using the entire string as the salt (or the first portion).
77.El
78.Pp
79All routines are designed to be time-consuming.  A brief test on a
80Pentium 166/MMX shows the DES crypt to do approximately 2640 crypts
81a CPU second, MD5 to do about 62 crypts a CPU second and SHA1
82to do about 18 crypts a CPU second.
83.Ss DES Extended Format:
84.Pp
85The
86.Ar key
87is divided into groups of 8 characters (the last group is null-padded)
88and the low-order 7 bits of each each character (56 bits per group) are
89used to form the DES key as follows:
90the first group of 56 bits becomes the initial DES key.
91For each additional group, the XOR of the encryption of the current DES
92key with itself and the group bits becomes the next DES key.
93.Pp
94The salt is a 9-character array consisting of an underscore followed
95by 4 bytes of iteration count and 4 bytes of salt.
96These are encoded as printable characters, 6 bits per character,
97least significant character first.
98The values 0 to 63 are encoded as ``./0-9A-Za-z''.
99This allows 24 bits for both
100.Fa count
101and
102.Fa salt .
103.Pp
104The
105.Fa salt
106introduces disorder in the
107.Tn DES
108algorithm in one of 16777216 or 4096 possible ways
109(ie. with 24 or 12 bits: if bit
110.Em i
111of the
112.Ar salt
113is set, then bits
114.Em i
115and
116.Em i+24
117are swapped in the
118.Tn DES
119E-box output).
120.Pp
121The DES key is used to encrypt a 64-bit constant using
122.Ar count
123iterations of
124.Tn DES .
125The value returned is a
126.Dv null Ns -terminated
127string, 20 or 13 bytes (plus null) in length, consisting of the
128.Ar salt
129followed by the encoded 64-bit encryption.
130.Ss "Modular" crypt:
131.Pp
132If the salt begins with the string
133.Fa $digit$
134then the Modular Crypt Format is used.  The
135.Fa digit
136represents which algorithm is used in encryption.  Following the token is
137the actual salt to use in the encryption.  The length of the salt is limited
138to 16 characters--because the length of the returned output is also limited
139(_PASSWORD_LEN).  The salt must be terminated with the end of the string
140(NULL) or a dollar sign.  Any characters after the dollar sign are ignored.
141.Pp
142Currently supported algorithms are:
143.Pp
144.Bl -tag -width 012345678 -compact -offset indent
145.It 1
146MD5
147.It 3
148SHA1
149.El
150.Pp
151Other crypt formats may be easilly added.  An example salt would be:
152.Bl -tag -offset indent
153.It Cm "$3$thesalt$rest"
154.El
155.Pp
156.Ss "Traditional" crypt:
157.Pp
158The algorithm used will depend upon whether DES is installed or not.  If it is,
159DES will be used.  Otherwise, the best algorithm is used, which is currently
160.\"
161.\" NOTICE: Also make sure to update this
162.\"
163SHA-1.
164.Pp
165How the salt is used will depend upon the algorithm for the hash.  For
166best results, specify at least two characters of salt.
167.Sh RETURN VALUES
168.Pp
169.Fn crypt
170returns a pointer to the encrypted value on success, and NULL on failure.
171Note: this is not a standard behaviour, AT&T
172.Fn crypt
173will always return a pointer to a string.
174.Sh SEE ALSO
175.Xr login 1 ,
176.Xr passwd 1 ,
177.Xr getpass 3 ,
178.Xr passwd 5 ,
179.Xr shs 3 ,
180.Sh BUGS
181The
182.Fn crypt
183function returns a pointer to static data, and subsequent calls to
184.Fn crypt
185will modify the same data.
186.Sh HISTORY
187A rotor-based
188.Fn crypt
189function appeared in
190.At v6 .
191The current style
192.Fn crypt
193first appeared in
194.At v7 .
195.Pp
196The DES section of the code (FreeSec 1.0) was developed outside the United
197States of America as an unencumbered replacement for the U.S.-only NetBSD
198libcrypt encryption library.
199Users should be aware that this code (and programs staticly linked with it)
200may not be exported from the U.S., although it apparently can be imported.
201.Sh AUTHORS
202Originally written by David Burren <davidb@werj.com.au>, later additions
203and changes by Brandon Gillespie, Poul-henning Kamp and Mark R V Murray.
204