/* $Id: alt-sha256.cpp 51861 2014-07-03 22:56:18Z vboxsync $ */ /** @file * IPRT - SHA-256 and SHA-224 hash functions, Alternative Implementation. */ /* * Copyright (C) 2009-2014 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. * * The contents of this file may alternatively be used under the terms * of the Common Development and Distribution License Version 1.0 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the * VirtualBox OSE distribution, in which case the provisions of the * CDDL are applicable instead of those of the GPL. * * You may elect to license modified versions of this file under the * terms and conditions of either the GPL or the CDDL or both. */ /******************************************************************************* * Defined Constants And Macros * *******************************************************************************/ /** The SHA-256 block size (in bytes). */ #define RTSHA256_BLOCK_SIZE 64U /******************************************************************************* * Header Files * *******************************************************************************/ #include "internal/iprt.h" #include #include #include #include /** Our private context structure. */ typedef struct RTSHA256ALTPRIVATECTX { /** The W array. * Buffering happens in the first 16 words, converted from big endian to host * endian immediately before processing. The amount of buffered data is kept * in the 6 least significant bits of cbMessage. */ uint32_t auW[64]; /** The message length (in bytes). */ uint64_t cbMessage; /** The 8 hash values. */ uint32_t auH[8]; } RTSHA256ALTPRIVATECTX; #define RT_SHA256_PRIVATE_ALT_CONTEXT #include AssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, AltPrivate)); AssertCompileMemberSize(RTSHA256ALTPRIVATECTX, auH, RTSHA256_HASH_SIZE); /******************************************************************************* * Global Variables * *******************************************************************************/ /** The K constants */ static uint32_t const g_auKs[] = { UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5), UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5), UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3), UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174), UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc), UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da), UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7), UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967), UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13), UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85), UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3), UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070), UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5), UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3), UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208), UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2), }; RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx) { pCtx->AltPrivate.cbMessage = 0; pCtx->AltPrivate.auH[0] = UINT32_C(0x6a09e667); pCtx->AltPrivate.auH[1] = UINT32_C(0xbb67ae85); pCtx->AltPrivate.auH[2] = UINT32_C(0x3c6ef372); pCtx->AltPrivate.auH[3] = UINT32_C(0xa54ff53a); pCtx->AltPrivate.auH[4] = UINT32_C(0x510e527f); pCtx->AltPrivate.auH[5] = UINT32_C(0x9b05688c); pCtx->AltPrivate.auH[6] = UINT32_C(0x1f83d9ab); pCtx->AltPrivate.auH[7] = UINT32_C(0x5be0cd19); } RT_EXPORT_SYMBOL(RTSha256Init); /** Function 4.2. */ DECL_FORCE_INLINE(uint32_t) rtSha256Ch(uint32_t uX, uint32_t uY, uint32_t uZ) { uint32_t uResult = uX & uY; uResult ^= ~uX & uZ; return uResult; } /** Function 4.3. */ DECL_FORCE_INLINE(uint32_t) rtSha256Maj(uint32_t uX, uint32_t uY, uint32_t uZ) { uint32_t uResult = uX & uY; uResult ^= uX & uZ; uResult ^= uY & uZ; return uResult; } /** Function 4.4. */ DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma0(uint32_t uX) { uint32_t uResult = uX = ASMRotateRightU32(uX, 2); uX = ASMRotateRightU32(uX, 13 - 2); uResult ^= uX; uX = ASMRotateRightU32(uX, 22 - 13); uResult ^= uX; return uResult; } /** Function 4.5. */ DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma1(uint32_t uX) { uint32_t uResult = uX = ASMRotateRightU32(uX, 6); uX = ASMRotateRightU32(uX, 11 - 6); uResult ^= uX; uX = ASMRotateRightU32(uX, 25 - 11); uResult ^= uX; return uResult; } /** Function 4.6. */ DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma0(uint32_t uX) { uint32_t uResult = uX >> 3; uX = ASMRotateRightU32(uX, 7); uResult ^= uX; uX = ASMRotateRightU32(uX, 18 - 7); uResult ^= uX; return uResult; } /** Function 4.7. */ DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma1(uint32_t uX) { uint32_t uResult = uX >> 10; uX = ASMRotateRightU32(uX, 17); uResult ^= uX; uX = ASMRotateRightU32(uX, 19 - 17); uResult ^= uX; return uResult; } /** * Initializes the auW array from the specfied input block. * * @param pCtx The SHA-256 context. * @param pbBlock The block. Must be 32-bit aligned. */ DECLINLINE(void) rtSha256BlockInit(PRTSHA256CONTEXT pCtx, uint8_t const *pbBlock) { uint32_t const *pu32Block = (uint32_t const *)pbBlock; Assert(!((uintptr_t)pu32Block & 3)); unsigned iWord; for (iWord = 0; iWord < 16; iWord++) pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]); for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++) { uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]); u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]); u32 += pCtx->AltPrivate.auW[iWord - 7]; u32 += pCtx->AltPrivate.auW[iWord - 16]; pCtx->AltPrivate.auW[iWord] = u32; } } /** * Initializes the auW array from data buffered in the first part of the array. * * @param pCtx The SHA-256 context. */ DECLINLINE(void) rtSha256BlockInitBuffered(PRTSHA256CONTEXT pCtx) { unsigned iWord; for (iWord = 0; iWord < 16; iWord++) pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]); for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++) { uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]); u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]); u32 += pCtx->AltPrivate.auW[iWord - 7]; u32 += pCtx->AltPrivate.auW[iWord - 16]; pCtx->AltPrivate.auW[iWord] = u32; } } /** * Process the current block. * * Requires one of the rtSha256BlockInit functions to be called first. * * @param pCtx The SHA-256 context. */ static void rtSha256BlockProcess(PRTSHA256CONTEXT pCtx) { uint32_t uA = pCtx->AltPrivate.auH[0]; uint32_t uB = pCtx->AltPrivate.auH[1]; uint32_t uC = pCtx->AltPrivate.auH[2]; uint32_t uD = pCtx->AltPrivate.auH[3]; uint32_t uE = pCtx->AltPrivate.auH[4]; uint32_t uF = pCtx->AltPrivate.auH[5]; uint32_t uG = pCtx->AltPrivate.auH[6]; uint32_t uH = pCtx->AltPrivate.auH[7]; for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++) { uint32_t uT1 = uH; uT1 += rtSha256CapitalSigma1(uE); uT1 += rtSha256Ch(uE, uF, uG); uT1 += g_auKs[iWord]; uT1 += pCtx->AltPrivate.auW[iWord]; uint32_t uT2 = rtSha256CapitalSigma0(uA); uT2 += rtSha256Maj(uA, uB, uC); uH = uG; uG = uF; uF = uE; uE = uD + uT1; uD = uC; uC = uB; uB = uA; uA = uT1 + uT2; } pCtx->AltPrivate.auH[0] += uA; pCtx->AltPrivate.auH[1] += uB; pCtx->AltPrivate.auH[2] += uC; pCtx->AltPrivate.auH[3] += uD; pCtx->AltPrivate.auH[4] += uE; pCtx->AltPrivate.auH[5] += uF; pCtx->AltPrivate.auH[6] += uG; pCtx->AltPrivate.auH[7] += uH; } RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf) { Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8); uint8_t const *pbBuf = (uint8_t const *)pvBuf; /* * Deal with buffered bytes first. */ size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U); if (cbBuffered) { size_t cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered; if (cbBuf >= cbMissing) { memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing); pCtx->AltPrivate.cbMessage += cbMissing; pbBuf += cbMissing; cbBuf -= cbMissing; rtSha256BlockInitBuffered(pCtx); rtSha256BlockProcess(pCtx); } else { memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf); pCtx->AltPrivate.cbMessage += cbBuf; return; } } if (!((uintptr_t)pbBuf & 3)) { /* * Process full blocks directly from the input buffer. */ while (cbBuf >= RTSHA256_BLOCK_SIZE) { rtSha256BlockInit(pCtx, pbBuf); rtSha256BlockProcess(pCtx); pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE; pbBuf += RTSHA256_BLOCK_SIZE; cbBuf -= RTSHA256_BLOCK_SIZE; } } else { /* * Unaligned input, so buffer it. */ while (cbBuf >= RTSHA256_BLOCK_SIZE) { memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA256_BLOCK_SIZE); rtSha256BlockInitBuffered(pCtx); rtSha256BlockProcess(pCtx); pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE; pbBuf += RTSHA256_BLOCK_SIZE; cbBuf -= RTSHA256_BLOCK_SIZE; } } /* * Stash any remaining bytes into the context buffer. */ if (cbBuf > 0) { memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf); pCtx->AltPrivate.cbMessage += cbBuf; } } RT_EXPORT_SYMBOL(RTSha256Update); /** * Internal worker for RTSha256Final and RTSha224Final that finalizes the * computation but does not copy out the hash value. * * @param pCtx The SHA-256 context. */ static void rtSha256FinalInternal(PRTSHA256CONTEXT pCtx) { Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8); /* * Complete the message by adding a single bit (0x80), padding till * the next 448-bit boundrary, the add the message length. */ uint64_t const cMessageBits = pCtx->AltPrivate.cbMessage * 8; unsigned cbMissing = RTSHA256_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U)); static uint8_t const s_abSingleBitAndSomePadding[12] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; if (cbMissing < 1U + 8U) /* Less than 64+8 bits left in the current block, force a new block. */ RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding)); else RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, 1); unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U); cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered; Assert(cbMissing >= 8); memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 8); *(uint64_t *)&pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits); /* * Process the last buffered block constructed/completed above. */ rtSha256BlockInitBuffered(pCtx); rtSha256BlockProcess(pCtx); /* * Convert the byte order of the hash words and we're done. */ pCtx->AltPrivate.auH[0] = RT_H2BE_U32(pCtx->AltPrivate.auH[0]); pCtx->AltPrivate.auH[1] = RT_H2BE_U32(pCtx->AltPrivate.auH[1]); pCtx->AltPrivate.auH[2] = RT_H2BE_U32(pCtx->AltPrivate.auH[2]); pCtx->AltPrivate.auH[3] = RT_H2BE_U32(pCtx->AltPrivate.auH[3]); pCtx->AltPrivate.auH[4] = RT_H2BE_U32(pCtx->AltPrivate.auH[4]); pCtx->AltPrivate.auH[5] = RT_H2BE_U32(pCtx->AltPrivate.auH[5]); pCtx->AltPrivate.auH[6] = RT_H2BE_U32(pCtx->AltPrivate.auH[6]); pCtx->AltPrivate.auH[7] = RT_H2BE_U32(pCtx->AltPrivate.auH[7]); RT_ZERO(pCtx->AltPrivate.auW); pCtx->AltPrivate.cbMessage = UINT64_MAX; } RT_EXPORT_SYMBOL(RTSha256Final); RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE]) { rtSha256FinalInternal(pCtx); memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA256_HASH_SIZE); RT_ZERO(pCtx->AltPrivate.auH); } RT_EXPORT_SYMBOL(RTSha256Final); RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE]) { RTSHA256CONTEXT Ctx; RTSha256Init(&Ctx); RTSha256Update(&Ctx, pvBuf, cbBuf); RTSha256Final(&Ctx, pabDigest); } RT_EXPORT_SYMBOL(RTSha256); /* * SHA-224 is just SHA-256 with different initial values an a truncated result. */ RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx) { pCtx->AltPrivate.cbMessage = 0; pCtx->AltPrivate.auH[0] = UINT32_C(0xc1059ed8); pCtx->AltPrivate.auH[1] = UINT32_C(0x367cd507); pCtx->AltPrivate.auH[2] = UINT32_C(0x3070dd17); pCtx->AltPrivate.auH[3] = UINT32_C(0xf70e5939); pCtx->AltPrivate.auH[4] = UINT32_C(0xffc00b31); pCtx->AltPrivate.auH[5] = UINT32_C(0x68581511); pCtx->AltPrivate.auH[6] = UINT32_C(0x64f98fa7); pCtx->AltPrivate.auH[7] = UINT32_C(0xbefa4fa4); } RT_EXPORT_SYMBOL(RTSha224Init); RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf) { RTSha256Update(pCtx, pvBuf, cbBuf); } RT_EXPORT_SYMBOL(RTSha224Update); RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE]) { rtSha256FinalInternal(pCtx); memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA224_HASH_SIZE); RT_ZERO(pCtx->AltPrivate.auH); } RT_EXPORT_SYMBOL(RTSha224Final); RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE]) { RTSHA224CONTEXT Ctx; RTSha224Init(&Ctx); RTSha224Update(&Ctx, pvBuf, cbBuf); RTSha224Final(&Ctx, pabDigest); } RT_EXPORT_SYMBOL(RTSha224);