diff --git a/ChangeLog.txt b/ChangeLog.txt index 153caf15f8..50e6a9cc7a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -7,6 +7,29 @@ Entries may not always be in chronological/commit order. See license at the end of file. */ +2025-11-16 22:00 UTC+0200 Aleksander Czajczynski (hb fki.pl) + ; import hbcrypto.h header that allows reusing core sha functions + by contribs, coming from Viktor's fork: + 2015-07-13 19:39 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com) + 2015-07-13 23:12 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com) + + include/hbcrypto.h + + move sha2 and hmac/sha2 related headers to Harbour + header directory so they can be used by other components. + + export sha2 and hmac/sha2 low-level functions from Harbour dynlib + * src/rtl/sha1.c + * src/rtl/sha1.h + * src/rtl/sha1hmac.c + * src/rtl/sha1hmac.h + * src/rtl/sha2.c + * src/rtl/sha2hmac.c + * src/rtl/hbsha1.c + * src/rtl/hbsha1hm.c + * src/rtl/hbsha2.c + * src/rtl/hbsha2hm.c + + use Harbour index type in low-level SHA code + % drop high-level hack compensating for fixed sized + low-level indexes in 64-bit builds + 2025-10-23 21:45 UTC+0200 Aleksander Czajczynski (hb fki.pl) * src/vm/extrap.c + added Windows ARM64 CPU register dump diff --git a/src/rtl/hbsha1.c b/src/rtl/hbsha1.c index 76d11a75d1..71e7a1e70c 100644 --- a/src/rtl/hbsha1.c +++ b/src/rtl/hbsha1.c @@ -1,7 +1,7 @@ /* * SHA1 Harbour wrappers * - * Copyright 2009 Viktor Szakats (vszakats.net/harbour) + * Copyright 2009 Viktor Szakats (vsz.me/hb) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -48,43 +48,13 @@ #include "sha1.h" -HB_FUNC( HB_SHA1 ) +HB_FUNC( HB_SHA1 ) /* Considered insecure. Use SHA256 or higher instead. */ { sha1_byte digest[ SHA1_DIGEST_LENGTH ]; - SHA_CTX ctx; + HB_SHA_CTX ctx; hb_SHA1_Init( &ctx ); - - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - 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; - } - } - #else - hb_SHA1_Update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - + hb_SHA1_Update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); hb_SHA1_Final( digest, &ctx ); if( ! hb_parl( 2 ) ) diff --git a/src/rtl/hbsha1hm.c b/src/rtl/hbsha1hm.c index e31f7b0057..eb201ffa15 100644 --- a/src/rtl/hbsha1hm.c +++ b/src/rtl/hbsha1hm.c @@ -1,7 +1,7 @@ /* * HMAC-SHA1 Harbour wrappers * - * Copyright 2009 Viktor Szakats (vszakats.net/harbour) + * Copyright 2009 Viktor Szakats (vsz.me/hb) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -54,67 +54,11 @@ HB_FUNC( HB_HMAC_SHA1 ) HMAC_SHA1_CTX ctx; hb_HMAC_SHA1_Init( &ctx ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 2 ); - HB_SIZE nCount = hb_parclen( 2 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - 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; - } - } - #else - hb_HMAC_SHA1_UpdateKey( &ctx, hb_parcx( 2 ), hb_parclen( 2 ) ); - #endif + hb_HMAC_SHA1_UpdateKey( &ctx, hb_parcx( 2 ), hb_parclen( 2 ) ); hb_HMAC_SHA1_EndKey( &ctx ); 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; - - while( nCount ) - { - 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_HMAC_SHA1_UpdateMessage( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif + hb_HMAC_SHA1_UpdateMessage( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); hb_HMAC_SHA1_EndMessage( mac, &ctx ); hb_HMAC_SHA1_Done( &ctx ); diff --git a/src/rtl/hbsha2.c b/src/rtl/hbsha2.c index fbdc1bc42e..2cbf434dfd 100644 --- a/src/rtl/hbsha2.c +++ b/src/rtl/hbsha2.c @@ -1,7 +1,7 @@ /* * SHA2 Harbour wrappers * - * Copyright 2009 Viktor Szakats (vszakats.net/harbour) + * Copyright 2009 Viktor Szakats * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,50 +46,19 @@ #include "hbapi.h" -#include "sha2.h" +#include "hbcrypto.h" HB_FUNC( HB_SHA224 ) { - unsigned char digest[ SHA224_DIGEST_SIZE ]; - sha224_ctx ctx; + unsigned char digest[ HB_SHA224_DIGEST_SIZE ]; - hb_sha224_init( &ctx ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_sha224_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_sha224_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_sha224_final( &ctx, digest ); + hb_sha224( hb_parcx( 1 ), hb_parclen( 1 ), 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 ); + hb_retclen( hex, sizeof( hex ) - 1 ); } else hb_retclen( ( char * ) digest, sizeof( digest ) ); @@ -97,46 +66,15 @@ HB_FUNC( HB_SHA224 ) HB_FUNC( HB_SHA256 ) { - unsigned char digest[ SHA256_DIGEST_SIZE ]; - sha256_ctx ctx; + unsigned char digest[ HB_SHA256_DIGEST_SIZE ]; - hb_sha256_init( &ctx ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_sha256_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_sha256_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_sha256_final( &ctx, digest ); + hb_sha256( hb_parcx( 1 ), hb_parclen( 1 ), 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 ); + hb_retclen( hex, sizeof( hex ) - 1 ); } else hb_retclen( ( char * ) digest, sizeof( digest ) ); @@ -144,46 +82,15 @@ HB_FUNC( HB_SHA256 ) HB_FUNC( HB_SHA384 ) { - unsigned char digest[ SHA384_DIGEST_SIZE ]; - sha384_ctx ctx; + unsigned char digest[ HB_SHA384_DIGEST_SIZE ]; - hb_sha384_init( &ctx ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_sha384_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_sha384_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_sha384_final( &ctx, digest ); + hb_sha384( hb_parcx( 1 ), hb_parclen( 1 ), 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 ); + hb_retclen( hex, sizeof( hex ) - 1 ); } else hb_retclen( ( char * ) digest, sizeof( digest ) ); @@ -191,46 +98,15 @@ HB_FUNC( HB_SHA384 ) HB_FUNC( HB_SHA512 ) { - unsigned char digest[ SHA512_DIGEST_SIZE ]; - sha512_ctx ctx; + unsigned char digest[ HB_SHA512_DIGEST_SIZE ]; - hb_sha512_init( &ctx ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - unsigned int uiChunk; - - if( nCount > ( HB_SIZE ) UINT_MAX ) - { - uiChunk = UINT_MAX; - nCount -= ( HB_SIZE ) uiChunk; - } - else - { - uiChunk = ( unsigned int ) nCount; - nCount = 0; - } - - hb_sha512_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_sha512_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_sha512_final( &ctx, digest ); + hb_sha512( hb_parcx( 1 ), hb_parclen( 1 ), 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 ); + hb_retclen( hex, sizeof( hex ) - 1 ); } else hb_retclen( ( char * ) digest, sizeof( digest ) ); diff --git a/src/rtl/hbsha2hm.c b/src/rtl/hbsha2hm.c index 6866d0394b..abfb034f49 100644 --- a/src/rtl/hbsha2hm.c +++ b/src/rtl/hbsha2hm.c @@ -1,7 +1,7 @@ /* * HMAC-SHA2 Harbour wrappers * - * Copyright 2009 Viktor Szakats (vszakats.net/harbour) + * Copyright 2009 Viktor Szakats * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,44 +46,13 @@ #include "hbapi.h" -#include "sha2hmac.h" +#include "hbcrypto.h" HB_FUNC( HB_HMAC_SHA224 ) { - unsigned char mac[ SHA224_DIGEST_SIZE ]; - hmac_sha224_ctx ctx; + unsigned char mac[ HB_SHA224_DIGEST_SIZE ]; - hb_hmac_sha224_init( &ctx, hb_parcx( 2 ), ( unsigned int ) hb_parclen( 2 ) ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - 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_sha224_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_hmac_sha224_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_hmac_sha224_final( &ctx, mac, HB_SIZEOFARRAY( mac ) ); + hb_hmac_sha224( hb_parcx( 2 ), hb_parclen( 2 ), hb_parcx( 1 ), hb_parclen( 1 ), mac, sizeof( mac ) ); if( ! hb_parl( 3 ) ) { @@ -97,40 +66,9 @@ HB_FUNC( HB_HMAC_SHA224 ) HB_FUNC( HB_HMAC_SHA256 ) { - unsigned char mac[ SHA256_DIGEST_SIZE ]; - hmac_sha256_ctx ctx; + unsigned char mac[ HB_SHA256_DIGEST_SIZE ]; - hb_hmac_sha256_init( &ctx, hb_parcx( 2 ), ( unsigned int ) hb_parclen( 2 ) ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - 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_sha256_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_hmac_sha256_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_hmac_sha256_final( &ctx, mac, HB_SIZEOFARRAY( mac ) ); + hb_hmac_sha256( hb_parcx( 2 ), hb_parclen( 2 ), hb_parcx( 1 ), hb_parclen( 1 ), mac, sizeof( mac ) ); if( ! hb_parl( 3 ) ) { @@ -144,40 +82,9 @@ HB_FUNC( HB_HMAC_SHA256 ) HB_FUNC( HB_HMAC_SHA384 ) { - unsigned char mac[ SHA384_DIGEST_SIZE ]; - hmac_sha384_ctx ctx; + unsigned char mac[ HB_SHA384_DIGEST_SIZE ]; - hb_hmac_sha384_init( &ctx, hb_parcx( 2 ), ( unsigned int ) hb_parclen( 2 ) ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - 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_sha384_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_hmac_sha384_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_hmac_sha384_final( &ctx, mac, HB_SIZEOFARRAY( mac ) ); + hb_hmac_sha384( hb_parcx( 2 ), hb_parclen( 2 ), hb_parcx( 1 ), hb_parclen( 1 ), mac, sizeof( mac ) ); if( ! hb_parl( 3 ) ) { @@ -191,40 +98,9 @@ HB_FUNC( HB_HMAC_SHA384 ) HB_FUNC( HB_HMAC_SHA512 ) { - unsigned char mac[ SHA512_DIGEST_SIZE ]; - hmac_sha512_ctx ctx; + unsigned char mac[ HB_SHA512_DIGEST_SIZE ]; - hb_hmac_sha512_init( &ctx, hb_parcx( 2 ), ( unsigned int ) hb_parclen( 2 ) ); - #if HB_SIZE_MAX > UINT_MAX - { - const char * buffer = hb_parcx( 1 ); - HB_SIZE nCount = hb_parclen( 1 ); - HB_SIZE nDone = 0; - - while( nCount ) - { - 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_sha512_update( &ctx, buffer + nDone, uiChunk ); - - nDone += ( HB_SIZE ) uiChunk; - } - } - #else - hb_hmac_sha512_update( &ctx, hb_parcx( 1 ), hb_parclen( 1 ) ); - #endif - hb_hmac_sha512_final( &ctx, mac, HB_SIZEOFARRAY( mac ) ); + hb_hmac_sha512( hb_parcx( 2 ), hb_parclen( 2 ), hb_parcx( 1 ), hb_parclen( 1 ), mac, sizeof( mac ) ); if( ! hb_parl( 3 ) ) { diff --git a/src/rtl/sha1.c b/src/rtl/sha1.c index 31451adfad..36e8acd59d 100644 --- a/src/rtl/sha1.c +++ b/src/rtl/sha1.c @@ -8,7 +8,7 @@ * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN * * The original unmodified version is available at: - * http://www.nic.funet.fi/pub/crypt/hash/sha/sha1.c + * https://www.nic.funet.fi/pub/crypt/hash/sha/sha1.c * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -109,7 +109,7 @@ static void SHA1_Transform(sha1_quadbyte state[5], sha1_byte buffer[64]) { /* SHA1_Init - Initialize new context */ -void hb_SHA1_Init(SHA_CTX* context) { +void hb_SHA1_Init(HB_SHA_CTX* context) { /* SHA1 initialization constants */ context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; @@ -120,13 +120,13 @@ void hb_SHA1_Init(SHA_CTX* context) { } /* Run your data through this. */ -void hb_SHA1_Update(SHA_CTX *context, const void *datav, unsigned int len) { +void hb_SHA1_Update(HB_SHA_CTX *context, const void *datav, HB_SIZE len) { const sha1_byte * data = ( const sha1_byte * ) datav; - unsigned int i, j; + HB_SIZE i, j; j = (context->count[0] >> 3) & 63; - if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; - context->count[1] += (len >> 29); + if ((context->count[0] += (sha1_quadbyte) len << 3) < ((sha1_quadbyte) len << 3)) context->count[1]++; + context->count[1] += ((sha1_quadbyte) len >> 29); if ((j + len) > 63) { memcpy(&context->buffer[j], data, (i = 64-j)); SHA1_Transform(context->state, context->buffer); @@ -143,7 +143,7 @@ void hb_SHA1_Update(SHA_CTX *context, const void *datav, unsigned int len) { /* Add padding and return the message digest. */ -void hb_SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX *context) { +void hb_SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], HB_SHA_CTX *context) { sha1_quadbyte i; sha1_byte finalcount[8]; diff --git a/src/rtl/sha1.h b/src/rtl/sha1.h index 837bf3347a..fc851d0d0a 100644 --- a/src/rtl/sha1.h +++ b/src/rtl/sha1.h @@ -9,7 +9,7 @@ * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN * * The original unmodified version is available at: - * http://www.nic.funet.fi/pub/crypt/hash/sha/sha1.c + * https://www.nic.funet.fi/pub/crypt/hash/sha/sha1.c * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -55,23 +55,23 @@ typedef unsigned char sha1_byte; /* single byte type */ #define SHA1_DIGEST_LENGTH 20 /* The SHA1 structure: */ -typedef struct _SHA_CTX { +typedef struct { sha1_quadbyte state[5]; sha1_quadbyte count[2]; sha1_byte buffer[SHA1_BLOCK_LENGTH]; -} SHA_CTX; +} HB_SHA_CTX; #ifndef NOPROTO -void hb_SHA1_Init(SHA_CTX *context); -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); +void hb_SHA1_Init(HB_SHA_CTX *context); +void hb_SHA1_Update(HB_SHA_CTX *context, const void *data, HB_SIZE len); +void hb_SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], HB_SHA_CTX* context); #else void hb_SHA1_Init(); void hb_SHA1_Update(); void hb_SHA1_Final(); #endif -#ifdef __cplusplus +#ifdef __cplusplus } #endif diff --git a/src/rtl/sha1hmac.c b/src/rtl/sha1hmac.c index 005cd3fb1c..f6321a17fd 100644 --- a/src/rtl/sha1hmac.c +++ b/src/rtl/sha1hmac.c @@ -59,10 +59,6 @@ #include "sha1hmac.h" #include -#ifdef __cplusplus -extern "C" { -#endif - /* Filler bytes: */ #define IPAD_BYTE 0x36 #define OPAD_BYTE 0x5c @@ -76,7 +72,7 @@ void hb_HMAC_SHA1_Init(HMAC_SHA1_CTX *ctx) { ctx->hashkey = 0; } -void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, unsigned int keylen) { +void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, HB_SIZE keylen) { /* Do we have anything to work with? If not, return right away. */ if (keylen < 1) @@ -132,7 +128,7 @@ void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, unsigned int ke void hb_HMAC_SHA1_EndKey(HMAC_SHA1_CTX *ctx) { unsigned char *ipad, *opad, *key; - unsigned int i; + HB_SIZE i; /* Did we end up hashing the key? */ if (ctx->hashkey) { @@ -163,13 +159,13 @@ 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, const void *data, unsigned int datalen) { +void hb_HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, const void *data, HB_SIZE datalen) { hb_SHA1_Update(&ctx->shactx, data, datalen); } void hb_HMAC_SHA1_EndMessage(unsigned char *out, HMAC_SHA1_CTX *ctx) { unsigned char buf[HMAC_SHA1_DIGEST_LENGTH]; - SHA_CTX *c = &ctx->shactx; + HB_SHA_CTX *c = &ctx->shactx; hb_SHA1_Final(&(buf[0]), c); hb_SHA1_Init(c); @@ -187,8 +183,4 @@ void hb_HMAC_SHA1_Done(HMAC_SHA1_CTX *ctx) { ctx->hashkey = 0; } -#ifdef __cplusplus -} -#endif - #endif diff --git a/src/rtl/sha1hmac.h b/src/rtl/sha1hmac.h index 3055769ccb..4ece4adb53 100644 --- a/src/rtl/sha1hmac.h +++ b/src/rtl/sha1hmac.h @@ -50,9 +50,7 @@ #include "sha1.h" -#ifdef __cplusplus -extern "C" { -#endif +HB_EXTERN_BEGIN #define HMAC_SHA1_DIGEST_LENGTH 20 #define HMAC_SHA1_BLOCK_LENGTH 64 @@ -61,18 +59,18 @@ extern "C" { typedef struct _HMAC_SHA1_CTX { unsigned char ipad[HMAC_SHA1_BLOCK_LENGTH]; unsigned char opad[HMAC_SHA1_BLOCK_LENGTH]; - SHA_CTX shactx; + HB_SHA_CTX shactx; unsigned char key[HMAC_SHA1_BLOCK_LENGTH]; - unsigned int keylen; - unsigned int hashkey; + HB_SIZE keylen; + HB_SIZE hashkey; } HMAC_SHA1_CTX; #ifndef NOPROTO void hb_HMAC_SHA1_Init(HMAC_SHA1_CTX *ctx); -void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, unsigned int keylen); +void hb_HMAC_SHA1_UpdateKey(HMAC_SHA1_CTX *ctx, const void *key, HB_SIZE 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, const void *data, unsigned int datalen); +void hb_HMAC_SHA1_UpdateMessage(HMAC_SHA1_CTX *ctx, const void *data, HB_SIZE datalen); void hb_HMAC_SHA1_EndMessage(unsigned char *out, HMAC_SHA1_CTX *ctx); void hb_HMAC_SHA1_Done(HMAC_SHA1_CTX *ctx); #else @@ -85,8 +83,6 @@ void hb_HMAC_SHA1_EndMessage(); void hb_HMAC_SHA1_Done(); #endif -#ifdef __cplusplus -} -#endif +HB_EXTERN_END #endif diff --git a/src/rtl/sha2.c b/src/rtl/sha2.c index 1a1bcacaa5..590eb424c4 100644 --- a/src/rtl/sha2.c +++ b/src/rtl/sha2.c @@ -35,10 +35,8 @@ #define UNROLL_LOOPS /* Enable loops unrolling */ #endif -#include - -#include "sha2.h" -#include "hbdefs.h" +#include "hbapi.h" +#include "hbcrypto.h" #define SHFR(x, n) (x >> n) #define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) @@ -58,42 +56,42 @@ #define UNPACK32(x, str) \ { \ - *((str) + 3) = (uint8) ((x) ); \ - *((str) + 2) = (uint8) ((x) >> 8); \ - *((str) + 1) = (uint8) ((x) >> 16); \ - *((str) + 0) = (uint8) ((x) >> 24); \ + *((str) + 3) = (HB_U8) ((x) ); \ + *((str) + 2) = (HB_U8) ((x) >> 8); \ + *((str) + 1) = (HB_U8) ((x) >> 16); \ + *((str) + 0) = (HB_U8) ((x) >> 24); \ } #define PACK32(str, x) \ { \ - *(x) = ((uint32) *((str) + 3) ) \ - | ((uint32) *((str) + 2) << 8) \ - | ((uint32) *((str) + 1) << 16) \ - | ((uint32) *((str) + 0) << 24); \ + *(x) = ((HB_U32) *((str) + 3) ) \ + | ((HB_U32) *((str) + 2) << 8) \ + | ((HB_U32) *((str) + 1) << 16) \ + | ((HB_U32) *((str) + 0) << 24); \ } #define UNPACK64(x, str) \ { \ - *((str) + 7) = (uint8) ((x) ); \ - *((str) + 6) = (uint8) ((x) >> 8); \ - *((str) + 5) = (uint8) ((x) >> 16); \ - *((str) + 4) = (uint8) ((x) >> 24); \ - *((str) + 3) = (uint8) ((x) >> 32); \ - *((str) + 2) = (uint8) ((x) >> 40); \ - *((str) + 1) = (uint8) ((x) >> 48); \ - *((str) + 0) = (uint8) ((x) >> 56); \ + *((str) + 7) = (HB_U8) ((x) ); \ + *((str) + 6) = (HB_U8) ((x) >> 8); \ + *((str) + 5) = (HB_U8) ((x) >> 16); \ + *((str) + 4) = (HB_U8) ((x) >> 24); \ + *((str) + 3) = (HB_U8) ((x) >> 32); \ + *((str) + 2) = (HB_U8) ((x) >> 40); \ + *((str) + 1) = (HB_U8) ((x) >> 48); \ + *((str) + 0) = (HB_U8) ((x) >> 56); \ } #define PACK64(str, x) \ { \ - *(x) = ((uint64) *((str) + 7) ) \ - | ((uint64) *((str) + 6) << 8) \ - | ((uint64) *((str) + 5) << 16) \ - | ((uint64) *((str) + 4) << 24) \ - | ((uint64) *((str) + 3) << 32) \ - | ((uint64) *((str) + 2) << 40) \ - | ((uint64) *((str) + 1) << 48) \ - | ((uint64) *((str) + 0) << 56); \ + *(x) = ((HB_U64) *((str) + 7) ) \ + | ((HB_U64) *((str) + 6) << 8) \ + | ((HB_U64) *((str) + 5) << 16) \ + | ((HB_U64) *((str) + 4) << 24) \ + | ((HB_U64) *((str) + 3) << 32) \ + | ((HB_U64) *((str) + 2) << 40) \ + | ((HB_U64) *((str) + 1) << 48) \ + | ((HB_U64) *((str) + 0) << 56); \ } /* Macros used for loops unrolling */ @@ -128,27 +126,27 @@ wv[h] = t1 + t2; \ } -static const uint32 sha224_h0[8] = +static const HB_U32 sha224_h0[8] = {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4}; -static const uint32 sha256_h0[8] = +static const HB_U32 sha256_h0[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; -static const uint64 sha384_h0[8] = +static const HB_U64 sha384_h0[8] = {HB_ULL( 0xcbbb9d5dc1059ed8 ), HB_ULL( 0x629a292a367cd507 ), HB_ULL( 0x9159015a3070dd17 ), HB_ULL( 0x152fecd8f70e5939 ), HB_ULL( 0x67332667ffc00b31 ), HB_ULL( 0x8eb44a8768581511 ), HB_ULL( 0xdb0c2e0d64f98fa7 ), HB_ULL( 0x47b5481dbefa4fa4 )}; -static const uint64 sha512_h0[8] = +static const HB_U64 sha512_h0[8] = {HB_ULL( 0x6a09e667f3bcc908 ), HB_ULL( 0xbb67ae8584caa73b ), HB_ULL( 0x3c6ef372fe94f82b ), HB_ULL( 0xa54ff53a5f1d36f1 ), HB_ULL( 0x510e527fade682d1 ), HB_ULL( 0x9b05688c2b3e6c1f ), HB_ULL( 0x1f83d9abfb41bd6b ), HB_ULL( 0x5be0cd19137e2179 )}; -static const uint32 sha256_k[64] = +static const HB_U32 sha256_k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, @@ -166,7 +164,7 @@ static const uint32 sha256_k[64] = 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; -static const uint64 sha512_k[80] = +static const HB_U64 sha512_k[80] = {HB_ULL( 0x428a2f98d728ae22 ), HB_ULL( 0x7137449123ef65cd ), HB_ULL( 0xb5c0fbcfec4d3b2f ), HB_ULL( 0xe9b5dba58189dbbc ), HB_ULL( 0x3956c25bf348b538 ), HB_ULL( 0x59f111f1b605d019 ), @@ -210,20 +208,20 @@ static const uint64 sha512_k[80] = /* SHA-256 functions */ -static void sha256_transf(sha256_ctx *ctx, const unsigned char *message, - unsigned int block_nb) +static void sha256_transf(hb_sha256_ctx *ctx, const unsigned char *message, + HB_SIZE block_nb) { - uint32 w[64]; - uint32 wv[8]; - uint32 t1, t2; + HB_U32 w[64]; + HB_U32 wv[8]; + HB_U32 t1, t2; const unsigned char *sub_block; - int i; + HB_ISIZ i; + for (i = 0; i < ( HB_ISIZ ) block_nb; i++) { #ifndef UNROLL_LOOPS - int j; + int j; #endif - for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 6); #ifndef UNROLL_LOOPS @@ -325,16 +323,16 @@ static void sha256_transf(sha256_ctx *ctx, const unsigned char *message, } } -void hb_sha256(const void *message, unsigned int len, unsigned char *digest) +void hb_sha256(const void *message, HB_SIZE len, unsigned char *digest) { - sha256_ctx ctx; + hb_sha256_ctx ctx; hb_sha256_init(&ctx); hb_sha256_update(&ctx, message, len); hb_sha256_final(&ctx, digest); } -void hb_sha256_init(sha256_ctx *ctx) +void hb_sha256_init(hb_sha256_ctx *ctx) { #ifndef UNROLL_LOOPS int i; @@ -352,33 +350,33 @@ void hb_sha256_init(sha256_ctx *ctx) ctx->tot_len = 0; } -void hb_sha256_update(sha256_ctx *ctx, const void *messagev, - unsigned int len) +void hb_sha256_update(hb_sha256_ctx *ctx, const void *messagev, + HB_SIZE len) { const unsigned char * message = ( const unsigned char * ) messagev; - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; + HB_SIZE block_nb; + HB_SIZE new_len, rem_len, tmp_len; const unsigned char *shifted_message; - tmp_len = SHA256_BLOCK_SIZE - ctx->len; + tmp_len = HB_SHA256_BLOCK_SIZE - ctx->len; rem_len = len < tmp_len ? len : tmp_len; memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SHA256_BLOCK_SIZE) { + if (ctx->len + len < HB_SHA256_BLOCK_SIZE) { ctx->len += len; return; } new_len = len - rem_len; - block_nb = new_len / SHA256_BLOCK_SIZE; + block_nb = new_len / HB_SHA256_BLOCK_SIZE; shifted_message = message + rem_len; sha256_transf(ctx, ctx->block, 1); sha256_transf(ctx, shifted_message, block_nb); - rem_len = new_len % SHA256_BLOCK_SIZE; + rem_len = new_len % HB_SHA256_BLOCK_SIZE; memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len); @@ -387,18 +385,18 @@ void hb_sha256_update(sha256_ctx *ctx, const void *messagev, ctx->tot_len += (block_nb + 1) << 6; } -void hb_sha256_final(sha256_ctx *ctx, unsigned char *digest) +void hb_sha256_final(hb_sha256_ctx *ctx, unsigned char *digest) { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; + HB_SIZE block_nb; + HB_SIZE pm_len; + HB_SIZE len_b; #ifndef UNROLL_LOOPS int i; #endif - block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) - < (ctx->len % SHA256_BLOCK_SIZE))); + block_nb = (1 + ((HB_SHA256_BLOCK_SIZE - 9) + < (ctx->len % HB_SHA256_BLOCK_SIZE))); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 6; @@ -427,16 +425,20 @@ void hb_sha256_final(sha256_ctx *ctx, unsigned char *digest) /* SHA-512 functions */ -static void sha512_transf(sha512_ctx *ctx, const unsigned char *message, - unsigned int block_nb) +static void sha512_transf(hb_sha512_ctx *ctx, const unsigned char *message, + HB_SIZE block_nb) { - uint64 w[80]; - uint64 wv[8]; - uint64 t1, t2; + HB_U64 w[80]; + HB_U64 wv[8]; + HB_U64 t1, t2; const unsigned char *sub_block; - int i, j; + HB_ISIZ i; + + for (i = 0; i < ( HB_ISIZ ) block_nb; i++) { +#ifndef UNROLL_LOOPS + int j; +#endif - for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 7); #ifndef UNROLL_LOOPS @@ -522,17 +524,17 @@ static void sha512_transf(sha512_ctx *ctx, const unsigned char *message, } } -void hb_sha512(const void *message, unsigned int len, +void hb_sha512(const void *message, HB_SIZE len, unsigned char *digest) { - sha512_ctx ctx; + hb_sha512_ctx ctx; hb_sha512_init(&ctx); hb_sha512_update(&ctx, message, len); hb_sha512_final(&ctx, digest); } -void hb_sha512_init(sha512_ctx *ctx) +void hb_sha512_init(hb_sha512_ctx *ctx) { #ifndef UNROLL_LOOPS int i; @@ -550,33 +552,33 @@ void hb_sha512_init(sha512_ctx *ctx) ctx->tot_len = 0; } -void hb_sha512_update(sha512_ctx *ctx, const void *messagev, - unsigned int len) +void hb_sha512_update(hb_sha512_ctx *ctx, const void *messagev, + HB_SIZE len) { const unsigned char * message = ( const unsigned char * ) messagev; - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; + HB_SIZE block_nb; + HB_SIZE new_len, rem_len, tmp_len; const unsigned char *shifted_message; - tmp_len = SHA512_BLOCK_SIZE - ctx->len; + tmp_len = HB_SHA512_BLOCK_SIZE - ctx->len; rem_len = len < tmp_len ? len : tmp_len; memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SHA512_BLOCK_SIZE) { + if (ctx->len + len < HB_SHA512_BLOCK_SIZE) { ctx->len += len; return; } new_len = len - rem_len; - block_nb = new_len / SHA512_BLOCK_SIZE; + block_nb = new_len / HB_SHA512_BLOCK_SIZE; shifted_message = message + rem_len; sha512_transf(ctx, ctx->block, 1); sha512_transf(ctx, shifted_message, block_nb); - rem_len = new_len % SHA512_BLOCK_SIZE; + rem_len = new_len % HB_SHA512_BLOCK_SIZE; memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); @@ -585,18 +587,18 @@ void hb_sha512_update(sha512_ctx *ctx, const void *messagev, ctx->tot_len += (block_nb + 1) << 7; } -void hb_sha512_final(sha512_ctx *ctx, unsigned char *digest) +void hb_sha512_final(hb_sha512_ctx *ctx, unsigned char *digest) { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; + HB_SIZE block_nb; + HB_SIZE pm_len; + HB_SIZE len_b; #ifndef UNROLL_LOOPS int i; #endif - block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) - < (ctx->len % SHA512_BLOCK_SIZE)); + block_nb = 1 + ((HB_SHA512_BLOCK_SIZE - 17) + < (ctx->len % HB_SHA512_BLOCK_SIZE)); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 7; @@ -625,17 +627,17 @@ void hb_sha512_final(sha512_ctx *ctx, unsigned char *digest) /* SHA-384 functions */ -void hb_sha384(const void *message, unsigned int len, +void hb_sha384(const void *message, HB_SIZE len, unsigned char *digest) { - sha384_ctx ctx; + hb_sha384_ctx ctx; hb_sha384_init(&ctx); hb_sha384_update(&ctx, message, len); hb_sha384_final(&ctx, digest); } -void hb_sha384_init(sha384_ctx *ctx) +void hb_sha384_init(hb_sha384_ctx *ctx) { #ifndef UNROLL_LOOPS int i; @@ -653,33 +655,33 @@ void hb_sha384_init(sha384_ctx *ctx) ctx->tot_len = 0; } -void hb_sha384_update(sha384_ctx *ctx, const void *messagev, - unsigned int len) +void hb_sha384_update(hb_sha384_ctx *ctx, const void *messagev, + HB_SIZE len) { const unsigned char * message = ( const unsigned char * ) messagev; - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; + HB_SIZE block_nb; + HB_SIZE new_len, rem_len, tmp_len; const unsigned char *shifted_message; - tmp_len = SHA384_BLOCK_SIZE - ctx->len; + tmp_len = HB_SHA384_BLOCK_SIZE - ctx->len; rem_len = len < tmp_len ? len : tmp_len; memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SHA384_BLOCK_SIZE) { + if (ctx->len + len < HB_SHA384_BLOCK_SIZE) { ctx->len += len; return; } new_len = len - rem_len; - block_nb = new_len / SHA384_BLOCK_SIZE; + block_nb = new_len / HB_SHA384_BLOCK_SIZE; shifted_message = message + rem_len; sha512_transf(ctx, ctx->block, 1); sha512_transf(ctx, shifted_message, block_nb); - rem_len = new_len % SHA384_BLOCK_SIZE; + rem_len = new_len % HB_SHA384_BLOCK_SIZE; memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); @@ -688,18 +690,18 @@ void hb_sha384_update(sha384_ctx *ctx, const void *messagev, ctx->tot_len += (block_nb + 1) << 7; } -void hb_sha384_final(sha384_ctx *ctx, unsigned char *digest) +void hb_sha384_final(hb_sha384_ctx *ctx, unsigned char *digest) { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; + HB_SIZE block_nb; + HB_SIZE pm_len; + HB_SIZE len_b; #ifndef UNROLL_LOOPS int i; #endif - block_nb = (1 + ((SHA384_BLOCK_SIZE - 17) - < (ctx->len % SHA384_BLOCK_SIZE))); + block_nb = (1 + ((HB_SHA384_BLOCK_SIZE - 17) + < (ctx->len % HB_SHA384_BLOCK_SIZE))); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 7; @@ -726,17 +728,17 @@ void hb_sha384_final(sha384_ctx *ctx, unsigned char *digest) /* SHA-224 functions */ -void hb_sha224(const void *message, unsigned int len, +void hb_sha224(const void *message, HB_SIZE len, unsigned char *digest) { - sha224_ctx ctx; + hb_sha224_ctx ctx; hb_sha224_init(&ctx); hb_sha224_update(&ctx, message, len); hb_sha224_final(&ctx, digest); } -void hb_sha224_init(sha224_ctx *ctx) +void hb_sha224_init(hb_sha224_ctx *ctx) { #ifndef UNROLL_LOOPS int i; @@ -754,33 +756,33 @@ void hb_sha224_init(sha224_ctx *ctx) ctx->tot_len = 0; } -void hb_sha224_update(sha224_ctx *ctx, const void *messagev, - unsigned int len) +void hb_sha224_update(hb_sha224_ctx *ctx, const void *messagev, + HB_SIZE len) { const unsigned char * message = ( const unsigned char * ) messagev; - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; + HB_SIZE block_nb; + HB_SIZE new_len, rem_len, tmp_len; const unsigned char *shifted_message; - tmp_len = SHA224_BLOCK_SIZE - ctx->len; + tmp_len = HB_SHA224_BLOCK_SIZE - ctx->len; rem_len = len < tmp_len ? len : tmp_len; memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SHA224_BLOCK_SIZE) { + if (ctx->len + len < HB_SHA224_BLOCK_SIZE) { ctx->len += len; return; } new_len = len - rem_len; - block_nb = new_len / SHA224_BLOCK_SIZE; + block_nb = new_len / HB_SHA224_BLOCK_SIZE; shifted_message = message + rem_len; sha256_transf(ctx, ctx->block, 1); sha256_transf(ctx, shifted_message, block_nb); - rem_len = new_len % SHA224_BLOCK_SIZE; + rem_len = new_len % HB_SHA224_BLOCK_SIZE; memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len); @@ -789,18 +791,18 @@ void hb_sha224_update(sha224_ctx *ctx, const void *messagev, ctx->tot_len += (block_nb + 1) << 6; } -void hb_sha224_final(sha224_ctx *ctx, unsigned char *digest) +void hb_sha224_final(hb_sha224_ctx *ctx, unsigned char *digest) { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; + HB_SIZE block_nb; + HB_SIZE pm_len; + HB_SIZE len_b; #ifndef UNROLL_LOOPS int i; #endif - block_nb = (1 + ((SHA224_BLOCK_SIZE - 9) - < (ctx->len % SHA224_BLOCK_SIZE))); + block_nb = (1 + ((HB_SHA224_BLOCK_SIZE - 9) + < (ctx->len % HB_SHA224_BLOCK_SIZE))); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 6; diff --git a/src/rtl/sha2hmac.c b/src/rtl/sha2hmac.c index f838f64d4c..3bddba6b78 100644 --- a/src/rtl/sha2hmac.c +++ b/src/rtl/sha2hmac.c @@ -31,36 +31,35 @@ * SUCH DAMAGE. */ -#include - -#include "sha2hmac.h" +#include "hbapi.h" +#include "hbcrypto.h" /* HMAC-SHA-224 functions */ -void hb_hmac_sha224_init(hmac_sha224_ctx *ctx, const void *keyv, - unsigned int key_size) +void hb_hmac_sha224_init(hb_hmac_sha224_ctx *ctx, const void *keyv, + HB_SIZE key_size) { - unsigned int fill; - unsigned int num; + HB_SIZE num; const unsigned char *key = ( const unsigned char * ) keyv; const unsigned char *key_used; - unsigned char key_temp[SHA224_DIGEST_SIZE]; - unsigned int i; + unsigned char key_temp[HB_SHA224_DIGEST_SIZE]; + HB_SIZE i; - if (key_size == SHA224_BLOCK_SIZE) { + if (key_size == HB_SHA224_BLOCK_SIZE) { key_used = key; - num = SHA224_BLOCK_SIZE; + num = HB_SHA224_BLOCK_SIZE; } else { - if (key_size > SHA224_BLOCK_SIZE){ + HB_SIZE fill; + if (key_size > HB_SHA224_BLOCK_SIZE){ hb_sha224(key, key_size, key_temp); key_used = key_temp; - num = SHA224_DIGEST_SIZE; - } else { /* key_size > SHA224_BLOCK_SIZE */ + num = HB_SHA224_DIGEST_SIZE; + } else { /* key_size > HB_SHA224_BLOCK_SIZE */ key_used = key; num = key_size; } - fill = SHA224_BLOCK_SIZE - num; + fill = HB_SHA224_BLOCK_SIZE - num; memset(ctx->block_ipad + num, 0x36, fill); memset(ctx->block_opad + num, 0x5c, fill); @@ -72,50 +71,50 @@ void hb_hmac_sha224_init(hmac_sha224_ctx *ctx, const void *keyv, } hb_sha224_init(&ctx->ctx_inside); - hb_sha224_update(&ctx->ctx_inside, ctx->block_ipad, SHA224_BLOCK_SIZE); + hb_sha224_update(&ctx->ctx_inside, ctx->block_ipad, HB_SHA224_BLOCK_SIZE); hb_sha224_init(&ctx->ctx_outside); hb_sha224_update(&ctx->ctx_outside, ctx->block_opad, - SHA224_BLOCK_SIZE); + HB_SHA224_BLOCK_SIZE); /* for hmac_reinit */ memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, - sizeof(sha224_ctx)); + sizeof(hb_sha224_ctx)); memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, - sizeof(sha224_ctx)); + sizeof(hb_sha224_ctx)); } -void hb_hmac_sha224_reinit(hmac_sha224_ctx *ctx) +void hb_hmac_sha224_reinit(hb_hmac_sha224_ctx *ctx) { memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, - sizeof(sha224_ctx)); + sizeof(hb_sha224_ctx)); memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, - sizeof(sha224_ctx)); + sizeof(hb_sha224_ctx)); } -void hb_hmac_sha224_update(hmac_sha224_ctx *ctx, const void *message, - unsigned int message_len) +void hb_hmac_sha224_update(hb_hmac_sha224_ctx *ctx, const void *message, + HB_SIZE message_len) { hb_sha224_update(&ctx->ctx_inside, message, message_len); } -void hb_hmac_sha224_final(hmac_sha224_ctx *ctx, unsigned char *mac, - unsigned int mac_size) +void hb_hmac_sha224_final(hb_hmac_sha224_ctx *ctx, unsigned char *mac, + HB_SIZE mac_size) { - unsigned char digest_inside[SHA224_DIGEST_SIZE]; - unsigned char mac_temp[SHA224_DIGEST_SIZE]; + unsigned char digest_inside[HB_SHA224_DIGEST_SIZE]; + unsigned char mac_temp[HB_SHA224_DIGEST_SIZE]; hb_sha224_final(&ctx->ctx_inside, digest_inside); - hb_sha224_update(&ctx->ctx_outside, digest_inside, SHA224_DIGEST_SIZE); + hb_sha224_update(&ctx->ctx_outside, digest_inside, HB_SHA224_DIGEST_SIZE); hb_sha224_final(&ctx->ctx_outside, mac_temp); memcpy(mac, mac_temp, mac_size); } -void hb_hmac_sha224(const void *key, unsigned int key_size, - const void *message, unsigned int message_len, - unsigned char *mac, unsigned mac_size) +void hb_hmac_sha224(const void *key, HB_SIZE key_size, + const void *message, HB_SIZE message_len, + unsigned char *mac, HB_SIZE mac_size) { - hmac_sha224_ctx ctx; + hb_hmac_sha224_ctx ctx; hb_hmac_sha224_init(&ctx, key, key_size); hb_hmac_sha224_update(&ctx, message, message_len); @@ -124,30 +123,30 @@ void hb_hmac_sha224(const void *key, unsigned int key_size, /* HMAC-SHA-256 functions */ -void hb_hmac_sha256_init(hmac_sha256_ctx *ctx, const void *keyv, - unsigned int key_size) +void hb_hmac_sha256_init(hb_hmac_sha256_ctx *ctx, const void *keyv, + HB_SIZE key_size) { - unsigned int fill; - unsigned int num; + HB_SIZE num; const unsigned char *key = ( const unsigned char * ) keyv; const unsigned char *key_used; - unsigned char key_temp[SHA256_DIGEST_SIZE]; - unsigned int i; + unsigned char key_temp[HB_SHA256_DIGEST_SIZE]; + HB_SIZE i; - if (key_size == SHA256_BLOCK_SIZE) { + if (key_size == HB_SHA256_BLOCK_SIZE) { key_used = key; - num = SHA256_BLOCK_SIZE; + num = HB_SHA256_BLOCK_SIZE; } else { - if (key_size > SHA256_BLOCK_SIZE){ + HB_SIZE fill; + if (key_size > HB_SHA256_BLOCK_SIZE){ hb_sha256(key, key_size, key_temp); key_used = key_temp; - num = SHA256_DIGEST_SIZE; - } else { /* key_size > SHA256_BLOCK_SIZE */ + num = HB_SHA256_DIGEST_SIZE; + } else { /* key_size > HB_SHA256_BLOCK_SIZE */ key_used = key; num = key_size; } - fill = SHA256_BLOCK_SIZE - num; + fill = HB_SHA256_BLOCK_SIZE - num; memset(ctx->block_ipad + num, 0x36, fill); memset(ctx->block_opad + num, 0x5c, fill); @@ -159,50 +158,50 @@ void hb_hmac_sha256_init(hmac_sha256_ctx *ctx, const void *keyv, } hb_sha256_init(&ctx->ctx_inside); - hb_sha256_update(&ctx->ctx_inside, ctx->block_ipad, SHA256_BLOCK_SIZE); + hb_sha256_update(&ctx->ctx_inside, ctx->block_ipad, HB_SHA256_BLOCK_SIZE); hb_sha256_init(&ctx->ctx_outside); hb_sha256_update(&ctx->ctx_outside, ctx->block_opad, - SHA256_BLOCK_SIZE); + HB_SHA256_BLOCK_SIZE); /* for hmac_reinit */ memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, - sizeof(sha256_ctx)); + sizeof(hb_sha256_ctx)); memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, - sizeof(sha256_ctx)); + sizeof(hb_sha256_ctx)); } -void hb_hmac_sha256_reinit(hmac_sha256_ctx *ctx) +void hb_hmac_sha256_reinit(hb_hmac_sha256_ctx *ctx) { memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, - sizeof(sha256_ctx)); + sizeof(hb_sha256_ctx)); memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, - sizeof(sha256_ctx)); + sizeof(hb_sha256_ctx)); } -void hb_hmac_sha256_update(hmac_sha256_ctx *ctx, const void *message, - unsigned int message_len) +void hb_hmac_sha256_update(hb_hmac_sha256_ctx *ctx, const void *message, + HB_SIZE message_len) { hb_sha256_update(&ctx->ctx_inside, message, message_len); } -void hb_hmac_sha256_final(hmac_sha256_ctx *ctx, unsigned char *mac, - unsigned int mac_size) +void hb_hmac_sha256_final(hb_hmac_sha256_ctx *ctx, unsigned char *mac, + HB_SIZE mac_size) { - unsigned char digest_inside[SHA256_DIGEST_SIZE]; - unsigned char mac_temp[SHA256_DIGEST_SIZE]; + unsigned char digest_inside[HB_SHA256_DIGEST_SIZE]; + unsigned char mac_temp[HB_SHA256_DIGEST_SIZE]; hb_sha256_final(&ctx->ctx_inside, digest_inside); - hb_sha256_update(&ctx->ctx_outside, digest_inside, SHA256_DIGEST_SIZE); + hb_sha256_update(&ctx->ctx_outside, digest_inside, HB_SHA256_DIGEST_SIZE); hb_sha256_final(&ctx->ctx_outside, mac_temp); memcpy(mac, mac_temp, mac_size); } -void hb_hmac_sha256(const void *key, unsigned int key_size, - const void *message, unsigned int message_len, - unsigned char *mac, unsigned mac_size) +void hb_hmac_sha256(const void *key, HB_SIZE key_size, + const void *message, HB_SIZE message_len, + unsigned char *mac, HB_SIZE mac_size) { - hmac_sha256_ctx ctx; + hb_hmac_sha256_ctx ctx; hb_hmac_sha256_init(&ctx, key, key_size); hb_hmac_sha256_update(&ctx, message, message_len); @@ -211,30 +210,30 @@ void hb_hmac_sha256(const void *key, unsigned int key_size, /* HMAC-SHA-384 functions */ -void hb_hmac_sha384_init(hmac_sha384_ctx *ctx, const void *keyv, - unsigned int key_size) +void hb_hmac_sha384_init(hb_hmac_sha384_ctx *ctx, const void *keyv, + HB_SIZE key_size) { - unsigned int fill; - unsigned int num; + HB_SIZE num; const unsigned char *key = ( const unsigned char * ) keyv; const unsigned char *key_used; - unsigned char key_temp[SHA384_DIGEST_SIZE]; - unsigned int i; + unsigned char key_temp[HB_SHA384_DIGEST_SIZE]; + HB_SIZE i; - if (key_size == SHA384_BLOCK_SIZE) { + if (key_size == HB_SHA384_BLOCK_SIZE) { key_used = key; - num = SHA384_BLOCK_SIZE; + num = HB_SHA384_BLOCK_SIZE; } else { - if (key_size > SHA384_BLOCK_SIZE){ + HB_SIZE fill; + if (key_size > HB_SHA384_BLOCK_SIZE){ hb_sha384(key, key_size, key_temp); key_used = key_temp; - num = SHA384_DIGEST_SIZE; - } else { /* key_size > SHA384_BLOCK_SIZE */ + num = HB_SHA384_DIGEST_SIZE; + } else { /* key_size > HB_SHA384_BLOCK_SIZE */ key_used = key; num = key_size; } - fill = SHA384_BLOCK_SIZE - num; + fill = HB_SHA384_BLOCK_SIZE - num; memset(ctx->block_ipad + num, 0x36, fill); memset(ctx->block_opad + num, 0x5c, fill); @@ -246,50 +245,50 @@ void hb_hmac_sha384_init(hmac_sha384_ctx *ctx, const void *keyv, } hb_sha384_init(&ctx->ctx_inside); - hb_sha384_update(&ctx->ctx_inside, ctx->block_ipad, SHA384_BLOCK_SIZE); + hb_sha384_update(&ctx->ctx_inside, ctx->block_ipad, HB_SHA384_BLOCK_SIZE); hb_sha384_init(&ctx->ctx_outside); hb_sha384_update(&ctx->ctx_outside, ctx->block_opad, - SHA384_BLOCK_SIZE); + HB_SHA384_BLOCK_SIZE); /* for hmac_reinit */ memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, - sizeof(sha384_ctx)); + sizeof(hb_sha384_ctx)); memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, - sizeof(sha384_ctx)); + sizeof(hb_sha384_ctx)); } -void hb_hmac_sha384_reinit(hmac_sha384_ctx *ctx) +void hb_hmac_sha384_reinit(hb_hmac_sha384_ctx *ctx) { memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, - sizeof(sha384_ctx)); + sizeof(hb_sha384_ctx)); memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, - sizeof(sha384_ctx)); + sizeof(hb_sha384_ctx)); } -void hb_hmac_sha384_update(hmac_sha384_ctx *ctx, const void *message, - unsigned int message_len) +void hb_hmac_sha384_update(hb_hmac_sha384_ctx *ctx, const void *message, + HB_SIZE message_len) { hb_sha384_update(&ctx->ctx_inside, message, message_len); } -void hb_hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac, - unsigned int mac_size) +void hb_hmac_sha384_final(hb_hmac_sha384_ctx *ctx, unsigned char *mac, + HB_SIZE mac_size) { - unsigned char digest_inside[SHA384_DIGEST_SIZE]; - unsigned char mac_temp[SHA384_DIGEST_SIZE]; + unsigned char digest_inside[HB_SHA384_DIGEST_SIZE]; + unsigned char mac_temp[HB_SHA384_DIGEST_SIZE]; hb_sha384_final(&ctx->ctx_inside, digest_inside); - hb_sha384_update(&ctx->ctx_outside, digest_inside, SHA384_DIGEST_SIZE); + hb_sha384_update(&ctx->ctx_outside, digest_inside, HB_SHA384_DIGEST_SIZE); hb_sha384_final(&ctx->ctx_outside, mac_temp); memcpy(mac, mac_temp, mac_size); } -void hb_hmac_sha384(const void *key, unsigned int key_size, - const void *message, unsigned int message_len, - unsigned char *mac, unsigned mac_size) +void hb_hmac_sha384(const void *key, HB_SIZE key_size, + const void *message, HB_SIZE message_len, + unsigned char *mac, HB_SIZE mac_size) { - hmac_sha384_ctx ctx; + hb_hmac_sha384_ctx ctx; hb_hmac_sha384_init(&ctx, key, key_size); hb_hmac_sha384_update(&ctx, message, message_len); @@ -298,30 +297,30 @@ void hb_hmac_sha384(const void *key, unsigned int key_size, /* HMAC-SHA-512 functions */ -void hb_hmac_sha512_init(hmac_sha512_ctx *ctx, const void *keyv, - unsigned int key_size) +void hb_hmac_sha512_init(hb_hmac_sha512_ctx *ctx, const void *keyv, + HB_SIZE key_size) { - unsigned int fill; - unsigned int num; + HB_SIZE num; const unsigned char *key = ( const unsigned char * ) keyv; const unsigned char *key_used; - unsigned char key_temp[SHA512_DIGEST_SIZE]; - unsigned int i; + unsigned char key_temp[HB_SHA512_DIGEST_SIZE]; + HB_SIZE i; - if (key_size == SHA512_BLOCK_SIZE) { + if (key_size == HB_SHA512_BLOCK_SIZE) { key_used = key; - num = SHA512_BLOCK_SIZE; + num = HB_SHA512_BLOCK_SIZE; } else { - if (key_size > SHA512_BLOCK_SIZE){ + HB_SIZE fill; + if (key_size > HB_SHA512_BLOCK_SIZE){ hb_sha512(key, key_size, key_temp); key_used = key_temp; - num = SHA512_DIGEST_SIZE; - } else { /* key_size > SHA512_BLOCK_SIZE */ + num = HB_SHA512_DIGEST_SIZE; + } else { /* key_size > HB_SHA512_BLOCK_SIZE */ key_used = key; num = key_size; } - fill = SHA512_BLOCK_SIZE - num; + fill = HB_SHA512_BLOCK_SIZE - num; memset(ctx->block_ipad + num, 0x36, fill); memset(ctx->block_opad + num, 0x5c, fill); @@ -333,50 +332,50 @@ void hb_hmac_sha512_init(hmac_sha512_ctx *ctx, const void *keyv, } hb_sha512_init(&ctx->ctx_inside); - hb_sha512_update(&ctx->ctx_inside, ctx->block_ipad, SHA512_BLOCK_SIZE); + hb_sha512_update(&ctx->ctx_inside, ctx->block_ipad, HB_SHA512_BLOCK_SIZE); hb_sha512_init(&ctx->ctx_outside); hb_sha512_update(&ctx->ctx_outside, ctx->block_opad, - SHA512_BLOCK_SIZE); + HB_SHA512_BLOCK_SIZE); /* for hmac_reinit */ memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, - sizeof(sha512_ctx)); + sizeof(hb_sha512_ctx)); memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, - sizeof(sha512_ctx)); + sizeof(hb_sha512_ctx)); } -void hb_hmac_sha512_reinit(hmac_sha512_ctx *ctx) +void hb_hmac_sha512_reinit(hb_hmac_sha512_ctx *ctx) { memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, - sizeof(sha512_ctx)); + sizeof(hb_sha512_ctx)); memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, - sizeof(sha512_ctx)); + sizeof(hb_sha512_ctx)); } -void hb_hmac_sha512_update(hmac_sha512_ctx *ctx, const void *message, - unsigned int message_len) +void hb_hmac_sha512_update(hb_hmac_sha512_ctx *ctx, const void *message, + HB_SIZE message_len) { hb_sha512_update(&ctx->ctx_inside, message, message_len); } -void hb_hmac_sha512_final(hmac_sha512_ctx *ctx, unsigned char *mac, - unsigned int mac_size) +void hb_hmac_sha512_final(hb_hmac_sha512_ctx *ctx, unsigned char *mac, + HB_SIZE mac_size) { - unsigned char digest_inside[SHA512_DIGEST_SIZE]; - unsigned char mac_temp[SHA512_DIGEST_SIZE]; + unsigned char digest_inside[HB_SHA512_DIGEST_SIZE]; + unsigned char mac_temp[HB_SHA512_DIGEST_SIZE]; hb_sha512_final(&ctx->ctx_inside, digest_inside); - hb_sha512_update(&ctx->ctx_outside, digest_inside, SHA512_DIGEST_SIZE); + hb_sha512_update(&ctx->ctx_outside, digest_inside, HB_SHA512_DIGEST_SIZE); hb_sha512_final(&ctx->ctx_outside, mac_temp); memcpy(mac, mac_temp, mac_size); } -void hb_hmac_sha512(const void *key, unsigned int key_size, - const void *message, unsigned int message_len, - unsigned char *mac, unsigned mac_size) +void hb_hmac_sha512(const void *key, HB_SIZE key_size, + const void *message, HB_SIZE message_len, + unsigned char *mac, HB_SIZE mac_size) { - hmac_sha512_ctx ctx; + hb_hmac_sha512_ctx ctx; hb_hmac_sha512_init(&ctx, key, key_size); hb_hmac_sha512_update(&ctx, message, message_len);