1.\" FreeSec: libcrypt for NetBSD 2.\" 3.\" Copyright (c) 1994 David Burren 4.\" 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.\" 4. Neither the name of the author nor the names of other contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" $FreeBSD: src/lib/libcrypt/crypt.3,v 1.6.2.14 2002/12/29 16:35:35 schweikh Exp $ 31.\" $DragonFly: src/lib/libcrypt/crypt.3,v 1.5 2006/03/26 22:56:56 swildner Exp $ 32.\" 33.\" Manual page, using -mandoc macros 34.\" 35.Dd January 19, 1997 36.Dt CRYPT 3 37.Os 38.Sh NAME 39.Nm crypt 40.Nd Trapdoor encryption 41.Sh LIBRARY 42.Lb libcrypt 43.Sh SYNOPSIS 44.In unistd.h 45.Ft char * 46.Fn crypt "const char *key" "const char *salt" 47.Ft const char * 48.Fn crypt_get_format "void" 49.Ft int 50.Fn crypt_set_format "const char *string" 51.Sh DESCRIPTION 52The 53.Fn crypt 54function performs password hashing with additional code added to 55deter key search attempts. Different algorithms can be used to 56in the hash. 57.\" 58.\" NOTICE: 59.\" If you add more algorithms, make sure to update this list 60.\" and the default used for the Traditional format, below. 61.\" 62Currently these include the 63.Tn NBS 64.Tn Data Encryption Standard (DES) , 65.Tn MD5 66.Tn SHA256 67.Tn SHA512 68and 69.Tn Blowfish . 70The algorithm used will depend upon the format of the Salt (following 71the Modular Crypt Format (MCF)), if 72.Tn DES 73and/or 74.Tn Blowfish 75is installed or not, and whether 76.Fn crypt_set_format 77has been called to change the default. 78.Pp 79The first argument to 80.Nm 81is the data to hash (usually a password), in a 82.Dv null Ns -terminated 83string. 84The second is the salt, in one of three forms: 85.Pp 86.Bl -tag -width Traditional -compact -offset indent 87.It Extended 88If it begins with an underscore 89.Pq Dq _ 90then the 91.Tn DES 92Extended Format 93is used in interpreting both the key and the salt, as outlined below. 94.It Modular 95If it begins with the string 96.Dq $digit$ 97then the Modular Crypt Format is used, as outlined below. 98.It Traditional 99If neither of the above is true, it assumes the Traditional Format, 100using the entire string as the salt (or the first portion). 101.El 102.Pp 103All routines are designed to be time-consuming. A brief test on a 104.Tn Pentium 105166/MMX shows the 106.Tn DES 107crypt to do approximately 2640 crypts 108a CPU second and MD5 to do about 62 crypts a CPU second. 109.Ss DES Extended Format: 110The 111.Ar key 112is divided into groups of 8 characters (the last group is null-padded) 113and the low-order 7 bits of each character (56 bits per group) are 114used to form the 115.Tn DES 116key as follows: 117the first group of 56 bits becomes the initial 118.Tn DES 119key. 120For each additional group, the XOR of the encryption of the current 121.Tn DES 122key with itself and the group bits becomes the next 123.Tn DES 124key. 125.Pp 126The salt is a 9-character array consisting of an underscore followed 127by 4 bytes of iteration count and 4 bytes of salt. 128These are encoded as printable characters, 6 bits per character, 129least significant character first. 130The values 0 to 63 are encoded as ``./0-9A-Za-z''. 131This allows 24 bits for both 132.Fa count 133and 134.Fa salt . 135.Pp 136The 137.Fa salt 138introduces disorder in the 139.Tn DES 140algorithm in one of 16777216 or 4096 possible ways 141(ie. with 24 or 12 bits: if bit 142.Em i 143of the 144.Ar salt 145is set, then bits 146.Em i 147and 148.Em i+24 149are swapped in the 150.Tn DES 151E-box output). 152.Pp 153The 154.Tn DES 155key is used to encrypt a 64-bit constant using 156.Ar count 157iterations of 158.Tn DES . 159The value returned is a 160.Dv null Ns -terminated 161string, 20 or 13 bytes (plus null) in length, consisting of the 162.Ar salt 163followed by the encoded 64-bit encryption. 164.Ss "Modular" crypt: 165If the salt begins with the string 166.Fa $digit$ 167then the Modular Crypt Format is used. The 168.Fa digit 169represents which algorithm is used in encryption. Following the token is 170the actual salt to use in the encryption. The length of the salt is limited 171to 8 characters--because the length of the returned output is also limited 172(_PASSWORD_LEN). The salt must be terminated with the end of the string 173(NULL) or a dollar sign. Any characters after the dollar sign are ignored. 174.Pp 175Currently supported algorithms are: 176.Pp 177.Bl -enum -compact -offset indent 178.It 179MD5 180.It 181SHA256 182.It 183SHA512 184.It 185Blowfish 186.El 187.Pp 188Other crypt formats may be easily added. An example salt would be: 189.Bl -tag -offset indent 190.It Cm "$3$thesalt$rest" 191.El 192.Ss "Traditional" crypt: 193The algorithm used will depend upon whether 194.Fn crypt_set_format 195has been called and whether a global default format has been specified. 196Unless a global default has been specified or 197.Fn crypt_set_format 198has set the format to something else, the built-in default format is 199used. 200This is currently 201.\" 202.\" NOTICE: Also make sure to update this 203.\" 204DES 205if it is available, or MD5 if not. 206.Pp 207How the salt is used will depend upon the algorithm for the hash. For 208best results, specify at least two characters of salt. 209.Pp 210The 211.Fn crypt_get_format 212function returns a constant string that represents the name of the 213algorithm currently used. 214Valid values are 215.\" 216.\" NOTICE: Also make sure to update this, too, as well 217.\" 218.Ql des , 219.Ql blf , 220.Ql sha256 , 221.Ql sha512 , 222and 223.Ql md5 . 224.Pp 225The 226.Fn crypt_set_format 227function sets the default encoding format according to the supplied 228.Fa string . 229.Pp 230The global default format can be set using the 231.Pa /etc/auth.conf 232file using the 233.Va crypt_default 234property. 235.Sh RETURN VALUES 236.Fn crypt 237returns a pointer to the encrypted value on success, and NULL on failure. 238Note: this is not a standard behaviour, AT&T 239.Fn crypt 240will always return a pointer to a string. 241.Pp 242.Fn crypt_set_format 243will return 1 if the supplied encoding format was valid. 244Otherwise, a value of 0 is returned. 245.Sh SEE ALSO 246.Xr login 1 , 247.Xr passwd 1 , 248.Xr auth_getval 3 , 249.Xr cipher 3 , 250.Xr getpass 3 , 251.Xr auth.conf 5 , 252.Xr passwd 5 253.Sh HISTORY 254A rotor-based 255.Fn crypt 256function appeared in 257.At v6 . 258The current style 259.Fn crypt 260first appeared in 261.At v7 . 262.Pp 263The 264.Tn DES 265section of the code (FreeSec 1.0) was developed outside the United 266States of America as an unencumbered replacement for the U.S.-only 267.Nx 268libcrypt encryption library. 269.Sh AUTHORS 270.An -nosplit 271Originally written by 272.An David Burren Aq davidb@werj.com.au , 273later additions and changes by 274.An Poul-Henning Kamp , 275.An Mark R V Murray , 276.An Kris Kennaway , 277.An Brian Feldman , 278.An Paul Herman 279and 280.An Niels Provos . 281.Sh BUGS 282The 283.Fn crypt 284function returns a pointer to static data, and subsequent calls to 285.Fn crypt 286will modify the same data. Likewise, 287.Fn crypt_set_format 288modifies static data. 289