/* * signv_sha256_calcu calculate the sha256 value upon one continous memory aera, there is no restriction on how many bytes the data are. * The SHA-256 method is a standard and the value can be calculated by mbedtls library. */ int sha256_calcu(const unsigned char *input, size_t len, unsigned char hash_value[]) { int ret = 0; // unsigned char *buf; mbedtls_sha256_context ctx; // buf = mbedtls_calloc( 1024, sizeof(unsigned char) ); mbedtls_sha256_init( &ctx ); do { /* SHA-256 */ if( ( ret = mbedtls_sha256_starts_ret( &ctx, 0 ) ) != 0 ) { mbedtls_sha256_free( &ctx ); // mbedtls_free( buf ); break; } ret = mbedtls_sha256_update_ret( &ctx, input, len); if( ret != 0 ) { mbedtls_sha256_free( &ctx ); // mbedtls_free( buf ); break; } if( ( ret = mbedtls_sha256_finish_ret( &ctx, hash_value ) ) != 0 ) { mbedtls_sha256_free( &ctx ); // mbedtls_free( buf ); break; } mbedtls_sha256_free( &ctx ); // mbedtls_free( buf ); }while(0); return( ret ); }