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