1.\" $NetBSD: pw_gensalt.3,v 1.7 2020/03/25 22:09:48 wiz Exp $ 2.\" 3.\" Copyright (c) 2020 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Christos Zoulas. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.\" 31.Dd March 25, 2020 32.Dt PW_GENSALT 3 33.Os 34.Sh NAME 35.Nm pw_gensalt 36.Nd passwd salt generation function 37.Sh LIBRARY 38.Lb libcrypt 39.Sh SYNOPSIS 40.In pwd.h 41.Ft int 42.Fn pw_gensalt "char *salt" "size_t saltlen" "const char *type" "const char *option" 43.Sh DESCRIPTION 44The 45.Fn pw_gensalt 46function generates a 47.Dq salt 48to be added to a password hashing function to guarantee uniqueness and 49slow down dictionary and brute force attacks. 50The function places a random array of 51.Ar saltlen 52bytes in 53.Ar salt 54using the hash function specified in 55.Ar type 56with the function-specific 57.Ar option . 58.Pp 59The new salt types follow the 60.Dq Modular Crypt Format 61(MCF) standard and are of the form: 62.Bd -literal -offset indent 63.Li $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]] 64.Ed 65.Pp 66The characters allowed in the password salt are alphanumeric and 67include a forward slash and a period (are in the regular expression 68format 69.Li [A-Za-z0-9/.] ) . 70.Pp 71The following types are available: 72.Bl -tag -width blowfish -offset indent 73.It old 74The original Unix implementation. 75This is of the form 76.Li _Gl/.???? , 77where 78.Li \&? 79denotes a random alphanumeric character. 80The minimum salt size is 81.Dv 3 . 82.It new 83The Seventh Edition Unix 12 bit salt. 84This has the same form as the 85.Sq old . 86The minimum salt size is 87.Dv 10 . 88The number of rounds can be specified in 89.Ar option 90and is enforced to be between 91.Dv 7250 92and 93.Dv 16777215 . 94.It newsalt 95An alias for 96.Sq new . 97.It md5 98A salt generated using the 99.Xr md5 1 100algorithm. 101This is of the form 102.Li $1$????????$ . 103The minimum salt size is 104.Dv 13 . 105.It sha1 106A salt generated using the 107.Xr sha1 1 108algorithm. 109This is of the form 110.Li $sha1$nrounds$????????$ , 111where 112.Ar nrounds 113is the number of rounds to be used. 114The number of rounds can be specified in 115.Ar option , 116and defaults to random if 117.Dv NULL . 118The minimum salt size is 119.Dv 8 120and the maximum is 121.Dv 64 . 122.It blowfish 123A salt generated using the 124.Sq blowfish 125algorithm. 126The minimum salt size is 127.Dv 30 128and the number of rounds needs to be specified in 129.Ar option . 130This is of the form: 131.Li $2a$nrounds$?????????????????????? . 132The 133.Li 2 134in the salt string indicates the current blowfish version. 135.\" .It argon2 136.\" .It argon2id 137.\" .It argon2i 138.\" .It argon2 139.El 140.Sh RETURN VALUES 141Upon successful completion, a value of 0 is returned. 142Otherwise, a value of \-1 is returned and 143.Va errno 144is set to indicate the error. 145.Sh ERRORS 146.Fn pw_gensalt 147will fail if: 148.Bl -tag -width Er 149.It Bq Er EINVAL 150If the 151.Ar option 152is not specified or has an illegal value. 153.It Bq Er ENOSPC 154The 155.Ar saltlen 156was not large enough to fit the salt for the specified 157.Ar type . 158.El 159.Sh SEE ALSO 160.Xr passwd 1 , 161.Xr pwhash 1 162.Sh HISTORY 163The 164.Fn pw_gensalt 165function was written in 1997 by 166.An Niels Provos Aq Mt provos@physnet.uni-hamburg.de . 167.Pp 168The 169.Lk https://passlib.readthedocs.io/en/stable/modular_crypt_format.html "Modular Crypt Format (MCF)" . 170.Pp 171The 172.Lk https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md "Password Hashing Competition (PHC) format" . 173