void mbedtls_sha256_clone(mbedtls_sha256_context *dst, const mbedtls_sha256_context *src) { if (src->ishw) { // Clone S/W ctx from H/W ctx dst->ishw = 0; dst->sw_ctx.total[0] = src->hw_ctx.total; dst->sw_ctx.total[1] = 0; { unsigned char output[32]; crypto_sha_getinternstate(output, sizeof (output)); unsigned char *output_pos = output; unsigned char *output_end = output + (sizeof (output) / sizeof (output[0])); uint32_t *state_pos = (uint32_t *) &(dst->sw_ctx.state[0]); while (output_pos != output_end) { *state_pos ++ = nu_get32_be(output_pos); output_pos += 4; } } memcpy(dst->sw_ctx.buffer, src->hw_ctx.buffer, src->hw_ctx.buffer_left); dst->sw_ctx.is224 = src->hw_ctx.is224; if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) { mbedtls_sha256_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer); } } else { // Clone S/W ctx from S/W ctx dst->sw_ctx = src->sw_ctx; } }
void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64]) { if (ctx->active_ctx == &ctx->hw_ctx) { mbedtls_sha256_hw_process(&ctx->hw_ctx, data); } else if (ctx->active_ctx == &ctx->sw_ctx) { mbedtls_sha256_sw_process(&ctx->sw_ctx, data); } }
void mbedtls_sha256_clone(mbedtls_sha256_context *dst, const mbedtls_sha256_context *src) { // Corner case: Destination/source contexts are the same if (dst == src) { return; } // If dst is H/W context, we need to change it to S/W context first before cloning to. if (dst->active_ctx == &dst->hw_ctx) { mbedtls_sha256_free(dst); // Force S/W context mbedtls_sha256_init_internal(dst, 0); } if (src->active_ctx == &src->hw_ctx) { // Clone S/W ctx from H/W ctx dst->sw_ctx.total[0] = src->hw_ctx.total; dst->sw_ctx.total[1] = 0; { unsigned char output[32]; crypto_sha_getinternstate(output, sizeof (output)); unsigned char *output_pos = output; unsigned char *output_end = output + (sizeof (output) / sizeof (output[0])); uint32_t *state_pos = (uint32_t *) &(dst->sw_ctx.state[0]); while (output_pos != output_end) { *state_pos ++ = nu_get32_be(output_pos); output_pos += 4; } } memcpy(dst->sw_ctx.buffer, src->hw_ctx.buffer, src->hw_ctx.buffer_left); dst->sw_ctx.is224 = src->hw_ctx.is224_384; if (src->hw_ctx.buffer_left == src->hw_ctx.blocksize) { mbedtls_sha256_sw_process(&dst->sw_ctx, dst->sw_ctx.buffer); } } else if (src->active_ctx == &src->sw_ctx) { // Clone S/W ctx from S/W ctx dst->sw_ctx = src->sw_ctx; } }