xref: /netbsd-src/lib/libcrypt/crypt.3 (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1.\"	$NetBSD: crypt.3,v 1.10 2002/01/15 02:40:05 wiz Exp $
2.\"
3.\" Copyright (c) 1989, 1991, 1993
4.\"	The Regents of the University of California.  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.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)crypt.3	8.2 (Berkeley) 12/11/93
35.\"
36.Dd December 11, 1993
37.Dt CRYPT 3
38.Os
39.Sh NAME
40.Nm crypt ,
41.Nm setkey ,
42.Nm encrypt ,
43.Nm des_setkey ,
44.Nm des_cipher
45.Nd DES encryption
46.Sh LIBRARY
47.Lb libcrypt
48.Sh SYNOPSIS
49.Fd #include <unistd.h>
50.Ft char
51.Fn *crypt "const char *key" "const char *setting"
52.Ft int
53.Fn encrypt "char *block" "int flag"
54.Ft int
55.Fn des_setkey "const char *key"
56.Ft int
57.Fn des_cipher "const char *in" "char *out" "long salt" "int count"
58.Fd #include <stdlib.h>
59.Ft int
60.Fn setkey "const char *key"
61.Sh DESCRIPTION
62The
63.Fn crypt
64function
65performs password encryption.  The encryption scheme used by
66.Fn crypt
67is dependent upon the contents of the
68.Dv NUL Ns -terminated
69string
70.Ar setting .
71If
72.Ar setting
73begins with the ``$'' character, a non-DES encryption scheme is selected
74(currently MD5 hashing only).  If
75.Ar setting
76begins with the ``_'' character, DES encryption with a user specified number
77of perturbations is selected.  If
78.Ar setting
79begins with any other character, DES encryption with a fixed number
80of perturbations is selected.
81.Ss DES encryption:
82The DES encryption scheme is derived from the
83.Tn NBS
84Data Encryption Standard.
85Additional code has been added to deter key search attempts and to use
86stronger hashing algorithms.  In the DES case, the first argument to
87.Fn crypt
88is a character array, 9 bytes in length, consisting of an underscore (``_'')
89followed by 4 bytes of iteration count and 4 bytes of salt.
90Both the iteration
91.Fa count
92and the
93.Fa salt
94are encoded with 6 bits per character, least significant bits first.
95The values 0 to 63 are encoded by the characters ``./0-9A-Za-z'',
96respectively.
97.Pp
98The
99.Fa salt
100is used to induce disorder in to the
101.Tn DES
102algorithm
103in one of 16777216
104possible ways
105(specifically, if bit
106.Em i
107of the
108.Ar salt
109is set then bits
110.Em i
111and
112.Em i+24
113are swapped in the
114.Tn DES
115``E'' box output).
116The
117.Ar key
118is divided into groups of 8 characters (a short final group is null-padded)
119and the low-order 7 bits of each character (56 bits per group) are
120used to form the DES key as follows: the first group of 56 bits becomes the
121initial DES key.
122For each additional group, the XOR of the group bits and the encryption of
123the DES key with itself becomes the next DES key.
124Then the final DES key is used to perform
125.Ar count
126cumulative encryptions of a 64-bit constant.
127The value returned is a
128.Dv NUL Ns -terminated
129string, 20 bytes in length, consisting
130of the
131.Ar setting
132followed by the encoded 64-bit encryption.
133.Pp
134For compatibility with historical versions of
135.Xr crypt 3 ,
136the
137.Ar setting
138may consist of 2 bytes of salt, encoded as above, in which case an
139iteration
140.Ar count
141of 25 is used, fewer perturbations of
142.Tn DES
143are available, at most 8
144characters of
145.Ar key
146are used, and the returned value is a
147.Dv NUL Ns -terminated
148string 13 bytes in length.
149.Pp
150The
151functions
152.Fn encrypt ,
153.Fn setkey ,
154.Fn des_setkey
155and
156.Fn des_cipher
157allow limited access to the
158.Tn DES
159algorithm itself.
160The
161.Ar key
162argument to
163.Fn setkey
164is a 64 character array of
165binary values (numeric 0 or 1).
166A 56-bit key is derived from this array by dividing the array
167into groups of 8 and ignoring the last bit in each group.
168.Pp
169The
170.Fn encrypt
171argument
172.Fa block
173is also a 64 character array of
174binary values.
175If the value of
176.Fa flag
177is 0,
178the argument
179.Fa block
180is encrypted, otherwise it
181is decrypted.
182The encryption or decryption is returned in the original
183array
184.Fa block
185after using the
186key specified
187by
188.Fn setkey
189to process it.
190.Pp
191The
192.Fn des_setkey
193and
194.Fn des_cipher
195functions are faster but less portable than
196.Fn setkey
197and
198.Fn encrypt .
199The argument to
200.Fn des_setkey
201is a character array of length 8.
202The
203.Em least
204significant bit in each character is ignored and the next 7 bits of each
205character are concatenated to yield a 56-bit key.
206The function
207.Fn des_cipher
208encrypts (or decrypts if
209.Fa count
210is negative) the 64-bits stored in the 8 characters at
211.Fa in
212using
213.Xr abs 3
214of
215.Fa count
216iterations of
217.Tn DES
218and stores the 64-bit result in the 8 characters at
219.Fa out .
220The
221.Fa salt
222specifies perturbations to
223.Tn DES
224as described above.
225.Ss MD5 encryption:
226For the
227.Tn MD5
228encryption scheme, the version number (in this case ``1''),
229.Fa salt
230and the hashed password are separated
231by the ``$'' character.  A valid password looks like this:
232.Pp
233``$1$2qGr5PPQ$eT08WBFev3RPLNChixg0H.''.
234.Pp
235The entire password string is passed as
236.Fa setting
237for interpretation.
238.Sh RETURN VALUES
239The function
240.Fn crypt
241returns a pointer to the encrypted value on success and NULL on failure.
242The functions
243.Fn setkey ,
244.Fn encrypt ,
245.Fn des_setkey ,
246and
247.Fn des_cipher
248return 0 on success and 1 on failure.
249Historically, the functions
250.Fn setkey
251and
252.Fn encrypt
253did not return any value.
254They have been provided return values primarily to distinguish
255implementations where hardware support is provided but not
256available or where the DES encryption is not available due to the
257usual political silliness.
258.Sh SEE ALSO
259.Xr login 1 ,
260.Xr passwd 1 ,
261.Xr getpass 3 ,
262.Xr md5 3 ,
263.Xr passwd 5 ,
264.Xr passwd.conf 5
265.sp
266.Rs
267.%T "Mathematical Cryptology for Computer Scientists and Mathematicians"
268.%A Wayne Patterson
269.%D 1987
270.%N ISBN 0-8476-7438-X
271.Re
272.Rs
273.%T "Password Security: A Case History"
274.%A R. Morris
275.%A Ken Thompson
276.%J "Communications of the ACM"
277.%V vol. 22
278.%P pp. 594-597
279.%D Nov. 1979
280.Re
281.Rs
282.%T "DES will be Totally Insecure within Ten Years"
283.%A M.E. Hellman
284.%J "IEEE Spectrum"
285.%V vol. 16
286.%P pp. 32-39
287.%D July 1979
288.Re
289.Sh HISTORY
290A rotor-based
291.Fn crypt
292function appeared in
293.At v6 .
294The current style
295.Fn crypt
296first appeared in
297.At v7 .
298.Sh BUGS
299Dropping the
300.Em least
301significant bit in each character of the argument to
302.Fn des_setkey
303is ridiculous.
304.Pp
305The
306.Fn crypt
307function leaves its result in an internal static object and returns
308a pointer to that object.
309Subsequent calls to
310.Fn crypt
311will modify the same object.
312