1
0
mirror of https://github.com/ARMmbed/mbedtls.git synced 2025-10-20 13:04:23 +08:00

Merge pull request #10256 from ariwo17/psa-dlopen-demo

Modify dlopen.c and tfpsacrypto_dlopen.c so that they use PSA API-only dynamic loading
This commit is contained in:
minosgalanakis
2025-07-15 22:06:49 +00:00
committed by GitHub
2 changed files with 34 additions and 9 deletions

View File

@@ -98,16 +98,41 @@ int main(void)
* "gcc -std=c99 -pedantic" complains about it, but it is perfectly
* fine on platforms that have dlsym(). */
#pragma GCC diagnostic ignored "-Wpedantic"
const int *(*md_list)(void) =
dlsym(crypto_so, "mbedtls_md_list");
psa_status_t (*dyn_psa_crypto_init)(void) =
dlsym(crypto_so, "psa_crypto_init");
psa_status_t (*dyn_psa_hash_compute)(psa_algorithm_t, const uint8_t *, size_t, uint8_t *,
size_t, size_t *) =
dlsym(crypto_so, "psa_hash_compute");
#pragma GCC diagnostic pop
CHECK_DLERROR("dlsym", "mbedtls_md_list");
const int *mds = md_list();
for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */
;
/* Demonstrate hashing a message with PSA Crypto */
CHECK_DLERROR("dlsym", "psa_crypto_init");
CHECK_DLERROR("dlsym", "psa_hash_compute");
psa_status_t status = dyn_psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "psa_crypto_init failed: %d\n", (int) status);
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
}
mbedtls_printf("dlopen(%s): %u hashes\n",
crypto_so_filename, n);
const uint8_t input[] = "hello world";
uint8_t hash[32]; // Buffer to hold the output hash
size_t hash_len = 0;
status = dyn_psa_hash_compute(PSA_ALG_SHA_256,
input, sizeof(input) - 1,
hash, sizeof(hash),
&hash_len);
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "psa_hash_compute failed: %d\n", (int) status);
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
}
mbedtls_printf("dlopen(%s): psa_hash_compute succeeded. SHA-256 output length: %zu\n",
crypto_so_filename, hash_len);
dlclose(crypto_so);
CHECK_DLERROR("dlclose", crypto_so_filename);
#endif /* MBEDTLS_MD_C */