Skip to content

Commit

Permalink
Init PSA in pkey programs
Browse files Browse the repository at this point in the history
Signed-off-by: Przemek Stekiel <[email protected]>
  • Loading branch information
mprse committed Apr 25, 2023
1 parent c4ddf92 commit 9c0fc2d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions programs/pkey/gen_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ int main(int argc, char *argv[])
mbedtls_ctr_drbg_init(&ctr_drbg);
memset(buf, 0, sizeof(buf));

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc < 2) {
usage:
mbedtls_printf(USAGE);
Expand Down Expand Up @@ -411,6 +420,7 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&key);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
mbedtls_psa_crypto_free();

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/key_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ int main(int argc, char *argv[])
mbedtls_pk_init(&pk);
memset(buf, 0, sizeof(buf));

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto cleanup;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Expand Down Expand Up @@ -275,6 +284,7 @@ int main(int argc, char *argv[])
#endif

mbedtls_pk_free(&pk);
mbedtls_psa_crypto_free();
mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/key_app_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ int main(int argc, char *argv[])
memset(buf, 0, sizeof(buf));
#endif

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Expand Down Expand Up @@ -400,6 +409,7 @@ int main(int argc, char *argv[])
mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);

mbedtls_pk_free(&key);
mbedtls_psa_crypto_free();

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/pk_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ int main(int argc, char *argv[])

memset(result, 0, sizeof(result));

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc != 2) {
mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n");

Expand Down Expand Up @@ -142,6 +151,7 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_entropy_free(&entropy);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_psa_crypto_free();

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/pk_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ int main(int argc, char *argv[])
mbedtls_entropy_init(&entropy);
mbedtls_pk_init(&pk);

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc != 3) {
mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");

Expand Down Expand Up @@ -144,6 +153,7 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_entropy_free(&entropy);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_psa_crypto_free();

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/pk_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ int main(int argc, char *argv[])
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_pk_init(&pk);

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc != 3) {
mbedtls_printf("usage: mbedtls_pk_sign <key_file> <filename>\n");

Expand Down Expand Up @@ -141,6 +150,7 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
mbedtls_psa_crypto_free();

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/pk_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ int main(int argc, char *argv[])

mbedtls_pk_init(&pk);

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc != 3) {
mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n");

Expand Down Expand Up @@ -117,6 +126,7 @@ int main(int argc, char *argv[])

exit:
mbedtls_pk_free(&pk);
mbedtls_psa_crypto_free();

#if defined(MBEDTLS_ERROR_C)
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/rsa_sign_pss.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ int main(int argc, char *argv[])
mbedtls_pk_init(&pk);
mbedtls_ctr_drbg_init(&ctr_drbg);

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc != 3) {
mbedtls_printf("usage: rsa_sign_pss <key_file> <filename>\n");

Expand Down Expand Up @@ -149,6 +158,7 @@ int main(int argc, char *argv[])
mbedtls_pk_free(&pk);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
mbedtls_psa_crypto_free();

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down
10 changes: 10 additions & 0 deletions programs/pkey/rsa_verify_pss.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ int main(int argc, char *argv[])

mbedtls_pk_init(&pk);

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) {
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
(int) status);
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */

if (argc != 3) {
mbedtls_printf("usage: rsa_verify_pss <key_file> <filename>\n");

Expand Down Expand Up @@ -127,6 +136,7 @@ int main(int argc, char *argv[])

exit:
mbedtls_pk_free(&pk);
mbedtls_psa_crypto_free();

#if defined(_WIN32)
mbedtls_printf(" + Press Enter to exit this program.\n");
Expand Down

0 comments on commit 9c0fc2d

Please sign in to comment.