Solved: rsa_varification using mbedtls libs

I was trying to verify a signature of a binary file using mbedtls library (v2.26.0)

I took the reference code from github.com/ARMmbed/mbedtls/blob/v2.26.0/programs/pkey/rsa_verify.c 

In the reference code, they are using the mbedtls_md_file() function to calculate the hash of the file.
But I’m using the FATFS library to access the files. so I had to modify the reference code as follows.

 

/**
 * Function to get hash of a file of path.
 * the hash will be stored in output buffer.
 * return 0 on success and error code on failure.
 */
int rsa_get_hash_of_file(const mbedtls_md_info_t *md_info, char *path,unsigned char *output)
{
	int ret = -1;
    FIL f;
    mbedtls_md_context_t ctx;
    FRESULT error;
    int br=0;
    unsigned char buf[1024];
    if( md_info == NULL )
    {
    	PRINTF("bad input datarn");
    	blogd("bad input datarn");
    	return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
    }
    error=f_open(&f, path, FA_READ);
    if( error != FR_OK )
    {
    	PRINTF("Unable to open update file for security checkrn");
    	blogd("Unable to open update file for security checkrn");
    	return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
    }
    mbedtls_md_init( &ctx );

    if( ( ret = mbedtls_md_init_ctx(&ctx, md_info4) ) != 0 )
        goto cleanup;
    if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
        goto cleanup;

    while( (f_read(&f, buf, sizeof(buf), &br) == FR_OK ) && br > 0)
        {
   	if( ( ret = mbedtls_md_update( &ctx, buf, br ) ) != 0 )
            goto cleanup;
        }
    if( f_error( &f ) != 0 )
        ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
    else
        ret = mbedtls_md_finish( &ctx, output );
cleanup:
	memset(buf, 0, sizeof(buf));
	f_close( &f );
	mbedtls_md_free( &ctx );
	return( ret );
}

 

the problem is im getting no values in the output buffer and no error code is returning from mbedtls functions.
is there any macro or to be enabled to get this work ? 
any help are appreciated .

thanks

Read more here: Source link