1.\" $OpenBSD: crypt.3,v 1.38 2014/05/16 22:11:00 jmc Exp $ 2.\" 3.\" FreeSec: libcrypt 4.\" 5.\" Copyright (c) 1994 David Burren 6.\" All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 4. Neither the name of the author nor the names of other contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" Manual page, using -mandoc macros 33.\" 34.Dd $Mdocdate: May 16 2014 $ 35.Dt CRYPT 3 36.Os 37.Sh NAME 38.Nm crypt , 39.Nm crypt_checkpass , 40.Nm setkey , 41.Nm encrypt , 42.Nm des_setkey , 43.Nm des_cipher , 44.Nm bcrypt_gensalt , 45.Nm bcrypt 46.Nd password hashing 47.Sh SYNOPSIS 48.In stdlib.h 49.Ft int 50.Fn setkey "const char *key" 51.Pp 52.In unistd.h 53.Ft char * 54.Fn crypt "const char *key" "const char *setting" 55.Ft int 56.Fn crypt_checkpass "const char *password" "const char *hash" 57.Ft int 58.Fn encrypt "char *block" "int flag" 59.Ft int 60.Fn des_setkey "const char *key" 61.Ft int 62.Fn des_cipher "const char *in" "char *out" "int32_t salt" "int count" 63.In pwd.h 64.Ft char * 65.Fn bcrypt_gensalt "u_int8_t log_rounds" 66.Ft char * 67.Fn bcrypt "const char *key" "const char *salt" 68.Sh DESCRIPTION 69The 70.Fn crypt 71function performs password hashing based on the 72.Tn NBS 73Data Encryption Standard (DES). 74Additional code has been added to deter key search attempts and to use 75stronger hashing algorithms. 76.Pp 77The first argument to 78.Fn crypt 79is a 80.Dv NUL Ns -terminated 81string, typically a user's typed password. 82The second is in one of three forms: 83if it begins with an underscore 84.Pq Ql _ 85then an extended format is used 86in interpreting both the key and the setting, as outlined below. 87If it begins 88with a string character 89.Pq Ql $ 90and a number then a different algorithm is used depending on the number. 91At the moment 92.Ql $2 93chooses Blowfish hashing; see below for more information. 94.Pp 95The 96.Fn crypt_checkpass 97function is provided to simplify checking a user's password. 98If both the hash and the password are the empty string, authentication 99is a success. 100Otherwise, the password is hashed and compared to the provided hash. 101If the hash is NULL, authentication will always fail, but a default 102amount of work is performed to simulate the hashing operation. 103A successful match will return 0. 104A failure will return \-1 and set errno. 105.Ss Extended crypt 106The 107.Ar key 108is divided into groups of 8 characters (the last group is null-padded) 109and the low-order 7 bits of each character (56 bits per group) are 110used to form the DES key as follows: 111the first group of 56 bits becomes the initial DES key. 112For each additional group, the XOR of the encryption of the current DES 113key with itself and the group bits becomes the next DES key. 114.Pp 115The setting is a 9-character array consisting of an underscore followed 116by 4 bytes of iteration count and 4 bytes of salt. 117These are encoded as printable characters, 6 bits per character, 118least significant character first. 119The values 0 to 63 are encoded as 120.Dq \&./0-9A-Za-z . 121This allows 24 bits for both 122.Fa count 123and 124.Fa salt . 125.Ss "Blowfish" crypt 126The 127.Tn Blowfish 128version of crypt has 128 bits of 129.Fa salt 130in order to make building dictionaries of common passwords space consuming. 131The initial state of the 132.Tn Blowfish 133cipher is expanded using the 134.Fa salt 135and the 136.Fa password 137repeating the process a variable number of rounds, which is encoded in 138the password string. 139The maximum password length is 72. 140The final Blowfish password entry is created by encrypting the string 141.Pp 142.Dq OrpheanBeholderScryDoubt 143.Pp 144with the 145.Tn Blowfish 146state 64 times. 147.Pp 148The version number, the logarithm of the number of rounds and 149the concatenation of salt and hashed password are separated by the 150.Ql $ 151character. 152An encoded 153.Sq 8 154would specify 256 rounds. 155A valid Blowfish password looks like this: 156.Pp 157.Dq $2b$12$FPWWO2RJ3CK4FINTw0Hi8OiPKJcX653gzSS.jqltHFMxyDmmQ0Hqq . 158.Pp 159The whole Blowfish password string is passed as 160.Fa setting 161for interpretation. 162.Ss "Traditional" crypt 163The first 8 bytes of the key are null-padded, and the low-order 7 bits of 164each character is used to form the 56-bit 165.Tn DES 166key. 167.Pp 168The setting is a 2-character array of the ASCII-encoded salt. 169Thus only 12 bits of 170.Fa salt 171are used. 172.Fa count 173is set to 25. 174.Ss DES Algorithm 175The 176.Fa salt 177introduces disorder in the 178.Tn DES 179algorithm in one of 16777216 or 4096 possible ways 180(i.e., with 24 or 12 bits: if bit 181.Em i 182of the 183.Ar salt 184is set, then bits 185.Em i 186and 187.Em i+24 188are swapped in the 189.Tn DES 190E-box output). 191.Pp 192The DES key is used to encrypt a 64-bit constant using 193.Ar count 194iterations of 195.Tn DES . 196The value returned is a 197.Dv NUL Ns -terminated 198string, 20 or 13 bytes (plus NUL) in length, consisting of the 199.Ar setting 200followed by the encoded 64-bit encryption. 201.Pp 202The functions 203.Fn encrypt , 204.Fn setkey , 205.Fn des_setkey , 206and 207.Fn des_cipher 208provide access to the 209.Tn DES 210algorithm itself. 211.Fn setkey 212is passed a 64-byte array of binary values (numeric 0 or 1). 213A 56-bit key is extracted from this array by dividing the 214array into groups of 8, and ignoring the last bit in each group. 215That bit is reserved for a byte parity check by DES, but is ignored 216by these functions. 217.Pp 218The 219.Fa block 220argument to 221.Fn encrypt 222is also a 64-byte array of binary values. 223If the value of 224.Fa flag 225is 0, 226.Fa block 227is encrypted otherwise it is decrypted. 228The result is returned in the original array 229.Fa block 230after using the key specified by 231.Fn setkey 232to process it. 233.Pp 234The argument to 235.Fn des_setkey 236is a character array of length 8. 237The least significant bit (the parity bit) in each character is ignored, 238and the remaining bits are concatenated to form a 56-bit key. 239The function 240.Fn des_cipher 241encrypts (or decrypts if 242.Fa count 243is negative) the 64-bits stored in the 8 characters at 244.Fa in 245using 246.Xr abs 3 247of 248.Fa count 249iterations of 250.Tn DES 251and stores the 64-bit result in the 8 characters at 252.Fa out 253(which may be the same as 254.Fa in ) . 255The 256.Fa salt 257specifies perturbations to the 258.Tn DES 259E-box output as described above. 260.Pp 261The 262.Fn crypt , 263.Fn setkey , 264and 265.Fn des_setkey 266functions all manipulate the same key space. 267.Sh RETURN VALUES 268The function 269.Fn crypt 270returns a pointer to the encrypted value on success, and 271.Dv NULL 272on failure. 273The functions 274.Fn setkey , 275.Fn encrypt , 276.Fn des_setkey , 277and 278.Fn des_cipher 279return 0 on success and 1 on failure. 280.Sh SEE ALSO 281.Xr encrypt 1 , 282.Xr login 1 , 283.Xr passwd 1 , 284.Xr blowfish 3 , 285.Xr getpass 3 , 286.Xr md5 3 , 287.Xr passwd 5 288.Sh HISTORY 289A rotor-based 290.Fn crypt 291function appeared in 292.At v3 . 293The current style 294.Fn crypt 295first appeared in 296.At v7 . 297.Sh AUTHORS 298.An David Burren Aq Mt davidb@werj.com.au 299wrote the original DES functions. 300.Sh BUGS 301The 302.Fn crypt 303function returns a pointer to static data, and subsequent calls to 304.Fn crypt 305will modify the same object. 306.Pp 307With DES hashing, passwords containing the byte 0x80 use less key entropy 308than other passwords. 309This is an implementation bug, not a bug in the DES cipher. 310