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