|
@ -1,4 +1,4 @@ |
|
|
/* $OpenBSD: sha2.c,v 1.4 2004/04/29 14:13:17 millert Exp $ */ |
|
|
|
|
|
|
|
|
/* $OpenBSD: sha2.c,v 1.5 2004/05/02 23:53:47 millert Exp $ */ |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
* FILE: sha2.c |
|
|
* FILE: sha2.c |
|
@ -35,7 +35,7 @@ |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
|
|
static const char rcsid[] = "$OpenBSD: sha2.c,v 1.4 2004/04/29 14:13:17 millert Exp $"; |
|
|
|
|
|
|
|
|
static const char rcsid[] = "$OpenBSD: sha2.c,v 1.5 2004/05/02 23:53:47 millert Exp $"; |
|
|
#endif /* LIBC_SCCS and not lint */ |
|
|
#endif /* LIBC_SCCS and not lint */ |
|
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
#include <sys/types.h> |
|
@ -273,10 +273,10 @@ const static u_int64_t sha512_initial_hash_value[8] = { |
|
|
|
|
|
|
|
|
/*** SHA-256: *********************************************************/ |
|
|
/*** SHA-256: *********************************************************/ |
|
|
void |
|
|
void |
|
|
SHA256_Init(SHA256_CTX *context) { |
|
|
|
|
|
if (context == (SHA256_CTX *)0) { |
|
|
|
|
|
|
|
|
SHA256_Init(SHA256_CTX *context) |
|
|
|
|
|
{ |
|
|
|
|
|
if (context == NULL) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
|
|
|
memcpy(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH); |
|
|
memcpy(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH); |
|
|
memset(context->buffer, 0, SHA256_BLOCK_LENGTH); |
|
|
memset(context->buffer, 0, SHA256_BLOCK_LENGTH); |
|
|
context->bitcount = 0; |
|
|
context->bitcount = 0; |
|
@ -320,7 +320,8 @@ SHA256_Init(SHA256_CTX *context) { |
|
|
j++ |
|
|
j++ |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA256_Transform(SHA256_CTX *context, const u_int32_t *data) { |
|
|
|
|
|
|
|
|
SHA256_Transform(SHA256_CTX *context, const u_int32_t *data) |
|
|
|
|
|
{ |
|
|
u_int32_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int32_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int32_t T1, *W256; |
|
|
u_int32_t T1, *W256; |
|
|
int j; |
|
|
int j; |
|
@ -379,7 +380,8 @@ SHA256_Transform(SHA256_CTX *context, const u_int32_t *data) { |
|
|
#else /* SHA2_UNROLL_TRANSFORM */ |
|
|
#else /* SHA2_UNROLL_TRANSFORM */ |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA256_Transform(SHA256_CTX *context, const u_int32_t *data) { |
|
|
|
|
|
|
|
|
SHA256_Transform(SHA256_CTX *context, const u_int32_t *data) |
|
|
|
|
|
{ |
|
|
u_int32_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int32_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int32_t T1, T2, *W256; |
|
|
u_int32_t T1, T2, *W256; |
|
|
int j; |
|
|
int j; |
|
@ -460,13 +462,13 @@ SHA256_Transform(SHA256_CTX *context, const u_int32_t *data) { |
|
|
#endif /* SHA2_UNROLL_TRANSFORM */ |
|
|
#endif /* SHA2_UNROLL_TRANSFORM */ |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len) { |
|
|
|
|
|
unsigned int freespace, usedspace; |
|
|
|
|
|
|
|
|
SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len) |
|
|
|
|
|
{ |
|
|
|
|
|
size_t freespace, usedspace; |
|
|
|
|
|
|
|
|
if (len == 0) { |
|
|
|
|
|
/* Calling with no data is valid - we do nothing */ |
|
|
|
|
|
|
|
|
/* Calling with no data is valid (we do nothing) */ |
|
|
|
|
|
if (len == 0) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
|
|
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
|
|
if (usedspace > 0) { |
|
|
if (usedspace > 0) { |
|
@ -506,12 +508,13 @@ SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA256_Final(u_int8_t digest[], SHA256_CTX *context) { |
|
|
|
|
|
|
|
|
SHA256_Final(u_int8_t digest[], SHA256_CTX *context) |
|
|
|
|
|
{ |
|
|
u_int32_t *d = (u_int32_t *)digest; |
|
|
u_int32_t *d = (u_int32_t *)digest; |
|
|
unsigned int usedspace; |
|
|
unsigned int usedspace; |
|
|
|
|
|
|
|
|
/* If no digest buffer is passed, we don't bother doing this: */ |
|
|
/* If no digest buffer is passed, we don't bother doing this: */ |
|
|
if (digest != (u_int8_t *)0) { |
|
|
|
|
|
|
|
|
if (digest != NULL) { |
|
|
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
|
|
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN |
|
|
/* Convert FROM host byte order */ |
|
|
/* Convert FROM host byte order */ |
|
@ -569,10 +572,10 @@ SHA256_Final(u_int8_t digest[], SHA256_CTX *context) { |
|
|
|
|
|
|
|
|
/*** SHA-512: *********************************************************/ |
|
|
/*** SHA-512: *********************************************************/ |
|
|
void |
|
|
void |
|
|
SHA512_Init(SHA512_CTX *context) { |
|
|
|
|
|
if (context == (SHA512_CTX *)0) { |
|
|
|
|
|
|
|
|
SHA512_Init(SHA512_CTX *context) |
|
|
|
|
|
{ |
|
|
|
|
|
if (context == NULL) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
|
|
|
memcpy(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); |
|
|
memcpy(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); |
|
|
memset(context->buffer, 0, SHA512_BLOCK_LENGTH); |
|
|
memset(context->buffer, 0, SHA512_BLOCK_LENGTH); |
|
|
context->bitcount[0] = context->bitcount[1] = 0; |
|
|
context->bitcount[0] = context->bitcount[1] = 0; |
|
@ -615,7 +618,8 @@ SHA512_Init(SHA512_CTX *context) { |
|
|
j++ |
|
|
j++ |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA512_Transform(SHA512_CTX *context, const u_int64_t *data) { |
|
|
|
|
|
|
|
|
SHA512_Transform(SHA512_CTX *context, const u_int64_t *data) |
|
|
|
|
|
{ |
|
|
u_int64_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int64_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int64_t T1, *W512 = (u_int64_t *)context->buffer; |
|
|
u_int64_t T1, *W512 = (u_int64_t *)context->buffer; |
|
|
int j; |
|
|
int j; |
|
@ -671,7 +675,8 @@ SHA512_Transform(SHA512_CTX *context, const u_int64_t *data) { |
|
|
#else /* SHA2_UNROLL_TRANSFORM */ |
|
|
#else /* SHA2_UNROLL_TRANSFORM */ |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA512_Transform(SHA512_CTX *context, const u_int64_t *data) { |
|
|
|
|
|
|
|
|
SHA512_Transform(SHA512_CTX *context, const u_int64_t *data) |
|
|
|
|
|
{ |
|
|
u_int64_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int64_t a, b, c, d, e, f, g, h, s0, s1; |
|
|
u_int64_t T1, T2, *W512 = (u_int64_t *)context->buffer; |
|
|
u_int64_t T1, T2, *W512 = (u_int64_t *)context->buffer; |
|
|
int j; |
|
|
int j; |
|
@ -748,13 +753,13 @@ SHA512_Transform(SHA512_CTX *context, const u_int64_t *data) { |
|
|
#endif /* SHA2_UNROLL_TRANSFORM */ |
|
|
#endif /* SHA2_UNROLL_TRANSFORM */ |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) { |
|
|
|
|
|
unsigned int freespace, usedspace; |
|
|
|
|
|
|
|
|
SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) |
|
|
|
|
|
{ |
|
|
|
|
|
size_t freespace, usedspace; |
|
|
|
|
|
|
|
|
if (len == 0) { |
|
|
|
|
|
/* Calling with no data is valid - we do nothing */ |
|
|
|
|
|
|
|
|
/* Calling with no data is valid (we do nothing) */ |
|
|
|
|
|
if (len == 0) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
|
|
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
|
|
if (usedspace > 0) { |
|
|
if (usedspace > 0) { |
|
@ -794,7 +799,8 @@ SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA512_Last(SHA512_CTX *context) { |
|
|
|
|
|
|
|
|
SHA512_Last(SHA512_CTX *context) |
|
|
|
|
|
{ |
|
|
unsigned int usedspace; |
|
|
unsigned int usedspace; |
|
|
|
|
|
|
|
|
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
|
|
usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
|
@ -836,11 +842,12 @@ SHA512_Last(SHA512_CTX *context) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA512_Final(u_int8_t digest[], SHA512_CTX *context) { |
|
|
|
|
|
|
|
|
SHA512_Final(u_int8_t digest[], SHA512_CTX *context) |
|
|
|
|
|
{ |
|
|
u_int64_t *d = (u_int64_t *)digest; |
|
|
u_int64_t *d = (u_int64_t *)digest; |
|
|
|
|
|
|
|
|
/* If no digest buffer is passed, we don't bother doing this: */ |
|
|
/* If no digest buffer is passed, we don't bother doing this: */ |
|
|
if (digest != (u_int8_t *)0) { |
|
|
|
|
|
|
|
|
if (digest != NULL) { |
|
|
SHA512_Last(context); |
|
|
SHA512_Last(context); |
|
|
|
|
|
|
|
|
/* Save the hash data for output: */ |
|
|
/* Save the hash data for output: */ |
|
@ -865,26 +872,28 @@ SHA512_Final(u_int8_t digest[], SHA512_CTX *context) { |
|
|
|
|
|
|
|
|
/*** SHA-384: *********************************************************/ |
|
|
/*** SHA-384: *********************************************************/ |
|
|
void |
|
|
void |
|
|
SHA384_Init(SHA384_CTX *context) { |
|
|
|
|
|
if (context == (SHA384_CTX *)0) { |
|
|
|
|
|
|
|
|
SHA384_Init(SHA384_CTX *context) |
|
|
|
|
|
{ |
|
|
|
|
|
if (context == NULL) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
|
|
|
memcpy(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH); |
|
|
memcpy(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH); |
|
|
memset(context->buffer, 0, SHA384_BLOCK_LENGTH); |
|
|
memset(context->buffer, 0, SHA384_BLOCK_LENGTH); |
|
|
context->bitcount[0] = context->bitcount[1] = 0; |
|
|
context->bitcount[0] = context->bitcount[1] = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA384_Update(SHA384_CTX *context, const u_int8_t *data, size_t len) { |
|
|
|
|
|
|
|
|
SHA384_Update(SHA384_CTX *context, const u_int8_t *data, size_t len) |
|
|
|
|
|
{ |
|
|
SHA512_Update((SHA512_CTX *)context, data, len); |
|
|
SHA512_Update((SHA512_CTX *)context, data, len); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void |
|
|
void |
|
|
SHA384_Final(u_int8_t digest[], SHA384_CTX *context) { |
|
|
|
|
|
|
|
|
SHA384_Final(u_int8_t digest[], SHA384_CTX *context) |
|
|
|
|
|
{ |
|
|
u_int64_t *d = (u_int64_t *)digest; |
|
|
u_int64_t *d = (u_int64_t *)digest; |
|
|
|
|
|
|
|
|
/* If no digest buffer is passed, we don't bother doing this: */ |
|
|
/* If no digest buffer is passed, we don't bother doing this: */ |
|
|
if (digest != (u_int8_t *)0) { |
|
|
|
|
|
|
|
|
if (digest != NULL) { |
|
|
SHA512_Last((SHA512_CTX *)context); |
|
|
SHA512_Last((SHA512_CTX *)context); |
|
|
|
|
|
|
|
|
/* Save the hash data for output: */ |
|
|
/* Save the hash data for output: */ |
|
|