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