1.\" $OpenBSD: crypt.3,v 1.42 2014/11/25 03:36:34 tedu 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: November 25 2014 $ 35.Dt CRYPT 3 36.Os 37.Sh NAME 38.Nm crypt , 39.Nm setkey , 40.Nm encrypt , 41.Nm des_setkey , 42.Nm des_cipher , 43.Nm bcrypt_gensalt , 44.Nm bcrypt 45.Nd password hashing 46.Sh SYNOPSIS 47.In stdlib.h 48.Ft int 49.Fn setkey "const char *key" 50.Pp 51.In unistd.h 52.Ft char * 53.Fn crypt "const char *key" "const char *setting" 54.Ft int 55.Fn encrypt "char *block" "int flag" 56.Ft int 57.Fn des_setkey "const char *key" 58.Ft int 59.Fn des_cipher "const char *in" "char *out" "int32_t salt" "int count" 60.In pwd.h 61.Ft char * 62.Fn bcrypt_gensalt "u_int8_t log_rounds" 63.Ft char * 64.Fn bcrypt "const char *key" "const char *salt" 65.Sh DESCRIPTION 66These functions are deprecated in favor of 67.Xr crypt_checkpass 3 68and 69.Xr crypt_newhash 3 . 70.Pp 71The 72.Fn crypt 73function performs password hashing based on the 74.Tn NBS 75Data Encryption Standard (DES). 76Additional code has been added to deter key search attempts and to use 77stronger hashing algorithms. 78.Pp 79The first argument to 80.Fn crypt 81is a 82.Dv NUL Ns -terminated 83string, typically a user's typed password. 84The second is in one of three forms: 85if it begins with an underscore 86.Pq Ql _ 87then an extended format is used 88in interpreting both the key and the setting, as outlined below. 89If it begins 90with a string character 91.Pq Ql $ 92and a number then a different algorithm is used depending on the number. 93At the moment 94.Ql $2 95chooses Blowfish hashing; see below for more information. 96.Ss Extended crypt 97The 98.Ar key 99is divided into groups of 8 characters (the last group is null-padded) 100and the low-order 7 bits of each character (56 bits per group) are 101used to form the DES key as follows: 102the first group of 56 bits becomes the initial DES key. 103For each additional group, the XOR of the encryption of the current DES 104key with itself and the group bits becomes the next DES key. 105.Pp 106The setting is a 9-character array consisting of an underscore followed 107by 4 bytes of iteration count and 4 bytes of salt. 108These are encoded as printable characters, 6 bits per character, 109least significant character first. 110The values 0 to 63 are encoded as 111.Dq \&./0-9A-Za-z . 112This allows 24 bits for both 113.Fa count 114and 115.Fa salt . 116.Ss "Blowfish" crypt 117The 118.Tn Blowfish 119version of crypt has 128 bits of 120.Fa salt 121in order to make building dictionaries of common passwords space consuming. 122The initial state of the 123.Tn Blowfish 124cipher is expanded using the 125.Fa salt 126and the 127.Fa password 128repeating the process a variable number of rounds, which is encoded in 129the password string. 130The maximum password length is 72. 131The final Blowfish password entry is created by encrypting the string 132.Pp 133.Dq OrpheanBeholderScryDoubt 134.Pp 135with the 136.Tn Blowfish 137state 64 times. 138.Pp 139The version number, the logarithm of the number of rounds and 140the concatenation of salt and hashed password are separated by the 141.Ql $ 142character. 143An encoded 144.Sq 8 145would specify 256 rounds. 146A valid Blowfish password looks like this: 147.Pp 148.Dq $2b$12$FPWWO2RJ3CK4FINTw0Hi8OiPKJcX653gzSS.jqltHFMxyDmmQ0Hqq . 149.Pp 150The whole Blowfish password string is passed as 151.Fa setting 152for interpretation. 153.Ss "Traditional" crypt 154The first 8 bytes of the key are null-padded, and the low-order 7 bits of 155each character is used to form the 56-bit 156.Tn DES 157key. 158.Pp 159The setting is a 2-character array of the ASCII-encoded salt. 160Thus only 12 bits of 161.Fa salt 162are used. 163.Fa count 164is set to 25. 165.Ss DES Algorithm 166The 167.Fa salt 168introduces disorder in the 169.Tn DES 170algorithm in one of 16777216 or 4096 possible ways 171(i.e., with 24 or 12 bits: if bit 172.Em i 173of the 174.Ar salt 175is set, then bits 176.Em i 177and 178.Em i+24 179are swapped in the 180.Tn DES 181E-box output). 182.Pp 183The DES key is used to encrypt a 64-bit constant using 184.Ar count 185iterations of 186.Tn DES . 187The value returned is a 188.Dv NUL Ns -terminated 189string, 20 or 13 bytes (plus NUL) in length, consisting of the 190.Ar setting 191followed by the encoded 64-bit encryption. 192.Pp 193The functions 194.Fn encrypt , 195.Fn setkey , 196.Fn des_setkey , 197and 198.Fn des_cipher 199provide access to the 200.Tn DES 201algorithm itself. 202.Fn setkey 203is passed a 64-byte array of binary values (numeric 0 or 1). 204A 56-bit key is extracted from this array by dividing the 205array into groups of 8, and ignoring the last bit in each group. 206That bit is reserved for a byte parity check by DES, but is ignored 207by these functions. 208.Pp 209The 210.Fa block 211argument to 212.Fn encrypt 213is also a 64-byte array of binary values. 214If the value of 215.Fa flag 216is 0, 217.Fa block 218is encrypted otherwise it is decrypted. 219The result is returned in the original array 220.Fa block 221after using the key specified by 222.Fn setkey 223to process it. 224.Pp 225The argument to 226.Fn des_setkey 227is a character array of length 8. 228The least significant bit (the parity bit) in each character is ignored, 229and the remaining bits are concatenated to form a 56-bit key. 230The function 231.Fn des_cipher 232encrypts (or decrypts if 233.Fa count 234is negative) the 64-bits stored in the 8 characters at 235.Fa in 236using 237.Xr abs 3 238of 239.Fa count 240iterations of 241.Tn DES 242and stores the 64-bit result in the 8 characters at 243.Fa out 244(which may be the same as 245.Fa in ) . 246The 247.Fa salt 248specifies perturbations to the 249.Tn DES 250E-box output as described above. 251.Pp 252The 253.Fn crypt , 254.Fn setkey , 255and 256.Fn des_setkey 257functions all manipulate the same key space. 258.Sh RETURN VALUES 259The function 260.Fn crypt 261returns a pointer to the encrypted value on success, and 262.Dv NULL 263on failure. 264The functions 265.Fn setkey , 266.Fn encrypt , 267.Fn des_setkey , 268and 269.Fn des_cipher 270return 0 on success and 1 on failure. 271.Sh SEE ALSO 272.Xr encrypt 1 , 273.Xr login 1 , 274.Xr passwd 1 , 275.Xr blowfish 3 , 276.Xr crypt_checkpass 3 , 277.Xr getpass 3 , 278.Xr passwd 5 279.Sh HISTORY 280A rotor-based 281.Fn crypt 282function appeared in 283.At v3 . 284The current style 285.Fn crypt 286first appeared in 287.At v7 . 288.Sh AUTHORS 289.An David Burren Aq Mt davidb@werj.com.au 290wrote the original DES functions. 291.Sh BUGS 292The 293.Fn crypt 294function returns a pointer to static data, and subsequent calls to 295.Fn crypt 296will modify the same object. 297.Pp 298With DES hashing, passwords containing the byte 0x80 use less key entropy 299than other passwords. 300This is an implementation bug, not a bug in the DES cipher. 301