1 | /** @file
|
---|
2 | * IPRT - Crypto - SHA-crypt.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2023-2024 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_crypto_shacrypt_h
|
---|
37 | #define IPRT_INCLUDED_crypto_shacrypt_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/sha.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | RT_C_DECLS_BEGIN
|
---|
46 |
|
---|
47 | /** @defgroup grp_rt_crshacrypt RTCrShaCrypt - SHA-crypt
|
---|
48 | * @ingroup grp_rt_crypto
|
---|
49 | *
|
---|
50 | * This implements SHA-crypt.txt v0.6 (2016-08-31), which is a scheme to encrypt
|
---|
51 | * passwords using SHA-256 and SHA-512.
|
---|
52 | *
|
---|
53 | * @{
|
---|
54 | */
|
---|
55 |
|
---|
56 | /** Minimum salt string length for SHA-crypt (inclusive). */
|
---|
57 | #define RT_SHACRYPT_SALT_MIN_LEN 8
|
---|
58 | /** Maximum salt string length for SHA-crypt (inclusive). */
|
---|
59 | #define RT_SHACRYPT_SALT_MAX_LEN 16
|
---|
60 |
|
---|
61 |
|
---|
62 | /** Minimum number of rounds for SHA-crypt (inclusive). */
|
---|
63 | #define RT_SHACRYPT_ROUNDS_MIN 1000
|
---|
64 | /** Default number of rounds for SHA-crypt. */
|
---|
65 | #define RT_SHACRYPT_ROUNDS_DEFAULT 5000
|
---|
66 | /** Maximum number of rounds for SHA-crypt (inclusive). */
|
---|
67 | #define RT_SHACRYPT_ROUNDS_MAX 999999999
|
---|
68 |
|
---|
69 |
|
---|
70 | /** The maximum string length of a password encrypted with SHA-256
|
---|
71 | * (including the terminator). */
|
---|
72 | #define RT_SHACRYPT_256_MAX_SIZE 80
|
---|
73 | /** The maximum string length of a password encrypted with SHA-512.
|
---|
74 | * (including the terminator). */
|
---|
75 | #define RT_SHACRYPT_512_MAX_SIZE 123
|
---|
76 |
|
---|
77 |
|
---|
78 | /** The extended password '$ID$' part for SHA-256 encrypted passwords. */
|
---|
79 | #define RT_SHACRYPT_ID_STR_256 "$5$"
|
---|
80 | /** The extended password '$ID$' part for SHA-512 encrypted passwords. */
|
---|
81 | #define RT_SHACRYPT_ID_STR_512 "$6$"
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Creates a randomized salt for the RTCrShaCryptXXX functions.
|
---|
86 | *
|
---|
87 | * @returns IPRT status code.
|
---|
88 | * @param pszSalt Where to return the generated salt string.
|
---|
89 | * @param cchSalt The length of the salt to generate. The buffer size is
|
---|
90 | * this value plus 1 (for the terminator character)!
|
---|
91 | *
|
---|
92 | * Must be in the range RT_SHACRYPT_MIN_SALT_LEN to
|
---|
93 | * RT_SHACRYPT_MAX_SALT_LEN (both inclusive).
|
---|
94 | *
|
---|
95 | * @warning Be very careful with the @a cchSalt parameter, as it must be at
|
---|
96 | * least one less than the actual buffer size!
|
---|
97 | */
|
---|
98 | RTDECL(int) RTCrShaCryptGenerateSalt(char *pszSalt, size_t cchSalt);
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Encrypts @a pszPhrase using SHA-256, @a pszSalt and @a cRounds.
|
---|
102 | *
|
---|
103 | * @returns IPRT status code.
|
---|
104 | * @param pszPhrase The passphrase (password) to encrypt.
|
---|
105 | * @param pszSalt Salt to use. This can either be a pure salt, like from
|
---|
106 | * RTCrShaCryptGenerateSalt(), or a 'password' string as
|
---|
107 | * generated by RTCrShaCrypt256ToString().
|
---|
108 | *
|
---|
109 | * The latter allows for easy password validation by
|
---|
110 | * comparing the encrypted string with the one stored in
|
---|
111 | * the passwd file.
|
---|
112 | *
|
---|
113 | * The length of the actual salt portion of the string must
|
---|
114 | * be within RT_SHACRYPT_MIN_SALT_LEN to
|
---|
115 | * RT_SHACRYPT_MAX_SALT_LEN, both inclusive.
|
---|
116 | *
|
---|
117 | * @param cRounds Number of rounds to use, must be in the range
|
---|
118 | * RT_SHACRYPT_ROUNDS_MIN thru RT_SHACRYPT_ROUNDS_MAX.
|
---|
119 | * If unsure, use RT_SHACRYPT_ROUNDS_DEFAULT. This is
|
---|
120 | * ignored if the salt includes a 'rounds=xxx$' part.
|
---|
121 | * @param pszString Where to store the string on success.
|
---|
122 | * @param cbString The size of the buffer pointed to by @a pszString.
|
---|
123 | *
|
---|
124 | * The minimum length is 3 + salt + 1 + 43 + zero
|
---|
125 | * terminator. If a non-default @a cRounds value is used,
|
---|
126 | * add 8 + strlen(cRounds as decimal). The max buffer
|
---|
127 | * needed is 80 bytes (RT_SHACRYPT_256_MAX_SIZE).
|
---|
128 | */
|
---|
129 | RTDECL(int) RTCrShaCrypt256(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, char *pszString, size_t cbString);
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Encrypts @a pszPhrase using SHA-512, @a pszSalt and @a cRounds.
|
---|
133 | *
|
---|
134 | * @returns IPRT status code.
|
---|
135 | * @param pszPhrase The passphrase (password) to encrypt.
|
---|
136 | * @param pszSalt Salt to use. This can either be a pure salt, like from
|
---|
137 | * RTCrShaCryptGenerateSalt(), or a 'password' string as
|
---|
138 | * generated by RTCrShaCrypt512ToString().
|
---|
139 | *
|
---|
140 | * The latter allows for easy password validation by
|
---|
141 | * comparing the encrypted string with the one stored in
|
---|
142 | * the passwd file.
|
---|
143 | *
|
---|
144 | * The length of the actual salt portion of the string must
|
---|
145 | * be within RT_SHACRYPT_MIN_SALT_LEN to
|
---|
146 | * RT_SHACRYPT_MAX_SALT_LEN, both inclusive.
|
---|
147 | *
|
---|
148 | * @param cRounds Number of rounds to use, must be in the range
|
---|
149 | * RT_SHACRYPT_ROUNDS_MIN thru RT_SHACRYPT_ROUNDS_MAX.
|
---|
150 | * If unsure, use RT_SHACRYPT_ROUNDS_DEFAULT. This is
|
---|
151 | * ignored if the salt includes a 'rounds=xxx$' part.
|
---|
152 | * @param pszString Where to store the string on success.
|
---|
153 | * @param cbString The size of the buffer pointed to by @a pszString.
|
---|
154 | *
|
---|
155 | * The minimum length is 3 + salt + 1 + 86 + zero
|
---|
156 | * terminator. If a non-default @a cRounds value is used,
|
---|
157 | * add 8 + strlen(cRounds as decimal). The max buffer
|
---|
158 | * needed is 123 bytes (RT_SHACRYPT_512_MAX_SIZE).
|
---|
159 | */
|
---|
160 | RTDECL(int) RTCrShaCrypt512(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, char *pszString, size_t cbString);
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Encrypts @a pszPhrase using SHA-256, @a pszSalt and @a cRounds, returning raw
|
---|
166 | * bytes.
|
---|
167 | *
|
---|
168 | * @returns IPRT status code.
|
---|
169 | * @param pszPhrase The passphrase (password) to encrypt.
|
---|
170 | * @param pszSalt Salt to use. This can either be a pure salt, like from
|
---|
171 | * RTCrShaCryptGenerateSalt(), or a 'password' string as
|
---|
172 | * generated by RTCrShaCrypt256ToString().
|
---|
173 | *
|
---|
174 | * The latter allows for easy password validation by
|
---|
175 | * comparing the encrypted string with the one stored in
|
---|
176 | * the passwd file.
|
---|
177 | *
|
---|
178 | * The length of the actual salt portion of the string must
|
---|
179 | * be within RT_SHACRYPT_MIN_SALT_LEN to
|
---|
180 | * RT_SHACRYPT_MAX_SALT_LEN, both inclusive.
|
---|
181 | *
|
---|
182 | * @param cRounds Number of rounds to use, must be in the range
|
---|
183 | * RT_SHACRYPT_ROUNDS_MIN thru RT_SHACRYPT_ROUNDS_MAX.
|
---|
184 | * If unsure, use RT_SHACRYPT_ROUNDS_DEFAULT. This is
|
---|
185 | * ignored if the salt includes a 'rounds=xxx$' part.
|
---|
186 | * @param pabHash Where to return the hash on success.
|
---|
187 | * @see RTCrShaCrypt256, RTCrShaCrypt256ToString
|
---|
188 | */
|
---|
189 | RTDECL(int) RTCrShaCrypt256Ex(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, uint8_t pabHash[RTSHA256_HASH_SIZE]);
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Encrypts @a pszPhrase using SHA-512, @a pszSalt and @a cRounds, returning raw
|
---|
194 | * bytes.
|
---|
195 | *
|
---|
196 | * @returns IPRT status code.
|
---|
197 | * @param pszPhrase The passphrase (password) to encrypt.
|
---|
198 | * @param pszSalt Salt to use. This can either be a pure salt, like from
|
---|
199 | * RTCrShaCryptGenerateSalt(), or a 'password' string as
|
---|
200 | * generated by RTCrShaCrypt512ToString().
|
---|
201 | *
|
---|
202 | * The latter allows for easy password validation by
|
---|
203 | * comparing the encrypted string with the one stored in
|
---|
204 | * the passwd file.
|
---|
205 | *
|
---|
206 | * The length of the actual salt portion of the string must
|
---|
207 | * be within RT_SHACRYPT_MIN_SALT_LEN to
|
---|
208 | * RT_SHACRYPT_MAX_SALT_LEN, both inclusive.
|
---|
209 | *
|
---|
210 | * @param cRounds Number of rounds to use, must be in the range
|
---|
211 | * RT_SHACRYPT_ROUNDS_MIN thru RT_SHACRYPT_ROUNDS_MAX.
|
---|
212 | * If unsure, use RT_SHACRYPT_ROUNDS_DEFAULT. This is
|
---|
213 | * ignored if the salt includes a 'rounds=xxx$' part.
|
---|
214 | * @param pabHash Where to return the hash on success.
|
---|
215 | * @see RTCrShaCrypt512, RTCrShaCrypt512ToString
|
---|
216 | */
|
---|
217 | RTDECL(int) RTCrShaCrypt512Ex(const char *pszPhrase, const char *pszSalt, uint32_t cRounds, uint8_t pabHash[RTSHA512_HASH_SIZE]);
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Formats the RTCrShaCrypt256Ex() result and non-secret inputs using the
|
---|
221 | * extended password format.
|
---|
222 | *
|
---|
223 | * @returns IPRT status code.
|
---|
224 | * @param pabHash The result from RTCrShaCrypt256().
|
---|
225 | * @param pszSalt The salt used when producing @a pabHash.
|
---|
226 | * @param cRounds Number of rounds used for producing @a pabHash.
|
---|
227 | * @param pszString Where to store the string on success.
|
---|
228 | * @param cbString The size of the buffer pointed to by @a pszString.
|
---|
229 | *
|
---|
230 | * The minimum length is 3 + salt + 1 + 43 + zero
|
---|
231 | * terminator. If a non-default @a cRounds value is used,
|
---|
232 | * add 8 + strlen(cRounds as decimal). The max buffer
|
---|
233 | * needed is 80 bytes (RT_SHACRYPT_256_MAX_SIZE).
|
---|
234 | *
|
---|
235 | * @note This implements step 22 of SHA-crypt.txt v0.6.
|
---|
236 | * @see RTCrShaCrypt256Ex
|
---|
237 | */
|
---|
238 | RTDECL(int) RTCrShaCrypt256ToString(uint8_t const pabHash[RTSHA256_HASH_SIZE], const char *pszSalt, uint32_t cRounds,
|
---|
239 | char *pszString, size_t cbString);
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Formats the RTCrShaCrypt512Ex() result and non-secret inputs using the
|
---|
243 | * extended password format.
|
---|
244 | *
|
---|
245 | * @returns IPRT status code.
|
---|
246 | * @param pabHash The result from RTCrShaCrypt512().
|
---|
247 | * @param pszSalt The salt used when producing @a pabHash.
|
---|
248 | * @param cRounds Number of rounds used for producing @a pabHash.
|
---|
249 | * @param pszString Where to store the string on success.
|
---|
250 | * @param cbString The size of the buffer pointed to by @a pszString.
|
---|
251 | *
|
---|
252 | * The minimum length is 3 + salt + 1 + 86 + zero
|
---|
253 | * terminator. If a non-default @a cRounds value is used,
|
---|
254 | * add 8 + strlen(cRounds as decimal). The max buffer
|
---|
255 | * needed is 123 bytes (RT_SHACRYPT_512_MAX_SIZE).
|
---|
256 | *
|
---|
257 | * @note This implements step 22 of SHA-crypt.txt v0.6.
|
---|
258 | * @see RTCrShaCrypt512Ex
|
---|
259 | */
|
---|
260 | RTDECL(int) RTCrShaCrypt512ToString(uint8_t const pabHash[RTSHA512_HASH_SIZE], const char *pszSalt, uint32_t cRounds,
|
---|
261 | char *pszString, size_t cbString);
|
---|
262 |
|
---|
263 | /** @} */
|
---|
264 |
|
---|
265 | RT_C_DECLS_END
|
---|
266 |
|
---|
267 | #endif /* !IPRT_INCLUDED_crypto_shacrypt_h */
|
---|
268 |
|
---|