Files
harbour-core/harbour/src/rtl/sha2hmac.c
Przemyslaw Czerpak 3134021021 2010-11-19 11:10 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl)
* harbour/ChangeLog
    * marked may last TODO note as DONE - thanks to Viktor and Mindaugas

  * harbour/include/hbmacro.h
    ! added missing const in hb_macroGenPCodeN() declaration

  * harbour/include/hbapi.h
    * added const to HB_CODEBLOCK.pCode

  * harbour/include/hbcompdf.h
    ! added missing const to HB_MACRO.string

  * harbour/src/pp/ppcore.c
  * harbour/src/common/hbstr.c
  * harbour/src/compiler/hbmain.c
  * harbour/src/compiler/hbfix.c
  * harbour/src/compiler/complex.c
  * harbour/src/compiler/hbdead.c
  * harbour/src/compiler/genc.c
  * harbour/src/compiler/hbident.c
  * harbour/src/compiler/gencc.c
  * harbour/src/compiler/hblbl.c
  * harbour/src/compiler/harbour.yyc
  * harbour/src/compiler/harbour.y
  * harbour/src/compiler/harbour.yyh
  * harbour/src/vm/macro.c
  * harbour/src/vm/codebloc.c
  * harbour/src/vm/itemapi.c
  * harbour/src/vm/hvm.c
  * harbour/src/rtl/hbjson.c
  * harbour/src/rtl/sha1.c
  * harbour/src/rtl/sha2hmac.c
  * harbour/src/rtl/hbsocket.c
  * harbour/src/rtl/hbbffnc.c
  * harbour/src/rtl/itemseri.c
  * harbour/src/rtl/filebuf.c
  * harbour/src/rtl/hbbfish.c
  * harbour/src/rtl/gttrm/gttrm.c
  * harbour/src/rdd/workarea.c
  * harbour/src/macro/macrolex.c
  * harbour/src/rdd/usrrdd/usrrdd.c
    * cleaned const pointer casting

  * harbour/src/rdd/usrrdd/usrrdd.c
    ! fixed very serious bug located during const pointer cleanup
      which should cause GPF on user code creating indexes

  * harbour/src/rtl/sha2.c
    * minor formatting

   ; TOFIX: Seems that there is sth wrong with src/rtl/sha1.c.
            Function SHA1_Transform() wrongly declares 2-nd parameter
            buffer[64] as const when in fact it changes it what can be
            seen if
               BYTE64QUAD16    *block;
            is changed to:
               const BYTE64QUAD16 *block;
            This casting which removes const hides potentially very serious
            bug - modifying readonly memory area. SHA1_Transform() changes
            the buffer so the 2-nd parameter must be declared without const.
            After such modification C compiler should warn when const buffers
            are passed to this function and such places should be fixed.
            In fact it seems to be only line 136:
               SHA1_Transform(context->state, &data[i]);
            Viktor can you look at it and fix the code?
2010-11-19 10:11:29 +00:00

389 lines
12 KiB
C

