alt-sha256.cpp revision 91c1a72ba0b6ccd8c9babda0928be38202073bec
/* $Id$ */
/** @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;
* 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
/** Enables the unrolled code. */
#define RTSHA256_UNROLLED 1
/*******************************************************************************
* Header Files *
*******************************************************************************/
/** 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. */
/** The message length (in bytes). */
/** The 8 hash values. */
#define RT_SHA256_PRIVATE_ALT_CONTEXT
AssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, AltPrivate));
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifndef RTSHA256_UNROLLED
/** The K constants */
{
};
#endif /* !RTSHA256_UNROLLED */
{
}
/** Function 4.2. */
{
#if 1
/* Optimization that saves one operation and probably a temporary variable. */
return uResult;
#else
/* The original. */
return uResult;
#endif
}
/** Function 4.3. */
{
#if 1
/* Optimization that save one operation and probably a temporary variable. */
return uResult;
#else
/* The original. */
return uResult;
#endif
}
/** Function 4.4. */
{
return uResult;
}
/** Function 4.5. */
{
return uResult;
}
/** Function 4.6. */
{
return uResult;
}
/** Function 4.7. */
{
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.
*/
{
#ifdef RTSHA256_UNROLLED
/* Copy and byte-swap the block. Initializing the rest of the Ws are done
in the processing loop. */
# ifdef RT_LITTLE_ENDIAN
# else
# endif
#else /* !RTSHA256_UNROLLED */
unsigned iWord;
{
}
#endif /* !RTSHA256_UNROLLED */
}
/**
* Initializes the auW array from data buffered in the first part of the array.
*
* @param pCtx The SHA-256 context.
*/
{
#ifdef RTSHA256_UNROLLED
/* Do the byte swap if necessary. Initializing the rest of the Ws are done
in the processing loop. */
# ifdef RT_LITTLE_ENDIAN
# endif
#else /* !RTSHA256_UNROLLED */
unsigned iWord;
{
}
#endif /* !RTSHA256_UNROLLED */
}
/**
* Process the current block.
*
* Requires one of the rtSha256BlockInit functions to be called first.
*
* @param pCtx The SHA-256 context.
*/
{
#ifdef RTSHA256_UNROLLED
do { \
if ((a_iWord) < 16) \
else \
{ \
} \
\
\
} while (0)
do { \
} while (0)
RTSHA256_EIGHT(UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
RTSHA256_EIGHT(UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
RTSHA256_EIGHT(UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
RTSHA256_EIGHT(UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
RTSHA256_EIGHT(UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
RTSHA256_EIGHT(UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
RTSHA256_EIGHT(UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
RTSHA256_EIGHT(UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
#else /* !RTSHA256_UNROLLED */
{
}
#endif /* !RTSHA256_UNROLLED */
}
{
/*
* Deal with buffered bytes first.
*/
if (cbBuffered)
{
{
}
else
{
return;
}
}
{
/*
* Process full blocks directly from the input buffer.
*/
while (cbBuf >= RTSHA256_BLOCK_SIZE)
{
}
}
else
{
/*
* Unaligned input, so buffer it.
*/
while (cbBuf >= RTSHA256_BLOCK_SIZE)
{
}
}
/*
* Stash any remaining bytes into the context buffer.
*/
if (cbBuf > 0)
{
}
}
/**
* Internal worker for RTSha256Final and RTSha224Final that finalizes the
* computation but does not copy out the hash value.
*
* @param pCtx The SHA-256 context.
*/
{
/*
* Complete the message by adding a single bit (0x80), padding till
* the next 448-bit boundrary, the add the message length.
*/
unsigned cbMissing = RTSHA256_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U));
/* Less than 64+8 bits left in the current block, force a new block. */
else
/*
* Process the last buffered block constructed/completed above.
*/
/*
* Convert the byte order of the hash words and we're done.
*/
}
{
}
{
RTSha256Init(&Ctx);
}
/*
* SHA-224 is just SHA-256 with different initial values an a truncated result.
*/
{
}
{
}
{
}
{
RTSha224Init(&Ctx);
}