1
0

crypto-helpers-mbedtls.cpp 778 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "crypto-helpers.hpp"
  2. #include "mbedtls/md.h"
  3. #include "mbedtls/pk.h"
  4. bool VerifySignature(const uint8_t *pubKey, const size_t pubKeyLen, const uint8_t *buf, const size_t len,
  5. const uint8_t *sig, const size_t sigLen)
  6. {
  7. bool result = false;
  8. int ret = 1;
  9. unsigned char hash[64];
  10. mbedtls_pk_context pk;
  11. mbedtls_pk_init(&pk);
  12. // Parse PEM key
  13. if ((ret = mbedtls_pk_parse_public_key(&pk, pubKey, pubKeyLen + 1)) != 0) {
  14. goto exit;
  15. }
  16. // Hash input buffer
  17. if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512), buf, len, hash)) != 0) {
  18. goto exit;
  19. }
  20. // Verify signautre
  21. if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA512, hash, 64, sig, sigLen)) != 0) {
  22. goto exit;
  23. }
  24. result = true;
  25. exit:
  26. mbedtls_pk_free(&pk);
  27. return result;
  28. }