/*
* $Id$
*/
/*-
* HMAC-SHA-224/256/384/512 implementation
* Last update: 06/15/2005
* Issue date: 06/15/2005
*
* Copyright (C) 2005 Olivier Gay <olivier.gay@a3.epfl.ch>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <string.h>
#include "sha2hmac.h"
/* HMAC-SHA-224 functions */
void hb_hmac_sha224_init(hmac_sha224_ctx *ctx, const void *keyv,
unsigned int key_size)
{
unsigned int fill;
unsigned int num;
const unsigned char *key = ( const unsigned char * ) keyv;
const unsigned char *key_used;
unsigned char key_temp[SHA224_DIGEST_SIZE];
unsigned int i;
if (key_size == SHA224_BLOCK_SIZE) {
key_used = key;
num = SHA224_BLOCK_SIZE;
} else {
if (key_size > SHA224_BLOCK_SIZE){
hb_sha224(key, key_size, key_temp);
key_used = key_temp;
num = SHA224_DIGEST_SIZE;
} else { /* key_size > SHA224_BLOCK_SIZE */
key_used = key;
num = key_size;
}
fill = SHA224_BLOCK_SIZE - num;
memset(ctx->block_ipad + num, 0x36, fill);
memset(ctx->block_opad + num, 0x5c, fill);
}
for (i = 0; i < num; i++) {
ctx->block_ipad[i] = key_used[i] ^ 0x36;
ctx->block_opad[i] = key_used[i] ^ 0x5c;
}
hb_sha224_init(&ctx->ctx_inside);
hb_sha224_update(&ctx->ctx_inside, ctx->block_ipad, SHA224_BLOCK_SIZE);
hb_sha224_init(&ctx->ctx_outside);
hb_sha224_update(&ctx->ctx_outside, ctx->block_opad,
SHA224_BLOCK_SIZE);
/* for hmac_reinit */
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
sizeof(sha224_ctx));
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
sizeof(sha224_ctx));
}
void hb_hmac_sha224_reinit(hmac_sha224_ctx *ctx)
{
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
sizeof(sha224_ctx));
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
sizeof(sha224_ctx));
}
void hb_hmac_sha224_update(hmac_sha224_ctx *ctx, const void *message,
unsigned int 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)
{
unsigned char digest_inside[SHA224_DIGEST_SIZE];
unsigned char mac_temp[SHA224_DIGEST_SIZE];
hb_sha224_final(&ctx->ctx_inside, digest_inside);
hb_sha224_update(&ctx->ctx_outside, digest_inside, 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)
{
hmac_sha224_ctx ctx;
hb_hmac_sha224_init(&ctx, key, key_size);
hb_hmac_sha224_update(&ctx, message, message_len);
hb_hmac_sha224_final(&ctx, mac, mac_size);
}
/* HMAC-SHA-256 functions */
void hb_hmac_sha256_init(hmac_sha256_ctx *ctx, const void *keyv,
unsigned int key_size)
{
unsigned int fill;
unsigned int num;
const unsigned char *key = ( const unsigned char * ) keyv;
const unsigned char *key_used;
unsigned char key_temp[SHA256_DIGEST_SIZE];
unsigned int i;
if (key_size == SHA256_BLOCK_SIZE) {
key_used = key;
num = SHA256_BLOCK_SIZE;
} else {
if (key_size > SHA256_BLOCK_SIZE){
hb_sha256(key, key_size, key_temp);
key_used = key_temp;
num = SHA256_DIGEST_SIZE;
} else { /* key_size > SHA256_BLOCK_SIZE */
key_used = key;
num = key_size;
}
fill = SHA256_BLOCK_SIZE - num;
memset(ctx->block_ipad + num, 0x36, fill);
memset(ctx->block_opad + num, 0x5c, fill);
}
for (i = 0; i < num; i++) {
ctx->block_ipad[i] = key_used[i] ^ 0x36;
ctx->block_opad[i] = key_used[i] ^ 0x5c;
}
hb_sha256_init(&ctx->ctx_inside);
hb_sha256_update(&ctx->ctx_inside, ctx->block_ipad, SHA256_BLOCK_SIZE);
hb_sha256_init(&ctx->ctx_outside);
hb_sha256_update(&ctx->ctx_outside, ctx->block_opad,
SHA256_BLOCK_SIZE);
/* for hmac_reinit */
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
sizeof(sha256_ctx));
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
sizeof(sha256_ctx));
}
void hb_hmac_sha256_reinit(hmac_sha256_ctx *ctx)
{
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
sizeof(sha256_ctx));
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
sizeof(sha256_ctx));
}
void hb_hmac_sha256_update(hmac_sha256_ctx *ctx, const void *message,
unsigned int 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)
{
unsigned char digest_inside[SHA256_DIGEST_SIZE];
unsigned char mac_temp[SHA256_DIGEST_SIZE];
hb_sha256_final(&ctx->ctx_inside, digest_inside);
hb_sha256_update(&ctx->ctx_outside, digest_inside, 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)
{
hmac_sha256_ctx ctx;
hb_hmac_sha256_init(&ctx, key, key_size);
hb_hmac_sha256_update(&ctx, message, message_len);
hb_hmac_sha256_final(&ctx, mac, mac_size);
}
/* HMAC-SHA-384 functions */
void hb_hmac_sha384_init(hmac_sha384_ctx *ctx, const void *keyv,
unsigned int key_size)
{
unsigned int fill;
unsigned int num;
const unsigned char *key = ( const unsigned char * ) keyv;
const unsigned char *key_used;
unsigned char key_temp[SHA384_DIGEST_SIZE];
unsigned int i;
if (key_size == SHA384_BLOCK_SIZE) {
key_used = key;
num = SHA384_BLOCK_SIZE;
} else {
if (key_size > SHA384_BLOCK_SIZE){
hb_sha384(key, key_size, key_temp);
key_used = key_temp;
num = SHA384_DIGEST_SIZE;
} else { /* key_size > SHA384_BLOCK_SIZE */
key_used = key;
num = key_size;
}
fill = SHA384_BLOCK_SIZE - num;
memset(ctx->block_ipad + num, 0x36, fill);
memset(ctx->block_opad + num, 0x5c, fill);
}
for (i = 0; i < num; i++) {
ctx->block_ipad[i] = key_used[i] ^ 0x36;
ctx->block_opad[i] = key_used[i] ^ 0x5c;
}
hb_sha384_init(&ctx->ctx_inside);
hb_sha384_update(&ctx->ctx_inside, ctx->block_ipad, SHA384_BLOCK_SIZE);
hb_sha384_init(&ctx->ctx_outside);
hb_sha384_update(&ctx->ctx_outside, ctx->block_opad,
SHA384_BLOCK_SIZE);
/* for hmac_reinit */
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
sizeof(sha384_ctx));
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
sizeof(sha384_ctx));
}
void hb_hmac_sha384_reinit(hmac_sha384_ctx *ctx)
{
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
sizeof(sha384_ctx));
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
sizeof(sha384_ctx));
}
void hb_hmac_sha384_update(hmac_sha384_ctx *ctx, const void *message,
unsigned int 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)
{
unsigned char digest_inside[SHA384_DIGEST_SIZE];
unsigned char mac_temp[SHA384_DIGEST_SIZE];
hb_sha384_final(&ctx->ctx_inside, digest_inside);
hb_sha384_update(&ctx->ctx_outside, digest_inside, 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)
{
hmac_sha384_ctx ctx;
hb_hmac_sha384_init(&ctx, key, key_size);
hb_hmac_sha384_update(&ctx, message, message_len);
hb_hmac_sha384_final(&ctx, mac, mac_size);
}
/* HMAC-SHA-512 functions */
void hb_hmac_sha512_init(hmac_sha512_ctx *ctx, const void *keyv,
unsigned int key_size)
{
unsigned int fill;
unsigned int num;
const unsigned char *key = ( const unsigned char * ) keyv;
const unsigned char *key_used;
unsigned char key_temp[SHA512_DIGEST_SIZE];
unsigned int i;
if (key_size == SHA512_BLOCK_SIZE) {
key_used = key;
num = SHA512_BLOCK_SIZE;
} else {
if (key_size > SHA512_BLOCK_SIZE){
hb_sha512(key, key_size, key_temp);
key_used = key_temp;
num = SHA512_DIGEST_SIZE;
} else { /* key_size > SHA512_BLOCK_SIZE */
key_used = key;
num = key_size;
}
fill = SHA512_BLOCK_SIZE - num;
memset(ctx->block_ipad + num, 0x36, fill);
memset(ctx->block_opad + num, 0x5c, fill);
}
for (i = 0; i < num; i++) {
ctx->block_ipad[i] = key_used[i] ^ 0x36;
ctx->block_opad[i] = key_used[i] ^ 0x5c;
}
hb_sha512_init(&ctx->ctx_inside);
hb_sha512_update(&ctx->ctx_inside, ctx->block_ipad, SHA512_BLOCK_SIZE);
hb_sha512_init(&ctx->ctx_outside);
hb_sha512_update(&ctx->ctx_outside, ctx->block_opad,
SHA512_BLOCK_SIZE);
/* for hmac_reinit */
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
sizeof(sha512_ctx));
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
sizeof(sha512_ctx));
}
void hb_hmac_sha512_reinit(hmac_sha512_ctx *ctx)
{
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
sizeof(sha512_ctx));
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
sizeof(sha512_ctx));
}
void hb_hmac_sha512_update(hmac_sha512_ctx *ctx, const void *message,
unsigned int 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)
{
unsigned char digest_inside[SHA512_DIGEST_SIZE];
unsigned char mac_temp[SHA512_DIGEST_SIZE];
hb_sha512_final(&ctx->ctx_inside, digest_inside);
hb_sha512_update(&ctx->ctx_outside, digest_inside, 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)
{
hmac_sha512_ctx ctx;
hb_hmac_sha512_init(&ctx, key, key_size);
hb_hmac_sha512_update(&ctx, message, message_len);
hb_hmac_sha512_final(&ctx, mac, mac_size);
}