1.\" Copyright (c) 1989, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" %sccs.include.redist.man% 5.\" 6.\" @(#)crypt.3 6.3 (Berkeley) 04/19/91 7.\" 8.Dd 9.Dt CRYPT 3 10.Os 11.Sh NAME 12.Nm crypt , 13.Nm setkey , 14.Nm encrypt , 15.Nm des_setkey , 16.Nm des_cipher 17.Nd DES encryption 18.Sh SYNOPSIS 19.Ft char 20.Fn *crypt "const char *key" "const char *setting" 21.Ft void 22.Fn setkey "char *key" 23.Ft void 24.Fn encrypt "char *block" "int flag" 25.Ft void 26.Fn des_setkey "const char *key" 27.Ft void 28.Fn des_cipher "const char *in" "char *out" "u_long salt" "int count" 29.Sh DESCRIPTION 30The 31.Xr crypt 32function 33performs password encryption. 34It is derived from the 35.Tn NBS 36Data Encryption Standard. 37Additional code has been added to deter 38key search attempts. 39The first argument to 40.Nm crypt 41is 42a 43.Dv NUL Ns -terminated 44string (normally a password typed by 45a user) 46first argument is a 47.Dv NUL Ns -terminated 48(normally a user's typed 49The second is a character array, 9 bytes in length, consisting of an 50underscore (``_'') followed by 4 bytes of iteration count and 4 bytes 51of salt. 52Both the iteration 53.Fa count 54and the 55.Fa salt 56are encoded as follows: only 6 bits per character are used, with the least 57significant bits occurring first. 58The values 0 to 63 are encoded by the characters ``./0-9A-Za-z'', 59respectively. 60The 61.Fa salt 62is used to induce disorder in to the 63.Tn DES 64algorithm 65in one of 16777216 66possible ways 67(specifically, if bit 68.Em N 69of the 70.Ar salt 71is set then bits 72.Em N 73and 74.Em N+24 75are swapped in the 76.Tn DES 77``E'' box output). 78Then the 79.Ar key 80is used to perform 81.Ar count 82cumulative encryptions of a 64-bit constant. 83The value returned is a character array, 20 bytes in length, consisting 84of the 85.Ar setting 86followed by the encoded 64-bit encryption. 87.Pp 88For compatibility with historical versions of 89.Xr crypt 3 , 90the 91.Ar setting 92may consist of 2 bytes of salt, encoded as above, in which case an 93iteration 94.Ar count 95of 25 is used, fewer perturbations of 96.Tn DES 97are available, at most 8 98characters of 99.Ar key 100are used, and the returned value is a character array 13 bytes in length. 101.Pp 102The 103functions, 104.Fn encrypt , 105.Fn setkey , 106.Fn des_setkey 107and 108.Fn des_cipher 109allow limited access to the 110.Tn DES 111algorithm itself. 112The 113.Ar key 114argument to 115.Fn setkey 116is a 64 character array of 117.Tn ASCII 118binary values (0 or 1). 119A 56-bit key is derived from dividing this array by dividing the string 120into groups of 8 and ignoring the last bit in each group. 121.Pp 122The 123.Fn encrypt 124argument 125.Fa block 126is also a 64 character array of 127.Tn ASCII 128binary values. 129If the value of 130.Fa flag 131is 0, 132the argument 133.Fa block 134is encrypted, otherwise it 135is decrypted. 136The encryption or decryption is returned in the original 137array 138.Fa block 139after using the 140key specified 141by 142.Fn setkey 143to process it. 144.Pp 145The 146.Fn des_setkey 147and 148.Fn des_cipher 149functions are faster but less portable than 150.Fn setkey 151and 152.Fn encrypt . 153The argument to 154.Fn des_setkey 155is a character array of length 8. 156The 157.Em least 158significant bit in each character is ignored and the next 7 bits of each 159character are concatenated to yield a 56-bit key. 160The function 161.Fn des_cipher 162encrypts (or decrypts if 163.Fa count 164is negative) the 64-bits stored in the 8 characters at 165.Fa in 166using 167.Xr abs 3 168of 169.Fa count 170iterations of 171.Tn DES 172and stores the 64-bit result in the 8 characters at 173.Fa out . 174The 175.Fa salt 176specifies perturbations to 177.Tn DES 178as described above. 179.Sh SEE ALSO 180.Xr login 1 , 181.Xr passwd 1 , 182.Xr getpass 3 , 183.Xr passwd 5 184.Rs 185.%T "Mathematical Cryptology for Computer Scientists and Mathematicians" 186.%A Wayne Patterson 187.%D 1987 188.%N ISBN 0-8476-7438-X 189.Re 190.Rs 191.%T "Password Security: A Case History" 192.%A R. Morris 193.%A Ken Thompson 194.%J "Communications of the ACM" 195.%V vol. 22 196.%P pp. 594-597 197.%D Nov. 1979 198.Re 199.Rs 200.%T "DES will be Totally Insecure within Ten Years" 201.%A M.E. Hellman 202.%J "IEEE Spectrum" 203.%V vol. 16 204.%P pp. 32-39 205.%D July 1979 206.Re 207.Sh HISTORY 208A 209.Fn crypt 210function appeared in 211.At v6 . 212.Sh BUGS 213Dropping the 214.Em least 215significant bit in each character of the argument to 216.Fn des_setkey 217is ridiculous. 218.Pp 219The 220.Fn crypt 221function leaves its result in an internal static object and returns 222a pointer to that object. Subsequent calls to 223.Fn crypt 224will modify the same object. 225