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.\" 32.\" Manual page, using -mandoc macros 33.\" 34.Dd January 21, 2012 35.Dt CRYPT 3 36.Os 37.Sh NAME 38.Nm crypt 39.Nd Trapdoor encryption 40.Sh LIBRARY 41.Lb libcrypt 42.Sh SYNOPSIS 43.In unistd.h 44.Ft char * 45.Fn crypt "const char *key" "const char *salt" 46.Ft const char * 47.Fn crypt_get_format "void" 48.Ft int 49.Fn crypt_set_format "const char *string" 50.Sh DESCRIPTION 51The 52.Fn crypt 53function performs password hashing with additional code added to 54deter key search attempts. 55Different 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. 104A brief test on a 105.Tn Pentium 106166/MMX shows the 107.Tn DES 108crypt to do approximately 2640 crypts 109a CPU second and MD5 to do about 62 crypts a CPU second. 110.Ss DES Extended Format: 111The 112.Ar key 113is divided into groups of 8 characters (the last group is null-padded) 114and the low-order 7 bits of each character (56 bits per group) are 115used to form the 116.Tn DES 117key as follows: 118the first group of 56 bits becomes the initial 119.Tn DES 120key. 121For each additional group, the XOR of the encryption of the current 122.Tn DES 123key with itself and the group bits becomes the next 124.Tn DES 125key. 126.Pp 127The salt is a 9-character array consisting of an underscore followed 128by 4 bytes of iteration count and 4 bytes of salt. 129These are encoded as printable characters, 6 bits per character, 130least significant character first. 131The values 0 to 63 are encoded as ``./0-9A-Za-z''. 132This allows 24 bits for both 133.Fa count 134and 135.Fa salt . 136.Pp 137The 138.Fa salt 139introduces disorder in the 140.Tn DES 141algorithm in one of 16777216 or 4096 possible ways 142(ie.\& with 24 or 12 bits: if bit 143.Em i 144of the 145.Ar salt 146is set, then bits 147.Em i 148and 149.Em i+24 150are swapped in the 151.Tn DES 152E-box output). 153.Pp 154The 155.Tn DES 156key is used to encrypt a 64-bit constant using 157.Ar count 158iterations of 159.Tn DES . 160The value returned is a 161.Dv null Ns -terminated 162string, 20 or 13 bytes (plus null) in length, consisting of the 163.Ar salt 164followed by the encoded 64-bit encryption. 165.Ss "Modular" crypt: 166If the salt begins with the string 167.Fa $digit$ 168then the Modular Crypt Format is used. 169The 170.Fa digit 171represents which algorithm is used in encryption. 172Following the token is 173the actual salt to use in the encryption. 174The length of the salt is limited 175to 8 characters--because the length of the returned output is also limited 176(_PASSWORD_LEN). 177The salt must be terminated with the end of the string 178(NULL) or a dollar sign. 179Any characters after the dollar sign are ignored. 180.Pp 181Currently supported algorithms are: 182.Pp 183.Bl -enum -compact -offset indent 184.It 185MD5 186.It 187Blowfish 188.It 189SHA256 deprecated implementation 190.It 191SHA512 deprecated implementation 192.It 193SHA256 194.It 195SHA512 196.El 197.Pp 198Other crypt formats may be easily added. 199An example salt would be: 200.Bl -tag -width ".Cm $3$thesalt$rest" -offset indent 201.It Cm "$3$thesalt$rest" 202.El 203.Ss "Traditional" crypt: 204The algorithm used will depend upon whether 205.Fn crypt_set_format 206has been called and whether a global default format has been specified. 207Unless a global default has been specified or 208.Fn crypt_set_format 209has set the format to something else, the built-in default format is 210used. 211This is currently 212.\" 213.\" NOTICE: Also make sure to update this 214.\" 215DES 216if it is available, or MD5 if not. 217.Pp 218How the salt is used will depend upon the algorithm for the hash. 219For best results, specify at least two characters of salt. 220.Pp 221The 222.Fn crypt_get_format 223function returns a constant string that represents the name of the 224algorithm currently used. 225Valid values are 226.\" 227.\" NOTICE: Also make sure to update this, too, as well 228.\" 229.Ql des , 230.Ql blf , 231.Ql sha256 , 232.Ql sha512 233and 234.Ql md5 . 235.Pp 236The 237.Fn crypt_set_format 238function sets the default encoding format according to the supplied 239.Fa string . 240.Pp 241The global default format can be set using the 242.Pa /etc/auth.conf 243file using the 244.Va crypt_default 245property. 246.Sh RETURN VALUES 247.Fn crypt 248returns a pointer to the encrypted value on success, and NULL on failure. 249Note: this is not a standard behaviour, AT&T 250.Fn crypt 251will always return a pointer to a string. 252.Pp 253.Fn crypt_set_format 254will return 1 if the supplied encoding format was valid. 255Otherwise, a value of 0 is returned. 256.Sh SEE ALSO 257.Xr login 1 , 258.Xr passwd 1 , 259.Xr auth_getval 3 , 260.Xr cipher 3 , 261.Xr getpass 3 , 262.Xr auth.conf 5 , 263.Xr passwd 5 264.Sh HISTORY 265A rotor-based 266.Fn crypt 267function appeared in 268.At v6 . 269The current style 270.Fn crypt 271first appeared in 272.At v7 . 273.Pp 274The 275.Tn DES 276section of the code (FreeSec 1.0) was developed outside the United 277States of America as an unencumbered replacement for the U.S.-only 278.Nx 279libcrypt encryption library. 280.Sh AUTHORS 281.An -nosplit 282Originally written by 283.An David Burren Aq Mt davidb@werj.com.au , 284later additions and changes by 285.An Poul-Henning Kamp , 286.An Mark R V Murray , 287.An Kris Kennaway , 288.An Brian Feldman , 289.An Paul Herman 290and 291.An Niels Provos . 292.Sh BUGS 293The 294.Fn crypt 295function returns a pointer to static data, and subsequent calls to 296.Fn crypt 297will modify the same data. 298Likewise, 299.Fn crypt_set_format 300modifies static data. 301