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