Esempio n. 1
0
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));
}
Esempio n. 2
0
File: sha2.c Progetto: xharbour/core
void hb_sha512( const void * message, unsigned int len,
                unsigned char * digest )
{
   sha512_ctx ctx;

   hb_sha512_init( &ctx );
   hb_sha512_update( &ctx, message, len );
   hb_sha512_final( &ctx, digest );
}