ioctl_tty.2: Document ioctls: TCGETS2, TCSETS2, TCSETSW2, TCSETSF2
[man-pages.git] / man3 / crypt.3
blobc972e61f0ddb79a6ac851b21052710c686334846
1 .\" Michael Haardt (michael@cantor.informatik.rwth.aachen.de)
2 .\"     Sat Sep  3 22:00:30 MET DST 1994
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\"
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
14 .\"
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 .\" GNU General Public License for more details.
19 .\"
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Sun Feb 19 21:32:25 1995, faith@cs.unc.edu edited details away
26 .\"
27 .\" TO DO: This manual page should go more into detail how DES is perturbed,
28 .\" which string will be encrypted, and what determines the repetition factor.
29 .\" Is a simple repetition using ECB used, or something more advanced?  I hope
30 .\" the presented explanations are at least better than nothing, but by no
31 .\" means enough.
32 .\"
33 .\" added _XOPEN_SOURCE, aeb, 970705
34 .\" added GNU MD5 stuff, aeb, 011223
35 .\"
36 .TH CRYPT 3 2021-03-22 "" "Linux Programmer's Manual"
37 .SH NAME
38 crypt, crypt_r \- password and data encryption
39 .SH SYNOPSIS
40 .nf
41 .B #include <unistd.h>
42 .PP
43 .BI "char *crypt(const char *" key ", const char *" salt );
44 .PP
45 .B #include <crypt.h>
46 .PP
47 .BI "char *crypt_r(const char *" key ", const char *" salt ,
48 .BI "              struct crypt_data *restrict " data );
49 .fi
50 .PP
51 Link with \fI\-lcrypt\fP.
52 .PP
53 .RS -4
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .RE
57 .PP
58 .BR crypt ():
59 .nf
60     Since glibc 2.28:
61         _DEFAULT_SOURCE
62     Glibc 2.27 and earlier:
63         _XOPEN_SOURCE
64 .fi
65 .BR
66 .PP
67 .BR crypt_r ():
68 .nf
69     _GNU_SOURCE
70 .fi
71 .SH DESCRIPTION
72 .BR crypt ()
73 is the password encryption function.
74 It is based on the Data Encryption
75 Standard algorithm with variations intended (among other things) to
76 discourage use of hardware implementations of a key search.
77 .PP
78 .I key
79 is a user's typed password.
80 .PP
81 .I salt
82 is a two-character string chosen from the set
83 [\fBa\-zA\-Z0\-9./\fP].
84 This string is used to
85 perturb the algorithm in one of 4096 different ways.
86 .PP
87 By taking the lowest 7 bits of each of the first eight characters of the
88 .IR key ,
89 a 56-bit key is obtained.
90 This 56-bit key is used to encrypt repeatedly a
91 constant string (usually a string consisting of all zeros).
92 The returned
93 value points to the encrypted password, a series of 13 printable ASCII
94 characters (the first two characters represent the salt itself).
95 The return value points to static data whose content is
96 overwritten by each call.
97 .PP
98 Warning: the key space consists of
99 .if t 2\s-2\u56\s0\d
100 .if n 2**56
101 equal 7.2e16 possible values.
102 Exhaustive searches of this key space are
103 possible using massively parallel computers.
104 Software, such as
105 .BR crack (1),
106 is available which will search the portion of this key space that is
107 generally used by humans for passwords.
108 Hence, password selection should,
109 at minimum, avoid common words and names.
110 The use of a
111 .BR passwd (1)
112 program that checks for crackable passwords during the selection process is
113 recommended.
115 The DES algorithm itself has a few quirks which make the use of the
116 .BR crypt ()
117 interface a very poor choice for anything other than password
118 authentication.
119 If you are planning on using the
120 .BR crypt ()
121 interface for a cryptography project, don't do it: get a good book on
122 encryption and one of the widely available DES libraries.
124 .BR crypt_r ()
125 is a reentrant version of
126 .BR crypt ().
127 The structure pointed to by
128 .I data
129 is used to store result data and bookkeeping information.
130 Other than allocating it,
131 the only thing that the caller should do with this structure is to set
132 .I data\->initialized
133 to zero before the first call to
134 .BR crypt_r ().
135 .SH RETURN VALUE
136 On success, a pointer to the encrypted password is returned.
137 On error, NULL is returned.
138 .SH ERRORS
140 .B EINVAL
141 .I salt
142 has the wrong format.
144 .B ENOSYS
146 .BR crypt ()
147 function was not implemented, probably because of U.S.A. export restrictions.
148 .\" This level of detail is not necessary in this man page. . .
149 .\" .PP
150 .\" When encrypting a plain text P using DES with the key K results in the
151 .\" encrypted text C, then the complementary plain text P' being encrypted
152 .\" using the complementary key K' will result in the complementary encrypted
153 .\" text C'.
154 .\" .PP
155 .\" Weak keys are keys which stay invariant under the DES key transformation.
156 .\" The four known weak keys 0101010101010101, fefefefefefefefe,
157 .\" 1f1f1f1f0e0e0e0e and e0e0e0e0f1f1f1f1 must be avoided.
158 .\" .PP
159 .\" There are six known half weak key pairs, which keys lead to the same
160 .\" encrypted data.  Keys which are part of such key clusters should be
161 .\" avoided.
162 .\" Sorry, I could not find out what they are.
163 .\""
164 .\" .PP
165 .\" Heavily redundant data causes trouble with DES encryption, when used in the
166 .\" .I codebook
167 .\" mode that
168 .\" .BR crypt ()
169 .\" implements.  The
170 .\" .BR crypt ()
171 .\" interface should be used only for its intended purpose of password
172 .\" verification, and should not be used as part of a data encryption tool.
173 .\" .PP
174 .\" The first and last three output bits of the fourth S-box can be
175 .\" represented as function of their input bits.  Empiric studies have
176 .\" shown that S-boxes partially compute the same output for similar input.
177 .\" It is suspected that this may contain a back door which could allow the
178 .\" NSA to decrypt DES encrypted data.
179 .\" .PP
180 .\" Making encrypted data computed using crypt() publicly available has
181 .\" to be considered insecure for the given reasons.
183 .B EPERM
184 .I /proc/sys/crypto/fips_enabled
185 has a nonzero value,
186 and an attempt was made to use a weak encryption type, such as DES.
187 .SH ATTRIBUTES
188 For an explanation of the terms used in this section, see
189 .BR attributes (7).
190 .ad l
193 allbox;
194 lbx lb lb
195 l l l.
196 Interface       Attribute       Value
198 .BR crypt ()
199 T}      Thread safety   MT-Unsafe race:crypt
201 .BR crypt_r ()
202 T}      Thread safety   MT-Safe
206 .sp 1
207 .SH CONFORMING TO
208 .BR crypt ():
209 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
210 .BR crypt_r ()
211 is a GNU extension.
212 .SH NOTES
213 .SS Availability in glibc
215 .BR crypt (),
216 .BR encrypt (3),
218 .BR setkey (3)
219 functions are part of the POSIX.1-2008 XSI Options Group for Encryption
220 and are optional.
221 If the interfaces are not available, then the symbolic constant
222 .BR _XOPEN_CRYPT
223 is either not defined,
224 or it is defined to \-1 and availability can be checked at run time with
225 .BR sysconf (3).
226 This may be the case if the downstream distribution has switched from glibc
227 crypt to
228 .IR libxcrypt .
229 When recompiling applications in such distributions,
230 the programmer must detect if
231 .BR _XOPEN_CRYPT
232 is not available and include
233 .I <crypt.h>
234 for the function prototypes;
235 otherwise
236 .I libxcrypt
237 is an ABI-compatible drop-in replacement.
238 .SS Features in glibc
239 The glibc version of this function supports additional
240 encryption algorithms.
243 .I salt
244 is a character string starting with the characters "$\fIid\fP$"
245 followed by a string optionally terminated by "$",
246 then the result has the form:
249 $\fIid\fP$\fIsalt\fP$\fIencrypted\fP
252 .I id
253 identifies the encryption method used instead of DES and this
254 then determines how the rest of the password string is interpreted.
255 The following values of
256 .I id
257 are supported:
260 lb lb
261 l lx.
262 ID      Method
264 1       MD5
265 2a      T{
266 Blowfish (not in mainline glibc; added in some
267 Linux distributions)
269 .\" openSUSE has Blowfish, but AFAICS, this option is not supported
270 .\" natively by glibc -- mtk, Jul 08
272 .\" md5 | Sun MD5
273 .\" glibc doesn't appear to natively support Sun MD5; I don't know
274 .\" if any distros add the support.
275 5       SHA-256 (since glibc 2.7)
276 6       SHA-512 (since glibc 2.7)
280 Thus, $5$\fIsalt\fP$\fIencrypted\fP and $6$\fIsalt\fP$\fIencrypted\fP
281 contain the password encrypted with, respectively, functions
282 based on SHA-256 and SHA-512.
284 "\fIsalt\fP" stands for the up to 16 characters
285 following "$\fIid\fP$" in the salt.
286 The "\fIencrypted\fP"
287 part of the password string is the actual computed password.
288 The size of this string is fixed:
291 lb l.
292 MD5     22 characters
293 SHA-256 43 characters
294 SHA-512 86 characters
298 The characters in "\fIsalt\fP" and "\fIencrypted\fP" are drawn from the set
299 [\fBa\-zA\-Z0\-9./\fP].
300 In the MD5 and SHA implementations the entire
301 .I key
302 is significant (instead of only the first
303 8 bytes in DES).
305 Since glibc 2.7,
306 .\" glibc commit 9425cb9eea6a62fc21d99aafe8a60f752b934b05
307 the SHA-256 and SHA-512 implementations support a user-supplied number of
308 hashing rounds, defaulting to 5000.
309 If the "$\fIid\fP$" characters in the salt are
310 followed by "rounds=\fIxxx\fP$", where \fIxxx\fP is an integer, then the
311 result has the form
314 $\fIid\fP$\fIrounds=yyy\fP$\fIsalt\fP$\fIencrypted\fP
317 where \fIyyy\fP is the number of hashing rounds actually used.
318 The number of rounds actually used is 1000 if
319 .I xxx
320 is less than
321 1000, 999999999 if
322 .I xxx
323 is greater than 999999999, and
324 is equal to
325 .I xxx
326 otherwise.
327 .SH SEE ALSO
328 .BR login (1),
329 .BR passwd (1),
330 .BR encrypt (3),
331 .BR getpass (3),
332 .BR passwd (5)