Exemplo n.º 1
0
/* Choose SHA S/W or H/W context and initialize it
 * 
 * try_hw:
 *   0: Initialize S/W context
 *   1: Try acquiring SHA H/W resource first and initialize its H/W context if successful. If failed, initialize S/W context.
 */
static void mbedtls_sha512_init_internal(mbedtls_sha512_context *ctx, int try_hw)
{
    if (try_hw && crypto_sha_try_acquire()) {
        ctx->active_ctx = &ctx->hw_ctx;
        mbedtls_sha512_hw_init(&ctx->hw_ctx);
    } else {
        ctx->active_ctx = &ctx->sw_ctx;
        mbedtls_sha512_sw_init(&ctx->sw_ctx);
    }
}
Exemplo n.º 2
0
void mbedtls_sha512_hw_finish(crypto_sha_context *ctx, unsigned char output[64])
{
    // H/W SHA cannot handle zero data well. Fall back to S/W SHA.
    if (ctx->total) {
        crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1);
        ctx->buffer_left = 0;
        crypto_sha_getinternstate(output, ctx->is224_384 ? 48 : 64);
    
        CRPT->HMAC_CTL |= CRPT_HMAC_CTL_STOP_Msk;
    }
    else {
        mbedtls_sha512_sw_context ctx_sw;
    
        mbedtls_sha512_sw_init(&ctx_sw);
        mbedtls_sha512_sw_starts(&ctx_sw, ctx->is224_384);
        mbedtls_sha512_sw_finish(&ctx_sw, output);
        mbedtls_sha512_sw_free(&ctx_sw);
    }
}