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$ 31.\" 32.\" Manual page, using -mandoc macros 33.\" 34.Dd January 19, 1997 35.Dt CRYPT 3 36.Os "FreeSec 1.0" 37.Sh NAME 38.Nm crypt 39.Nd Trapdoor encryption 40.Sh SYNOPSIS 41.Fd #include <unistd.h> 42.Ft char * 43.Fn crypt "const char *key" "const char *salt" 44.Sh DESCRIPTION 45The 46.Fn crypt 47function performs password hashing with additional code added to 48deter key search attempts. Different algorithms can be used to 49in the hash. 50.\" 51.\" NOTICE: 52.\" If you add more algorithms, make sure to update this list 53.\" and the default used for the Traditional format, below. 54.\" 55Currently these include the 56.Tn NBS 57Data Encryption Standard (DES), MD5 or SHS. The algorithm 58used will depend upon the format of the Salt--following the Modular 59Crypt Format (MCF)--and if DES is installed or not. 60.Pp 61The first argument to 62.Nm crypt 63is the data to hash (usually a password), in a 64.Dv null Ns -terminated 65string. 66The second is the salt, in one of three forms: 67.Pp 68.Bl -tag -width Traditional -compact -offset indent 69.It Extended 70If it begins with an underscore (``_'') then the DES Extended Format 71is used in interpreting both the the key and the salt, as outlined below. 72.It Modular 73If it begins with the string ``$digit$'' then the Modular Crypt Format 74is used, as outlined below. 75.It Traditional 76If neither of the above is true, it assumes the Traditional Format, 77using the entire string as the salt (or the first portion). 78.El 79.Pp 80All routines are designed to be time-consuming. A brief test on a 81Pentium 166/MMX shows the DES crypt to do approximately 2640 crypts 82a CPU second, MD5 to do about 62 crypts a CPU second and SHA1 83to do about 18 crypts a CPU second. 84.Ss DES Extended Format: 85.Pp 86The 87.Ar key 88is divided into groups of 8 characters (the last group is null-padded) 89and the low-order 7 bits of each each character (56 bits per group) are 90used to form the DES key as follows: 91the first group of 56 bits becomes the initial DES key. 92For each additional group, the XOR of the encryption of the current DES 93key with itself and the group bits becomes the next DES key. 94.Pp 95The salt is a 9-character array consisting of an underscore followed 96by 4 bytes of iteration count and 4 bytes of salt. 97These are encoded as printable characters, 6 bits per character, 98least significant character first. 99The values 0 to 63 are encoded as ``./0-9A-Za-z''. 100This allows 24 bits for both 101.Fa count 102and 103.Fa salt . 104.Pp 105The 106.Fa salt 107introduces disorder in the 108.Tn DES 109algorithm in one of 16777216 or 4096 possible ways 110(ie. with 24 or 12 bits: if bit 111.Em i 112of the 113.Ar salt 114is set, then bits 115.Em i 116and 117.Em i+24 118are swapped in the 119.Tn DES 120E-box output). 121.Pp 122The DES key is used to encrypt a 64-bit constant using 123.Ar count 124iterations of 125.Tn DES . 126The value returned is a 127.Dv null Ns -terminated 128string, 20 or 13 bytes (plus null) in length, consisting of the 129.Ar salt 130followed by the encoded 64-bit encryption. 131.Ss "Modular" crypt: 132.Pp 133If the salt begins with the string 134.Fa $digit$ 135then the Modular Crypt Format is used. The 136.Fa digit 137represents which algorithm is used in encryption. Following the token is 138the actual salt to use in the encryption. The length of the salt is limited 139to 16 characters--because the length of the returned output is also limited 140(_PASSWORD_LEN). The salt must be terminated with the end of the string 141(NULL) or a dollar sign. Any characters after the dollar sign are ignored. 142.Pp 143Currently supported algorithms are: 144.Pp 145.Bl -tag -width 012345678 -compact -offset indent 146.It 1 147MD5 148.It 3 149SHA1 150.El 151.Pp 152Other crypt formats may be easilly added. An example salt would be: 153.Bl -tag -offset indent 154.It Cm "$3$thesalt$rest" 155.El 156.Pp 157.Ss "Traditional" crypt: 158.Pp 159The algorithm used will depend upon whether DES is installed or not. If it is, 160DES will be used. Otherwise, the best algorithm is used, which is currently 161.\" 162.\" NOTICE: Also make sure to update this 163.\" 164SHA-1. 165.Pp 166How the salt is used will depend upon the algorithm for the hash. For 167best results, specify at least two characters of salt. 168.Sh RETURN VALUES 169.Pp 170.Fn crypt 171returns a pointer to the encrypted value on success, and NULL on failure. 172Note: this is not a standard behaviour, AT&T 173.Fn crypt 174will always return a pointer to a string. 175.Sh SEE ALSO 176.Xr login 1 , 177.Xr passwd 1 , 178.Xr getpass 3 , 179.Xr passwd 5 , 180.Xr shs 3 , 181.Sh BUGS 182The 183.Fn crypt 184function returns a pointer to static data, and subsequent calls to 185.Fn crypt 186will modify the same data. 187.Sh HISTORY 188A rotor-based 189.Fn crypt 190function appeared in 191.At v6 . 192The current style 193.Fn crypt 194first appeared in 195.At v7 . 196.Pp 197The DES section of the code (FreeSec 1.0) was developed outside the United 198States of America as an unencumbered replacement for the U.S.-only NetBSD 199libcrypt encryption library. 200Users should be aware that this code (and programs staticly linked with it) 201may not be exported from the U.S., although it apparently can be imported. 202.Sh AUTHORS 203Originally written by David Burren <davidb@werj.com.au>, later additions 204and changes by Brandon Gillespie, Poul-henning Kamp and Mark R V Murray. 205