xref: /freebsd/lib/libcrypt/crypt.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1e9a56ad5SMark Murray.\" FreeSec: libcrypt for NetBSD
2e9a56ad5SMark Murray.\"
3e9a56ad5SMark Murray.\" Copyright (c) 1994 David Burren
4e9a56ad5SMark Murray.\" All rights reserved.
5e9a56ad5SMark Murray.\"
6e9a56ad5SMark Murray.\" Redistribution and use in source and binary forms, with or without
7e9a56ad5SMark Murray.\" modification, are permitted provided that the following conditions
8e9a56ad5SMark Murray.\" are met:
9e9a56ad5SMark Murray.\" 1. Redistributions of source code must retain the above copyright
10e9a56ad5SMark Murray.\"    notice, this list of conditions and the following disclaimer.
11e9a56ad5SMark Murray.\" 2. Redistributions in binary form must reproduce the above copyright
12e9a56ad5SMark Murray.\"    notice, this list of conditions and the following disclaimer in the
13e9a56ad5SMark Murray.\"    documentation and/or other materials provided with the distribution.
14fbbd9655SWarner Losh.\" 3. Neither the name of the author nor the names of other contributors
15e9a56ad5SMark Murray.\"    may be used to endorse or promote products derived from this software
16e9a56ad5SMark Murray.\"    without specific prior written permission.
17e9a56ad5SMark Murray.\"
18e9a56ad5SMark Murray.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19e9a56ad5SMark Murray.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20e9a56ad5SMark Murray.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21e9a56ad5SMark Murray.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22e9a56ad5SMark Murray.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23e9a56ad5SMark Murray.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24e9a56ad5SMark Murray.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25e9a56ad5SMark Murray.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26e9a56ad5SMark Murray.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27e9a56ad5SMark Murray.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28e9a56ad5SMark Murray.\" SUCH DAMAGE.
29e9a56ad5SMark Murray.\"
30*a6c0c824SXin LI.Dd May 26, 2019
31e9a56ad5SMark Murray.Dt CRYPT 3
32a307d598SRuslan Ermilov.Os
33e9a56ad5SMark Murray.Sh NAME
34e9a56ad5SMark Murray.Nm crypt
35e9a56ad5SMark Murray.Nd Trapdoor encryption
36f45f23ddSAlexey Zelkin.Sh LIBRARY
37f45f23ddSAlexey Zelkin.Lb libcrypt
38e9a56ad5SMark Murray.Sh SYNOPSIS
3932eef9aeSRuslan Ermilov.In unistd.h
40d3f0d184SBruce Evans.Ft char *
41d3f0d184SBruce Evans.Fn crypt "const char *key" "const char *salt"
425f521d7bSEd Schouten.Ft char *
435f521d7bSEd Schouten.Fn crypt_r "const char *key" "const char *salt" "struct crypt_data *data"
4404c9749fSBrian Feldman.Ft const char *
4504c9749fSBrian Feldman.Fn crypt_get_format "void"
4604c9749fSBrian Feldman.Ft int
4704c9749fSBrian Feldman.Fn crypt_set_format "const char *string"
48e9a56ad5SMark Murray.Sh DESCRIPTION
49e9a56ad5SMark MurrayThe
50e9a56ad5SMark Murray.Fn crypt
51e9a56ad5SMark Murrayfunction performs password hashing with additional code added to
521a0a9345SRuslan Ermilovdeter key search attempts.
531a0a9345SRuslan ErmilovDifferent algorithms can be used to
54e9a56ad5SMark Murrayin the hash.
55e9a56ad5SMark Murray.\"
56e9a56ad5SMark Murray.\" NOTICE:
57e9a56ad5SMark Murray.\" If you add more algorithms, make sure to update this list
58e9a56ad5SMark Murray.\" and the default used for the Traditional format, below.
59e9a56ad5SMark Murray.\"
60e9a56ad5SMark MurrayCurrently these include the
61e9a56ad5SMark Murray.Tn NBS
62f45f23ddSAlexey Zelkin.Tn Data Encryption Standard (DES) ,
635c129616SMark Murray.Tn MD5
64bf513f69SMark Murrayhash,
65bf513f69SMark Murray.Tn NT-Hash
6641f55496SJohn-Mark Gurney.Pq compatible with Microsoft's NT scheme
67f32b1300SKris Kennawayand
685c129616SMark Murray.Tn Blowfish .
6941f55496SJohn-Mark GurneyThe algorithm used will depend upon the format of the Salt
7041f55496SJohn-Mark Gurney.Po
7141f55496SJohn-Mark Gurneyfollowing
7241f55496SJohn-Mark Gurneythe Modular Crypt Format
7341f55496SJohn-Mark Gurney.Pq MCF
7441f55496SJohn-Mark Gurney.Pc ,
7541f55496SJohn-Mark Gurneyif
76f45f23ddSAlexey Zelkin.Tn DES
775c129616SMark Murrayand/or
785c129616SMark Murray.Tn Blowfish
7904c9749fSBrian Feldmanis installed or not, and whether
8004c9749fSBrian Feldman.Fn crypt_set_format
8104c9749fSBrian Feldmanhas been called to change the default.
82e9a56ad5SMark Murray.Pp
83e9a56ad5SMark MurrayThe first argument to
84f45f23ddSAlexey Zelkin.Nm
8541f55496SJohn-Mark Gurneyis the data to hash
8641f55496SJohn-Mark Gurney.Pq usually a password ,
8741f55496SJohn-Mark Gurneyin a
8841f55496SJohn-Mark Gurney.Dv NUL Ns -terminated
89e9a56ad5SMark Murraystring.
90e9a56ad5SMark MurrayThe second is the salt, in one of three forms:
91e9a56ad5SMark Murray.Pp
92e9a56ad5SMark Murray.Bl -tag -width Traditional -compact -offset indent
93e9a56ad5SMark Murray.It Extended
94f45f23ddSAlexey ZelkinIf it begins with an underscore
95f45f23ddSAlexey Zelkin.Pq Dq _
96f45f23ddSAlexey Zelkinthen the
97f45f23ddSAlexey Zelkin.Tn DES
98f45f23ddSAlexey ZelkinExtended Format
99a910f192SDima Dorfmanis used in interpreting both the key and the salt, as outlined below.
100e9a56ad5SMark Murray.It Modular
101f45f23ddSAlexey ZelkinIf it begins with the string
102f45f23ddSAlexey Zelkin.Dq $digit$
103f45f23ddSAlexey Zelkinthen the Modular Crypt Format is used, as outlined below.
104e9a56ad5SMark Murray.It Traditional
105e9a56ad5SMark MurrayIf neither of the above is true, it assumes the Traditional Format,
10641f55496SJohn-Mark Gurneyusing the entire string as the salt
10741f55496SJohn-Mark Gurney.Pq or the first portion .
108e9a56ad5SMark Murray.El
109e9a56ad5SMark Murray.Pp
1101a0a9345SRuslan ErmilovAll routines are designed to be time-consuming.
111e9a56ad5SMark Murray.Ss DES Extended Format:
112e9a56ad5SMark MurrayThe
113e9a56ad5SMark Murray.Ar key
11441f55496SJohn-Mark Gurneyis divided into groups of 8 characters
11541f55496SJohn-Mark Gurney.Pq the last group is NUL-padded
11641f55496SJohn-Mark Gurneyand the low-order 7 bits of each character
11741f55496SJohn-Mark Gurney.Pq 56 bits per group
11841f55496SJohn-Mark Gurneyare used to form the
119f45f23ddSAlexey Zelkin.Tn DES
120f45f23ddSAlexey Zelkinkey as follows:
121f45f23ddSAlexey Zelkinthe first group of 56 bits becomes the initial
122f45f23ddSAlexey Zelkin.Tn DES
123f45f23ddSAlexey Zelkinkey.
124f45f23ddSAlexey ZelkinFor each additional group, the XOR of the encryption of the current
125f45f23ddSAlexey Zelkin.Tn DES
126f45f23ddSAlexey Zelkinkey with itself and the group bits becomes the next
127f45f23ddSAlexey Zelkin.Tn DES
128f45f23ddSAlexey Zelkinkey.
129e9a56ad5SMark Murray.Pp
130e9a56ad5SMark MurrayThe salt is a 9-character array consisting of an underscore followed
131e9a56ad5SMark Murrayby 4 bytes of iteration count and 4 bytes of salt.
132e9a56ad5SMark MurrayThese are encoded as printable characters, 6 bits per character,
133e9a56ad5SMark Murrayleast significant character first.
13441f55496SJohn-Mark GurneyThe values 0 to 63 are encoded as
13541f55496SJohn-Mark Gurney.Dq ./0-9A-Za-z .
136e9a56ad5SMark MurrayThis allows 24 bits for both
137e9a56ad5SMark Murray.Fa count
138e9a56ad5SMark Murrayand
139e9a56ad5SMark Murray.Fa salt .
140e9a56ad5SMark Murray.Pp
141e9a56ad5SMark MurrayThe
142e9a56ad5SMark Murray.Fa salt
143e9a56ad5SMark Murrayintroduces disorder in the
144e9a56ad5SMark Murray.Tn DES
145e9a56ad5SMark Murrayalgorithm in one of 16777216 or 4096 possible ways
14641f55496SJohn-Mark Gurney.Po
14741f55496SJohn-Mark Gurneyi.e., with 24 or 12 bits: if bit
148e9a56ad5SMark Murray.Em i
149e9a56ad5SMark Murrayof the
150e9a56ad5SMark Murray.Ar salt
151e9a56ad5SMark Murrayis set, then bits
152e9a56ad5SMark Murray.Em i
153e9a56ad5SMark Murrayand
154e9a56ad5SMark Murray.Em i+24
155e9a56ad5SMark Murrayare swapped in the
156e9a56ad5SMark Murray.Tn DES
15741f55496SJohn-Mark GurneyE-box output
15841f55496SJohn-Mark Gurney.Pc .
159e9a56ad5SMark Murray.Pp
160f45f23ddSAlexey ZelkinThe
161f45f23ddSAlexey Zelkin.Tn DES
162f45f23ddSAlexey Zelkinkey is used to encrypt a 64-bit constant using
163e9a56ad5SMark Murray.Ar count
164e9a56ad5SMark Murrayiterations of
165e9a56ad5SMark Murray.Tn DES .
166e9a56ad5SMark MurrayThe value returned is a
16741f55496SJohn-Mark Gurney.Dv NUL Ns -terminated
16841f55496SJohn-Mark Gurneystring, 20 or 13 bytes
16941f55496SJohn-Mark Gurney.Pq plus NUL
17041f55496SJohn-Mark Gurneyin length, consisting of the
171e9a56ad5SMark Murray.Ar salt
172e9a56ad5SMark Murrayfollowed by the encoded 64-bit encryption.
17341f55496SJohn-Mark Gurney.Ss Modular crypt:
174e9a56ad5SMark MurrayIf the salt begins with the string
175e9a56ad5SMark Murray.Fa $digit$
1761a0a9345SRuslan Ermilovthen the Modular Crypt Format is used.
1771a0a9345SRuslan ErmilovThe
178e9a56ad5SMark Murray.Fa digit
1791a0a9345SRuslan Ermilovrepresents which algorithm is used in encryption.
1801a0a9345SRuslan ErmilovFollowing the token is
1811a0a9345SRuslan Ermilovthe actual salt to use in the encryption.
18241f55496SJohn-Mark GurneyThe maximum length of the salt used depends upon the module.
18341f55496SJohn-Mark GurneyThe salt must be terminated with the end of the string character
18441f55496SJohn-Mark Gurney.Pq NUL
18541f55496SJohn-Mark Gurneyor a dollar sign.
1861a0a9345SRuslan ErmilovAny characters after the dollar sign are ignored.
187e9a56ad5SMark Murray.Pp
188e9a56ad5SMark MurrayCurrently supported algorithms are:
189e9a56ad5SMark Murray.Pp
19042635956SRuslan Ermilov.Bl -enum -compact -offset indent
191067f2c3fSRuslan Ermilov.It
192e9a56ad5SMark MurrayMD5
193067f2c3fSRuslan Ermilov.It
1945c129616SMark MurrayBlowfish
195bf513f69SMark Murray.It
196bf513f69SMark MurrayNT-Hash
197a5c28e29SMark Murray.It
198b54c79e1SEitan Adler(unused)
199b54c79e1SEitan Adler.It
200a5c28e29SMark MurraySHA-256
201a5c28e29SMark Murray.It
202a5c28e29SMark MurraySHA-512
203e9a56ad5SMark Murray.El
204e9a56ad5SMark Murray.Pp
2051a0a9345SRuslan ErmilovOther crypt formats may be easily added.
2061a0a9345SRuslan ErmilovAn example salt would be:
2079ee2158bSJoel Dahl.Bl -tag -width 6n -offset indent
208bf513f69SMark Murray.It Cm "$4$thesalt$rest"
209e9a56ad5SMark Murray.El
21041f55496SJohn-Mark Gurney.Ss Traditional crypt:
211f45f23ddSAlexey ZelkinThe algorithm used will depend upon whether
21204c9749fSBrian Feldman.Fn crypt_set_format
2139886bcdfSPeter Wemmhas been called and whether a global default format has been specified.
2149886bcdfSPeter WemmUnless a global default has been specified or
21504c9749fSBrian Feldman.Fn crypt_set_format
21688b471a1SPeter Wemmhas set the format to something else, the built-in default format is
21788b471a1SPeter Wemmused.
21888b471a1SPeter WemmThis is currently
219e9a56ad5SMark Murray.\"
220e9a56ad5SMark Murray.\" NOTICE: Also make sure to update this
221e9a56ad5SMark Murray.\"
22288b471a1SPeter WemmDES
223*a6c0c824SXin LIif it is available, or SHA-512 if not.
224e9a56ad5SMark Murray.Pp
2251a0a9345SRuslan ErmilovHow the salt is used will depend upon the algorithm for the hash.
2261a0a9345SRuslan ErmilovFor
22741f55496SJohn-Mark Gurneybest results, specify at least eight characters of salt.
22804c9749fSBrian Feldman.Pp
22904c9749fSBrian FeldmanThe
23004c9749fSBrian Feldman.Fn crypt_get_format
23104c9749fSBrian Feldmanfunction returns a constant string that represents the name of the
23204c9749fSBrian Feldmanalgorithm currently used.
23304c9749fSBrian FeldmanValid values are
23404c9749fSBrian Feldman.\"
23504c9749fSBrian Feldman.\" NOTICE: Also make sure to update this, too, as well
23604c9749fSBrian Feldman.\"
2375c129616SMark Murray.Ql des ,
238bf513f69SMark Murray.Ql blf ,
239a5c28e29SMark Murray.Ql md5 ,
240a5c28e29SMark Murray.Ql sha256 ,
241a5c28e29SMark Murray.Ql sha512
24204c9749fSBrian Feldmanand
243bf513f69SMark Murray.Ql nth .
24404c9749fSBrian Feldman.Pp
24504c9749fSBrian FeldmanThe
24604c9749fSBrian Feldman.Fn crypt_set_format
24704c9749fSBrian Feldmanfunction sets the default encoding format according to the supplied
24804c9749fSBrian Feldman.Fa string .
2495f521d7bSEd Schouten.Pp
2505f521d7bSEd SchoutenThe
2515f521d7bSEd Schouten.Fn crypt_r
2525f521d7bSEd Schoutenfunction behaves identically to
2535f521d7bSEd Schouten.Fn crypt ,
2545f521d7bSEd Schoutenexcept that the resulting string is stored in
2555f521d7bSEd Schouten.Fa data ,
2565f521d7bSEd Schoutenmaking it thread-safe.
257e9a56ad5SMark Murray.Sh RETURN VALUES
258e4f2c10bSPhilippe CharnierThe
259e9a56ad5SMark Murray.Fn crypt
2605f521d7bSEd Schoutenand
2615f521d7bSEd Schouten.Fn crypt_r
2625f521d7bSEd Schoutenfunctions return a pointer to the encrypted value on success, and NULL on
263e4f2c10bSPhilippe Charnierfailure.
264e9a56ad5SMark MurrayNote: this is not a standard behaviour, AT&T
265e9a56ad5SMark Murray.Fn crypt
266e9a56ad5SMark Murraywill always return a pointer to a string.
26704c9749fSBrian Feldman.Pp
268e4f2c10bSPhilippe CharnierThe
26904c9749fSBrian Feldman.Fn crypt_set_format
270e4f2c10bSPhilippe Charnierfunction will return 1 if the supplied encoding format was valid.
27104c9749fSBrian FeldmanOtherwise, a value of 0 is returned.
272e9a56ad5SMark Murray.Sh SEE ALSO
273e9a56ad5SMark Murray.Xr login 1 ,
274e9a56ad5SMark Murray.Xr passwd 1 ,
275e9a56ad5SMark Murray.Xr getpass 3 ,
276eb894267SRuslan Ermilov.Xr passwd 5
277e9a56ad5SMark Murray.Sh HISTORY
278e9a56ad5SMark MurrayA rotor-based
279e9a56ad5SMark Murray.Fn crypt
280e9a56ad5SMark Murrayfunction appeared in
281e9a56ad5SMark Murray.At v6 .
282e9a56ad5SMark MurrayThe current style
283e9a56ad5SMark Murray.Fn crypt
284e9a56ad5SMark Murrayfirst appeared in
285e9a56ad5SMark Murray.At v7 .
286e9a56ad5SMark Murray.Pp
287f45f23ddSAlexey ZelkinThe
288f45f23ddSAlexey Zelkin.Tn DES
289f45f23ddSAlexey Zelkinsection of the code (FreeSec 1.0) was developed outside the United
290f45f23ddSAlexey ZelkinStates of America as an unencumbered replacement for the U.S.-only
291f45f23ddSAlexey Zelkin.Nx
292e9a56ad5SMark Murraylibcrypt encryption library.
2935f521d7bSEd Schouten.Pp
2945f521d7bSEd SchoutenThe
2955f521d7bSEd Schouten.Fn crypt_r
2965f521d7bSEd Schoutenfunction was added in
2975f521d7bSEd Schouten.Fx 12.0 .
298e9a56ad5SMark Murray.Sh AUTHORS
299725ab628SRuslan Ermilov.An -nosplit
300f45f23ddSAlexey ZelkinOriginally written by
3012b7af31cSBaptiste Daroussin.An David Burren Aq Mt davidb@werj.com.au ,
302f45f23ddSAlexey Zelkinlater additions and changes by
303725ab628SRuslan Ermilov.An Poul-Henning Kamp ,
30404c9749fSBrian Feldman.An Mark R V Murray ,
305bf513f69SMark Murray.An Michael Bretterklieber ,
3065c129616SMark Murray.An Kris Kennaway ,
3075c129616SMark Murray.An Brian Feldman ,
3085c129616SMark Murray.An Paul Herman
30904c9749fSBrian Feldmanand
3105c129616SMark Murray.An Niels Provos .
31124a0682cSRuslan Ermilov.Sh BUGS
31224a0682cSRuslan ErmilovThe
31324a0682cSRuslan Ermilov.Fn crypt
31424a0682cSRuslan Ermilovfunction returns a pointer to static data, and subsequent calls to
31524a0682cSRuslan Ermilov.Fn crypt
31624a0682cSRuslan Ermilovwill modify the same data.
31724a0682cSRuslan ErmilovLikewise,
31824a0682cSRuslan Ermilov.Fn crypt_set_format
31924a0682cSRuslan Ermilovmodifies static data.
32024a0682cSRuslan Ermilov.Pp
32124a0682cSRuslan ErmilovThe NT-hash scheme does not use a salt,
32224a0682cSRuslan Ermilovand is not hard
32324a0682cSRuslan Ermilovfor a competent attacker
32424a0682cSRuslan Ermilovto break.
32524a0682cSRuslan ErmilovIts use is not recommended.
326