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.2 2003/06/17 04:26:49 dillon 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 66and 67.Tn Blowfish . 68The algorithm used will depend upon the format of the Salt (following 69the Modular Crypt Format (MCF)), if 70.Tn DES 71and/or 72.Tn Blowfish 73is installed or not, and whether 74.Fn crypt_set_format 75has been called to change the default. 76.Pp 77The first argument to 78.Nm 79is the data to hash (usually a password), in a 80.Dv null Ns -terminated 81string. 82The second is the salt, in one of three forms: 83.Pp 84.Bl -tag -width Traditional -compact -offset indent 85.It Extended 86If it begins with an underscore 87.Pq Dq _ 88then the 89.Tn DES 90Extended Format 91is used in interpreting both the key and the salt, as outlined below. 92.It Modular 93If it begins with the string 94.Dq $digit$ 95then the Modular Crypt Format is used, as outlined below. 96.It Traditional 97If neither of the above is true, it assumes the Traditional Format, 98using the entire string as the salt (or the first portion). 99.El 100.Pp 101All routines are designed to be time-consuming. A brief test on a 102.Tn Pentium 103166/MMX shows the 104.Tn DES 105crypt to do approximately 2640 crypts 106a CPU second and MD5 to do about 62 crypts a CPU second. 107.Ss DES Extended Format: 108.Pp 109The 110.Ar key 111is divided into groups of 8 characters (the last group is null-padded) 112and the low-order 7 bits of each character (56 bits per group) are 113used to form the 114.Tn DES 115key as follows: 116the first group of 56 bits becomes the initial 117.Tn DES 118key. 119For each additional group, the XOR of the encryption of the current 120.Tn DES 121key with itself and the group bits becomes the next 122.Tn DES 123key. 124.Pp 125The salt is a 9-character array consisting of an underscore followed 126by 4 bytes of iteration count and 4 bytes of salt. 127These are encoded as printable characters, 6 bits per character, 128least significant character first. 129The values 0 to 63 are encoded as ``./0-9A-Za-z''. 130This allows 24 bits for both 131.Fa count 132and 133.Fa salt . 134.Pp 135The 136.Fa salt 137introduces disorder in the 138.Tn DES 139algorithm in one of 16777216 or 4096 possible ways 140(ie. with 24 or 12 bits: if bit 141.Em i 142of the 143.Ar salt 144is set, then bits 145.Em i 146and 147.Em i+24 148are swapped in the 149.Tn DES 150E-box output). 151.Pp 152The 153.Tn DES 154key is used to encrypt a 64-bit constant using 155.Ar count 156iterations of 157.Tn DES . 158The value returned is a 159.Dv null Ns -terminated 160string, 20 or 13 bytes (plus null) in length, consisting of the 161.Ar salt 162followed by the encoded 64-bit encryption. 163.Ss "Modular" crypt: 164.Pp 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 181Blowfish 182.El 183.Pp 184Other crypt formats may be easily added. An example salt would be: 185.Bl -tag -offset indent 186.It Cm "$3$thesalt$rest" 187.El 188.Pp 189.Ss "Traditional" crypt: 190.Pp 191The algorithm used will depend upon whether 192.Fn crypt_set_format 193has been called and whether a global default format has been specified. 194Unless a global default has been specified or 195.Fn crypt_set_format 196has set the format to something else, the built-in default format is 197used. 198This is currently 199.\" 200.\" NOTICE: Also make sure to update this 201.\" 202DES 203if it is available, or MD5 if not. 204.Pp 205How the salt is used will depend upon the algorithm for the hash. For 206best results, specify at least two characters of salt. 207.Pp 208The 209.Fn crypt_get_format 210function returns a constant string that represents the name of the 211algorithm currently used. 212Valid values are 213.\" 214.\" NOTICE: Also make sure to update this, too, as well 215.\" 216.Ql des , 217.Ql blf 218and 219.Ql md5 . 220.Pp 221The 222.Fn crypt_set_format 223function sets the default encoding format according to the supplied 224.Fa string . 225.Pp 226The global default format can be set using the 227.Pa /etc/auth.conf 228file using the 229.Va crypt_default 230property. 231.Sh RETURN VALUES 232.Fn crypt 233returns a pointer to the encrypted value on success, and NULL on failure. 234Note: this is not a standard behaviour, AT&T 235.Fn crypt 236will always return a pointer to a string. 237.Pp 238.Fn crypt_set_format 239will return 1 if the supplied encoding format was valid. 240Otherwise, a value of 0 is returned. 241.Sh SEE ALSO 242.Xr login 1 , 243.Xr passwd 1 , 244.Xr auth_getval 3 , 245.Xr cipher 3 , 246.Xr getpass 3 , 247.Xr auth.conf 5 , 248.Xr passwd 5 249.Sh BUGS 250The 251.Fn crypt 252function returns a pointer to static data, and subsequent calls to 253.Fn crypt 254will modify the same data. Likewise, 255.Fn crypt_set_format 256modifies static data. 257.Sh HISTORY 258A rotor-based 259.Fn crypt 260function appeared in 261.At v6 . 262The current style 263.Fn crypt 264first appeared in 265.At v7 . 266.Pp 267The 268.Tn DES 269section of the code (FreeSec 1.0) was developed outside the United 270States of America as an unencumbered replacement for the U.S.-only 271.Nx 272libcrypt encryption library. 273.Sh AUTHORS 274.An -nosplit 275Originally written by 276.An David Burren Aq davidb@werj.com.au , 277later additions and changes by 278.An Poul-Henning Kamp , 279.An Mark R V Murray , 280.An Kris Kennaway , 281.An Brian Feldman , 282.An Paul Herman 283and 284.An Niels Provos . 285