1 | /** @file
|
---|
2 | * IPRT - SHA1 digest creation
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_sha_h
|
---|
27 | #define ___iprt_sha_h
|
---|
28 |
|
---|
29 | #include <iprt/types.h>
|
---|
30 |
|
---|
31 | RT_C_DECLS_BEGIN
|
---|
32 |
|
---|
33 | /** @defgroup grp_rt_sha RTSha - SHA Family of Hash Functions
|
---|
34 | * @ingroup grp_rt
|
---|
35 | * @{
|
---|
36 | */
|
---|
37 |
|
---|
38 | /** The size of a SHA-1 hash. */
|
---|
39 | #define RTSHA1_HASH_SIZE 20
|
---|
40 | /** The length of a SHA-1 digest string. The terminator is not included. */
|
---|
41 | #define RTSHA1_DIGEST_LEN 40
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * SHA-1 context.
|
---|
45 | */
|
---|
46 | typedef union RTSHA1CONTEXT
|
---|
47 | {
|
---|
48 | uint64_t u64BetterAlignment;
|
---|
49 | uint8_t abPadding[8 + (5 + 80) * 4 + 4];
|
---|
50 | #ifdef RT_SHA1_PRIVATE_CONTEXT
|
---|
51 | SHA_CTX Private;
|
---|
52 | #endif
|
---|
53 | #ifdef RT_SHA1_PRIVATE_ALT_CONTEXT
|
---|
54 | RTSHA1ALTPRIVATECTX AltPrivate;
|
---|
55 | #endif
|
---|
56 | } RTSHA1CONTEXT;
|
---|
57 | /** Pointer to an SHA-1 context. */
|
---|
58 | typedef RTSHA1CONTEXT *PRTSHA1CONTEXT;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Compute the SHA-1 hash of the data.
|
---|
62 | *
|
---|
63 | * @param pvBuf Pointer to the data.
|
---|
64 | * @param cbBuf The amount of data (in bytes).
|
---|
65 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
66 | * the caller's buffer.)
|
---|
67 | */
|
---|
68 | RTDECL(void) RTSha1(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Initializes the SHA-1 context.
|
---|
72 | *
|
---|
73 | * @param pCtx Pointer to the SHA-1 context.
|
---|
74 | */
|
---|
75 | RTDECL(void) RTSha1Init(PRTSHA1CONTEXT pCtx);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Feed data into the SHA-1 computation.
|
---|
79 | *
|
---|
80 | * @param pCtx Pointer to the SHA-1 context.
|
---|
81 | * @param pvBuf Pointer to the data.
|
---|
82 | * @param cbBuf The length of the data (in bytes).
|
---|
83 | */
|
---|
84 | RTDECL(void) RTSha1Update(PRTSHA1CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Compute the SHA-1 hash of the data.
|
---|
88 | *
|
---|
89 | * @param pCtx Pointer to the SHA-1 context.
|
---|
90 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
91 | * the caller's buffer.)
|
---|
92 | */
|
---|
93 | RTDECL(void) RTSha1Final(PRTSHA1CONTEXT pCtx, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Converts a SHA-1 hash to a digest string.
|
---|
97 | *
|
---|
98 | * @returns IPRT status code.
|
---|
99 | *
|
---|
100 | * @param pabDigest The binary digest returned by RTSha1Final or RTSha1.
|
---|
101 | * @param pszDigest Where to return the stringified digest.
|
---|
102 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
103 | * RTSHA1_DIGEST_LEN + 1 bytes.
|
---|
104 | */
|
---|
105 | RTDECL(int) RTSha1ToString(uint8_t const pabDigest[RTSHA1_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Converts a SHA-1 hash to a digest string.
|
---|
109 | *
|
---|
110 | * @returns IPRT status code.
|
---|
111 | *
|
---|
112 | * @param pszDigest The stringified digest. Leading and trailing spaces are
|
---|
113 | * ignored.
|
---|
114 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
115 | * the caller's buffer.)
|
---|
116 | */
|
---|
117 | RTDECL(int) RTSha1FromString(char const *pszDigest, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Creates a SHA1 digest for the given memory buffer.
|
---|
121 | *
|
---|
122 | * @returns iprt status code.
|
---|
123 | *
|
---|
124 | * @param pvBuf Memory buffer to create a SHA1 digest for.
|
---|
125 | * @param cbBuf The amount of data (in bytes).
|
---|
126 | * @param ppszDigest On success the SHA1 digest.
|
---|
127 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
128 | * @param pvUser user defined pointer for the callback
|
---|
129 | */
|
---|
130 | RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Creates a SHA1 digest for the given file.
|
---|
134 | *
|
---|
135 | * @returns iprt status code.
|
---|
136 | *
|
---|
137 | * @param pszFile Filename to create a SHA1 digest for.
|
---|
138 | * @param ppszDigest On success the SHA1 digest.
|
---|
139 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
140 | * @param pvUser user defined pointer for the callback
|
---|
141 | */
|
---|
142 | RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
143 |
|
---|
144 |
|
---|
145 | /** The size of a SHA-256 hash. */
|
---|
146 | #define RTSHA256_HASH_SIZE 32
|
---|
147 | /** The length of a SHA-256 digest string. The terminator is not included. */
|
---|
148 | #define RTSHA256_DIGEST_LEN 64
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * SHA-256 context.
|
---|
152 | */
|
---|
153 | typedef union RTSHA256CONTEXT
|
---|
154 | {
|
---|
155 | uint64_t u64BetterAlignment;
|
---|
156 | uint8_t abPadding[8 + (8 + 80) * 4];
|
---|
157 | #ifdef RT_SHA256_PRIVATE_CONTEXT
|
---|
158 | SHA256_CTX Private;
|
---|
159 | #endif
|
---|
160 | #ifdef RT_SHA256_PRIVATE_ALT_CONTEXT
|
---|
161 | RTSHA256ALTPRIVATECTX AltPrivate;
|
---|
162 | #endif
|
---|
163 | } RTSHA256CONTEXT;
|
---|
164 | /** Pointer to an SHA-256 context. */
|
---|
165 | typedef RTSHA256CONTEXT *PRTSHA256CONTEXT;
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Compute the SHA-256 hash of the data.
|
---|
169 | *
|
---|
170 | * @param pvBuf Pointer to the data.
|
---|
171 | * @param cbBuf The amount of data (in bytes).
|
---|
172 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
173 | * the caller's buffer.)
|
---|
174 | */
|
---|
175 | RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Initializes the SHA-256 context.
|
---|
179 | *
|
---|
180 | * @param pCtx Pointer to the SHA-256 context.
|
---|
181 | */
|
---|
182 | RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx);
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Feed data into the SHA-256 computation.
|
---|
186 | *
|
---|
187 | * @param pCtx Pointer to the SHA-256 context.
|
---|
188 | * @param pvBuf Pointer to the data.
|
---|
189 | * @param cbBuf The length of the data (in bytes).
|
---|
190 | */
|
---|
191 | RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Compute the SHA-256 hash of the data.
|
---|
195 | *
|
---|
196 | * @param pCtx Pointer to the SHA-256 context.
|
---|
197 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
198 | * the caller's buffer.)
|
---|
199 | */
|
---|
200 | RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Converts a SHA-256 hash to a digest string.
|
---|
204 | *
|
---|
205 | * @returns IPRT status code.
|
---|
206 | *
|
---|
207 | * @param pabDigest The binary digest returned by RTSha256Final or RTSha256.
|
---|
208 | * @param pszDigest Where to return the stringified digest.
|
---|
209 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
210 | * RTSHA256_DIGEST_LEN + 1 bytes.
|
---|
211 | */
|
---|
212 | RTDECL(int) RTSha256ToString(uint8_t const pabDigest[RTSHA256_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Converts a SHA-256 hash to a digest string.
|
---|
216 | *
|
---|
217 | * @returns IPRT status code.
|
---|
218 | *
|
---|
219 | * @param pszDigest The stringified digest. Leading and trailing spaces are
|
---|
220 | * ignored.
|
---|
221 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
222 | * the caller's buffer.)
|
---|
223 | */
|
---|
224 | RTDECL(int) RTSha256FromString(char const *pszDigest, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Creates a SHA256 digest for the given memory buffer.
|
---|
228 | *
|
---|
229 | * @returns iprt status code.
|
---|
230 | *
|
---|
231 | * @param pvBuf Memory buffer to create a
|
---|
232 | * SHA256 digest for.
|
---|
233 | * @param cbBuf The amount of data (in bytes).
|
---|
234 | * @param ppszDigest On success the SHA256 digest.
|
---|
235 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
236 | * @param pvUser user defined pointer for the callback
|
---|
237 | */
|
---|
238 | RTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Creates a SHA256 digest for the given file.
|
---|
242 | *
|
---|
243 | * @returns iprt status code.
|
---|
244 | *
|
---|
245 | * @param pszFile Filename to create a SHA256
|
---|
246 | * digest for.
|
---|
247 | * @param ppszDigest On success the SHA256 digest.
|
---|
248 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
249 | * @param pvUser user defined pointer for the callback
|
---|
250 | */
|
---|
251 | RTR3DECL(int) RTSha256DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
252 |
|
---|
253 |
|
---|
254 | /** The size of a SHA-224 hash. */
|
---|
255 | #define RTSHA224_HASH_SIZE 28
|
---|
256 | /** The length of a SHA-224 digest string. The terminator is not included. */
|
---|
257 | #define RTSHA224_DIGEST_LEN 56
|
---|
258 |
|
---|
259 | /** SHA-224 context (same as for SHA-256). */
|
---|
260 | typedef RTSHA256CONTEXT RTSHA224CONTEXT;
|
---|
261 | /** Pointer to an SHA-224 context. */
|
---|
262 | typedef RTSHA256CONTEXT *PRTSHA224CONTEXT;
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Compute the SHA-224 hash of the data.
|
---|
266 | *
|
---|
267 | * @param pvBuf Pointer to the data.
|
---|
268 | * @param cbBuf The amount of data (in bytes).
|
---|
269 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
270 | * the caller's buffer.)
|
---|
271 | */
|
---|
272 | RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE]);
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Initializes the SHA-224 context.
|
---|
276 | *
|
---|
277 | * @param pCtx Pointer to the SHA-224 context.
|
---|
278 | */
|
---|
279 | RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx);
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Feed data into the SHA-224 computation.
|
---|
283 | *
|
---|
284 | * @param pCtx Pointer to the SHA-224 context.
|
---|
285 | * @param pvBuf Pointer to the data.
|
---|
286 | * @param cbBuf The length of the data (in bytes).
|
---|
287 | */
|
---|
288 | RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Compute the SHA-224 hash of the data.
|
---|
292 | *
|
---|
293 | * @param pCtx Pointer to the SHA-224 context.
|
---|
294 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
295 | * the caller's buffer.)
|
---|
296 | */
|
---|
297 | RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE]);
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Converts a SHA-224 hash to a digest string.
|
---|
301 | *
|
---|
302 | * @returns IPRT status code.
|
---|
303 | *
|
---|
304 | * @param pabDigest The binary digest returned by RTSha224Final or RTSha224.
|
---|
305 | * @param pszDigest Where to return the stringified digest.
|
---|
306 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
307 | * RTSHA224_DIGEST_LEN + 1 bytes.
|
---|
308 | */
|
---|
309 | RTDECL(int) RTSha224ToString(uint8_t const pabDigest[RTSHA224_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Converts a SHA-224 hash to a digest string.
|
---|
313 | *
|
---|
314 | * @returns IPRT status code.
|
---|
315 | *
|
---|
316 | * @param pszDigest The stringified digest. Leading and trailing spaces are
|
---|
317 | * ignored.
|
---|
318 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
319 | * the caller's buffer.)
|
---|
320 | */
|
---|
321 | RTDECL(int) RTSha224FromString(char const *pszDigest, uint8_t pabDigest[RTSHA224_HASH_SIZE]);
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Creates a SHA224 digest for the given memory buffer.
|
---|
325 | *
|
---|
326 | * @returns iprt status code.
|
---|
327 | *
|
---|
328 | * @param pvBuf Memory buffer to create a SHA224 digest for.
|
---|
329 | * @param cbBuf The amount of data (in bytes).
|
---|
330 | * @param ppszDigest On success the SHA224 digest.
|
---|
331 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
332 | * @param pvUser user defined pointer for the callback
|
---|
333 | */
|
---|
334 | RTR3DECL(int) RTSha224Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Creates a SHA224 digest for the given file.
|
---|
338 | *
|
---|
339 | * @returns iprt status code.
|
---|
340 | *
|
---|
341 | * @param pszFile Filename to create a SHA224 digest for.
|
---|
342 | * @param ppszDigest On success the SHA224 digest.
|
---|
343 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
344 | * @param pvUser user defined pointer for the callback
|
---|
345 | */
|
---|
346 | RTR3DECL(int) RTSha224DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
347 |
|
---|
348 |
|
---|
349 | /** The size of a SHA-512 hash. */
|
---|
350 | #define RTSHA512_HASH_SIZE 64
|
---|
351 | /** The length of a SHA-512 digest string. The terminator is not included. */
|
---|
352 | #define RTSHA512_DIGEST_LEN 128
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * SHA-512 context.
|
---|
356 | */
|
---|
357 | typedef union RTSHA512CONTEXT
|
---|
358 | {
|
---|
359 | uint64_t u64BetterAlignment;
|
---|
360 | uint8_t abPadding[ARCH_BITS == 32 ? 216 : 256];
|
---|
361 | #ifdef RT_SHA512_PRIVATE_CONTEXT
|
---|
362 | SHA512_CTX Private;
|
---|
363 | #endif
|
---|
364 | } RTSHA512CONTEXT;
|
---|
365 | /** Pointer to an SHA-512 context. */
|
---|
366 | typedef RTSHA512CONTEXT *PRTSHA512CONTEXT;
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Compute the SHA-512 hash of the data.
|
---|
370 | *
|
---|
371 | * @param pvBuf Pointer to the data.
|
---|
372 | * @param cbBuf The amount of data (in bytes).
|
---|
373 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
374 | * the caller's buffer.)
|
---|
375 | */
|
---|
376 | RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Initializes the SHA-512 context.
|
---|
380 | *
|
---|
381 | * @param pCtx Pointer to the SHA-512 context.
|
---|
382 | */
|
---|
383 | RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx);
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Feed data into the SHA-512 computation.
|
---|
387 | *
|
---|
388 | * @param pCtx Pointer to the SHA-512 context.
|
---|
389 | * @param pvBuf Pointer to the data.
|
---|
390 | * @param cbBuf The length of the data (in bytes).
|
---|
391 | */
|
---|
392 | RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Compute the SHA-512 hash of the data.
|
---|
396 | *
|
---|
397 | * @param pCtx Pointer to the SHA-512 context.
|
---|
398 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
399 | * the caller's buffer.)
|
---|
400 | */
|
---|
401 | RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Converts a SHA-512 hash to a digest string.
|
---|
405 | *
|
---|
406 | * @returns IPRT status code.
|
---|
407 | *
|
---|
408 | * @param pabDigest The binary digest returned by RTSha512Final or RTSha512.
|
---|
409 | * @param pszDigest Where to return the stringified digest.
|
---|
410 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
411 | * RTSHA512_DIGEST_LEN + 1 bytes.
|
---|
412 | */
|
---|
413 | RTDECL(int) RTSha512ToString(uint8_t const pabDigest[RTSHA512_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Converts a SHA-512 hash to a digest string.
|
---|
417 | *
|
---|
418 | * @returns IPRT status code.
|
---|
419 | *
|
---|
420 | * @param pszDigest The stringified digest. Leading and trailing spaces are
|
---|
421 | * ignored.
|
---|
422 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
423 | * the caller's buffer.)
|
---|
424 | */
|
---|
425 | RTDECL(int) RTSha512FromString(char const *pszDigest, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
426 |
|
---|
427 | /** @} */
|
---|
428 |
|
---|
429 | RT_C_DECLS_END
|
---|
430 |
|
---|
431 | #endif /* ___iprt_sha1_h */
|
---|
432 |
|
---|