VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/digest-core.cpp@ 58171

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

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: digest-core.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - Cryptographic Hash / Message Digest API
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/crypto/digest.h>
33
34#include <iprt/asm.h>
35#include <iprt/err.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43/**
44 * Generic message digest instance.
45 */
46typedef struct RTCRDIGESTINT
47{
48 /** Magic value (RTCRDIGESTINT_MAGIC). */
49 uint32_t u32Magic;
50 /** Reference counter. */
51 uint32_t volatile cRefs;
52 /** Pointer to the message digest descriptor. */
53 PCRTCRDIGESTDESC pDesc;
54 /** The offset into abState of the storage space . At
55 * least RTCRDIGESTDESC::cbHash bytes is available at that location. */
56 uint32_t offHash;
57 /** State. */
58 uint32_t uState;
59 /** The number of bytes consumed. */
60 uint64_t cbConsumed;
61 /** Opaque data specific to the message digest algorithm, size given by
62 * RTCRDIGESTDESC::cbState. This is followed by space for the final hash at
63 * offHash with size RTCRDIGESTDESC::cbHash. */
64 uint8_t abState[1];
65} RTCRDIGESTINT;
66/** Pointer to a message digest instance. */
67typedef RTCRDIGESTINT *PRTCRDIGESTINT;
68
69/** Magic value for RTCRDIGESTINT::u32Magic (Ralph C. Merkle). */
70#define RTCRDIGESTINT_MAGIC UINT32_C(0x19520202)
71
72/** @name RTCRDIGESTINT::uState values.
73 * @{ */
74/** Ready for more data. */
75#define RTCRDIGEST_STATE_READY UINT32_C(1)
76/** The hash has been finalized and can be found at offHash. */
77#define RTCRDIGEST_STATE_FINAL UINT32_C(2)
78/** Busted state, can happen after re-init. */
79#define RTCRDIGEST_STATE_BUSTED UINT32_C(3)
80/** @} */
81
82
83
84RTDECL(int) RTCrDigestCreate(PRTCRDIGEST phDigest, PCRTCRDIGESTDESC pDesc, void *pvOpaque)
85{
86 AssertPtrReturn(phDigest, VERR_INVALID_POINTER);
87 AssertPtrReturn(pDesc, VERR_INVALID_POINTER);
88
89 int rc = VINF_SUCCESS;
90 uint32_t offHash = RT_ALIGN_32(pDesc->cbState, 8);
91 PRTCRDIGESTINT pThis = (PRTCRDIGESTINT)RTMemAllocZ(RT_OFFSETOF(RTCRDIGESTINT, abState[offHash + pDesc->cbHash]));
92 if (pThis)
93 {
94 pThis->u32Magic = RTCRDIGESTINT_MAGIC;
95 pThis->cRefs = 1;
96 pThis->offHash = offHash;
97 pThis->pDesc = pDesc;
98 pThis->uState = RTCRDIGEST_STATE_READY;
99 if (pDesc->pfnInit)
100 rc = pDesc->pfnInit(pThis->abState, pvOpaque, false /*fReInit*/);
101 if (RT_SUCCESS(rc))
102 {
103 *phDigest = pThis;
104 return VINF_SUCCESS;
105 }
106 pThis->u32Magic = 0;
107 RTMemFree(pThis);
108 }
109 else
110 rc = VERR_NO_MEMORY;
111 return rc;
112}
113
114
115RTDECL(int) RTCrDigestClone(PRTCRDIGEST phDigest, RTCRDIGEST hSrc)
116{
117 AssertPtrReturn(phDigest, VERR_INVALID_POINTER);
118 AssertPtrReturn(hSrc, VERR_INVALID_HANDLE);
119 AssertReturn(hSrc->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
120
121 int rc = VINF_SUCCESS;
122 uint32_t const offHash = hSrc->offHash;
123 PRTCRDIGESTINT pThis = (PRTCRDIGESTINT)RTMemAllocZ(RT_OFFSETOF(RTCRDIGESTINT, abState[offHash + hSrc->pDesc->cbHash]));
124 if (pThis)
125 {
126 pThis->u32Magic = RTCRDIGESTINT_MAGIC;
127 pThis->cRefs = 1;
128 pThis->offHash = offHash;
129 pThis->pDesc = hSrc->pDesc;
130 if (hSrc->pDesc->pfnClone)
131 rc = hSrc->pDesc->pfnClone(pThis->abState, hSrc->abState);
132 else
133 memcpy(pThis->abState, hSrc->abState, offHash);
134 memcpy(&pThis->abState[offHash], &hSrc->abState[offHash], hSrc->pDesc->cbHash);
135 pThis->uState = hSrc->uState;
136
137 if (RT_SUCCESS(rc))
138 {
139 *phDigest = pThis;
140 return VINF_SUCCESS;
141 }
142 pThis->u32Magic = 0;
143 RTMemFree(pThis);
144 }
145 else
146 rc = VERR_NO_MEMORY;
147 return rc;
148}
149
150
151RTDECL(int) RTCrDigestReset(RTCRDIGEST hDigest)
152{
153 PRTCRDIGESTINT pThis = hDigest;
154 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
155 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
156
157 pThis->cbConsumed = 0;
158 pThis->uState = RTCRDIGEST_STATE_READY;
159
160 int rc = VINF_SUCCESS;
161 if (pThis->pDesc->pfnInit)
162 {
163 rc = pThis->pDesc->pfnInit(pThis->abState, NULL, true /*fReInit*/);
164 if (RT_FAILURE(rc))
165 pThis->uState = RTCRDIGEST_STATE_BUSTED;
166 RT_BZERO(&pThis->abState[pThis->offHash], pThis->pDesc->cbHash);
167 }
168 else
169 RT_BZERO(pThis->abState, pThis->offHash + pThis->pDesc->cbHash);
170 return rc;
171}
172
173
174RTDECL(uint32_t) RTCrDigestRetain(RTCRDIGEST hDigest)
175{
176 PRTCRDIGESTINT pThis = hDigest;
177 AssertPtrReturn(pThis, UINT32_MAX);
178 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, UINT32_MAX);
179
180 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
181 Assert(cRefs < 64);
182 return cRefs;
183}
184
185
186RTDECL(uint32_t) RTCrDigestRelease(RTCRDIGEST hDigest)
187{
188 PRTCRDIGESTINT pThis = hDigest;
189 if (pThis == NIL_RTCRDIGEST)
190 return 0;
191 AssertPtrReturn(pThis, UINT32_MAX);
192 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, UINT32_MAX);
193
194 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
195 if (!cRefs)
196 {
197 pThis->u32Magic = ~RTCRDIGESTINT_MAGIC;
198 if (pThis->pDesc->pfnDelete)
199 pThis->pDesc->pfnDelete(pThis->abState);
200 RTMemFree(pThis);
201 }
202 Assert(cRefs < 64);
203 return cRefs;
204}
205
206
207RTDECL(int) RTCrDigestUpdate(RTCRDIGEST hDigest, void const *pvData, size_t cbData)
208{
209 PRTCRDIGESTINT pThis = hDigest;
210 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
211 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
212 AssertReturn(pThis->uState == RTCRDIGEST_STATE_READY, VERR_INVALID_STATE);
213
214 pThis->pDesc->pfnUpdate(pThis->abState, pvData, cbData);
215 pThis->cbConsumed += cbData;
216 return VINF_SUCCESS;
217}
218
219
220RTDECL(int) RTCrDigestFinal(RTCRDIGEST hDigest, void *pvHash, size_t cbHash)
221{
222 PRTCRDIGESTINT pThis = hDigest;
223 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
224 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
225 AssertReturn(pThis->uState == RTCRDIGEST_STATE_READY || pThis->uState == RTCRDIGEST_STATE_FINAL, VERR_INVALID_STATE);
226 AssertPtrNullReturn(pvHash, VERR_INVALID_POINTER);
227
228 /*
229 * Make sure the hash calculation is final.
230 */
231 if (pThis->uState == RTCRDIGEST_STATE_READY)
232 {
233 pThis->pDesc->pfnFinal(pThis->abState, &pThis->abState[pThis->offHash]);
234 pThis->uState = RTCRDIGEST_STATE_FINAL;
235 }
236 else
237 AssertReturn(pThis->uState == RTCRDIGEST_STATE_FINAL, VERR_INVALID_STATE);
238
239 /*
240 * Copy out the hash if requested.
241 */
242 if (cbHash > 0)
243 {
244 uint32_t cbNeeded = pThis->pDesc->cbHash;
245 if (pThis->pDesc->pfnGetHashSize)
246 cbNeeded = pThis->pDesc->pfnGetHashSize(pThis->abState);
247 Assert(cbNeeded > 0);
248
249 if (cbNeeded == cbHash)
250 memcpy(pvHash, &pThis->abState[pThis->offHash], cbNeeded);
251 else if (cbNeeded > cbHash)
252 {
253 memcpy(pvHash, &pThis->abState[pThis->offHash], cbNeeded);
254 memset((uint8_t *)pvHash + cbNeeded, 0, cbHash - cbNeeded);
255 return VINF_BUFFER_UNDERFLOW;
256 }
257 else
258 {
259 memcpy(pvHash, &pThis->abState[pThis->offHash], cbHash);
260 return VERR_BUFFER_OVERFLOW;
261 }
262 }
263
264 return VINF_SUCCESS;
265}
266
267
268RTDECL(bool) RTCrDigestMatch(RTCRDIGEST hDigest, void const *pvHash, size_t cbHash)
269{
270 PRTCRDIGESTINT pThis = hDigest;
271
272 int rc = RTCrDigestFinal(pThis, NULL, 0);
273 AssertRCReturn(rc, false);
274
275 AssertPtrReturn(pvHash, false);
276 return pThis->pDesc->cbHash == cbHash
277 && !memcmp(&pThis->abState[pThis->offHash], pvHash, cbHash);
278}
279
280
281RTDECL(uint8_t const *) RTCrDigestGetHash(RTCRDIGEST hDigest)
282{
283 PRTCRDIGESTINT pThis = hDigest;
284 AssertPtrReturn(pThis, NULL);
285 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, NULL);
286
287 int rc = RTCrDigestFinal(pThis, NULL, 0);
288 AssertRCReturn(rc, NULL);
289
290 return &pThis->abState[pThis->offHash];
291}
292
293
294RTDECL(uint32_t) RTCrDigestGetHashSize(RTCRDIGEST hDigest)
295{
296 PRTCRDIGESTINT pThis = hDigest;
297 AssertPtrReturn(pThis, 0);
298 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, 0);
299 if (pThis->pDesc->pfnGetHashSize)
300 {
301 uint32_t cbHash = pThis->pDesc->pfnGetHashSize(pThis->abState);
302 Assert(cbHash <= pThis->pDesc->cbHash);
303 return cbHash;
304 }
305 return pThis->pDesc->cbHash;
306}
307
308
309RTDECL(uint64_t) RTCrDigestGetConsumedSize(RTCRDIGEST hDigest)
310{
311 PRTCRDIGESTINT pThis = hDigest;
312 AssertPtrReturn(pThis, 0);
313 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, 0);
314 return pThis->cbConsumed;
315}
316
317
318RTDECL(bool) RTCrDigestIsFinalized(RTCRDIGEST hDigest)
319{
320 PRTCRDIGESTINT pThis = hDigest;
321 AssertPtrReturn(pThis, false);
322 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, false);
323 return pThis->uState == RTCRDIGEST_STATE_FINAL;
324}
325
326
327RTDECL(RTDIGESTTYPE) RTCrDigestGetType(RTCRDIGEST hDigest)
328{
329 PRTCRDIGESTINT pThis = hDigest;
330 AssertPtrReturn(pThis, RTDIGESTTYPE_INVALID);
331 AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, RTDIGESTTYPE_INVALID);
332
333 RTDIGESTTYPE enmType = pThis->pDesc->enmType;
334 if (pThis->pDesc->pfnGetDigestType)
335 enmType = pThis->pDesc->pfnGetDigestType(pThis->abState);
336 return enmType;
337}
338
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