#if !defined( _sha1_h )
#define _sha1_h

#include "types.h"
#include "endian.h"

#if defined( __cplusplus )
extern "C" {
#endif

#define SHA1_INPUT_BYTES 64	/* 512 bits */
#define SHA1_INPUT_WORDS ( SHA1_INPUT_BYTES >> 2 )
#define SHA1_DIGEST_WORDS 5	/* 160 bits */
#define SHA1_DIGEST_BYTES ( SHA1_DIGEST_WORDS * 4 )

typedef struct {
    word32 H[ SHA1_DIGEST_WORDS ];
#if defined( word64 )
    word64 bits;		/* we want a 64 bit word */
#else
    word32 hbits, lbits;	/* if we don't have one we simulate it */
#endif
    byte M[ SHA1_INPUT_BYTES ];
} SHA1_ctx;

#define SHA1_set_IV( ctx, IV ) \
    memcpy( (ctx)->H, IV, SHA1_DIGEST_BYTES )

#if defined( word64 )
#define SHA1_zero_bitcount( ctx )\
    (ctx)->bits = 0
#else\
#define SHA1_zero_bitcount( ctx )\
    (ctx)->lbits = 0;\
    (ctx)->hbits = 0
#endif

extern word32 SHA1_IV[ 5 ];

#define SHA1_init( ctx ) \
    SHA1_zero_bitcount( ctx ); \
    SHA1_set_IV( ctx, SHA1_IV )

void SHA1_update( SHA1_ctx*, const void*, word32 );
void SHA1_final ( SHA1_ctx* );

/* called by macro */
const byte* SHA1_get_digest( const SHA1_ctx* );

/* result is in reused memory, copy it if you want to keep it */
#define SHA1_digest( ctx ) \
    ( little_endian ? SHA1_get_digest( ctx ) : (const byte*) (ctx)->H )

/* these provide extra access to internals of SHA1 for MDC and MACs */

void SHA1_init_with_IV( SHA1_ctx*, const byte[ SHA1_DIGEST_BYTES ] );
void SHA1_transform( word32[ SHA1_DIGEST_WORDS ], 
		     const byte[ SHA1_INPUT_BYTES ] );

#if defined( __cplusplus )
}
#endif

#endif
