1*f5435c74SLionel Sambuc.\" $NetBSD: crypt.3,v 1.27 2012/03/23 18:08:35 njoly Exp $ 2ebffaa42SBen Gras.\" 3ebffaa42SBen Gras.\" Copyright (c) 1989, 1991, 1993 4ebffaa42SBen Gras.\" The Regents of the University of California. All rights reserved. 5ebffaa42SBen Gras.\" 6ebffaa42SBen Gras.\" Redistribution and use in source and binary forms, with or without 7ebffaa42SBen Gras.\" modification, are permitted provided that the following conditions 8ebffaa42SBen Gras.\" are met: 9ebffaa42SBen Gras.\" 1. Redistributions of source code must retain the above copyright 10ebffaa42SBen Gras.\" notice, this list of conditions and the following disclaimer. 11ebffaa42SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 12ebffaa42SBen Gras.\" notice, this list of conditions and the following disclaimer in the 13ebffaa42SBen Gras.\" documentation and/or other materials provided with the distribution. 14ebffaa42SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 15ebffaa42SBen Gras.\" may be used to endorse or promote products derived from this software 16ebffaa42SBen Gras.\" without specific prior written permission. 17ebffaa42SBen Gras.\" 18ebffaa42SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19ebffaa42SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20ebffaa42SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21ebffaa42SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22ebffaa42SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23ebffaa42SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24ebffaa42SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25ebffaa42SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26ebffaa42SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27ebffaa42SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28ebffaa42SBen Gras.\" SUCH DAMAGE. 29ebffaa42SBen Gras.\" 30ebffaa42SBen Gras.\" @(#)crypt.3 8.2 (Berkeley) 12/11/93 31ebffaa42SBen Gras.\" 32*f5435c74SLionel Sambuc.Dd January 1, 2012 33ebffaa42SBen Gras.Dt CRYPT 3 34ebffaa42SBen Gras.Os 35ebffaa42SBen Gras.Sh NAME 36ebffaa42SBen Gras.Nm crypt , 37ebffaa42SBen Gras.Nm setkey , 38ebffaa42SBen Gras.Nm encrypt , 39ebffaa42SBen Gras.Nm des_setkey , 40ebffaa42SBen Gras.Nm des_cipher 41ebffaa42SBen Gras.Nd password encryption 42ebffaa42SBen Gras.Sh LIBRARY 43ebffaa42SBen Gras.Lb libcrypt 44ebffaa42SBen Gras.Sh SYNOPSIS 45ebffaa42SBen Gras.In unistd.h 46*f5435c74SLionel Sambuc.Ft "char *" 47*f5435c74SLionel Sambuc.Fn crypt "const char *key" "const char *setting" 48ebffaa42SBen Gras.Ft int 49ebffaa42SBen Gras.Fn encrypt "char *block" "int flag" 50ebffaa42SBen Gras.Ft int 51ebffaa42SBen Gras.Fn des_setkey "const char *key" 52ebffaa42SBen Gras.Ft int 53ebffaa42SBen Gras.Fn des_cipher "const char *in" "char *out" "long salt" "int count" 54ebffaa42SBen Gras.In stdlib.h 55ebffaa42SBen Gras.Ft int 56ebffaa42SBen Gras.Fn setkey "const char *key" 57ebffaa42SBen Gras.Sh DESCRIPTION 58ebffaa42SBen GrasThe 59ebffaa42SBen Gras.Fn crypt 60ebffaa42SBen Grasfunction 61ebffaa42SBen Grasperforms password encryption. 62ebffaa42SBen GrasThe encryption scheme used by 63ebffaa42SBen Gras.Fn crypt 64ebffaa42SBen Grasis dependent upon the contents of the 65ebffaa42SBen Gras.Dv NUL Ns -terminated 66ebffaa42SBen Grasstring 67ebffaa42SBen Gras.Ar setting . 68ebffaa42SBen GrasIf it begins 69ebffaa42SBen Graswith a string character 70ebffaa42SBen Gras.Pq Ql $ 71ebffaa42SBen Grasand a number then a different algorithm is used depending on the number. 72ebffaa42SBen GrasAt the moment a 73ebffaa42SBen Gras.Ql $1 74ebffaa42SBen Graschooses MD5 hashing and a 75ebffaa42SBen Gras.Ql $2 76ebffaa42SBen Graschooses Blowfish hashing; see below for more information. 77ebffaa42SBen GrasIf 78ebffaa42SBen Gras.Ar setting 79ebffaa42SBen Grasbegins with the ``_'' character, DES encryption with a user specified number 80ebffaa42SBen Grasof perturbations is selected. 81ebffaa42SBen GrasIf 82ebffaa42SBen Gras.Ar setting 83ebffaa42SBen Grasbegins with any other character, DES encryption with a fixed number 84ebffaa42SBen Grasof perturbations is selected. 85ebffaa42SBen Gras.Ss DES encryption 86ebffaa42SBen GrasThe DES encryption scheme is derived from the 87ebffaa42SBen Gras.Tn NBS 88ebffaa42SBen GrasData Encryption Standard. 89ebffaa42SBen GrasAdditional code has been added to deter key search attempts and to use 90ebffaa42SBen Grasstronger hashing algorithms. 91ebffaa42SBen GrasIn the DES case, the second argument to 92ebffaa42SBen Gras.Fn crypt 93ebffaa42SBen Grasis a character array, 9 bytes in length, consisting of an underscore (``_'') 94ebffaa42SBen Grasfollowed by 4 bytes of iteration count and 4 bytes of salt. 95ebffaa42SBen GrasBoth the iteration 96ebffaa42SBen Gras.Fa count 97ebffaa42SBen Grasand the 98ebffaa42SBen Gras.Fa salt 99ebffaa42SBen Grasare encoded with 6 bits per character, least significant bits first. 100ebffaa42SBen GrasThe values 0 to 63 are encoded by the characters ``./0-9A-Za-z'', 101ebffaa42SBen Grasrespectively. 102ebffaa42SBen Gras.Pp 103ebffaa42SBen GrasThe 104ebffaa42SBen Gras.Fa salt 105ebffaa42SBen Grasis used to induce disorder in to the 106ebffaa42SBen Gras.Tn DES 107ebffaa42SBen Grasalgorithm 108ebffaa42SBen Grasin one of 16777216 109ebffaa42SBen Graspossible ways 110ebffaa42SBen Gras(specifically, if bit 111ebffaa42SBen Gras.Em i 112ebffaa42SBen Grasof the 113ebffaa42SBen Gras.Ar salt 114ebffaa42SBen Grasis set then bits 115ebffaa42SBen Gras.Em i 116ebffaa42SBen Grasand 117ebffaa42SBen Gras.Em i+24 118ebffaa42SBen Grasare swapped in the 119ebffaa42SBen Gras.Tn DES 120ebffaa42SBen Gras``E'' box output). 121ebffaa42SBen GrasThe 122ebffaa42SBen Gras.Ar key 123ebffaa42SBen Grasis divided into groups of 8 characters (a short final group is null-padded) 124ebffaa42SBen Grasand the low-order 7 bits of each character (56 bits per group) are 125ebffaa42SBen Grasused to form the DES key as follows: the first group of 56 bits becomes the 126ebffaa42SBen Grasinitial DES key. 127ebffaa42SBen GrasFor each additional group, the XOR of the group bits and the encryption of 128ebffaa42SBen Grasthe DES key with itself becomes the next DES key. 129ebffaa42SBen GrasThen the final DES key is used to perform 130ebffaa42SBen Gras.Ar count 131ebffaa42SBen Grascumulative encryptions of a 64-bit constant. 132ebffaa42SBen GrasThe value returned is a 133ebffaa42SBen Gras.Dv NUL Ns -terminated 134ebffaa42SBen Grasstring, 20 bytes in length, consisting 135ebffaa42SBen Grasof the 136ebffaa42SBen Gras.Ar setting 137ebffaa42SBen Grasfollowed by the encoded 64-bit encryption. 138ebffaa42SBen Gras.Pp 139ebffaa42SBen GrasFor compatibility with historical versions of 140*f5435c74SLionel Sambuc.Fn crypt , 141ebffaa42SBen Grasthe 142ebffaa42SBen Gras.Ar setting 143ebffaa42SBen Grasmay consist of 2 bytes of salt, encoded as above, in which case an 144ebffaa42SBen Grasiteration 145ebffaa42SBen Gras.Ar count 146ebffaa42SBen Grasof 25 is used, fewer perturbations of 147ebffaa42SBen Gras.Tn DES 148ebffaa42SBen Grasare available, at most 8 149ebffaa42SBen Grascharacters of 150ebffaa42SBen Gras.Ar key 151ebffaa42SBen Grasare used, and the returned value is a 152ebffaa42SBen Gras.Dv NUL Ns -terminated 153ebffaa42SBen Grasstring 13 bytes in length. 154ebffaa42SBen Gras.Pp 155ebffaa42SBen GrasThe 156ebffaa42SBen Grasfunctions 157ebffaa42SBen Gras.Fn encrypt , 158ebffaa42SBen Gras.Fn setkey , 159ebffaa42SBen Gras.Fn des_setkey 160ebffaa42SBen Grasand 161ebffaa42SBen Gras.Fn des_cipher 162ebffaa42SBen Grasallow limited access to the 163ebffaa42SBen Gras.Tn DES 164ebffaa42SBen Grasalgorithm itself. 165ebffaa42SBen GrasThe 166ebffaa42SBen Gras.Ar key 167ebffaa42SBen Grasargument to 168ebffaa42SBen Gras.Fn setkey 169ebffaa42SBen Grasis a 64 character array of 170ebffaa42SBen Grasbinary values (numeric 0 or 1). 171ebffaa42SBen GrasA 56-bit key is derived from this array by dividing the array 172ebffaa42SBen Grasinto groups of 8 and ignoring the last bit in each group. 173ebffaa42SBen Gras.Pp 174ebffaa42SBen GrasThe 175ebffaa42SBen Gras.Fn encrypt 176ebffaa42SBen Grasargument 177ebffaa42SBen Gras.Fa block 178ebffaa42SBen Grasis also a 64 character array of 179ebffaa42SBen Grasbinary values. 180ebffaa42SBen GrasIf the value of 181ebffaa42SBen Gras.Fa flag 182ebffaa42SBen Grasis 0, 183ebffaa42SBen Grasthe argument 184ebffaa42SBen Gras.Fa block 185ebffaa42SBen Grasis encrypted, otherwise it 186ebffaa42SBen Grasis decrypted. 187ebffaa42SBen GrasThe encryption or decryption is returned in the original 188ebffaa42SBen Grasarray 189ebffaa42SBen Gras.Fa block 190ebffaa42SBen Grasafter using the 191ebffaa42SBen Graskey specified 192ebffaa42SBen Grasby 193ebffaa42SBen Gras.Fn setkey 194ebffaa42SBen Grasto process it. 195ebffaa42SBen Gras.Pp 196ebffaa42SBen GrasThe 197ebffaa42SBen Gras.Fn des_setkey 198ebffaa42SBen Grasand 199ebffaa42SBen Gras.Fn des_cipher 200ebffaa42SBen Grasfunctions are faster but less portable than 201ebffaa42SBen Gras.Fn setkey 202ebffaa42SBen Grasand 203ebffaa42SBen Gras.Fn encrypt . 204ebffaa42SBen GrasThe argument to 205ebffaa42SBen Gras.Fn des_setkey 206ebffaa42SBen Grasis a character array of length 8. 207ebffaa42SBen GrasThe 208ebffaa42SBen Gras.Em least 209ebffaa42SBen Grassignificant bit in each character is ignored and the next 7 bits of each 210ebffaa42SBen Grascharacter are concatenated to yield a 56-bit key. 211ebffaa42SBen GrasThe function 212ebffaa42SBen Gras.Fn des_cipher 213ebffaa42SBen Grasencrypts (or decrypts if 214ebffaa42SBen Gras.Fa count 215ebffaa42SBen Grasis negative) the 64-bits stored in the 8 characters at 216ebffaa42SBen Gras.Fa in 217ebffaa42SBen Grasusing 218ebffaa42SBen Gras.Xr abs 3 219ebffaa42SBen Grasof 220ebffaa42SBen Gras.Fa count 221ebffaa42SBen Grasiterations of 222ebffaa42SBen Gras.Tn DES 223ebffaa42SBen Grasand stores the 64-bit result in the 8 characters at 224ebffaa42SBen Gras.Fa out . 225ebffaa42SBen GrasThe 226ebffaa42SBen Gras.Fa salt 227ebffaa42SBen Grasspecifies perturbations to 228ebffaa42SBen Gras.Tn DES 229ebffaa42SBen Grasas described above. 230ebffaa42SBen Gras.Ss MD5 encryption 231ebffaa42SBen GrasFor the 232ebffaa42SBen Gras.Tn MD5 233ebffaa42SBen Grasencryption scheme, the version number (in this case ``1''), 234ebffaa42SBen Gras.Fa salt 235ebffaa42SBen Grasand the hashed password are separated 236ebffaa42SBen Grasby the ``$'' character. 237ebffaa42SBen GrasA valid password looks like this: 238ebffaa42SBen Gras.Pp 239ebffaa42SBen Gras``$1$2qGr5PPQ$eT08WBFev3RPLNChixg0H.''. 240ebffaa42SBen Gras.Pp 241ebffaa42SBen GrasThe entire password string is passed as 242ebffaa42SBen Gras.Fa setting 243ebffaa42SBen Grasfor interpretation. 244ebffaa42SBen Gras.Ss "Blowfish" crypt 245ebffaa42SBen GrasThe 246ebffaa42SBen Gras.Tn Blowfish 247*f5435c74SLionel Sambucversion of 248*f5435c74SLionel Sambuc.Fn crypt 249*f5435c74SLionel Sambuchas 128 bits of 250ebffaa42SBen Gras.Fa salt 251ebffaa42SBen Grasin order to make building dictionaries of common passwords space consuming. 252ebffaa42SBen GrasThe initial state of the 253ebffaa42SBen Gras.Tn Blowfish 254ebffaa42SBen Grascipher is expanded using the 255ebffaa42SBen Gras.Fa salt 256ebffaa42SBen Grasand the 257ebffaa42SBen Gras.Fa password 258ebffaa42SBen Grasrepeating the process a variable number of rounds, which is encoded in 259ebffaa42SBen Grasthe password string. 260ebffaa42SBen GrasThe maximum password length is 72. 261ebffaa42SBen GrasThe final Blowfish password entry is created by encrypting the string 262ebffaa42SBen Gras.Pp 263ebffaa42SBen Gras.Dq OrpheanBeholderScryDoubt 264ebffaa42SBen Gras.Pp 265ebffaa42SBen Graswith the 266ebffaa42SBen Gras.Tn Blowfish 267ebffaa42SBen Grasstate 64 times. 268ebffaa42SBen Gras.Pp 269ebffaa42SBen GrasThe version number, the logarithm of the number of rounds and 270ebffaa42SBen Grasthe concatenation of salt and hashed password are separated by the 271ebffaa42SBen Gras.Ql $ 272ebffaa42SBen Grascharacter. 273ebffaa42SBen GrasAn encoded 274ebffaa42SBen Gras.Sq 8 275ebffaa42SBen Graswould specify 256 rounds. 276ebffaa42SBen GrasA valid Blowfish password looks like this: 277ebffaa42SBen Gras.Pp 278ebffaa42SBen Gras.Dq $2a$12$eIAq8PR8sIUnJ1HaohxX2O9x9Qlm2vK97LJ5dsXdmB.eXF42qjchC . 279ebffaa42SBen Gras.Pp 280ebffaa42SBen GrasThe whole Blowfish password string is passed as 281ebffaa42SBen Gras.Fa setting 282ebffaa42SBen Grasfor interpretation. 283ebffaa42SBen Gras.Sh RETURN VALUES 284ebffaa42SBen GrasThe function 285ebffaa42SBen Gras.Fn crypt 286*f5435c74SLionel Sambucreturns a pointer to the encrypted value on success. 287*f5435c74SLionel Sambuc.Pp 288*f5435c74SLionel SambucThe behavior of 289*f5435c74SLionel Sambuc.Fn crypt 290*f5435c74SLionel Sambucon errors isn't well standardized. 291*f5435c74SLionel SambucSome implementations simply can't fail (unless the process dies, in which 292*f5435c74SLionel Sambuccase they obviously can't return), others return 293*f5435c74SLionel Sambuc.Dv NULL 294*f5435c74SLionel Sambucor a fixed string. 295*f5435c74SLionel SambucMost implementations don't set 296*f5435c74SLionel Sambuc.Va errno , 297*f5435c74SLionel Sambucbut some do. 298*f5435c74SLionel Sambuc.St -susv2 299*f5435c74SLionel Sambucspecifies 300*f5435c74SLionel Sambuconly returning 301*f5435c74SLionel Sambuc.Dv NULL 302*f5435c74SLionel Sambucand setting 303*f5435c74SLionel Sambuc.Va errno 304*f5435c74SLionel Sambucas a valid behavior, and defines 305*f5435c74SLionel Sambuconly one possible error 306*f5435c74SLionel Sambuc.Er ( ENOSYS , 307*f5435c74SLionel Sambuc.Dq "The functionality is not supported on this implementation." ) 308*f5435c74SLionel SambucUnfortunately, most existing applications aren't prepared to handle 309*f5435c74SLionel Sambuc.Dv NULL 310*f5435c74SLionel Sambucreturns from 311*f5435c74SLionel Sambuc.Fn crypt . 312*f5435c74SLionel SambucThe description below corresponds to this implementation of 313*f5435c74SLionel Sambuc.Fn crypt 314*f5435c74SLionel Sambuconly. 315*f5435c74SLionel SambucThe behavior may change to match standards, other implementations or existing 316*f5435c74SLionel Sambucapplications. 317*f5435c74SLionel Sambuc.Pp 318*f5435c74SLionel Sambuc.Fn crypt 319*f5435c74SLionel Sambucmay only fail (and return) when passed an invalid or unsupported 320*f5435c74SLionel Sambuc.Fa setting , 321*f5435c74SLionel Sambucin which case it returns a pointer to a magic string that is shorter than 13 322*f5435c74SLionel Sambuccharacters and is guaranteed to differ from 323*f5435c74SLionel Sambuc.Fa setting . 324*f5435c74SLionel SambucThis behavior is safe for older applications which assume that 325*f5435c74SLionel Sambuc.Fn crypt 326*f5435c74SLionel Sambuccan't fail, when both setting new passwords and authenticating against 327*f5435c74SLionel Sambucexisting password hashes. 328*f5435c74SLionel Sambuc.Pp 329ebffaa42SBen GrasThe functions 330ebffaa42SBen Gras.Fn setkey , 331ebffaa42SBen Gras.Fn encrypt , 332ebffaa42SBen Gras.Fn des_setkey , 333ebffaa42SBen Grasand 334ebffaa42SBen Gras.Fn des_cipher 335ebffaa42SBen Grasreturn 0 on success and 1 on failure. 336ebffaa42SBen GrasHistorically, the functions 337ebffaa42SBen Gras.Fn setkey 338ebffaa42SBen Grasand 339ebffaa42SBen Gras.Fn encrypt 340ebffaa42SBen Grasdid not return any value. 341ebffaa42SBen GrasThey have been provided return values primarily to distinguish 342ebffaa42SBen Grasimplementations where hardware support is provided but not 343ebffaa42SBen Grasavailable or where the DES encryption is not available due to the 344ebffaa42SBen Grasusual political silliness. 345ebffaa42SBen Gras.Sh SEE ALSO 346ebffaa42SBen Gras.Xr login 1 , 347ebffaa42SBen Gras.Xr passwd 1 , 348ebffaa42SBen Gras.Xr pwhash 1 , 349ebffaa42SBen Gras.Xr getpass 3 , 350ebffaa42SBen Gras.Xr md5 3 , 351ebffaa42SBen Gras.Xr passwd 5 , 352ebffaa42SBen Gras.Xr passwd.conf 5 353ebffaa42SBen Gras.Rs 354ebffaa42SBen Gras.%T "Mathematical Cryptology for Computer Scientists and Mathematicians" 355ebffaa42SBen Gras.%A Wayne Patterson 356ebffaa42SBen Gras.%D 1987 357ebffaa42SBen Gras.%N ISBN 0-8476-7438-X 358ebffaa42SBen Gras.Re 359ebffaa42SBen Gras.Rs 360ebffaa42SBen Gras.%T "Password Security: A Case History" 361ebffaa42SBen Gras.%A R. Morris 362ebffaa42SBen Gras.%A Ken Thompson 363ebffaa42SBen Gras.%J "Communications of the ACM" 364ebffaa42SBen Gras.%V vol. 22 365ebffaa42SBen Gras.%P pp. 594-597 366ebffaa42SBen Gras.%D Nov. 1979 367ebffaa42SBen Gras.Re 368ebffaa42SBen Gras.Rs 369ebffaa42SBen Gras.%T "DES will be Totally Insecure within Ten Years" 370ebffaa42SBen Gras.%A M.E. Hellman 371ebffaa42SBen Gras.%J "IEEE Spectrum" 372ebffaa42SBen Gras.%V vol. 16 373ebffaa42SBen Gras.%P pp. 32-39 374ebffaa42SBen Gras.%D July 1979 375ebffaa42SBen Gras.Re 376ebffaa42SBen Gras.Sh HISTORY 377ebffaa42SBen GrasA rotor-based 378ebffaa42SBen Gras.Fn crypt 379ebffaa42SBen Grasfunction appeared in 380ebffaa42SBen Gras.At v6 . 381ebffaa42SBen GrasThe current style 382ebffaa42SBen Gras.Fn crypt 383ebffaa42SBen Grasfirst appeared in 384ebffaa42SBen Gras.At v7 . 385ebffaa42SBen Gras.Sh BUGS 386ebffaa42SBen GrasDropping the 387ebffaa42SBen Gras.Em least 388ebffaa42SBen Grassignificant bit in each character of the argument to 389ebffaa42SBen Gras.Fn des_setkey 390ebffaa42SBen Grasis ridiculous. 391ebffaa42SBen Gras.Pp 392ebffaa42SBen GrasThe 393ebffaa42SBen Gras.Fn crypt 394ebffaa42SBen Grasfunction leaves its result in an internal static object and returns 395ebffaa42SBen Grasa pointer to that object. 396ebffaa42SBen GrasSubsequent calls to 397ebffaa42SBen Gras.Fn crypt 398ebffaa42SBen Graswill modify the same object. 399*f5435c74SLionel Sambuc.Pp 400*f5435c74SLionel SambucBefore 401*f5435c74SLionel Sambuc.Nx 6.0 402*f5435c74SLionel Sambuc.Fn crypt 403*f5435c74SLionel Sambucreturned either 404*f5435c74SLionel Sambuc.Dv NULL 405*f5435c74SLionel Sambucor 406*f5435c74SLionel Sambuc.Dv \&: 407*f5435c74SLionel Sambucon error. 408