]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libcrypt/crypt.3
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 .Pp
111 The
112 .Ar key
113 is divided into groups of 8 characters (the last group is null-padded)
114 and the low-order 7 bits of each character (56 bits per group) are
115 used to form the
116 .Tn DES
117 key as follows:
118 the first group of 56 bits becomes the initial
119 .Tn DES
120 key.
121 For each additional group, the XOR of the encryption of the current
122 .Tn DES
123 key with itself and the group bits becomes the next
124 .Tn DES
125 key.
126 .Pp
127 The salt is a 9-character array consisting of an underscore followed
128 by 4 bytes of iteration count and 4 bytes of salt.
129 These are encoded as printable characters, 6 bits per character,
130 least significant character first.
131 The values 0 to 63 are encoded as ``./0-9A-Za-z''.
132 This allows 24 bits for both
133 .Fa count
134 and
135 .Fa salt .
136 .Pp
137 The
138 .Fa salt
139 introduces disorder in the
140 .Tn DES
141 algorithm in one of 16777216 or 4096 possible ways
142 (i.e., with 24 or 12 bits: if bit
143 .Em i
144 of the
145 .Ar salt
146 is set, then bits
147 .Em i
148 and
149 .Em i+24
150 are swapped in the
151 .Tn DES
152 E-box output).
153 .Pp
154 The
155 .Tn DES
156 key is used to encrypt a 64-bit constant using
157 .Ar count
158 iterations of
159 .Tn DES .
160 The value returned is a
161 .Dv null Ns -terminated
162 string, 20 or 13 bytes (plus null) in length, consisting of the
163 .Ar salt
164 followed by the encoded 64-bit encryption.
165 .Ss "Modular" crypt:
166 .Pp
167 If the salt begins with the string
168 .Fa $digit$
169 then the Modular Crypt Format is used.
170 The
171 .Fa digit
172 represents which algorithm is used in encryption.
173 Following the token is
174 the actual salt to use in the encryption.
175 The length of the salt is limited
176 to 8 characters--because the length of the returned output is also limited
177 (_PASSWORD_LEN).
178 The salt must be terminated with the end of the string
179 (NULL) or a dollar sign.
180 Any characters after the dollar sign are ignored.
181 .Pp
182 Currently supported algorithms are:
183 .Pp
184 .Bl -enum -compact -offset indent
185 .It
186 MD5
187 .It
188 Blowfish
189 .It
190 NT-Hash
191 .It
192 (unused)
193 .It
194 SHA-256
195 .It
196 SHA-512
197 .El
198 .Pp
199 Other crypt formats may be easily added.
200 An example salt would be:
201 .Bl -tag -offset indent
202 .It Cm "$4$thesalt$rest"
203 .El
204 .Pp
205 .Ss "Traditional" crypt:
206 .Pp
207 The algorithm used will depend upon whether
208 .Fn crypt_set_format
209 has been called and whether a global default format has been specified.
210 Unless a global default has been specified or
211 .Fn crypt_set_format
212 has set the format to something else, the built-in default format is
213 used.
214 This is currently
215 .\"
216 .\" NOTICE: Also make sure to update this
217 .\"
218 DES
219 if it is available, or MD5 if not.
220 .Pp
221 How the salt is used will depend upon the algorithm for the hash.
222 For
223 best results, specify at least two characters of salt.
224 .Pp
225 The
226 .Fn crypt_get_format
227 function returns a constant string that represents the name of the
228 algorithm currently used.
229 Valid values are
230 .\"
231 .\" NOTICE: Also make sure to update this, too, as well
232 .\"
233 .Ql des ,
234 .Ql blf ,
235 .Ql md5 ,
236 .Ql sha256 ,
237 .Ql sha512
238 and
239 .Ql nth .
240 .Pp
241 The
242 .Fn crypt_set_format
243 function sets the default encoding format according to the supplied
244 .Fa string .
245 .Sh RETURN VALUES
246 The
247 .Fn crypt
248 function returns a pointer to the encrypted value on success, and NULL on
249 failure.
250 Note: this is not a standard behaviour, AT&T
251 .Fn crypt
252 will always return a pointer to a string.
253 .Pp
254 The
255 .Fn crypt_set_format
256 function will return 1 if the supplied encoding format was valid.
257 Otherwise, a value of 0 is returned.
258 .Sh SEE ALSO
259 .Xr login 1 ,
260 .Xr passwd 1 ,
261 .Xr getpass 3 ,
262 .Xr passwd 5
263 .Sh HISTORY
264 A rotor-based
265 .Fn crypt
266 function appeared in
267 .At v6 .
268 The current style
269 .Fn crypt
270 first appeared in
271 .At v7 .
272 .Pp
273 The
274 .Tn DES
275 section of the code (FreeSec 1.0) was developed outside the United
276 States of America as an unencumbered replacement for the U.S.-only
277 .Nx
278 libcrypt encryption library.
279 .Sh AUTHORS
280 .An -nosplit
281 Originally written by
282 .An David Burren Aq davidb@werj.com.au ,
283 later additions and changes by
284 .An Poul-Henning Kamp ,
285 .An Mark R V Murray ,
286 .An Michael Bretterklieber ,
287 .An Kris Kennaway ,
288 .An Brian Feldman ,
289 .An Paul Herman
290 and
291 .An Niels Provos .
292 .Sh BUGS
293 The
294 .Fn crypt
295 function returns a pointer to static data, and subsequent calls to
296 .Fn crypt
297 will modify the same data.
298 Likewise,
299 .Fn crypt_set_format
300 modifies static data.
301 .Pp
302 The NT-hash scheme does not use a salt,
303 and is not hard
304 for a competent attacker
305 to break.
306 Its use is not recommended.