cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync/** @file
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * IPRT - SHA256 digest creation
ed6697871be690c5826c252d598ea9965c2816b4vboxsync *
ed6697871be690c5826c252d598ea9965c2816b4vboxsync * @todo Replace this with generic RTCrDigest based implementation. Too much
ed6697871be690c5826c252d598ea9965c2816b4vboxsync * stupid code duplication.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync/*
ed6697871be690c5826c252d598ea9965c2816b4vboxsync * Copyright (C) 2009-2014 Oracle Corporation
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * available from http://www.virtualbox.org. This file is free software;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * you can redistribute it and/or modify it under the terms of the GNU
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * General Public License (GPL) as published by the Free Software
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * The contents of this file may alternatively be used under the terms
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * of the Common Development and Distribution License Version 1.0
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * VirtualBox OSE distribution, in which case the provisions of the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * CDDL are applicable instead of those of the GPL.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * You may elect to license modified versions of this file under the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * terms and conditions of either the GPL or the CDDL or both.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync/*******************************************************************************
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync* Header Files *
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync*******************************************************************************/
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include "internal/iprt.h"
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include <iprt/sha.h>
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include <iprt/alloca.h>
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include <iprt/assert.h>
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include <iprt/mem.h>
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include <iprt/string.h>
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync#include <iprt/file.h>
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsyncRTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync{
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Validate input */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync int rc = VINF_SUCCESS;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *ppszDigest = NULL;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
ed6697871be690c5826c252d598ea9965c2816b4vboxsync /* Initialize the hash context. */
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSHA256CONTEXT Ctx;
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSha256Init(&Ctx);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Buffer size for progress callback */
ed6697871be690c5826c252d598ea9965c2816b4vboxsync double rdMulti = 100.0 / (cbBuf ? cbBuf : 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Working buffer */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync char *pvTmp = (char*)pvBuf;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Process the memory in blocks */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync size_t cbReadTotal = 0;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync for (;;)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
ed6697871be690c5826c252d598ea9965c2816b4vboxsync size_t cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSha256Update(&Ctx, pvTmp, cbRead);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync cbReadTotal += cbRead;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync pvTmp += cbRead;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Call the progress callback if one is defined */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (pfnProgressCallback)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_FAILURE(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync break; /* canceled */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Finished? */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (cbReadTotal == cbBuf)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync break;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_SUCCESS(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Finally calculate & format the SHA256 sum */
ed6697871be690c5826c252d598ea9965c2816b4vboxsync uint8_t abHash[RTSHA256_HASH_SIZE];
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSha256Final(&Ctx, abHash);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync char *pszDigest;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_SUCCESS(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
ed6697871be690c5826c252d598ea9965c2816b4vboxsync rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_SUCCESS(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *ppszDigest = pszDigest;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync else
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync RTStrFree(pszDigest);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync return rc;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync}
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsyncRTR3DECL(int) RTSha256DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync{
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Validate input */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *ppszDigest = NULL;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
ed6697871be690c5826c252d598ea9965c2816b4vboxsync /* Initialize the hash context. */
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSHA256CONTEXT Ctx;
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSha256Init(&Ctx);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Open the file to calculate a SHA256 sum of */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync RTFILE hFile;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_FAILURE(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync return rc;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Fetch the file size. Only needed if there is a progress callback. */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync double rdMulti = 0;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (pfnProgressCallback)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync uint64_t cbFile;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = RTFileGetSize(hFile, &cbFile);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_FAILURE(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync RTFileClose(hFile);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync return rc;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
ed6697871be690c5826c252d598ea9965c2816b4vboxsync rdMulti = 100.0 / (cbFile ? cbFile : 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Allocate a reasonably large buffer, fall back on a tiny one. */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync void *pvBufFree;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync size_t cbBuf = _1M;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync void *pvBuf = pvBufFree = RTMemTmpAlloc(cbBuf);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (!pvBuf)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync cbBuf = 0x1000;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync pvBuf = alloca(cbBuf);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Read that file in blocks */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync size_t cbReadTotal = 0;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync for (;;)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
ed6697871be690c5826c252d598ea9965c2816b4vboxsync size_t cbRead;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_FAILURE(rc) || !cbRead)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync break;
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSha256Update(&Ctx, pvBuf, cbRead);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync cbReadTotal += cbRead;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Call the progress callback if one is defined */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (pfnProgressCallback)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_FAILURE(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync break; /* canceled */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync RTMemTmpFree(pvBufFree);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync RTFileClose(hFile);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_FAILURE(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync return rc;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Finally calculate & format the SHA256 sum */
ed6697871be690c5826c252d598ea9965c2816b4vboxsync uint8_t abHash[RTSHA256_HASH_SIZE];
ed6697871be690c5826c252d598ea9965c2816b4vboxsync RTSha256Final(&Ctx, abHash);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync char *pszDigest;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_SUCCESS(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync {
ed6697871be690c5826c252d598ea9965c2816b4vboxsync rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync if (RT_SUCCESS(rc))
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync *ppszDigest = pszDigest;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync else
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync RTStrFree(pszDigest);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync }
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync return rc;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync}
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync