VirtualBox

source: vbox/trunk/include/iprt/sha.h@ 56291

Last change on this file since 56291 was 56291, checked in by vboxsync, 9 years ago

include: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/** @file
2 * IPRT - SHA1 digest creation
3 */
4
5/*
6 * Copyright (C) 2009-2015 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
31RT_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 */
46typedef 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. */
58typedef 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 */
68RTDECL(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 */
75RTDECL(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 */
84RTDECL(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 */
93RTDECL(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 */
105RTDECL(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 */
117RTDECL(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 */
130RTR3DECL(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 */
142RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
143
144
145
146/** The size of a SHA-256 hash. */
147#define RTSHA256_HASH_SIZE 32
148/** The length of a SHA-256 digest string. The terminator is not included. */
149#define RTSHA256_DIGEST_LEN 64
150
151/**
152 * SHA-256 context.
153 */
154typedef union RTSHA256CONTEXT
155{
156 uint64_t u64BetterAlignment;
157 uint8_t abPadding[8 + (8 + 80) * 4];
158#ifdef RT_SHA256_PRIVATE_CONTEXT
159 SHA256_CTX Private;
160#endif
161#ifdef RT_SHA256_PRIVATE_ALT_CONTEXT
162 RTSHA256ALTPRIVATECTX AltPrivate;
163#endif
164} RTSHA256CONTEXT;
165/** Pointer to an SHA-256 context. */
166typedef RTSHA256CONTEXT *PRTSHA256CONTEXT;
167
168/**
169 * Compute the SHA-256 hash of the data.
170 *
171 * @param pvBuf Pointer to the data.
172 * @param cbBuf The amount of data (in bytes).
173 * @param pabDigest Where to store the hash. (What is passed is a pointer to
174 * the caller's buffer.)
175 */
176RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
177
178/**
179 * Initializes the SHA-256 context.
180 *
181 * @param pCtx Pointer to the SHA-256 context.
182 */
183RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx);
184
185/**
186 * Feed data into the SHA-256 computation.
187 *
188 * @param pCtx Pointer to the SHA-256 context.
189 * @param pvBuf Pointer to the data.
190 * @param cbBuf The length of the data (in bytes).
191 */
192RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
193
194/**
195 * Compute the SHA-256 hash of the data.
196 *
197 * @param pCtx Pointer to the SHA-256 context.
198 * @param pabDigest Where to store the hash. (What is passed is a pointer to
199 * the caller's buffer.)
200 */
201RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
202
203/**
204 * Converts a SHA-256 hash to a digest string.
205 *
206 * @returns IPRT status code.
207 *
208 * @param pabDigest The binary digest returned by RTSha256Final or RTSha256.
209 * @param pszDigest Where to return the stringified digest.
210 * @param cchDigest The size of the output buffer. Should be at least
211 * RTSHA256_DIGEST_LEN + 1 bytes.
212 */
213RTDECL(int) RTSha256ToString(uint8_t const pabDigest[RTSHA256_HASH_SIZE], char *pszDigest, size_t cchDigest);
214
215/**
216 * Converts a SHA-256 hash to a digest string.
217 *
218 * @returns IPRT status code.
219 *
220 * @param pszDigest The stringified digest. Leading and trailing spaces are
221 * ignored.
222 * @param pabDigest Where to store the hash. (What is passed is a pointer to
223 * the caller's buffer.)
224 */
225RTDECL(int) RTSha256FromString(char const *pszDigest, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
226
227/**
228 * Creates a SHA256 digest for the given memory buffer.
229 *
230 * @returns iprt status code.
231 *
232 * @param pvBuf Memory buffer to create a
233 * SHA256 digest for.
234 * @param cbBuf The amount of data (in bytes).
235 * @param ppszDigest On success the SHA256 digest.
236 * @param pfnProgressCallback optional callback for the progress indication
237 * @param pvUser user defined pointer for the callback
238 */
239RTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
240
241/**
242 * Creates a SHA256 digest for the given file.
243 *
244 * @returns iprt status code.
245 *
246 * @param pszFile Filename to create a SHA256
247 * digest for.
248 * @param ppszDigest On success the SHA256 digest.
249 * @param pfnProgressCallback optional callback for the progress indication
250 * @param pvUser user defined pointer for the callback
251 */
252RTR3DECL(int) RTSha256DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
253
254
255
256/** The size of a SHA-224 hash. */
257#define RTSHA224_HASH_SIZE 28
258/** The length of a SHA-224 digest string. The terminator is not included. */
259#define RTSHA224_DIGEST_LEN 56
260
261/** SHA-224 context (same as for SHA-256). */
262typedef RTSHA256CONTEXT RTSHA224CONTEXT;
263/** Pointer to an SHA-224 context. */
264typedef RTSHA256CONTEXT *PRTSHA224CONTEXT;
265
266/**
267 * Compute the SHA-224 hash of the data.
268 *
269 * @param pvBuf Pointer to the data.
270 * @param cbBuf The amount of data (in bytes).
271 * @param pabDigest Where to store the hash. (What is passed is a pointer to
272 * the caller's buffer.)
273 */
274RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE]);
275
276/**
277 * Initializes the SHA-224 context.
278 *
279 * @param pCtx Pointer to the SHA-224 context.
280 */
281RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx);
282
283/**
284 * Feed data into the SHA-224 computation.
285 *
286 * @param pCtx Pointer to the SHA-224 context.
287 * @param pvBuf Pointer to the data.
288 * @param cbBuf The length of the data (in bytes).
289 */
290RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
291
292/**
293 * Compute the SHA-224 hash of the data.
294 *
295 * @param pCtx Pointer to the SHA-224 context.
296 * @param pabDigest Where to store the hash. (What is passed is a pointer to
297 * the caller's buffer.)
298 */
299RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE]);
300
301/**
302 * Converts a SHA-224 hash to a digest string.
303 *
304 * @returns IPRT status code.
305 *
306 * @param pabDigest The binary digest returned by RTSha224Final or RTSha224.
307 * @param pszDigest Where to return the stringified digest.
308 * @param cchDigest The size of the output buffer. Should be at least
309 * RTSHA224_DIGEST_LEN + 1 bytes.
310 */
311RTDECL(int) RTSha224ToString(uint8_t const pabDigest[RTSHA224_HASH_SIZE], char *pszDigest, size_t cchDigest);
312
313/**
314 * Converts a SHA-224 hash to a digest string.
315 *
316 * @returns IPRT status code.
317 *
318 * @param pszDigest The stringified digest. Leading and trailing spaces are
319 * ignored.
320 * @param pabDigest Where to store the hash. (What is passed is a pointer to
321 * the caller's buffer.)
322 */
323RTDECL(int) RTSha224FromString(char const *pszDigest, uint8_t pabDigest[RTSHA224_HASH_SIZE]);
324
325/**
326 * Creates a SHA224 digest for the given memory buffer.
327 *
328 * @returns iprt status code.
329 *
330 * @param pvBuf Memory buffer to create a SHA224 digest for.
331 * @param cbBuf The amount of data (in bytes).
332 * @param ppszDigest On success the SHA224 digest.
333 * @param pfnProgressCallback optional callback for the progress indication
334 * @param pvUser user defined pointer for the callback
335 */
336RTR3DECL(int) RTSha224Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
337
338/**
339 * Creates a SHA224 digest for the given file.
340 *
341 * @returns iprt status code.
342 *
343 * @param pszFile Filename to create a SHA224 digest for.
344 * @param ppszDigest On success the SHA224 digest.
345 * @param pfnProgressCallback optional callback for the progress indication
346 * @param pvUser user defined pointer for the callback
347 */
348RTR3DECL(int) RTSha224DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
349
350
351
352/** The size of a SHA-512 hash. */
353#define RTSHA512_HASH_SIZE 64
354/** The length of a SHA-512 digest string. The terminator is not included. */
355#define RTSHA512_DIGEST_LEN 128
356
357/**
358 * SHA-512 context.
359 */
360typedef union RTSHA512CONTEXT
361{
362 uint64_t u64BetterAlignment;
363 uint8_t abPadding[16 + (80 + 8) * 8];
364#ifdef RT_SHA512_PRIVATE_CONTEXT
365 SHA512_CTX Private;
366#endif
367#ifdef RT_SHA512_PRIVATE_ALT_CONTEXT
368 RTSHA512ALTPRIVATECTX AltPrivate;
369#endif
370} RTSHA512CONTEXT;
371/** Pointer to an SHA-512 context. */
372typedef RTSHA512CONTEXT *PRTSHA512CONTEXT;
373
374/**
375 * Compute the SHA-512 hash of the data.
376 *
377 * @param pvBuf Pointer to the data.
378 * @param cbBuf The amount of data (in bytes).
379 * @param pabDigest Where to store the hash. (What is passed is a pointer to
380 * the caller's buffer.)
381 */
382RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
383
384/**
385 * Initializes the SHA-512 context.
386 *
387 * @param pCtx Pointer to the SHA-512 context.
388 */
389RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx);
390
391/**
392 * Feed data into the SHA-512 computation.
393 *
394 * @param pCtx Pointer to the SHA-512 context.
395 * @param pvBuf Pointer to the data.
396 * @param cbBuf The length of the data (in bytes).
397 */
398RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
399
400/**
401 * Compute the SHA-512 hash of the data.
402 *
403 * @param pCtx Pointer to the SHA-512 context.
404 * @param pabDigest Where to store the hash. (What is passed is a pointer to
405 * the caller's buffer.)
406 */
407RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
408
409/**
410 * Converts a SHA-512 hash to a digest string.
411 *
412 * @returns IPRT status code.
413 *
414 * @param pabDigest The binary digest returned by RTSha512Final or RTSha512.
415 * @param pszDigest Where to return the stringified digest.
416 * @param cchDigest The size of the output buffer. Should be at least
417 * RTSHA512_DIGEST_LEN + 1 bytes.
418 */
419RTDECL(int) RTSha512ToString(uint8_t const pabDigest[RTSHA512_HASH_SIZE], char *pszDigest, size_t cchDigest);
420
421/**
422 * Converts a SHA-512 hash to a digest string.
423 *
424 * @returns IPRT status code.
425 *
426 * @param pszDigest The stringified digest. Leading and trailing spaces are
427 * ignored.
428 * @param pabDigest Where to store the hash. (What is passed is a pointer to
429 * the caller's buffer.)
430 */
431RTDECL(int) RTSha512FromString(char const *pszDigest, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
432
433
434/** Macro for declaring the interface for a SHA-512 variation.
435 * @internal */
436#define RTSHA512_DECLARE_VARIANT(a_Name, a_UName) \
437 typedef RTSHA512CONTEXT RT_CONCAT3(RTSHA,a_UName,CONTEXT); \
438 typedef RTSHA512CONTEXT *RT_CONCAT3(PRTSHA,a_UName,CONTEXT); \
439 RTDECL(void) RT_CONCAT(RTSha,a_Name)(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RT_CONCAT3(RTSHA,a_UName,_HASH_SIZE)]); \
440 RTDECL(void) RT_CONCAT3(RTSha,a_Name,Init)(RT_CONCAT3(PRTSHA,a_UName,CONTEXT) pCtx); \
441 RTDECL(void) RT_CONCAT3(RTSha,a_Name,Update)(RT_CONCAT3(PRTSHA,a_UName,CONTEXT) pCtx, const void *pvBuf, size_t cbBuf); \
442 RTDECL(void) RT_CONCAT3(RTSha,a_Name,Final)(RT_CONCAT3(PRTSHA,a_UName,CONTEXT) pCtx, uint8_t pabDigest[RT_CONCAT3(RTSHA,a_UName,_HASH_SIZE)]); \
443 RTDECL(int) RT_CONCAT3(RTSha,a_Name,ToString)(uint8_t const pabDigest[RT_CONCAT3(RTSHA,a_UName,_HASH_SIZE)], char *pszDigest, size_t cchDigest); \
444 RTDECL(int) RT_CONCAT3(RTSha,a_Name,FromString)(char const *pszDigest, uint8_t pabDigest[RT_CONCAT3(RTSHA,a_UName,_HASH_SIZE)])
445
446
447/** The size of a SHA-384 hash. */
448#define RTSHA384_HASH_SIZE 48
449/** The length of a SHA-384 digest string. The terminator is not included. */
450#define RTSHA384_DIGEST_LEN 96
451RTSHA512_DECLARE_VARIANT(384,384);
452
453/** The size of a SHA-512/224 hash. */
454#define RTSHA512T224_HASH_SIZE 28
455/** The length of a SHA-512/224 digest string. The terminator is not
456 * included. */
457#define RTSHA512T224_DIGEST_LEN 56
458RTSHA512_DECLARE_VARIANT(512t224,512T224);
459
460/** The size of a SHA-512/256 hash. */
461#define RTSHA512T256_HASH_SIZE 32
462/** The length of a SHA-512/256 digest string. The terminator is not
463 * included. */
464#define RTSHA512T256_DIGEST_LEN 64
465RTSHA512_DECLARE_VARIANT(512t256,512T256);
466
467
468/** @} */
469
470RT_C_DECLS_END
471
472#endif
473
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette