From d9fa4aa703ffb19f3dd3c349de96a34aa0a6458e Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sat, 20 Nov 2010 11:15:44 +0000 Subject: [PATCH] 2010-11-20 12:14 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * src/rtl/sha1hmac.h * src/rtl/sha1hmac.c * src/rtl/hbsha1.c * src/rtl/hbsha1hm.c * src/rtl/sha1.c * src/rtl/sha1.h * Reverted previous two fix attempts (for the most part) and replaced it with patch posted by Przemek. This fixes low level SHA1 code, and it's the efficient solution. --- harbour/ChangeLog | 11 +++ harbour/src/rtl/hbsha1.c | 77 +++++++++------------ harbour/src/rtl/hbsha1hm.c | 135 +++++++++++++++++-------------------- harbour/src/rtl/sha1.c | 6 +- harbour/src/rtl/sha1.h | 2 +- harbour/src/rtl/sha1hmac.c | 4 +- harbour/src/rtl/sha1hmac.h | 4 +- 7 files changed, 113 insertions(+), 126 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 396940728a..6a26f9b690 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,17 @@ The license applies to all entries newer than 2009-04-28. */ +2010-11-20 12:14 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) + * src/rtl/sha1hmac.h + * src/rtl/sha1hmac.c + * src/rtl/hbsha1.c + * src/rtl/hbsha1hm.c + * src/rtl/sha1.c + * src/rtl/sha1.h + * Reverted previous two fix attempts (for the most part) and replaced + it with patch posted by Przemek. This fixes low level SHA1 code, + and it's the efficient solution. + 2010-11-20 11:28 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * src/rtl/sha1hmac.h * src/rtl/sha1hmac.c diff --git a/harbour/src/rtl/hbsha1.c b/harbour/src/rtl/hbsha1.c index 79d6f7a486..34eee4cdd1 100644 --- a/harbour/src/rtl/hbsha1.c +++ b/harbour/src/rtl/hbsha1.c @@ -51,65 +51,54 @@ */ #include "hbapi.h" -#include "hbapiitm.h" -#include "hbapierr.h" #include "sha1.h" HB_FUNC( HB_SHA1 ) { - PHB_ITEM pBuffer = hb_param( 1, HB_IT_STRING ); + sha1_byte digest[ SHA1_DIGEST_LENGTH ]; + SHA_CTX ctx; - if( pBuffer ) + hb_SHA1_Init( &ctx ); + + #if HB_SIZE_MAX > UINT_MAX { - char * buffer = hb_itemGetC( pBuffer ); - sha1_byte digest[ SHA1_DIGEST_LENGTH ]; - SHA_CTX ctx; + const char * buffer = hb_parcx( 1 ); + HB_SIZE nCount = hb_parclen( 1 ); + HB_SIZE nDone = 0; - hb_SHA1_Init( &ctx ); - - #if HB_SIZE_MAX > UINT_MAX + while( nCount ) { - HB_SIZE nCount = hb_itemGetCLen( pBuffer ); - HB_SIZE nDone = 0; + unsigned int uiChunk; - while( nCount ) + if( nCount > ( HB_SIZE ) UINT_MAX ) { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_SHA1_Update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; + uiChunk = UINT_MAX; + nCount -= ( HB_SIZE ) uiChunk; } + else + { + uiChunk = ( unsigned int ) nCount; + nCount = 0; + } + + hb_SHA1_Update( &ctx, buffer + nDone, uiChunk ); + + nDone += ( HB_SIZE ) uiChunk; } - #else - hb_SHA1_Update( &ctx, buffer, hb_itemGetCLen( pBuffer ) ); - #endif + } + #else + hb_SHA1_Update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); + #endif - hb_SHA1_Final( digest, &ctx ); + hb_SHA1_Final( digest, &ctx ); - hb_itemFreeC( buffer ); - - if( ! hb_parl( 2 ) ) - { - char hex[ ( sizeof( digest ) * 2 ) + 1 ]; - hb_strtohex( ( char * ) digest, sizeof( digest ), hex ); - hb_retclen( hex, HB_SIZEOFARRAY( hex ) - 1 ); - } - else - hb_retclen( ( char * ) digest, sizeof( digest ) ); + if( ! hb_parl( 2 ) ) + { + char hex[ ( sizeof( digest ) * 2 ) + 1 ]; + hb_strtohex( ( char * ) digest, sizeof( digest ), hex ); + hb_retclen( hex, HB_SIZEOFARRAY( hex ) - 1 ); } else - hb_errRT_BASE_SubstR( EG_ARG, 3999, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); + hb_retclen( ( char * ) digest, sizeof( digest ) ); } diff --git a/harbour/src/rtl/hbsha1hm.c b/harbour/src/rtl/hbsha1hm.c index 5ae866386c..6e3ea9ee90 100644 --- a/harbour/src/rtl/hbsha1hm.c +++ b/harbour/src/rtl/hbsha1hm.c @@ -51,100 +51,85 @@ */ #include "hbapi.h" -#include "hbapiitm.h" -#include "hbapierr.h" #include "sha1hmac.h" HB_FUNC( HB_HMAC_SHA1 ) { - PHB_ITEM pKey = hb_param( 1, HB_IT_STRING ); - PHB_ITEM pBuffer = hb_param( 2, HB_IT_STRING ); + unsigned char mac[ HMAC_SHA1_DIGEST_LENGTH ]; + HMAC_SHA1_CTX ctx; - if( pKey && pBuffer ) + hb_HMAC_SHA1_Init( &ctx ); + #if HB_SIZE_MAX > UINT_MAX { - char * buffer = hb_itemGetC( pBuffer ); - unsigned char mac[ HMAC_SHA1_DIGEST_LENGTH ]; - HMAC_SHA1_CTX ctx; + const char * buffer = hb_parcx( 2 ); + HB_SIZE nCount = hb_parclen( 2 ); + HB_SIZE nDone = 0; - hb_HMAC_SHA1_Init( &ctx ); - #if HB_SIZE_MAX > UINT_MAX + while( nCount ) { - HB_SIZE nCount = hb_itemGetCLen( pBuffer ); - HB_SIZE nDone = 0; + unsigned int uiChunk; - while( nCount ) + if( nCount > ( HB_SIZE ) UINT_MAX ) { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_HMAC_SHA1_UpdateKey( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; + uiChunk = UINT_MAX; + nCount -= ( HB_SIZE ) uiChunk; } - } - #else - hb_HMAC_SHA1_UpdateKey( &ctx, buffer, hb_itemGetCLen( pBuffer ) ); - #endif - hb_HMAC_SHA1_EndKey( &ctx ); - - hb_itemFreeC( buffer ); - - buffer = hb_itemGetC( pKey ); - - hb_HMAC_SHA1_StartMessage( &ctx ); - #if HB_SIZE_MAX > UINT_MAX - { - HB_SIZE nCount = hb_itemGetCLen( pKey ); - HB_SIZE nDone = 0; - - while( nCount ) + else { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_HMAC_SHA1_UpdateMessage( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; + uiChunk = ( unsigned int ) nCount; + nCount = 0; } + + hb_HMAC_SHA1_UpdateKey( &ctx, buffer + nDone, uiChunk ); + + nDone += ( HB_SIZE ) uiChunk; } - #else - hb_HMAC_SHA1_UpdateMessage( &ctx, buffer, hb_itemGetCLen( pKey ) ); - #endif - hb_HMAC_SHA1_EndMessage( mac, &ctx ); - hb_HMAC_SHA1_Done( &ctx ); + } + #else + hb_HMAC_SHA1_UpdateKey( &ctx, hb_parcx( 2 ), hb_parclen( 2 ) ); + #endif + hb_HMAC_SHA1_EndKey( &ctx ); - hb_itemFreeC( buffer ); + hb_HMAC_SHA1_StartMessage( &ctx ); + #if HB_SIZE_MAX > UINT_MAX + { + const char * buffer = hb_parcx( 1 ); + HB_SIZE nCount = hb_parclen( 1 ); + HB_SIZE nDone = 0; - if( ! hb_parl( 3 ) ) + while( nCount ) { - char hex[ ( sizeof( mac ) * 2 ) + 1 ]; - hb_strtohex( ( char * ) mac, sizeof( mac ), hex ); - hb_retclen( hex, HB_SIZEOFARRAY( hex ) - 1 ); + unsigned int uiChunk; + + if( nCount > ( HB_SIZE ) UINT_MAX ) + { + uiChunk = UINT_MAX; + nCount -= ( HB_SIZE ) uiChunk; + } + else + { + uiChunk = ( unsigned int ) nCount; + nCount = 0; + } + + hb_HMAC_SHA1_UpdateMessage( &ctx, buffer + nDone, uiChunk ); + + nDone += ( HB_SIZE ) uiChunk; } - else - hb_retclen( ( char * ) mac, sizeof( mac ) ); + } + #else + hb_HMAC_SHA1_UpdateMessage( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); + #endif + hb_HMAC_SHA1_EndMessage( mac, &ctx ); + hb_HMAC_SHA1_Done( &ctx ); + + if( ! hb_parl( 3 ) ) + { + char hex[ ( sizeof( mac ) * 2 ) + 1 ]; + hb_strtohex( ( char * ) mac, sizeof( mac ), hex ); + hb_retclen( hex, HB_SIZEOFARRAY( hex ) - 1 ); } else - hb_errRT_BASE_SubstR( EG_ARG, 3999, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); + hb_retclen( ( char * ) mac, sizeof( mac ) ); } diff --git a/harbour/src/rtl/sha1.c b/harbour/src/rtl/sha1.c index 53604317d7..7c6e5956ff 100644 --- a/harbour/src/rtl/sha1.c +++ b/harbour/src/rtl/sha1.c @@ -122,7 +122,7 @@ void hb_SHA1_Init(SHA_CTX* context) { } /* Run your data through this. */ -void hb_SHA1_Update(SHA_CTX *context, void *datav, unsigned int len) { +void hb_SHA1_Update(SHA_CTX *context, const void *datav, unsigned int len) { sha1_byte * data = ( sha1_byte * ) datav; unsigned int i, j; @@ -133,7 +133,9 @@ void hb_SHA1_Update(SHA_CTX *context, void *datav, unsigned int len) { memcpy(&context->buffer[j], data, (i = 64-j)); SHA1_Transform(context->state, context->buffer); for ( ; i + 63 < len; i += 64) { - SHA1_Transform(context->state, &data[i]); + sha1_byte buffer[64]; + memcpy(buffer, &data[i], 64); + SHA1_Transform(context->state, buffer); } j = 0; } diff --git a/harbour/src/rtl/sha1.h b/harbour/src/rtl/sha1.h index ad5141bbaa..dbfefc6071 100644 --- a/harbour/src/rtl/sha1.h +++ b/harbour/src/rtl/sha1.h @@ -67,7 +67,7 @@ typedef struct _SHA_CTX { #ifndef NOPROTO void hb_SHA1_Init(SHA_CTX *context); -void hb_SHA1_Update(SHA_CTX *context, void *data, unsigned int len); +void hb_SHA1_Update(SHA_CTX *context, const void *data, unsigned int len); void hb_SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX* context); #else void hb_SHA1_Init(); diff --git a/harbour/src/rtl/sha1hmac.c b/harbour/src/rtl/sha1hmac.c index 3d5fe66405..9fbede1e41 100644 --- a/harbour/src/rtl/sha1hmac.c +++ b/harbour/src/rtl/sha1hmac.c @@ -80,7 +80,7 @@ void hb_HMAC_SHA1_Init(HMAC_SHA1_CTX *ctx) { ctx->hashkey = 0; } -void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, void *key, unsigned int keylen) { +void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, unsigned int keylen) { /* Do we have anything to work with? If not, return right away. */ if (keylen < 1) @@ -167,7 +167,7 @@ void hb_HMAC_SHA1_StartMessage(HMAC_SHA1_CTX *ctx) { hb_SHA1_Update(&ctx->shactx, &(ctx->ipad[0]), HMAC_SHA1_BLOCK_LENGTH); } -void hb_HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, void *data, unsigned int datalen) { +void hb_HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, const void *data, unsigned int datalen) { hb_SHA1_Update(&ctx->shactx, data, datalen); } diff --git a/harbour/src/rtl/sha1hmac.h b/harbour/src/rtl/sha1hmac.h index 794d046a56..516579e911 100644 --- a/harbour/src/rtl/sha1hmac.h +++ b/harbour/src/rtl/sha1hmac.h @@ -73,10 +73,10 @@ typedef struct _HMAC_SHA1_CTX { #ifndef NOPROTO void hb_HMAC_SHA1_Init(HMAC_SHA1_CTX *ctx); -void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, void *key, unsigned int keylen); +void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, unsigned int keylen); void hb_HMAC_SHA1_EndKey(HMAC_SHA1_CTX *ctx); void hb_HMAC_SHA1_StartMessage(HMAC_SHA1_CTX *ctx); -void hb_HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, void *data, unsigned int datalen); +void hb_HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, const void *data, unsigned int datalen); void hb_HMAC_SHA1_EndMessage(unsigned char *out, HMAC_SHA1_CTX *ctx); void hb_HMAC_SHA1_Done(HMAC_SHA1_CTX *ctx); #else