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