alt-sha256.cpp revision 2941e0b052a22ff7c978763a3fadc814b2b34e18
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/* $Id$ */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** @file
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * IPRT - SHA-256 and SHA-224 hash functions, Alternative Implementation.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Copyright (C) 2009-2014 Oracle Corporation
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *
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 *
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 *
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
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Defined Constants And Macros *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The SHA-256 block size (in bytes). */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RTSHA256_BLOCK_SIZE 64U
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Enables the unrolled code. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RTSHA256_UNROLLED 1
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Header Files *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#include "internal/iprt.h"
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/types.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/assert.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/asm.h>
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#include <iprt/string.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** Our private context structure. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsynctypedef struct RTSHA256ALTPRIVATECTX
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The W array.
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * Buffering happens in the first 16 words, converted from big endian to host
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * endian immediately before processing. The amount of buffered data is kept
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync * in the 6 least significant bits of cbMessage. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t auW[64];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The message length (in bytes). */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint64_t cbMessage;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /** The 8 hash values. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t auH[8];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync} RTSHA256ALTPRIVATECTX;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#define RT_SHA256_PRIVATE_ALT_CONTEXT
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#include <iprt/sha.h>
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncAssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, AltPrivate));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncAssertCompileMemberSize(RTSHA256ALTPRIVATECTX, auH, RTSHA256_HASH_SIZE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/*******************************************************************************
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync* Global Variables *
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync*******************************************************************************/
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#ifndef RTSHA256_UNROLLED
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync/** The K constants */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncstatic uint32_t const g_auKs[] =
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync};
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#endif /* !RTSHA256_UNROLLED */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsyncRTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync{
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pCtx->AltPrivate.cbMessage = 0;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[0] = UINT32_C(0x6a09e667);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[1] = UINT32_C(0xbb67ae85);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[2] = UINT32_C(0x3c6ef372);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[3] = UINT32_C(0xa54ff53a);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[4] = UINT32_C(0x510e527f);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[5] = UINT32_C(0x9b05688c);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[6] = UINT32_C(0x1f83d9ab);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[7] = UINT32_C(0x5be0cd19);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncRT_EXPORT_SYMBOL(RTSha256Init);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/** Function 4.2. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECL_FORCE_INLINE(uint32_t) rtSha256Ch(uint32_t uX, uint32_t uY, uint32_t uZ)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#if 1
9f997e760f610c92e3a365be21ead6972bc46130vboxsync /* Optimization that saves one operation and probably a temporary variable. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uY;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult &= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#else
9f997e760f610c92e3a365be21ead6972bc46130vboxsync /* The original. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uX & uY;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= ~uX & uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#endif
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/** Function 4.3. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECL_FORCE_INLINE(uint32_t) rtSha256Maj(uint32_t uX, uint32_t uY, uint32_t uZ)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#if 1
9f997e760f610c92e3a365be21ead6972bc46130vboxsync /* Optimization that save one operation and probably a temporary variable. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uY;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult &= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uY & uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#else
9f997e760f610c92e3a365be21ead6972bc46130vboxsync /* The original. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uX & uY;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX & uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uY & uZ;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#endif
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/** Function 4.4. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma0(uint32_t uX)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uX = ASMRotateRightU32(uX, 2);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 13 - 2);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 22 - 13);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/** Function 4.5. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma1(uint32_t uX)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uX = ASMRotateRightU32(uX, 6);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 11 - 6);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 25 - 11);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/** Function 4.6. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma0(uint32_t uX)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uX >> 3;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 7);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 18 - 7);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/** Function 4.7. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma1(uint32_t uX)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uResult = uX >> 10;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 17);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uX = ASMRotateRightU32(uX, 19 - 17);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uResult ^= uX;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync return uResult;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/**
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * Initializes the auW array from the specfied input block.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * @param pCtx The SHA-256 context.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * @param pbBlock The block. Must be arch-bit-width aligned.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECLINLINE(void) rtSha256BlockInit(PRTSHA256CONTEXT pCtx, uint8_t const *pbBlock)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#ifdef RTSHA256_UNROLLED
9f997e760f610c92e3a365be21ead6972bc46130vboxsync /* Copy and byte-swap the block. Initializing the rest of the Ws are done
9f997e760f610c92e3a365be21ead6972bc46130vboxsync in the processing loop. */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync# ifdef RT_LITTLE_ENDIAN
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync# if 0 /* Just an idea... very little gain as this isn't the expensive code. */
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync __m128i const uBSwapConst = { 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12 };
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync __m128i const *puSrc = (__m128i const *)pbBlock;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync __m128i *puDst = (__m128i *)&pCtx->AltPrivate.auW[0];
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync# elif ARCH_BITS == 64
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync uint64_t const *puSrc = (uint64_t const *)pbBlock;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync uint64_t *puW = (uint64_t *)&pCtx->AltPrivate.auW[0];
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync Assert(!((uintptr_t)puSrc & 7));
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync Assert(!((uintptr_t)puW & 7));
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync /* b0 b1 b2 b3 b4 b5 b6 b7 --bwap--> b7 b6 b5 b4 b3 b2 b1 b0 --ror--> b3 b2 b1 b0 b7 b6 b5 b4; */
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync# else
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync uint32_t const *puSrc = (uint32_t const *)pbBlock;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync uint32_t *puW = &pCtx->AltPrivate.auW[0];
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync Assert(!((uintptr_t)puSrc & 3));
9f997e760f610c92e3a365be21ead6972bc46130vboxsync Assert(!((uintptr_t)puW & 3));
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW++ = ASMByteSwapU32(*puSrc++);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync# endif
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync# else /* RT_BIG_ENDIAN */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync memcpy(&pCtx->AltPrivate.auW[0], pbBlock, RTSHA256_BLOCK_SIZE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync# endif /* RT_BIG_ENDIAN */
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync#else /* !RTSHA256_UNROLLED */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t const *pu32Block = (uint32_t const *)pbBlock;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync Assert(!((uintptr_t)pu32Block & 3));
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync unsigned iWord;
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync for (iWord = 0; iWord < 16; iWord++)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
30f07af559efcbd967e801903746fc21f81ee533vboxsync {
30f07af559efcbd967e801903746fc21f81ee533vboxsync uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
30f07af559efcbd967e801903746fc21f81ee533vboxsync u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync u32 += pCtx->AltPrivate.auW[iWord - 7];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync u32 += pCtx->AltPrivate.auW[iWord - 16];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync pCtx->AltPrivate.auW[iWord] = u32;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync }
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#endif /* !RTSHA256_UNROLLED */
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync}
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/**
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * Initializes the auW array from data buffered in the first part of the array.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync * @param pCtx The SHA-256 context.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncDECLINLINE(void) rtSha256BlockInitBuffered(PRTSHA256CONTEXT pCtx)
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync{
30f07af559efcbd967e801903746fc21f81ee533vboxsync#ifdef RTSHA256_UNROLLED
30f07af559efcbd967e801903746fc21f81ee533vboxsync /* Do the byte swap if necessary. Initializing the rest of the Ws are done
30f07af559efcbd967e801903746fc21f81ee533vboxsync in the processing loop. */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync# ifdef RT_LITTLE_ENDIAN
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync# if ARCH_BITS == 64
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint64_t *puW = (uint64_t *)&pCtx->AltPrivate.auW[0];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync Assert(!((uintptr_t)puW & 7));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync /* b0 b1 b2 b3 b4 b5 b6 b7 --bwap--> b7 b6 b5 b4 b3 b2 b1 b0 --ror--> b3 b2 b1 b0 b7 b6 b5 b4; */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync# else
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t *puW = &pCtx->AltPrivate.auW[0];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync Assert(!((uintptr_t)puW & 3));
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync *puW = ASMByteSwapU32(*puW); puW++;
30f07af559efcbd967e801903746fc21f81ee533vboxsync *puW = ASMByteSwapU32(*puW); puW++;
30f07af559efcbd967e801903746fc21f81ee533vboxsync
30f07af559efcbd967e801903746fc21f81ee533vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *puW = ASMByteSwapU32(*puW); puW++;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync# endif
9f997e760f610c92e3a365be21ead6972bc46130vboxsync# endif
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#else /* !RTSHA256_UNROLLED */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync unsigned iWord;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync for (iWord = 0; iWord < 16; iWord++)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync {
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync u32 += pCtx->AltPrivate.auW[iWord - 7];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync u32 += pCtx->AltPrivate.auW[iWord - 16];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auW[iWord] = u32;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync }
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#endif /* !RTSHA256_UNROLLED */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync/**
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * Process the current block.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * Requires one of the rtSha256BlockInit functions to be called first.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync *
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * @param pCtx The SHA-256 context.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync */
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncstatic void rtSha256BlockProcess(PRTSHA256CONTEXT pCtx)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uA = pCtx->AltPrivate.auH[0];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uB = pCtx->AltPrivate.auH[1];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uC = pCtx->AltPrivate.auH[2];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uD = pCtx->AltPrivate.auH[3];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uE = pCtx->AltPrivate.auH[4];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uF = pCtx->AltPrivate.auH[5];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uG = pCtx->AltPrivate.auH[6];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint32_t uH = pCtx->AltPrivate.auH[7];
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#ifdef RTSHA256_UNROLLED
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync uint32_t *puW = &pCtx->AltPrivate.auW[0];
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync# define RTSHA256_BODY(a_iWord, a_uK, a_uA, a_uB, a_uC, a_uD, a_uE, a_uF, a_uG, a_uH) \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync do { \
30f07af559efcbd967e801903746fc21f81ee533vboxsync if ((a_iWord) < 16) \
30f07af559efcbd967e801903746fc21f81ee533vboxsync a_uH += *puW++; \
30f07af559efcbd967e801903746fc21f81ee533vboxsync else \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync { \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t u32 = puW[-16]; \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync u32 += rtSha256SmallSigma0(puW[-15]); \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync u32 += puW[-7]; \
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync u32 += rtSha256SmallSigma1(puW[-2]); \
30f07af559efcbd967e801903746fc21f81ee533vboxsync if (a_iWord < 64-2) *puW++ = u32; else puW++; \
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync a_uH += u32; \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync } \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync a_uH += rtSha256CapitalSigma1(a_uE); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync a_uH += a_uK; \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync a_uH += rtSha256Ch(a_uE, a_uF, a_uG); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync a_uD += a_uH; \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync a_uH += rtSha256CapitalSigma0(a_uA); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync a_uH += rtSha256Maj(a_uA, a_uB, a_uC); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync } while (0)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync# define RTSHA256_EIGHT(a_uK0, a_uK1, a_uK2, a_uK3, a_uK4, a_uK5, a_uK6, a_uK7, a_iFirst) \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync do { \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 0, a_uK0, uA, uB, uC, uD, uE, uF, uG, uH); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 1, a_uK1, uH, uA, uB, uC, uD, uE, uF, uG); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 2, a_uK2, uG, uH, uA, uB, uC, uD, uE, uF); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 3, a_uK3, uF, uG, uH, uA, uB, uC, uD, uE); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 4, a_uK4, uE, uF, uG, uH, uA, uB, uC, uD); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 5, a_uK5, uD, uE, uF, uG, uH, uA, uB, uC); \
9f997e760f610c92e3a365be21ead6972bc46130vboxsync RTSHA256_BODY(a_iFirst + 6, a_uK6, uC, uD, uE, uF, uG, uH, uA, uB); \
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync RTSHA256_BODY(a_iFirst + 7, a_uK7, uB, uC, uD, uE, uF, uG, uH, uA); \
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync } while (0)
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync RTSHA256_EIGHT(UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
79a6b20f984859d9cc49305172355d6c3901f1c5vboxsync UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5), 0);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174), 8);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da), 16);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967), 24);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85), 32);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070), 40);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3), 48);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync RTSHA256_EIGHT(UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2), 56);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync#else /* !RTSHA256_UNROLLED */
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync {
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t uT1 = uH;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uT1 += rtSha256CapitalSigma1(uE);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uT1 += rtSha256Ch(uE, uF, uG);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uT1 += g_auKs[iWord];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uT1 += pCtx->AltPrivate.auW[iWord];
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uint32_t uT2 = rtSha256CapitalSigma0(uA);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uT2 += rtSha256Maj(uA, uB, uC);
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uH = uG;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uG = uF;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uF = uE;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uE = uD + uT1;
13493ab7596e827b8d0caab2c89e635dd65f78f9vboxsync uD = uC;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uC = uB;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uB = uA;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uA = uT1 + uT2;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync }
9f997e760f610c92e3a365be21ead6972bc46130vboxsync#endif /* !RTSHA256_UNROLLED */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[0] += uA;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[1] += uB;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[2] += uC;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[3] += uD;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[4] += uE;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[5] += uF;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[6] += uG;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.auH[7] += uH;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync}
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsyncRTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync{
9f997e760f610c92e3a365be21ead6972bc46130vboxsync Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync uint8_t const *pbBuf = (uint8_t const *)pvBuf;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync
9f997e760f610c92e3a365be21ead6972bc46130vboxsync /*
9f997e760f610c92e3a365be21ead6972bc46130vboxsync * Deal with buffered bytes first.
9f997e760f610c92e3a365be21ead6972bc46130vboxsync */
9f997e760f610c92e3a365be21ead6972bc46130vboxsync size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync if (cbBuffered)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync {
9f997e760f610c92e3a365be21ead6972bc46130vboxsync size_t cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync if (cbBuf >= cbMissing)
9f997e760f610c92e3a365be21ead6972bc46130vboxsync {
9f997e760f610c92e3a365be21ead6972bc46130vboxsync memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
9f997e760f610c92e3a365be21ead6972bc46130vboxsync pCtx->AltPrivate.cbMessage += cbMissing;
9f997e760f610c92e3a365be21ead6972bc46130vboxsync 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 & (sizeof(void *) - 1)))
{
/*
* 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);