148352Scael.\" Copyright (c) 1989, 1991 The Regents of the University of California. 247038Sbostic.\" All rights reserved. 320293Smckusick.\" 448352Scael.\" %sccs.include.redist.man% 547038Sbostic.\" 6*49832Sbostic.\" @(#)crypt.3 6.7 (Berkeley) 05/21/91 747038Sbostic.\" 848352Scael.Dd 948352Scael.Dt CRYPT 3 1048352Scael.Os 1148352Scael.Sh NAME 1248352Scael.Nm crypt , 1348352Scael.Nm setkey , 1448352Scael.Nm encrypt , 1548352Scael.Nm des_setkey , 1648352Scael.Nm des_cipher 1748352Scael.Nd DES encryption 1848352Scael.Sh SYNOPSIS 1948352Scael.Ft char 2048352Scael.Fn *crypt "const char *key" "const char *setting" 2149803Sbostic.Ft int 2248352Scael.Fn setkey "char *key" 2349803Sbostic.Ft int 2448352Scael.Fn encrypt "char *block" "int flag" 2549803Sbostic.Ft int 2648352Scael.Fn des_setkey "const char *key" 2749803Sbostic.Ft int 2848486Sbostic.Fn des_cipher "const char *in" "char *out" "long salt" "int count" 2948352Scael.Sh DESCRIPTION 3048352ScaelThe 3148352Scael.Xr crypt 3248352Scaelfunction 3348352Scaelperforms password encryption. 3448352ScaelIt is derived from the 3548352Scael.Tn NBS 3648352ScaelData Encryption Standard. 3748352ScaelAdditional code has been added to deter 3848352Scaelkey search attempts. 3948352ScaelThe first argument to 4048352Scael.Nm crypt 4148352Scaelis 4248352Scaela 4348352Scael.Dv NUL Ns -terminated 4448486Sbosticstring (normally a password typed by a user). 4547038SbosticThe second is a character array, 9 bytes in length, consisting of an 4647038Sbosticunderscore (``_'') followed by 4 bytes of iteration count and 4 bytes 4747038Sbosticof salt. 4847038SbosticBoth the iteration 4948352Scael.Fa count 5047038Sbosticand the 5148352Scael.Fa salt 52*49832Sbosticare encoded with 6 bits per character, least significant bits first. 5347038SbosticThe values 0 to 63 are encoded by the characters ``./0-9A-Za-z'', 5447038Sbosticrespectively. 55*49832Sbostic.Pp 5620293SmckusickThe 5748352Scael.Fa salt 5848352Scaelis used to induce disorder in to the 5948352Scael.Tn DES 6048352Scaelalgorithm 6148352Scaelin one of 16777216 6248352Scaelpossible ways 6347038Sbostic(specifically, if bit 64*49832Sbostic.Em i 6547038Sbosticof the 6648352Scael.Ar salt 6747038Sbosticis set then bits 68*49832Sbostic.Em i 6947038Sbosticand 70*49832Sbostic.Em i+24 7148352Scaelare swapped in the 7248352Scael.Tn DES 7348352Scael``E'' box output). 74*49832SbosticThe 7548352Scael.Ar key 76*49832Sbosticis divided into groups of 8 characters (a short final group is null-padded) 77*49832Sbosticand the low-order 7 bits of each each character (56 bits per group) are 78*49832Sbosticused to form the DES key as follows: the first group of 56 bits becomes the 79*49832Sbosticinitial DES key. 80*49832SbosticFor each additional group, the XOR of the group bits and the encryption of 81*49832Sbosticthe DES key with itself becomes the next DES key. 82*49832SbosticThen the final DES key is used to perform 8348352Scael.Ar count 8447038Sbosticcumulative encryptions of a 64-bit constant. 85*49832SbosticThe value returned is a 86*49832Sbostic.Dv NUL Ns -terminated 87*49832Sbosticstring, 20 bytes in length, consisting 8847038Sbosticof the 8948352Scael.Ar setting 9047038Sbosticfollowed by the encoded 64-bit encryption. 9148352Scael.Pp 9247038SbosticFor compatibility with historical versions of 9348352Scael.Xr crypt 3 , 9447038Sbosticthe 9548352Scael.Ar setting 9647038Sbosticmay consist of 2 bytes of salt, encoded as above, in which case an 9747038Sbosticiteration 9848352Scael.Ar count 9948352Scaelof 25 is used, fewer perturbations of 10048352Scael.Tn DES 10148352Scaelare available, at most 8 10247038Sbosticcharacters of 10348352Scael.Ar key 104*49832Sbosticare used, and the returned value is a 105*49832Sbostic.Dv NUL Ns -terminated 106*49832Sbosticstring 13 bytes in length. 10748352Scael.Pp 10848352ScaelThe 10948352Scaelfunctions, 11048352Scael.Fn encrypt , 11148352Scael.Fn setkey , 11248352Scael.Fn des_setkey 11348352Scaeland 11448352Scael.Fn des_cipher 11548352Scaelallow limited access to the 11648352Scael.Tn DES 11748352Scaelalgorithm itself. 11848352ScaelThe 11948352Scael.Ar key 12048352Scaelargument to 12148352Scael.Fn setkey 12248352Scaelis a 64 character array of 123*49832Sbosticbinary values (numeric 0 or 1). 124*49832SbosticA 56-bit key is derived from this array by dividing the array 12547038Sbosticinto groups of 8 and ignoring the last bit in each group. 12648352Scael.Pp 12748352ScaelThe 12848352Scael.Fn encrypt 12948352Scaelargument 13048352Scael.Fa block 13148352Scaelis also a 64 character array of 13248352Scaelbinary values. 13348352ScaelIf the value of 13448352Scael.Fa flag 13548352Scaelis 0, 13648352Scaelthe argument 13748352Scael.Fa block 13848352Scaelis encrypted, otherwise it 13948352Scaelis decrypted. 14048352ScaelThe encryption or decryption is returned in the original 14148352Scaelarray 14248352Scael.Fa block 14348352Scaelafter using the 14448352Scaelkey specified 14548352Scaelby 14648352Scael.Fn setkey 14748352Scaelto process it. 14848352Scael.Pp 14948352ScaelThe 15048352Scael.Fn des_setkey 15147038Sbosticand 15248352Scael.Fn des_cipher 15348352Scaelfunctions are faster but less portable than 15448352Scael.Fn setkey 15547038Sbosticand 15648352Scael.Fn encrypt . 15747038SbosticThe argument to 15848352Scael.Fn des_setkey 15947038Sbosticis a character array of length 8. 16029774SmckusickThe 16148352Scael.Em least 16247038Sbosticsignificant bit in each character is ignored and the next 7 bits of each 16347038Sbosticcharacter are concatenated to yield a 56-bit key. 16448352ScaelThe function 16548352Scael.Fn des_cipher 16647038Sbosticencrypts (or decrypts if 16748352Scael.Fa count 16847038Sbosticis negative) the 64-bits stored in the 8 characters at 16948352Scael.Fa in 17047038Sbosticusing 17148352Scael.Xr abs 3 17248352Scaelof 17348352Scael.Fa count 17448352Scaeliterations of 17548352Scael.Tn DES 17648352Scaeland stores the 64-bit result in the 8 characters at 17748352Scael.Fa out . 17847038SbosticThe 17948352Scael.Fa salt 18048352Scaelspecifies perturbations to 18148352Scael.Tn DES 18248352Scaelas described above. 18349803Sbostic.Pp 18449803SbosticThe function 18549803Sbostic.Fn crypt 18649803Sbosticreturns a pointer to the encrypted value on success and NULL on failure. 18749803SbosticThe functions 18849803Sbostic.Fn setkey , 18949803Sbostic.Fn encrypt , 19049803Sbostic.Fn des_setkey , 19149803Sbosticand 19249803Sbostic.Fn des_cipher 19349803Sbosticreturn 0 on success and 1 on failure. 19449803SbosticHistorically, the functions 19549803Sbostic.Fn setkey 19649803Sbosticand 19749803Sbostic.Fn encrypt 19849803Sbosticdid not return any value. 19949803SbosticThey have been provided return values primarily to distinguish 20049803Sbosticimplementations where hardware support is provided but not 20149803Sbosticavailable or where the DES encryption is not available due to the 20249803Sbosticusual political silliness. 20348352Scael.Sh SEE ALSO 20448352Scael.Xr login 1 , 20548352Scael.Xr passwd 1 , 20648352Scael.Xr getpass 3 , 20748352Scael.Xr passwd 5 20848486Sbostic.sp 20948352Scael.Rs 21048352Scael.%T "Mathematical Cryptology for Computer Scientists and Mathematicians" 21148352Scael.%A Wayne Patterson 21248352Scael.%D 1987 21348352Scael.%N ISBN 0-8476-7438-X 21448352Scael.Re 21548352Scael.Rs 21648352Scael.%T "Password Security: A Case History" 21748352Scael.%A R. Morris 21848352Scael.%A Ken Thompson 21948352Scael.%J "Communications of the ACM" 22048352Scael.%V vol. 22 22148352Scael.%P pp. 594-597 22248352Scael.%D Nov. 1979 22348352Scael.Re 22448352Scael.Rs 22548352Scael.%T "DES will be Totally Insecure within Ten Years" 22648352Scael.%A M.E. Hellman 22748352Scael.%J "IEEE Spectrum" 22848352Scael.%V vol. 16 22948352Scael.%P pp. 32-39 23048352Scael.%D July 1979 23148352Scael.Re 23248352Scael.Sh HISTORY 23349224SbosticA rotor-based 23448352Scael.Fn crypt 23548352Scaelfunction appeared in 23648352Scael.At v6 . 23749224SbosticThe current style 23849224Sbostic.Fn crypt 23949224Sbosticfirst appeared in 24049224Sbostic.At v7 . 24148352Scael.Sh BUGS 24247038SbosticDropping the 24348352Scael.Em least 24447038Sbosticsignificant bit in each character of the argument to 24548352Scael.Fn des_setkey 24647038Sbosticis ridiculous. 24748352Scael.Pp 24848352ScaelThe 24948352Scael.Fn crypt 25048352Scaelfunction leaves its result in an internal static object and returns 25149803Sbostica pointer to that object. 25249803SbosticSubsequent calls to 25348352Scael.Fn crypt 25448352Scaelwill modify the same object. 255