Hello Nikos,
First of all, I think you should use the function wc_ecc_verify_hash_ex() https://www.wolfssl.com/documentation/m … fy_hash_ex. This would eliminate the need for converting the signature from raw (R,S) format to signature, and also make the code smaller. signature.c and asn.c should not be needed in your build if you stick to the APIs in ecc.h instead.
To create the "mp_int" object to import the R,S components in the format expected by this call, you could do the following:
mp_int mp_r, mp_s;
mp_init(&mp_r);
mp_init(&mp_s);
mp_read_unsigned_bin(&mp_r, r, sizeof(r));
mp_read_unsigned_bin(&mp_s, s, sizeof(s));
Very important: before using your ecc_pubkey structure to import the key, you must initialize it:
eccret = wc_ecc_init(&ecc_pubkey);
Your import call then looks fine as is:
// Import the key buffer to the ecc_key structure format
eccret = wc_ecc_import_unsigned(&ecc_pubkey, pubkeyX, pubkeyY, NULL, ECC_SECP256R1);
Then finally you can call the correct verify function as:
eccret = wc_ecc_verify_hash_ex(&mp_r, &mp_s, hash_data, hash_len, &result, &eccpubkey);
If all goes as expected, eccret should be zero and result should be 1, indicating successful verification.
Another thing I have noticed in your configuration: the SP assembly optimizations don't match your current MCU. Cortex-M0 is ARMv6-M and we do provide thumb instructions optimization for it. I suggest you remove the option WOLFSSL_SP_ARM_CORTEX_M_ASM and use WOLFSSL_SP_ARM_THUMB_ASM instead. Also the correct assembly file for Cortex-M0 is sp_armthumb.c if you are compiling with options "-mcortex-m0 -mthumb" as expected for this MCU. The sp_cortexm.c source file contains assembly optimizations for Cortex-M3 and later (up to newer ARMv8-M).
I would also recommend to add the option WOLFSSL_HAVE_SP_ECC to ensure ecc is using those optimizations.
Furthermore, since you are defining WOLFSSL_NO_MALLOC, I expect that you don't have/don't want to use dynamic allocation. In this case, in combination with SP_MATH, I would also suggest the following:
#define WOLFSSL_SP_NO_MALLOC
#define WOLFSSL_SP_NO_DYN_STACK
I hope this helps. Please let us know if you are still experiencing issues after the suggested fixes.
Thanks,
Daniele @wolfSSL Support