1e9a56ad5SMark Murray.\" FreeSec: libcrypt for NetBSD 2e9a56ad5SMark Murray.\" 3e9a56ad5SMark Murray.\" Copyright (c) 1994 David Burren 4e9a56ad5SMark Murray.\" All rights reserved. 5e9a56ad5SMark Murray.\" 6e9a56ad5SMark Murray.\" Redistribution and use in source and binary forms, with or without 7e9a56ad5SMark Murray.\" modification, are permitted provided that the following conditions 8e9a56ad5SMark Murray.\" are met: 9e9a56ad5SMark Murray.\" 1. Redistributions of source code must retain the above copyright 10e9a56ad5SMark Murray.\" notice, this list of conditions and the following disclaimer. 11e9a56ad5SMark Murray.\" 2. Redistributions in binary form must reproduce the above copyright 12e9a56ad5SMark Murray.\" notice, this list of conditions and the following disclaimer in the 13e9a56ad5SMark Murray.\" documentation and/or other materials provided with the distribution. 14e9a56ad5SMark Murray.\" 4. Neither the name of the author nor the names of other contributors 15e9a56ad5SMark Murray.\" may be used to endorse or promote products derived from this software 16e9a56ad5SMark Murray.\" without specific prior written permission. 17e9a56ad5SMark Murray.\" 18e9a56ad5SMark Murray.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19e9a56ad5SMark Murray.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20e9a56ad5SMark Murray.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21e9a56ad5SMark Murray.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22e9a56ad5SMark Murray.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23e9a56ad5SMark Murray.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24e9a56ad5SMark Murray.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25e9a56ad5SMark Murray.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26e9a56ad5SMark Murray.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27e9a56ad5SMark Murray.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28e9a56ad5SMark Murray.\" SUCH DAMAGE. 29e9a56ad5SMark Murray.\" 30e9a56ad5SMark Murray.\" $FreeBSD$ 31e9a56ad5SMark Murray.\" 32e9a56ad5SMark Murray.\" Manual page, using -mandoc macros 33e9a56ad5SMark Murray.\" 34e9a56ad5SMark Murray.Dd January 19, 1997 35e9a56ad5SMark Murray.Dt CRYPT 3 36e9a56ad5SMark Murray.Os "FreeSec 1.0" 37e9a56ad5SMark Murray.Sh NAME 38e9a56ad5SMark Murray.Nm crypt 39e9a56ad5SMark Murray.Nd Trapdoor encryption 40f45f23ddSAlexey Zelkin.Sh LIBRARY 41f45f23ddSAlexey Zelkin.Lb libcrypt 42e9a56ad5SMark Murray.Sh SYNOPSIS 43f92c744fSBruce Evans.Fd #include <unistd.h> 44d3f0d184SBruce Evans.Ft char * 45d3f0d184SBruce Evans.Fn crypt "const char *key" "const char *salt" 4604c9749fSBrian Feldman.Ft const char * 4704c9749fSBrian Feldman.Fn crypt_get_format "void" 4804c9749fSBrian Feldman.Ft int 4904c9749fSBrian Feldman.Fn crypt_set_format "const char *string" 50e9a56ad5SMark Murray.Sh DESCRIPTION 51e9a56ad5SMark MurrayThe 52e9a56ad5SMark Murray.Fn crypt 53e9a56ad5SMark Murrayfunction performs password hashing with additional code added to 54e9a56ad5SMark Murraydeter key search attempts. Different algorithms can be used to 55e9a56ad5SMark Murrayin the hash. 56e9a56ad5SMark Murray.\" 57e9a56ad5SMark Murray.\" NOTICE: 58e9a56ad5SMark Murray.\" If you add more algorithms, make sure to update this list 59e9a56ad5SMark Murray.\" and the default used for the Traditional format, below. 60e9a56ad5SMark Murray.\" 61e9a56ad5SMark MurrayCurrently these include the 62e9a56ad5SMark Murray.Tn NBS 63f45f23ddSAlexey Zelkin.Tn Data Encryption Standard (DES) , 64f32b1300SKris Kennawayand 65f32b1300SKris Kennaway.Tn MD5 . 6604c9749fSBrian FeldmanThe algorithm used will depend upon the format of the Salt (following 6704c9749fSBrian Feldmanthe Modular Crypt Format (MCF)), if 68f45f23ddSAlexey Zelkin.Tn DES 6904c9749fSBrian Feldmanis installed or not, and whether 7004c9749fSBrian Feldman.Fn crypt_set_format 7104c9749fSBrian Feldmanhas been called to change the default. 72e9a56ad5SMark Murray.Pp 73e9a56ad5SMark MurrayThe first argument to 74f45f23ddSAlexey Zelkin.Nm 75e9a56ad5SMark Murrayis the data to hash (usually a password), in a 76e9a56ad5SMark Murray.Dv null Ns -terminated 77e9a56ad5SMark Murraystring. 78e9a56ad5SMark MurrayThe second is the salt, in one of three forms: 79e9a56ad5SMark Murray.Pp 80e9a56ad5SMark Murray.Bl -tag -width Traditional -compact -offset indent 81e9a56ad5SMark Murray.It Extended 82f45f23ddSAlexey ZelkinIf it begins with an underscore 83f45f23ddSAlexey Zelkin.Pq Dq _ 84f45f23ddSAlexey Zelkinthen the 85f45f23ddSAlexey Zelkin.Tn DES 86f45f23ddSAlexey ZelkinExtended Format 87e9a56ad5SMark Murrayis used in interpreting both the the key and the salt, as outlined below. 88e9a56ad5SMark Murray.It Modular 89f45f23ddSAlexey ZelkinIf it begins with the string 90f45f23ddSAlexey Zelkin.Dq $digit$ 91f45f23ddSAlexey Zelkinthen the Modular Crypt Format is used, as outlined below. 92e9a56ad5SMark Murray.It Traditional 93e9a56ad5SMark MurrayIf neither of the above is true, it assumes the Traditional Format, 94e9a56ad5SMark Murrayusing the entire string as the salt (or the first portion). 95e9a56ad5SMark Murray.El 96e9a56ad5SMark Murray.Pp 97e9a56ad5SMark MurrayAll routines are designed to be time-consuming. A brief test on a 98f45f23ddSAlexey Zelkin.Tn Pentium 99f45f23ddSAlexey Zelkin166/MMX shows the 100f45f23ddSAlexey Zelkin.Tn DES 101f45f23ddSAlexey Zelkincrypt to do approximately 2640 crypts 10200587201SMark Murraya CPU second and MD5 to do about 62 crypts a CPU second. 103e9a56ad5SMark Murray.Ss DES Extended Format: 104e9a56ad5SMark Murray.Pp 105e9a56ad5SMark MurrayThe 106e9a56ad5SMark Murray.Ar key 107e9a56ad5SMark Murrayis divided into groups of 8 characters (the last group is null-padded) 108e9a56ad5SMark Murrayand the low-order 7 bits of each each character (56 bits per group) are 109f45f23ddSAlexey Zelkinused to form the 110f45f23ddSAlexey Zelkin.Tn DES 111f45f23ddSAlexey Zelkinkey as follows: 112f45f23ddSAlexey Zelkinthe first group of 56 bits becomes the initial 113f45f23ddSAlexey Zelkin.Tn DES 114f45f23ddSAlexey Zelkinkey. 115f45f23ddSAlexey ZelkinFor each additional group, the XOR of the encryption of the current 116f45f23ddSAlexey Zelkin.Tn DES 117f45f23ddSAlexey Zelkinkey with itself and the group bits becomes the next 118f45f23ddSAlexey Zelkin.Tn DES 119f45f23ddSAlexey Zelkinkey. 120e9a56ad5SMark Murray.Pp 121e9a56ad5SMark MurrayThe salt is a 9-character array consisting of an underscore followed 122e9a56ad5SMark Murrayby 4 bytes of iteration count and 4 bytes of salt. 123e9a56ad5SMark MurrayThese are encoded as printable characters, 6 bits per character, 124e9a56ad5SMark Murrayleast significant character first. 125e9a56ad5SMark MurrayThe values 0 to 63 are encoded as ``./0-9A-Za-z''. 126e9a56ad5SMark MurrayThis allows 24 bits for both 127e9a56ad5SMark Murray.Fa count 128e9a56ad5SMark Murrayand 129e9a56ad5SMark Murray.Fa salt . 130e9a56ad5SMark Murray.Pp 131e9a56ad5SMark MurrayThe 132e9a56ad5SMark Murray.Fa salt 133e9a56ad5SMark Murrayintroduces disorder in the 134e9a56ad5SMark Murray.Tn DES 135e9a56ad5SMark Murrayalgorithm in one of 16777216 or 4096 possible ways 136e9a56ad5SMark Murray(ie. with 24 or 12 bits: if bit 137e9a56ad5SMark Murray.Em i 138e9a56ad5SMark Murrayof the 139e9a56ad5SMark Murray.Ar salt 140e9a56ad5SMark Murrayis set, then bits 141e9a56ad5SMark Murray.Em i 142e9a56ad5SMark Murrayand 143e9a56ad5SMark Murray.Em i+24 144e9a56ad5SMark Murrayare swapped in the 145e9a56ad5SMark Murray.Tn DES 146e9a56ad5SMark MurrayE-box output). 147e9a56ad5SMark Murray.Pp 148f45f23ddSAlexey ZelkinThe 149f45f23ddSAlexey Zelkin.Tn DES 150f45f23ddSAlexey Zelkinkey is used to encrypt a 64-bit constant using 151e9a56ad5SMark Murray.Ar count 152e9a56ad5SMark Murrayiterations of 153e9a56ad5SMark Murray.Tn DES . 154e9a56ad5SMark MurrayThe value returned is a 155e9a56ad5SMark Murray.Dv null Ns -terminated 156e9a56ad5SMark Murraystring, 20 or 13 bytes (plus null) in length, consisting of the 157e9a56ad5SMark Murray.Ar salt 158e9a56ad5SMark Murrayfollowed by the encoded 64-bit encryption. 159e9a56ad5SMark Murray.Ss "Modular" crypt: 160e9a56ad5SMark Murray.Pp 161e9a56ad5SMark MurrayIf the salt begins with the string 162e9a56ad5SMark Murray.Fa $digit$ 163e9a56ad5SMark Murraythen the Modular Crypt Format is used. The 164e9a56ad5SMark Murray.Fa digit 165e9a56ad5SMark Murrayrepresents which algorithm is used in encryption. Following the token is 166e9a56ad5SMark Murraythe actual salt to use in the encryption. The length of the salt is limited 167e9a56ad5SMark Murrayto 16 characters--because the length of the returned output is also limited 168e9a56ad5SMark Murray(_PASSWORD_LEN). The salt must be terminated with the end of the string 169e9a56ad5SMark Murray(NULL) or a dollar sign. Any characters after the dollar sign are ignored. 170e9a56ad5SMark Murray.Pp 171e9a56ad5SMark MurrayCurrently supported algorithms are: 172e9a56ad5SMark Murray.Pp 173e9a56ad5SMark Murray.Bl -tag -width 012345678 -compact -offset indent 174e9a56ad5SMark Murray.It 1 175e9a56ad5SMark MurrayMD5 176e9a56ad5SMark Murray.El 177e9a56ad5SMark Murray.Pp 178e9a56ad5SMark MurrayOther crypt formats may be easilly added. An example salt would be: 179e9a56ad5SMark Murray.Bl -tag -offset indent 180e9a56ad5SMark Murray.It Cm "$3$thesalt$rest" 181e9a56ad5SMark Murray.El 182e9a56ad5SMark Murray.Pp 183e9a56ad5SMark Murray.Ss "Traditional" crypt: 184e9a56ad5SMark Murray.Pp 185f45f23ddSAlexey ZelkinThe algorithm used will depend upon whether 18604c9749fSBrian Feldman.Fn crypt_set_format 1879886bcdfSPeter Wemmhas been called and whether a global default format has been specified. 1889886bcdfSPeter WemmUnless a global default has been specified or 18904c9749fSBrian Feldman.Fn crypt_set_format 19088b471a1SPeter Wemmhas set the format to something else, the built-in default format is 19188b471a1SPeter Wemmused. 19288b471a1SPeter WemmThis is currently 193e9a56ad5SMark Murray.\" 194e9a56ad5SMark Murray.\" NOTICE: Also make sure to update this 195e9a56ad5SMark Murray.\" 19688b471a1SPeter WemmDES 19788b471a1SPeter Wemmif it is available, or MD5 if not. 198e9a56ad5SMark Murray.Pp 199e9a56ad5SMark MurrayHow the salt is used will depend upon the algorithm for the hash. For 200e9a56ad5SMark Murraybest results, specify at least two characters of salt. 20104c9749fSBrian Feldman.Pp 20204c9749fSBrian FeldmanThe 20304c9749fSBrian Feldman.Fn crypt_get_format 20404c9749fSBrian Feldmanfunction returns a constant string that represents the name of the 20504c9749fSBrian Feldmanalgorithm currently used. 20604c9749fSBrian FeldmanValid values are 20704c9749fSBrian Feldman.\" 20804c9749fSBrian Feldman.\" NOTICE: Also make sure to update this, too, as well 20904c9749fSBrian Feldman.\" 21004c9749fSBrian Feldman.Ql des 21104c9749fSBrian Feldmanand 21204c9749fSBrian Feldman.Ql md5 . 21304c9749fSBrian Feldman.Pp 21404c9749fSBrian FeldmanThe 21504c9749fSBrian Feldman.Fn crypt_set_format 21604c9749fSBrian Feldmanfunction sets the default encoding format according to the supplied 21704c9749fSBrian Feldman.Fa string . 2189886bcdfSPeter Wemm.Pp 2199886bcdfSPeter WemmThe global default format can be set using the 2209886bcdfSPeter Wemm.Pa /etc/auth.conf 2219886bcdfSPeter Wemmfile using the 2229886bcdfSPeter Wemm.Ql crypt_format 2239886bcdfSPeter Wemmproperty. 224e9a56ad5SMark Murray.Sh RETURN VALUES 225e9a56ad5SMark Murray.Pp 226e9a56ad5SMark Murray.Fn crypt 227e9a56ad5SMark Murrayreturns a pointer to the encrypted value on success, and NULL on failure. 228e9a56ad5SMark MurrayNote: this is not a standard behaviour, AT&T 229e9a56ad5SMark Murray.Fn crypt 230e9a56ad5SMark Murraywill always return a pointer to a string. 23104c9749fSBrian Feldman.Pp 23204c9749fSBrian Feldman.Fn crypt_set_format 23304c9749fSBrian Feldmanwill return 1 if the supplied encoding format was valid. 23404c9749fSBrian FeldmanOtherwise, a value of 0 is returned. 235e9a56ad5SMark Murray.Sh SEE ALSO 236e9a56ad5SMark Murray.Xr login 1 , 237e9a56ad5SMark Murray.Xr passwd 1 , 2389886bcdfSPeter Wemm.Xr auth_getval 3 , 23900587201SMark Murray.Xr cipher 3 , 240e9a56ad5SMark Murray.Xr getpass 3 , 2419886bcdfSPeter Wemm.Xr auth.conf 5 , 242e9a56ad5SMark Murray.Xr passwd 5 , 243e9a56ad5SMark Murray.Sh BUGS 244e9a56ad5SMark MurrayThe 245e9a56ad5SMark Murray.Fn crypt 246e9a56ad5SMark Murrayfunction returns a pointer to static data, and subsequent calls to 247e9a56ad5SMark Murray.Fn crypt 24804c9749fSBrian Feldmanwill modify the same data. Likewise, 24904c9749fSBrian Feldman.Fn crypt_set_format 25004c9749fSBrian Feldmanmodifies static data. 251e9a56ad5SMark Murray.Sh HISTORY 252e9a56ad5SMark MurrayA rotor-based 253e9a56ad5SMark Murray.Fn crypt 254e9a56ad5SMark Murrayfunction appeared in 255e9a56ad5SMark Murray.At v6 . 256e9a56ad5SMark MurrayThe current style 257e9a56ad5SMark Murray.Fn crypt 258e9a56ad5SMark Murrayfirst appeared in 259e9a56ad5SMark Murray.At v7 . 260e9a56ad5SMark Murray.Pp 261f45f23ddSAlexey ZelkinThe 262f45f23ddSAlexey Zelkin.Tn DES 263f45f23ddSAlexey Zelkinsection of the code (FreeSec 1.0) was developed outside the United 264f45f23ddSAlexey ZelkinStates of America as an unencumbered replacement for the U.S.-only 265f45f23ddSAlexey Zelkin.Nx 266e9a56ad5SMark Murraylibcrypt encryption library. 267e9a56ad5SMark MurrayUsers should be aware that this code (and programs staticly linked with it) 268e9a56ad5SMark Murraymay not be exported from the U.S., although it apparently can be imported. 269e9a56ad5SMark Murray.Sh AUTHORS 270725ab628SRuslan Ermilov.An -nosplit 271f45f23ddSAlexey ZelkinOriginally written by 272f45f23ddSAlexey Zelkin.An David Burren Aq davidb@werj.com.au , 273f45f23ddSAlexey Zelkinlater additions and changes by 274725ab628SRuslan Ermilov.An Poul-Henning Kamp , 27504c9749fSBrian Feldman.An Mark R V Murray , 276725ab628SRuslan Ermilov.An Kris Kennaway 27704c9749fSBrian Feldmanand 27804c9749fSBrian Feldman.An Brian Feldman . 279