diff options
Diffstat (limited to 'lib/crypto-mbedtls.c')
-rw-r--r-- | lib/crypto-mbedtls.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/crypto-mbedtls.c b/lib/crypto-mbedtls.c index 3fa4cfd..31b57f8 100644 --- a/lib/crypto-mbedtls.c +++ b/lib/crypto-mbedtls.c @@ -44,6 +44,15 @@ static mbedtls_entropy_context entropy; static void __attribute__((constructor)) load(); static void __attribute__((destructor)) unload(); +static void raise_mbedtls_exception(uc_vm_t *vm, int errnum, const char *msg) +{ + char buf[120] = ""; + + mbedtls_strerror(errnum, buf, sizeof(buf)); + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, msg, buf); +} + + static uc_value_t * md_digest(uc_vm_t *vm, size_t nargs) { @@ -268,9 +277,7 @@ pk_set_public_key(uc_vm_t *vm, size_t nargs) int err = mbedtls_pk_parse_public_key(&ctx->pk, (const unsigned char*)ucv_string_get(key), ucv_string_length(key)); if (err) - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "not a valid DER key %s: %s", - mbedtls_high_level_strerr(err), - mbedtls_low_level_strerr(err)); + raise_mbedtls_exception(vm, err, "not a valid DER key %s"); return NULL; } @@ -316,7 +323,7 @@ pk_sign(uc_vm_t *vm, size_t nargs) size_t sig_len = sizeof(sig); if (mbedtls_pk_sign(&ctx->pk, md_type, hash, hash_len, - sig, &sig_len, + sig, sig_len, &sig_len, mbedtls_ctr_drbg_random, &ctx->ctr_drbg)) { uc_vm_raise_exception(vm, EXCEPTION_TYPE, "pk sign failed"); return NULL; @@ -376,9 +383,7 @@ pk_verify(uc_vm_t *vm, size_t nargs) hash, hash_size, (const unsigned char*)ucv_string_get(sig), ucv_string_length(sig)); if (err) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "validation failed: %s: %s", - mbedtls_high_level_strerr(err), - mbedtls_low_level_strerr(err)); + raise_mbedtls_exception(vm, err, "validation failed: %s"); return FALSE; } |