Browse Source

Ensure numbers are 32bits in sha2.c

nfactor-troky
Kano 13 years ago
parent
commit
0c27237031
  1. 20
      sha2.c
  2. 6
      sha2.h

20
sha2.c

@ -40,10 +40,10 @@
#ifndef GET_ULONG_BE #ifndef GET_ULONG_BE
#define GET_ULONG_BE(n,b,i) \ #define GET_ULONG_BE(n,b,i) \
{ \ { \
(n) = ( (unsigned long) (b)[(i) ] << 24 ) \ (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
| ( (unsigned long) (b)[(i) + 1] << 16 ) \ | ( (uint32_t) (b)[(i) + 1] << 16 ) \
| ( (unsigned long) (b)[(i) + 2] << 8 ) \ | ( (uint32_t) (b)[(i) + 2] << 8 ) \
| ( (unsigned long) (b)[(i) + 3] ); \ | ( (uint32_t) (b)[(i) + 3] ); \
} }
#endif #endif
@ -95,8 +95,8 @@ void sha2_starts( sha2_context *ctx, int is224 )
static void sha2_process( sha2_context *ctx, const unsigned char data[64] ) static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
{ {
unsigned long temp1, temp2, W[64]; uint32_t temp1, temp2, W[64];
unsigned long A, B, C, D, E, F, G, H; uint32_t A, B, C, D, E, F, G, H;
GET_ULONG_BE( W[ 0], data, 0 ); GET_ULONG_BE( W[ 0], data, 0 );
GET_ULONG_BE( W[ 1], data, 4 ); GET_ULONG_BE( W[ 1], data, 4 );
@ -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 ) void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
{ {
int fill; int fill;
unsigned long left; uint32_t left;
if( ilen <= 0 ) if( ilen <= 0 )
return; return;
@ -241,7 +241,7 @@ void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
ctx->total[0] += ilen; ctx->total[0] += ilen;
ctx->total[0] &= 0xFFFFFFFF; ctx->total[0] &= 0xFFFFFFFF;
if( ctx->total[0] < (unsigned long) ilen ) if( ctx->total[0] < (uint32_t) ilen )
ctx->total[1]++; ctx->total[1]++;
if( left && ilen >= fill ) if( left && ilen >= fill )
@ -281,8 +281,8 @@ static const unsigned char sha2_padding[64] =
*/ */
void sha2_finish( sha2_context *ctx, unsigned char output[32] ) void sha2_finish( sha2_context *ctx, unsigned char output[32] )
{ {
unsigned long last, padn; uint32_t last, padn;
unsigned long high, low; uint32_t high, low;
unsigned char msglen[8]; unsigned char msglen[8];
high = ( ctx->total[0] >> 29 ) high = ( ctx->total[0] >> 29 )

6
sha2.h

@ -26,13 +26,15 @@
#ifndef POLARSSL_SHA2_H #ifndef POLARSSL_SHA2_H
#define POLARSSL_SHA2_H #define POLARSSL_SHA2_H
#include <stdint.h>
/** /**
* \brief SHA-256 context structure * \brief SHA-256 context structure
*/ */
typedef struct typedef struct
{ {
unsigned long total[2]; /*!< number of bytes processed */ uint32_t total[2]; /*!< number of bytes processed */
unsigned long state[8]; /*!< intermediate digest state */ uint32_t state[8]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */ unsigned char buffer[64]; /*!< data block being processed */
unsigned char ipad[64]; /*!< HMAC: inner padding */ unsigned char ipad[64]; /*!< HMAC: inner padding */

Loading…
Cancel
Save