1.\" $NetBSD: crypt.3,v 1.9 2000/08/03 08:30:43 ad 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: 82.Pp 83The DES encryption scheme is derived from the 84.Tn NBS 85Data Encryption Standard. 86Additional code has been added to deter key search attempts and to use 87stronger hashing algorithms. In the DES case, the first argument to 88.Fn crypt 89is a character array, 9 bytes in length, consisting of an underscore (``_'') 90followed by 4 bytes of iteration count and 4 bytes of salt. 91Both the iteration 92.Fa count 93and the 94.Fa salt 95are encoded with 6 bits per character, least significant bits first. 96The values 0 to 63 are encoded by the characters ``./0-9A-Za-z'', 97respectively. 98.Pp 99The 100.Fa salt 101is used to induce disorder in to the 102.Tn DES 103algorithm 104in one of 16777216 105possible ways 106(specifically, if bit 107.Em i 108of the 109.Ar salt 110is set then bits 111.Em i 112and 113.Em i+24 114are swapped in the 115.Tn DES 116``E'' box output). 117The 118.Ar key 119is divided into groups of 8 characters (a short final group is null-padded) 120and the low-order 7 bits of each character (56 bits per group) are 121used to form the DES key as follows: the first group of 56 bits becomes the 122initial DES key. 123For each additional group, the XOR of the group bits and the encryption of 124the DES key with itself becomes the next DES key. 125Then the final DES key is used to perform 126.Ar count 127cumulative encryptions of a 64-bit constant. 128The value returned is a 129.Dv NUL Ns -terminated 130string, 20 bytes in length, consisting 131of the 132.Ar setting 133followed by the encoded 64-bit encryption. 134.Pp 135For compatibility with historical versions of 136.Xr crypt 3 , 137the 138.Ar setting 139may consist of 2 bytes of salt, encoded as above, in which case an 140iteration 141.Ar count 142of 25 is used, fewer perturbations of 143.Tn DES 144are available, at most 8 145characters of 146.Ar key 147are used, and the returned value is a 148.Dv NUL Ns -terminated 149string 13 bytes in length. 150.Pp 151The 152functions 153.Fn encrypt , 154.Fn setkey , 155.Fn des_setkey 156and 157.Fn des_cipher 158allow limited access to the 159.Tn DES 160algorithm itself. 161The 162.Ar key 163argument to 164.Fn setkey 165is a 64 character array of 166binary values (numeric 0 or 1). 167A 56-bit key is derived from this array by dividing the array 168into groups of 8 and ignoring the last bit in each group. 169.Pp 170The 171.Fn encrypt 172argument 173.Fa block 174is also a 64 character array of 175binary values. 176If the value of 177.Fa flag 178is 0, 179the argument 180.Fa block 181is encrypted, otherwise it 182is decrypted. 183The encryption or decryption is returned in the original 184array 185.Fa block 186after using the 187key specified 188by 189.Fn setkey 190to process it. 191.Pp 192The 193.Fn des_setkey 194and 195.Fn des_cipher 196functions are faster but less portable than 197.Fn setkey 198and 199.Fn encrypt . 200The argument to 201.Fn des_setkey 202is a character array of length 8. 203The 204.Em least 205significant bit in each character is ignored and the next 7 bits of each 206character are concatenated to yield a 56-bit key. 207The function 208.Fn des_cipher 209encrypts (or decrypts if 210.Fa count 211is negative) the 64-bits stored in the 8 characters at 212.Fa in 213using 214.Xr abs 3 215of 216.Fa count 217iterations of 218.Tn DES 219and stores the 64-bit result in the 8 characters at 220.Fa out . 221The 222.Fa salt 223specifies perturbations to 224.Tn DES 225as described above. 226.Ss "MD5" encryption: 227.Pp 228For the 229.Tn MD5 230encryption scheme, the version number (in this case ``1''), 231.Fa salt 232and the hashed password are separated 233by the ``$'' character. A valid password looks like this: 234.Pp 235``$1$2qGr5PPQ$eT08WBFev3RPLNChixg0H.''. 236.Pp 237The entire password string is passed as 238.Fa setting 239for interpretation. 240.Sh RETURN VALUES 241The function 242.Fn crypt 243returns a pointer to the encrypted value on success and NULL on failure. 244The functions 245.Fn setkey , 246.Fn encrypt , 247.Fn des_setkey , 248and 249.Fn des_cipher 250return 0 on success and 1 on failure. 251Historically, the functions 252.Fn setkey 253and 254.Fn encrypt 255did not return any value. 256They have been provided return values primarily to distinguish 257implementations where hardware support is provided but not 258available or where the DES encryption is not available due to the 259usual political silliness. 260.Sh SEE ALSO 261.Xr login 1 , 262.Xr passwd 1 , 263.Xr getpass 3 , 264.Xr md5 3 , 265.Xr passwd 5 , 266.Xr passwd.conf 5 267.sp 268.Rs 269.%T "Mathematical Cryptology for Computer Scientists and Mathematicians" 270.%A Wayne Patterson 271.%D 1987 272.%N ISBN 0-8476-7438-X 273.Re 274.Rs 275.%T "Password Security: A Case History" 276.%A R. Morris 277.%A Ken Thompson 278.%J "Communications of the ACM" 279.%V vol. 22 280.%P pp. 594-597 281.%D Nov. 1979 282.Re 283.Rs 284.%T "DES will be Totally Insecure within Ten Years" 285.%A M.E. Hellman 286.%J "IEEE Spectrum" 287.%V vol. 16 288.%P pp. 32-39 289.%D July 1979 290.Re 291.Sh HISTORY 292A rotor-based 293.Fn crypt 294function appeared in 295.At v6 . 296The current style 297.Fn crypt 298first appeared in 299.At v7 . 300.Sh BUGS 301Dropping the 302.Em least 303significant bit in each character of the argument to 304.Fn des_setkey 305is ridiculous. 306.Pp 307The 308.Fn crypt 309function leaves its result in an internal static object and returns 310a pointer to that object. 311Subsequent calls to 312.Fn crypt 313will modify the same object. 314