xref: /dragonfly/lib/libcrypt/crypt.3 (revision 984263bc)
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: src/lib/libcrypt/crypt.3,v 1.6.2.14 2002/12/29 16:35:35 schweikh Exp $
31.\"
32.\" Manual page, using -mandoc macros
33.\"
34.Dd January 19, 1997
35.Dt CRYPT 3
36.Os
37.Sh NAME
38.Nm crypt
39.Nd Trapdoor encryption
40.Sh LIBRARY
41.Lb libcrypt
42.Sh SYNOPSIS
43.In unistd.h
44.Ft char *
45.Fn crypt "const char *key" "const char *salt"
46.Ft const char *
47.Fn crypt_get_format "void"
48.Ft int
49.Fn crypt_set_format "const char *string"
50.Sh DESCRIPTION
51The
52.Fn crypt
53function performs password hashing with additional code added to
54deter key search attempts.  Different algorithms can be used to
55in the hash.
56.\"
57.\" NOTICE:
58.\" If you add more algorithms, make sure to update this list
59.\" and the default used for the Traditional format, below.
60.\"
61Currently these include the
62.Tn NBS
63.Tn Data Encryption Standard (DES) ,
64.Tn MD5
65and
66.Tn Blowfish .
67The algorithm used will depend upon the format of the Salt (following
68the Modular Crypt Format (MCF)), if
69.Tn DES
70and/or
71.Tn Blowfish
72is installed or not, and whether
73.Fn crypt_set_format
74has been called to change the default.
75.Pp
76The first argument to
77.Nm
78is the data to hash (usually a password), in a
79.Dv null Ns -terminated
80string.
81The second is the salt, in one of three forms:
82.Pp
83.Bl -tag -width Traditional -compact -offset indent
84.It Extended
85If it begins with an underscore
86.Pq Dq _
87then the
88.Tn DES
89Extended Format
90is used in interpreting both the key and the salt, as outlined below.
91.It Modular
92If it begins with the string
93.Dq $digit$
94then the Modular Crypt Format is used, as outlined below.
95.It Traditional
96If neither of the above is true, it assumes the Traditional Format,
97using the entire string as the salt (or the first portion).
98.El
99.Pp
100All routines are designed to be time-consuming.  A brief test on a
101.Tn Pentium
102166/MMX shows the
103.Tn DES
104crypt to do approximately 2640 crypts
105a CPU second and MD5 to do about 62 crypts a CPU second.
106.Ss DES Extended Format:
107.Pp
108The
109.Ar key
110is divided into groups of 8 characters (the last group is null-padded)
111and the low-order 7 bits of each character (56 bits per group) are
112used to form the
113.Tn DES
114key as follows:
115the first group of 56 bits becomes the initial
116.Tn DES
117key.
118For each additional group, the XOR of the encryption of the current
119.Tn DES
120key with itself and the group bits becomes the next
121.Tn DES
122key.
123.Pp
124The salt is a 9-character array consisting of an underscore followed
125by 4 bytes of iteration count and 4 bytes of salt.
126These are encoded as printable characters, 6 bits per character,
127least significant character first.
128The values 0 to 63 are encoded as ``./0-9A-Za-z''.
129This allows 24 bits for both
130.Fa count
131and
132.Fa salt .
133.Pp
134The
135.Fa salt
136introduces disorder in the
137.Tn DES
138algorithm in one of 16777216 or 4096 possible ways
139(ie. with 24 or 12 bits: if bit
140.Em i
141of the
142.Ar salt
143is set, then bits
144.Em i
145and
146.Em i+24
147are swapped in the
148.Tn DES
149E-box output).
150.Pp
151The
152.Tn DES
153key is used to encrypt a 64-bit constant using
154.Ar count
155iterations of
156.Tn DES .
157The value returned is a
158.Dv null Ns -terminated
159string, 20 or 13 bytes (plus null) in length, consisting of the
160.Ar salt
161followed by the encoded 64-bit encryption.
162.Ss "Modular" crypt:
163.Pp
164If the salt begins with the string
165.Fa $digit$
166then the Modular Crypt Format is used.  The
167.Fa digit
168represents which algorithm is used in encryption.  Following the token is
169the actual salt to use in the encryption.  The length of the salt is limited
170to 8 characters--because the length of the returned output is also limited
171(_PASSWORD_LEN).  The salt must be terminated with the end of the string
172(NULL) or a dollar sign.  Any characters after the dollar sign are ignored.
173.Pp
174Currently supported algorithms are:
175.Pp
176.Bl -enum -compact -offset indent
177.It
178MD5
179.It
180Blowfish
181.El
182.Pp
183Other crypt formats may be easily added.  An example salt would be:
184.Bl -tag -offset indent
185.It Cm "$3$thesalt$rest"
186.El
187.Pp
188.Ss "Traditional" crypt:
189.Pp
190The algorithm used will depend upon whether
191.Fn crypt_set_format
192has been called and whether a global default format has been specified.
193Unless a global default has been specified or
194.Fn crypt_set_format
195has set the format to something else, the built-in default format is
196used.
197This is currently
198.\"
199.\" NOTICE: Also make sure to update this
200.\"
201DES
202if it is available, or MD5 if not.
203.Pp
204How the salt is used will depend upon the algorithm for the hash.  For
205best results, specify at least two characters of salt.
206.Pp
207The
208.Fn crypt_get_format
209function returns a constant string that represents the name of the
210algorithm currently used.
211Valid values are
212.\"
213.\" NOTICE: Also make sure to update this, too, as well
214.\"
215.Ql des ,
216.Ql blf
217and
218.Ql md5 .
219.Pp
220The
221.Fn crypt_set_format
222function sets the default encoding format according to the supplied
223.Fa string .
224.Pp
225The global default format can be set using the
226.Pa /etc/auth.conf
227file using the
228.Va crypt_default
229property.
230.Sh RETURN VALUES
231.Fn crypt
232returns a pointer to the encrypted value on success, and NULL on failure.
233Note: this is not a standard behaviour, AT&T
234.Fn crypt
235will always return a pointer to a string.
236.Pp
237.Fn crypt_set_format
238will return 1 if the supplied encoding format was valid.
239Otherwise, a value of 0 is returned.
240.Sh SEE ALSO
241.Xr login 1 ,
242.Xr passwd 1 ,
243.Xr auth_getval 3 ,
244.Xr cipher 3 ,
245.Xr getpass 3 ,
246.Xr auth.conf 5 ,
247.Xr passwd 5
248.Sh BUGS
249The
250.Fn crypt
251function returns a pointer to static data, and subsequent calls to
252.Fn crypt
253will modify the same data.  Likewise,
254.Fn crypt_set_format
255modifies static data.
256.Sh HISTORY
257A rotor-based
258.Fn crypt
259function appeared in
260.At v6 .
261The current style
262.Fn crypt
263first appeared in
264.At v7 .
265.Pp
266The
267.Tn DES
268section of the code (FreeSec 1.0) was developed outside the United
269States of America as an unencumbered replacement for the U.S.-only
270.Nx
271libcrypt encryption library.
272.Sh AUTHORS
273.An -nosplit
274Originally written by
275.An David Burren Aq davidb@werj.com.au ,
276later additions and changes by
277.An Poul-Henning Kamp ,
278.An Mark R V Murray ,
279.An Kris Kennaway ,
280.An Brian Feldman ,
281.An Paul Herman
282and
283.An Niels Provos .
284