/* Four of six logical functions used in SHA-384 and SHA-512: */ #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x))) #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
/*** INTERNAL FUNCTION PROTOTYPES *************************************/ /* NOTE: These should not be accessed directly from outside this *library--theyareintendedforprivateinternalvisibility/use *only.
*/ staticvoid SHA512_Last(pg_sha512_ctx *context); staticvoid SHA256_Transform(pg_sha256_ctx *context, const uint8 *data); staticvoid SHA512_Transform(pg_sha512_ctx *context, const uint8 *data);
staticvoid
SHA256_Transform(pg_sha256_ctx *context, const uint8 *data)
{
uint32 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint32 T1,
*W256; int j;
W256 = (uint32 *) context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0; do
{ /* Rounds 0 to 15 (unrolled): */
ROUND256_0_TO_15(a, b, c, d, e, f, g, h);
ROUND256_0_TO_15(h, a, b, c, d, e, f, g);
ROUND256_0_TO_15(g, h, a, b, c, d, e, f);
ROUND256_0_TO_15(f, g, h, a, b, c, d, e);
ROUND256_0_TO_15(e, f, g, h, a, b, c, d);
ROUND256_0_TO_15(d, e, f, g, h, a, b, c);
ROUND256_0_TO_15(c, d, e, f, g, h, a, b);
ROUND256_0_TO_15(b, c, d, e, f, g, h, a);
} while (j < 16);
/* Now for the remaining rounds to 64: */ do
{
ROUND256(a, b, c, d, e, f, g, h);
ROUND256(h, a, b, c, d, e, f, g);
ROUND256(g, h, a, b, c, d, e, f);
ROUND256(f, g, h, a, b, c, d, e);
ROUND256(e, f, g, h, a, b, c, d);
ROUND256(d, e, f, g, h, a, b, c);
ROUND256(c, d, e, f, g, h, a, b);
ROUND256(b, c, d, e, f, g, h, a);
} while (j < 64);
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
} #else/* SHA2_UNROLL_TRANSFORM */
staticvoid
SHA256_Transform(pg_sha256_ctx *context, const uint8 *data)
{
uint32 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint32 T1,
T2,
*W256; int j;
W256 = (uint32 *) context->buffer;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0; do
{
W256[j] = (uint32) data[3] | ((uint32) data[2] << 8) |
((uint32) data[1] << 16) | ((uint32) data[0] << 24);
data += 4; /* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 16);
do
{ /* Part of the message block expansion: */
s0 = W256[(j + 1) & 0x0f];
s0 = sigma0_256(s0);
s1 = W256[(j + 14) & 0x0f];
s1 = sigma1_256(s1);
/* Apply the SHA-256 compression function to update a..h */
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
(W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
T2 = Sigma0_256(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
/* Calling with no data is valid (we do nothing) */ if (len == 0) return;
usedspace = (context->bitcount >> 3) % PG_SHA256_BLOCK_LENGTH; if (usedspace > 0)
{ /* Calculate how much free space is available in the buffer */
freespace = PG_SHA256_BLOCK_LENGTH - usedspace;
if (len >= freespace)
{ /* Fill the buffer completely and process it */
memcpy(&context->buffer[usedspace], data, freespace);
context->bitcount += freespace << 3;
len -= freespace;
data += freespace;
SHA256_Transform(context, context->buffer);
} else
{ /* The buffer is not yet full */
memcpy(&context->buffer[usedspace], data, len);
context->bitcount += len << 3; /* Clean up: */
usedspace = freespace = 0; return;
}
} while (len >= PG_SHA256_BLOCK_LENGTH)
{ /* Process as many complete blocks as we can */
SHA256_Transform(context, data);
context->bitcount += PG_SHA256_BLOCK_LENGTH << 3;
len -= PG_SHA256_BLOCK_LENGTH;
data += PG_SHA256_BLOCK_LENGTH;
} if (len > 0)
{ /* There's left-overs, so save 'em */
memcpy(context->buffer, data, len);
context->bitcount += len << 3;
} /* Clean up: */
usedspace = freespace = 0;
}
usedspace = (context->bitcount >> 3) % PG_SHA256_BLOCK_LENGTH; #ifndef WORDS_BIGENDIAN /* Convert FROM host byte order */
REVERSE64(context->bitcount, context->bitcount); #endif if (usedspace > 0)
{ /* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= PG_SHA256_SHORT_BLOCK_LENGTH)
{ /* Set-up for the last transform: */
memset(&context->buffer[usedspace], 0, PG_SHA256_SHORT_BLOCK_LENGTH - usedspace);
} else
{ if (usedspace < PG_SHA256_BLOCK_LENGTH)
{
memset(&context->buffer[usedspace], 0, PG_SHA256_BLOCK_LENGTH - usedspace);
} /* Do second-to-last transform: */
SHA256_Transform(context, context->buffer);
/* And set-up for the last transform: */
memset(context->buffer, 0, PG_SHA256_SHORT_BLOCK_LENGTH);
}
} else
{ /* Set-up for the last transform: */
memset(context->buffer, 0, PG_SHA256_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
} /* Set the bit count: */
*(uint64 *) &context->buffer[PG_SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
/* Final transform: */
SHA256_Transform(context, context->buffer);
}
void
pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
{ /* If no digest buffer is passed, we don't bother doing this: */ if (digest != NULL)
{
SHA256_Last(context);
#ifndef WORDS_BIGENDIAN
{ /* Convert TO host byte order */ int j;
staticvoid
SHA512_Transform(pg_sha512_ctx *context, const uint8 *data)
{
uint64 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint64 T1,
*W512 = (uint64 *) context->buffer; int j;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0; do
{
ROUND512_0_TO_15(a, b, c, d, e, f, g, h);
ROUND512_0_TO_15(h, a, b, c, d, e, f, g);
ROUND512_0_TO_15(g, h, a, b, c, d, e, f);
ROUND512_0_TO_15(f, g, h, a, b, c, d, e);
ROUND512_0_TO_15(e, f, g, h, a, b, c, d);
ROUND512_0_TO_15(d, e, f, g, h, a, b, c);
ROUND512_0_TO_15(c, d, e, f, g, h, a, b);
ROUND512_0_TO_15(b, c, d, e, f, g, h, a);
} while (j < 16);
/* Now for the remaining rounds up to 79: */ do
{
ROUND512(a, b, c, d, e, f, g, h);
ROUND512(h, a, b, c, d, e, f, g);
ROUND512(g, h, a, b, c, d, e, f);
ROUND512(f, g, h, a, b, c, d, e);
ROUND512(e, f, g, h, a, b, c, d);
ROUND512(d, e, f, g, h, a, b, c);
ROUND512(c, d, e, f, g, h, a, b);
ROUND512(b, c, d, e, f, g, h, a);
} while (j < 80);
/* Clean up */
a = b = c = d = e = f = g = h = T1 = 0;
} #else/* SHA2_UNROLL_TRANSFORM */
staticvoid
SHA512_Transform(pg_sha512_ctx *context, const uint8 *data)
{
uint64 a,
b,
c,
d,
e,
f,
g,
h,
s0,
s1;
uint64 T1,
T2,
*W512 = (uint64 *) context->buffer; int j;
/* Initialize registers with the prev. intermediate value */
a = context->state[0];
b = context->state[1];
c = context->state[2];
d = context->state[3];
e = context->state[4];
f = context->state[5];
g = context->state[6];
h = context->state[7];
j = 0; do
{
W512[j] = (uint64) data[7] | ((uint64) data[6] << 8) |
((uint64) data[5] << 16) | ((uint64) data[4] << 24) |
((uint64) data[3] << 32) | ((uint64) data[2] << 40) |
((uint64) data[1] << 48) | ((uint64) data[0] << 56);
data += 8; /* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
j++;
} while (j < 16);
do
{ /* Part of the message block expansion: */
s0 = W512[(j + 1) & 0x0f];
s0 = sigma0_512(s0);
s1 = W512[(j + 14) & 0x0f];
s1 = sigma1_512(s1);
/* Apply the SHA-512 compression function to update a..h */
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
(W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
T2 = Sigma0_512(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
/* Calling with no data is valid (we do nothing) */ if (len == 0) return;
usedspace = (context->bitcount[0] >> 3) % PG_SHA512_BLOCK_LENGTH; if (usedspace > 0)
{ /* Calculate how much free space is available in the buffer */
freespace = PG_SHA512_BLOCK_LENGTH - usedspace;
if (len >= freespace)
{ /* Fill the buffer completely and process it */
memcpy(&context->buffer[usedspace], data, freespace);
ADDINC128(context->bitcount, freespace << 3);
len -= freespace;
data += freespace;
SHA512_Transform(context, context->buffer);
} else
{ /* The buffer is not yet full */
memcpy(&context->buffer[usedspace], data, len);
ADDINC128(context->bitcount, len << 3); /* Clean up: */
usedspace = freespace = 0; return;
}
} while (len >= PG_SHA512_BLOCK_LENGTH)
{ /* Process as many complete blocks as we can */
SHA512_Transform(context, data);
ADDINC128(context->bitcount, PG_SHA512_BLOCK_LENGTH << 3);
len -= PG_SHA512_BLOCK_LENGTH;
data += PG_SHA512_BLOCK_LENGTH;
} if (len > 0)
{ /* There's left-overs, so save 'em */
memcpy(context->buffer, data, len);
ADDINC128(context->bitcount, len << 3);
} /* Clean up: */
usedspace = freespace = 0;
}
usedspace = (context->bitcount[0] >> 3) % PG_SHA512_BLOCK_LENGTH; #ifndef WORDS_BIGENDIAN /* Convert FROM host byte order */
REVERSE64(context->bitcount[0], context->bitcount[0]);
REVERSE64(context->bitcount[1], context->bitcount[1]); #endif if (usedspace > 0)
{ /* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= PG_SHA512_SHORT_BLOCK_LENGTH)
{ /* Set-up for the last transform: */
memset(&context->buffer[usedspace], 0, PG_SHA512_SHORT_BLOCK_LENGTH - usedspace);
} else
{ if (usedspace < PG_SHA512_BLOCK_LENGTH)
{
memset(&context->buffer[usedspace], 0, PG_SHA512_BLOCK_LENGTH - usedspace);
} /* Do second-to-last transform: */
SHA512_Transform(context, context->buffer);
/* And set-up for the last transform: */
memset(context->buffer, 0, PG_SHA512_BLOCK_LENGTH - 2);
}
} else
{ /* Prepare for final transform: */
memset(context->buffer, 0, PG_SHA512_SHORT_BLOCK_LENGTH);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
} /* Store the length of input data (in bits): */
*(uint64 *) &context->buffer[PG_SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
*(uint64 *) &context->buffer[PG_SHA512_SHORT_BLOCK_LENGTH + 8] = context->bitcount[0];
/* Final transform: */
SHA512_Transform(context, context->buffer);
}
void
pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
{ /* If no digest buffer is passed, we don't bother doing this: */ if (digest != NULL)
{
SHA512_Last(context);
/* Save the hash data for output: */ #ifndef WORDS_BIGENDIAN
{ /* Convert TO host byte order */ int j;
void
pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
{ /* If no digest buffer is passed, we don't bother doing this: */ if (digest != NULL)
{
SHA256_Last(context);
#ifndef WORDS_BIGENDIAN
{ /* Convert TO host byte order */ int j;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.