1CRYPT(3) Linux Programmer's Manual CRYPT(3)
2
3
4
6 crypt, crypt_r - password and data encryption
7
9 #define _XOPEN_SOURCE
10 #include <unistd.h>
11
12 char *crypt(const char *key, const char *salt);
13
14 char *crypt_r(const char *key, const char *salt,
15 struct crypt_data *data);
16
17 Link with -lcrypt.
18
20 crypt() is the password encryption function. It is based on the Data
21 Encryption Standard algorithm with variations intended (among other
22 things) to discourage use of hardware implementations of a key search.
23
24 key is a user's typed password.
25
26 salt is a two-character string chosen from the set [a–zA–Z0–9./]. This
27 string is used to perturb the algorithm in one of 4096 different ways.
28
29 By taking the lowest 7 bits of each of the first eight characters of
30 the key, a 56-bit key is obtained. This 56-bit key is used to encrypt
31 repeatedly a constant string (usually a string consisting of all
32 zeros). The returned value points to the encrypted password, a series
33 of 13 printable ASCII characters (the first two characters represent
34 the salt itself). The return value points to static data whose content
35 is overwritten by each call.
36
37 Warning: The key space consists of 2**56 equal 7.2e16 possible values.
38 Exhaustive searches of this key space are possible using massively par‐
39 allel computers. Software, such as crack(1), is available which will
40 search the portion of this key space that is generally used by humans
41 for passwords. Hence, password selection should, at minimum, avoid
42 common words and names. The use of a passwd(1) program that checks for
43 crackable passwords during the selection process is recommended.
44
45 The DES algorithm itself has a few quirks which make the use of the
46 crypt() interface a very poor choice for anything other than password
47 authentication. If you are planning on using the crypt() interface for
48 a cryptography project, don't do it: get a good book on encryption and
49 one of the widely available DES libraries.
50
51 crypt_r() is a reentrant version of crypt(). The structure pointed to
52 by data is used to store result data and bookkeeping information.
53 Other than allocating it, the only thing that the caller should do with
54 this structure is to set data->initialized to zero before the first
55 call to crypt_r().
56
58 On success, a pointer to the encrypted password is returned. On error,
59 NULL is returned.
60
62 ENOSYS The crypt() function was not implemented, probably because of
63 U.S.A. export restrictions.
64
66 crypt(): SVr4, 4.3BSD, POSIX.1-2001. crypt_r() is a GNU extension.
67
69 Glibc Notes
70 The glibc2 version of this function supports additional encryption
71 algorithms.
72
73 If salt is a character string starting with the characters "$id$" fol‐
74 lowed by a string terminated by "$":
75
76 $id$salt$encrypted
77
78 then instead of using the DES machine, id identifies the encryption
79 method used and this then determines how the rest of the password
80 string is interpreted. The following values of id are supported:
81
82 ID | Method
83 ─────────────────────────────────────────────────────────
84 1 | MD5
85 2a | Blowfish (not in mainline glibc; added in some
86 | Linux distributions)
87 5 | SHA-256 (since glibc 2.7)
88 6 | SHA-512 (since glibc 2.7)
89
90 So $5$salt$encrypted is an SHA-256 encoded password and
91 $6$salt$encrypted is an SHA-512 encoded one.
92
93 "salt" stands for the up to 16 characters following "$id$" in the salt.
94 The encrypted part of the password string is the actual computed pass‐
95 word. The size of this string is fixed:
96
97 MD5 | 22 characters
98 SHA-256 | 43 characters
99 SHA-512 | 86 characters
100
101 The characters in "salt" and "encrypted" are drawn from the set
102 [a–zA–Z0–9./]. In the MD5 and SHA implementations the entire key is
103 significant (instead of only the first 8 bytes in DES).
104
106 login(1), passwd(1), encrypt(3), getpass(3), passwd(5), fea‐
107 ture_test_macros(7)
108
110 This page is part of release 3.22 of the Linux man-pages project. A
111 description of the project, and information about reporting bugs, can
112 be found at http://www.kernel.org/doc/man-pages/.
113
114
115
116 2010-06-20 CRYPT(3)