blob: b064078016482b2ace922514f52f5ba8c2676b18 [file] [log] [blame]
Paul Bakkered56b222011-07-13 11:26:43 +00001/*
2 * Key reading application
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkered56b222011-07-13 11:26:43 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakkered56b222011-07-13 11:26:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_BIGNUM_C) && \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020015 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
16 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010018#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020019#include "mbedtls/entropy.h"
20#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020021
Rich Evans18b78c72015-02-11 14:06:19 +000022#include <string.h>
23#endif
24
25#define MODE_NONE 0
26#define MODE_PRIVATE 1
27#define MODE_PUBLIC 2
28
29#define DFL_MODE MODE_NONE
30#define DFL_FILENAME "keyfile.key"
31#define DFL_PASSWORD ""
32#define DFL_PASSWORD_FILE ""
33#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000034
Rich Evans18b78c72015-02-11 14:06:19 +000035#define USAGE \
36 "\n usage: key_app param=<>...\n" \
37 "\n acceptable parameters:\n" \
38 " mode=private|public default: none\n" \
39 " filename=%%s default: keyfile.key\n" \
40 " password=%%s default: \"\"\n" \
41 " password_file=%%s default: \"\"\n" \
42 "\n"
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_BIGNUM_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020045 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
46 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010047int main(void)
Paul Bakker15254952013-09-17 11:24:56 +020048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010050 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
51 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
52 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020053}
54#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000055
Simon Butcher63cb97e2018-12-06 17:43:31 +000056
Gilles Peskine52cc2a62023-06-22 22:32:05 +020057#if defined(MBEDTLS_ECP_C)
58static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
59{
60 int ret = 0;
61
62 const mbedtls_ecp_curve_info *curve_info =
63 mbedtls_ecp_curve_info_from_grp_id(
64 mbedtls_ecp_keypair_get_group_id(ecp));
65 mbedtls_printf("curve: %s\n", curve_info->name);
66
67 mbedtls_ecp_group grp;
68 mbedtls_ecp_group_init(&grp);
69 mbedtls_mpi D;
70 mbedtls_mpi_init(&D);
71 mbedtls_ecp_point pt;
72 mbedtls_ecp_point_init(&pt);
73 mbedtls_mpi X, Y;
74 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
75
76 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
77 (has_private ? &D : NULL),
78 &pt));
79
80 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
81 size_t len = 0;
82 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
83 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
84 &len, point_bin, sizeof(point_bin)));
85 switch (mbedtls_ecp_get_type(&grp)) {
86 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
87 if ((len & 1) == 0 || point_bin[0] != 0x04) {
88 /* Point in an unxepected format. This shouldn't happen. */
89 ret = -1;
90 goto cleanup;
91 }
92 MBEDTLS_MPI_CHK(
93 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
94 MBEDTLS_MPI_CHK(
95 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
96 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
97 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
98 break;
99 case MBEDTLS_ECP_TYPE_MONTGOMERY:
100 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
101 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
102 break;
103 default:
104 mbedtls_printf(
105 "This program does not yet support listing coordinates for this curve type.\n");
106 break;
107 }
108
109 if (has_private) {
110 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
111 }
112
113cleanup:
114 mbedtls_ecp_group_free(&grp);
115 mbedtls_mpi_free(&D);
116 mbedtls_ecp_point_free(&pt);
117 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
118 return ret;
119}
120#endif
121
Paul Bakkered56b222011-07-13 11:26:43 +0000122/*
123 * global options
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125struct options {
Paul Bakkered56b222011-07-13 11:26:43 +0000126 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200127 const char *filename; /* filename of the key file */
128 const char *password; /* password for the private key */
129 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000130} opt;
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +0000133{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100134 int ret = 1;
135 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000136 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000137 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000138 char *p, *q;
139
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200140 const char *pers = "pkey/key_app";
141 mbedtls_entropy_context entropy;
142 mbedtls_ctr_drbg_context ctr_drbg;
143
Hanno Becker54ebf992017-08-23 06:45:38 +0100144 mbedtls_pk_context pk;
145 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
146
Paul Bakkered56b222011-07-13 11:26:43 +0000147 /*
148 * Set to sane values
149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_entropy_init(&entropy);
151 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_pk_init(&pk);
154 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +0000155
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200156#if defined(MBEDTLS_USE_PSA_CRYPTO)
157 psa_status_t status = psa_crypto_init();
158 if (status != PSA_SUCCESS) {
159 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
160 (int) status);
161 goto cleanup;
162 }
163#endif /* MBEDTLS_USE_PSA_CRYPTO */
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
166 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
167 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +0100168
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000169 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100170usage:
171 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300172 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000173 }
174
175 opt.mode = DFL_MODE;
176 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000177 opt.password = DFL_PASSWORD;
178 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000181 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000183 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
Paul Bakkered56b222011-07-13 11:26:43 +0000185 *q++ = '\0';
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (strcmp(p, "mode") == 0) {
188 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000189 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000191 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000193 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 }
195 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000196 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000198 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000200 opt.password_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000202 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 }
Paul Bakkered56b222011-07-13 11:26:43 +0000204 }
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (opt.mode == MODE_PRIVATE) {
207 if (strlen(opt.password) && strlen(opt.password_file)) {
208 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000209 goto usage;
210 }
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000213 FILE *f;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_printf("\n . Loading the password file ...");
216 if ((f = fopen(opt.password_file, "rb")) == NULL) {
217 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300218 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000219 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (fgets(buf, sizeof(buf), f) == NULL) {
221 fclose(f);
222 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300223 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200224 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 i = (int) strlen(buf);
228 if (buf[i - 1] == '\n') {
229 buf[i - 1] = '\0';
230 }
231 if (buf[i - 2] == '\r') {
232 buf[i - 2] = '\0';
233 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000234 opt.password = buf;
235 }
236
Paul Bakkered56b222011-07-13 11:26:43 +0000237 /*
238 * 1.1. Load the key
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_printf("\n . Loading the private key ...");
241 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
244 (const unsigned char *) pers,
245 strlen(pers))) != 0) {
246 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
247 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200248 goto cleanup;
249 }
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password,
252 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkered56b222011-07-13 11:26:43 +0000253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (ret != 0) {
255 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
256 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300257 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000258 }
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000261
262 /*
263 * 1.2 Print the key
264 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
268 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
271 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
272 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300273 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100274 }
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
277 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
278 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
279 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
280 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
281 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
282 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
283 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
284 } else
Paul Bakker15254952013-09-17 11:24:56 +0200285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200288 if (show_ecp_key(mbedtls_pk_ec(pk), 1) != 0) {
289 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
290 goto cleanup;
291 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 } else
Paul Bakker15254952013-09-17 11:24:56 +0200293#endif
294 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300296 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200297 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000299 /*
300 * 1.1. Load the key
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 mbedtls_printf("\n . Loading the public key ...");
303 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (ret != 0) {
308 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
309 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300310 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000311 }
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
318 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
321 NULL, &E)) != 0) {
322 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300323 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100324 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
326 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
327 } else
Paul Bakker15254952013-09-17 11:24:56 +0200328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200331 if (show_ecp_key(mbedtls_pk_ec(pk), 0) != 0) {
332 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
333 goto cleanup;
334 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else
Paul Bakker15254952013-09-17 11:24:56 +0200336#endif
337 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300339 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200340 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000342 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 }
Paul Bakkered56b222011-07-13 11:26:43 +0000344
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100345 exit_code = MBEDTLS_EXIT_SUCCESS;
346
Ron Eldor6a9257b2017-08-24 14:20:17 +0300347cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100351 mbedtls_printf("Error code: %d", ret);
352 /* mbedtls_strerror(ret, buf, sizeof(buf));
353 mbedtls_printf(" ! Last error was: %s\n", buf); */
Hanno Becker54ebf992017-08-23 06:45:38 +0100354 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200355#endif
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_ctr_drbg_free(&ctr_drbg);
358 mbedtls_entropy_free(&entropy);
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 mbedtls_pk_free(&pk);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200361 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200362#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
364 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
365 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Paul Bakkered56b222011-07-13 11:26:43 +0000366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_exit(exit_code);
Paul Bakkered56b222011-07-13 11:26:43 +0000368}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200369#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
370 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */