1CRYPT(3)                 BSD Library Functions Manual                 CRYPT(3)
2

NAME

4     crypt, crypt_r, crypt_rn, crypt_ra — passphrase hashing
5

LIBRARY

7     Crypt Library (libcrypt, -lcrypt)
8

SYNOPSIS

10     #include <crypt.h>
11
12     char *
13     crypt(const char *phrase, const char *setting);
14
15     char *
16     crypt_r(const char *phrase, const char *setting,
17         struct crypt_data *data);
18
19     char *
20     crypt_rn(const char *phrase, const char *setting,
21         struct crypt_data *data, int size);
22
23     char *
24     crypt_ra(const char *phrase, const char *setting, void **data,
25         int *size);
26

DESCRIPTION

28     The crypt, crypt_r, crypt_rn, and crypt_ra functions irreversibly “hash”
29     phrase for storage in the system password database (shadow(5)) using a
30     cryptographic “hashing method.” The result of this operation is called a
31     “hashed passphrase” or just a “hash.” Hashing methods are described in
32     crypt(5).
33
34     setting controls which hashing method to use, and also supplies various
35     parameters to the chosen method, most importantly a random “salt” which
36     ensures that no two stored hashes are the same, even if the phrase
37     strings are the same.
38
39     The data argument to crypt_r is a structure of type struct crypt_data.
40     It has at least these fields:
41
42           struct crypt_data {
43               char output[CRYPT_OUTPUT_SIZE];
44               char setting[CRYPT_OUTPUT_SIZE];
45               char phrase[CRYPT_MAX_PASSPHRASE_SIZE];
46               char initialized;
47           };
48
49     Upon a successful return from crypt_r, the hashed passphrase will be
50     stored in output.  Applications are encouraged, but not required, to use
51     the phrase and setting fields to store the strings that they will pass as
52     phrase and setting to crypt_r.  This will make it easier to erase all
53     sensitive data after it is no longer needed.
54
55     The initialized field must be set to zero before the first time a struct
56     crypt_data object is first used in a call to crypt_r().  We recommend
57     zeroing the entire object, not just initialized and not just the docu‐
58     mented fields, before the first use.  (Of course, do this before storing
59     anything in setting and phrase.)
60
61     The data argument to crypt_rn should also point to a struct crypt_data
62     object, and size should be the size of that object, cast to int.  When
63     used with crypt_rn, the entire data object (except for the phrase and
64     setting fields) must be zeroed before its first use; this is not just a
65     recommendation, as it is for crypt_r.  Otherwise, the fields of the
66     object have the same uses that they do for crypt_r.
67
68     On the first call to crypt_ra, data should be the address of a void *
69     variable set to NULL, and size should be the address of an int variable
70     set to zero.  crypt_ra will allocate and initialize a struct crypt_data
71     object, using malloc(3), and write its address and size into the vari‐
72     ables pointed to by data and size.  These can be reused in subsequent
73     calls.  After the application is done hashing passphrases, it should
74     deallocate the struct crypt_data object using free(3).
75

RETURN VALUES

77     Upon successful completion, crypt, crypt_r, crypt_rn, and crypt_ra return
78     a pointer to a string which encodes both the hashed passphrase, and the
79     settings that were used to encode it.  This string is directly usable as
80     setting in other calls to crypt, crypt_r, crypt_rn, and crypt_ra, and as
81     prefix in calls to crypt_gensalt, crypt_gensalt_rn, and crypt_gensalt_ra.
82     It will be entirely printable ASCII, and will not contain whitespace or
83     the characters ‘:’, ‘;’, ‘*’, ‘!’, or ‘\’.  See crypt(5) for more detail
84     on the format of hashed passphrases.
85
86     crypt places its result in a static storage area, which will be overwrit‐
87     ten by subsequent calls to crypt.  It is not safe to call crypt from mul‐
88     tiple threads simultaneously.
89
90     crypt_r, crypt_rn, and crypt_ra place their result in the output field of
91     their data argument.  It is safe to call them from multiple threads
92     simultaneously, as long as a separate data object is used for each
93     thread.
94
95     Upon error, crypt_r, crypt_rn, and crypt_ra write an invalid hashed
96     passphrase to the output field of their data argument, and crypt writes
97     an invalid hash to its static storage area.  This string will be shorter
98     than 13 characters, will begin with a ‘*’, and will not compare equal to
99     setting.
100
101     Upon error, crypt_rn and crypt_ra return a null pointer.  crypt_r and
102     crypt may also return a null pointer, or they may return a pointer to the
103     invalid hash, depending on how libcrypt was configured.  (The option to
104     return the invalid hash is for compatibility with old applications that
105     assume that crypt cannot return a null pointer.  See PORTABILITY NOTES
106     below.)
107
108     All four functions set errno when they fail.
109

ERRORS

111     EINVAL             setting is invalid, or requests a hashing method that
112                        is not supported.
113
114     ERANGE             phrase is too long (more than
115                        CRYPT_MAX_PASSPHRASE_SIZE characters; some hashing
116                        methods may have lower limits).
117                        crypt_rn only: size is too small for the hashing
118                        method requested by setting.
119
120     ENOMEM             Failed to allocate internal scratch memory.
121                        crypt_ra only: failed to allocate memory for data.
122
123     ENOSYS or EOPNOTSUPP
124                        Hashing passphrases is not supported at all on this
125                        installation, or the hashing method requested by
126                        setting is not supported.  These error codes are not
127                        used by this version of libcrypt, but may be encoun‐
128                        tered on other systems.
129

PORTABILITY NOTES

131     crypt is included in POSIX, but crypt_r, crypt_rn, and crypt_ra are not
132     part of any standard.
133
134     POSIX does not specify any hashing methods, and does not require hashed
135     passphrases to be portable between systems.  In practice, hashed
136     passphrases are portable as long as both systems support the hashing
137     method that was used.  However, the set of supported hashing methods
138     varies considerably from system to system.
139
140     The behavior of crypt on errors isn't well standardized.  Some implemen‐
141     tations simply can't fail (except by crashing the program), others return
142     a null pointer or a fixed string.  Most implementations don't set errno,
143     but some do.  POSIX specifies returning a null pointer and setting errno,
144     but it defines only one possible error, ENOSYS, in the case where crypt
145     is not supported at all.  Some older applications are not prepared to
146     handle null pointers returned by crypt.  The behavior described above for
147     this implementation, setting errno and returning an invalid hashed
148     passphrase different from setting, is chosen to make these applications
149     fail closed when an error occurs.
150
151     Due to historical restrictions on the export of cryptographic software
152     from the USA, crypt is an optional POSIX component.  Applications should
153     therefore be prepared for crypt not to be available, or to always fail
154     (setting errno to ENOSYS) at runtime.
155
156     POSIX specifies that crypt is declared in <unistd.h>, but only if the
157     macro _XOPEN_CRYPT is defined and has a value greater than or equal to
158     zero.  Since libcrypt does not provide <unistd.h>, it declares crypt,
159     crypt_r, crypt_rn, and crypt_ra in <crypt.h> instead.
160
161     On a minority of systems (notably recent versions of Solaris), crypt uses
162     a thread-specific static storage buffer, which makes it safe to call from
163     multiple threads simultaneously, but does not prevent each call within a
164     thread from overwriting the results of the previous one.
165

BUGS

167     Some implementations of crypt, upon error, return an invalid hash that is
168     stored in a read-only location or only initialized once, which means that
169     it is only safe to erase the buffer pointed to by the crypt return value
170     if an error did not occur.
171
172     struct crypt_data may be quite large (32kB in this implementation of
173     libcrypt; over 128kB in some other implementations).  This is large
174     enough that it may be unwise to allocate it on the stack.
175
176     Some recently designed hashing methods need even more scratch memory, but
177     the crypt_r interface makes it impossible to change the size of struct
178     crypt_data without breaking binary compatibility.  The crypt_rn interface
179     could accommodate larger allocations for specific hashing methods, but
180     the caller of crypt_rn has no way of knowing how much memory to allocate.
181     crypt_ra does the allocation itself, but can only make a single call to
182     malloc(3).
183

ATTRIBUTES

185     For an explanation of the terms used in this section, see attributes(7).
186     ┌───────────────────┬───────────────┬──────────────────────┐
187Interface          Attribute     Value                
188     ├───────────────────┼───────────────┼──────────────────────┤
189crypt              │ Thread safety │ MT-Unsafe race:crypt │
190     ├───────────────────┼───────────────┼──────────────────────┤
191crypt_r, crypt_rn, │ Thread safety │ MT-Safe              │
192crypt_ra           │               │                      │
193     └───────────────────┴───────────────┴──────────────────────┘
194

HISTORY

196     A rotor-based crypt function appeared in Version 6 AT&T UNIX.  The
197     “traditional” DES-based crypt first appeared in Version 7 AT&T UNIX.
198
199     crypt_r originates with the GNU C Library.  There's also a crypt_r func‐
200     tion on HP-UX and MKS Toolkit, but the prototypes and semantics differ.
201
202     crypt_rn and crypt_ra originate with the Openwall project.
203

SEE ALSO

205     crypt_gensalt(3), getpass(3), getpwent(3), shadow(3), login(1),
206     passwd(1), crypt(5), passwd(5), shadow(5), pam(8)
207
208Openwall Project               October 11, 2017               Openwall Project
Impressum