コード例 #1
0
ファイル: sha2hmac.c プロジェクト: AmericoBalboa/core
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));
}
コード例 #2
0
ファイル: sha2.c プロジェクト: xharbour/core
void hb_sha256( const void * message, unsigned int len, unsigned char * digest )
{
   sha256_ctx ctx;

   hb_sha256_init( &ctx );
   hb_sha256_update( &ctx, message, len );
   hb_sha256_final( &ctx, digest );
}
コード例 #3
0
ファイル: sha2hmac.c プロジェクト: AmericoBalboa/core
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);
}
コード例 #4
0
ファイル: sha2hmac.c プロジェクト: AmericoBalboa/core
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);
}