13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - Crypto - Cryptographic Hash / Message Digest API
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2006-2014 Oracle Corporation
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * available from http://www.virtualbox.org. This file is free software;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * General Public License (GPL) as published by the Free Software
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * The contents of this file may alternatively be used under the terms
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * of the Common Development and Distribution License Version 1.0
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * VirtualBox OSE distribution, in which case the provisions of the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * CDDL are applicable instead of those of the GPL.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * You may elect to license modified versions of this file under the
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * terms and conditions of either the GPL or the CDDL or both.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Structures and Typedefs *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Generic message digest instance.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Magic value (RTCRDIGESTINT_MAGIC). */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Reference counter. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Pointer to the message digest descriptor. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The offset into abState of the storage space . At
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * least RTCRDIGESTDESC::cbHash bytes is available at that location. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** State. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The number of bytes consumed. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** Opaque data specific to the message digest algorithm, size given by
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * RTCRDIGESTDESC::cbState. This is followed by space for the final hash at
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * offHash with size RTCRDIGESTDESC::cbHash. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Pointer to a message digest instance. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Magic value for RTCRDIGESTINT::u32Magic (Ralph C. Merkle). */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @name RTCRDIGESTINT::uState values.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Ready for more data. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The hash has been finalized and can be found at offHash. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Busted state, can happen after re-init. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTCrDigestCreate(PRTCRDIGEST phDigest, PCRTCRDIGESTDESC pDesc, void *pvOpaque)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PRTCRDIGESTINT pThis = (PRTCRDIGESTINT)RTMemAllocZ(RT_OFFSETOF(RTCRDIGESTINT, abState[offHash + pDesc->cbHash]));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = pDesc->pfnInit(pThis->abState, pvOpaque, false /*fReInit*/);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTCrDigestClone(PRTCRDIGEST phDigest, RTCRDIGEST hSrc)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(hSrc->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync PRTCRDIGESTINT pThis = (PRTCRDIGESTINT)RTMemAllocZ(RT_OFFSETOF(RTCRDIGESTINT, abState[offHash + hSrc->pDesc->cbHash]));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = hSrc->pDesc->pfnClone(pThis->abState, hSrc->abState);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync memcpy(&pThis->abState[offHash], &hSrc->abState[offHash], hSrc->pDesc->cbHash);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync rc = pThis->pDesc->pfnInit(pThis->abState, NULL, true /*fReInit*/);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_BZERO(&pThis->abState[pThis->offHash], pThis->pDesc->cbHash);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RT_BZERO(pThis->abState, pThis->offHash + pThis->pDesc->cbHash);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint32_t) RTCrDigestRetain(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, UINT32_MAX);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint32_t) RTCrDigestRelease(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, UINT32_MAX);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTCrDigestUpdate(RTCRDIGEST hDigest, void const *pvData, size_t cbData)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->uState == RTCRDIGEST_STATE_READY, VERR_INVALID_STATE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->pDesc->pfnUpdate(pThis->abState, pvData, cbData);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(int) RTCrDigestFinal(RTCRDIGEST hDigest, void *pvHash, size_t cbHash)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, VERR_INVALID_HANDLE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->uState == RTCRDIGEST_STATE_READY || pThis->uState == RTCRDIGEST_STATE_FINAL, VERR_INVALID_STATE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Make sure the hash calculation is final.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pThis->pDesc->pfnFinal(pThis->abState, &pThis->abState[pThis->offHash]);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->uState == RTCRDIGEST_STATE_FINAL, VERR_INVALID_STATE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copy out the hash if requested.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync cbNeeded = pThis->pDesc->pfnGetHashSize(pThis->abState);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync memcpy(pvHash, &pThis->abState[pThis->offHash], cbNeeded);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync memcpy(pvHash, &pThis->abState[pThis->offHash], cbNeeded);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync memset((uint8_t *)pvHash + cbNeeded, 0, cbHash - cbNeeded);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync memcpy(pvHash, &pThis->abState[pThis->offHash], cbHash);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(bool) RTCrDigestMatch(RTCRDIGEST hDigest, void const *pvHash, size_t cbHash)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync && !memcmp(&pThis->abState[pThis->offHash], pvHash, cbHash);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint8_t const *) RTCrDigestGetHash(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, NULL);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint32_t) RTCrDigestGetHashSize(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, 0);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t cbHash = pThis->pDesc->pfnGetHashSize(pThis->abState);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(uint64_t) RTCrDigestGetConsumedSize(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, 0);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(bool) RTCrDigestIsFinalized(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, false);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(RTDIGESTTYPE) RTCrDigestGetType(RTCRDIGEST hDigest)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync AssertReturn(pThis->u32Magic == RTCRDIGESTINT_MAGIC, RTDIGESTTYPE_INVALID);