Skip to content

Commit

Permalink
Ensure numbers are 32bits in sha2.c
Browse files Browse the repository at this point in the history
  • Loading branch information
kanoi committed Jan 30, 2012
1 parent 280588c commit 0c27237
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 10 additions & 10 deletions sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
#ifndef GET_ULONG_BE
#define GET_ULONG_BE(n,b,i) \
{ \
(n) = ( (unsigned long) (b)[(i) ] << 24 ) \
| ( (unsigned long) (b)[(i) + 1] << 16 ) \
| ( (unsigned long) (b)[(i) + 2] << 8 ) \
| ( (unsigned long) (b)[(i) + 3] ); \
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
| ( (uint32_t) (b)[(i) + 3] ); \
}
#endif

Expand Down Expand Up @@ -95,8 +95,8 @@ void sha2_starts( sha2_context *ctx, int is224 )

static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
{
unsigned long temp1, temp2, W[64];
unsigned long A, B, C, D, E, F, G, H;
uint32_t temp1, temp2, W[64];
uint32_t A, B, C, D, E, F, G, H;

GET_ULONG_BE( W[ 0], data, 0 );
GET_ULONG_BE( W[ 1], data, 4 );
Expand Down Expand Up @@ -230,7 +230,7 @@ static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
{
int fill;
unsigned long left;
uint32_t left;

if( ilen <= 0 )
return;
Expand All @@ -241,7 +241,7 @@ void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
ctx->total[0] += ilen;
ctx->total[0] &= 0xFFFFFFFF;

if( ctx->total[0] < (unsigned long) ilen )
if( ctx->total[0] < (uint32_t) ilen )
ctx->total[1]++;

if( left && ilen >= fill )
Expand Down Expand Up @@ -281,8 +281,8 @@ static const unsigned char sha2_padding[64] =
*/
void sha2_finish( sha2_context *ctx, unsigned char output[32] )
{
unsigned long last, padn;
unsigned long high, low;
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];

high = ( ctx->total[0] >> 29 )
Expand Down
6 changes: 4 additions & 2 deletions sha2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
#ifndef POLARSSL_SHA2_H
#define POLARSSL_SHA2_H

#include <stdint.h>

/**
* \brief SHA-256 context structure
*/
typedef struct
{
unsigned long total[2]; /*!< number of bytes processed */
unsigned long state[8]; /*!< intermediate digest state */
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[8]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */

unsigned char ipad[64]; /*!< HMAC: inner padding */
Expand Down

0 comments on commit 0c27237

Please sign in to comment.