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