blob: d01aa885255e3e9ab1b263fb414e744b42eca353 [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakkered56b222011-07-13 11:26:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_BIGNUM_C) && \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020013 defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
14 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010016#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020017#include "mbedtls/entropy.h"
18#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020019
Rich Evans18b78c72015-02-11 14:06:19 +000020#include <string.h>
21#endif
22
23#define MODE_NONE 0
24#define MODE_PRIVATE 1
25#define MODE_PUBLIC 2
26
27#define DFL_MODE MODE_NONE
28#define DFL_FILENAME "keyfile.key"
29#define DFL_PASSWORD ""
30#define DFL_PASSWORD_FILE ""
31#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000032
Rich Evans18b78c72015-02-11 14:06:19 +000033#define USAGE \
34 "\n usage: key_app param=<>...\n" \
35 "\n acceptable parameters:\n" \
36 " mode=private|public default: none\n" \
37 " filename=%%s default: keyfile.key\n" \
38 " password=%%s default: \"\"\n" \
39 " password_file=%%s default: \"\"\n" \
40 "\n"
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_BIGNUM_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020043 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
44 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010045int main(void)
Paul Bakker15254952013-09-17 11:24:56 +020046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010048 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
49 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
50 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020051}
52#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000053
Simon Butcher63cb97e2018-12-06 17:43:31 +000054
Gilles Peskine52cc2a62023-06-22 22:32:05 +020055#if defined(MBEDTLS_ECP_C)
56static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
57{
58 int ret = 0;
59
60 const mbedtls_ecp_curve_info *curve_info =
61 mbedtls_ecp_curve_info_from_grp_id(
62 mbedtls_ecp_keypair_get_group_id(ecp));
63 mbedtls_printf("curve: %s\n", curve_info->name);
64
65 mbedtls_ecp_group grp;
66 mbedtls_ecp_group_init(&grp);
67 mbedtls_mpi D;
68 mbedtls_mpi_init(&D);
69 mbedtls_ecp_point pt;
70 mbedtls_ecp_point_init(&pt);
71 mbedtls_mpi X, Y;
72 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
73
74 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
75 (has_private ? &D : NULL),
76 &pt));
77
78 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
79 size_t len = 0;
80 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
81 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
82 &len, point_bin, sizeof(point_bin)));
83 switch (mbedtls_ecp_get_type(&grp)) {
84 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
85 if ((len & 1) == 0 || point_bin[0] != 0x04) {
86 /* Point in an unxepected format. This shouldn't happen. */
87 ret = -1;
88 goto cleanup;
89 }
90 MBEDTLS_MPI_CHK(
91 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
92 MBEDTLS_MPI_CHK(
93 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
94 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
95 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
96 break;
97 case MBEDTLS_ECP_TYPE_MONTGOMERY:
98 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
99 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
100 break;
101 default:
102 mbedtls_printf(
103 "This program does not yet support listing coordinates for this curve type.\n");
104 break;
105 }
106
107 if (has_private) {
108 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
109 }
110
111cleanup:
112 mbedtls_ecp_group_free(&grp);
113 mbedtls_mpi_free(&D);
114 mbedtls_ecp_point_free(&pt);
115 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
116 return ret;
117}
118#endif
119
Paul Bakkered56b222011-07-13 11:26:43 +0000120/*
121 * global options
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123struct options {
Paul Bakkered56b222011-07-13 11:26:43 +0000124 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200125 const char *filename; /* filename of the key file */
126 const char *password; /* password for the private key */
127 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000128} opt;
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +0000131{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100132 int ret = 1;
133 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000134 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000135 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000136 char *p, *q;
137
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200138 const char *pers = "pkey/key_app";
139 mbedtls_entropy_context entropy;
140 mbedtls_ctr_drbg_context ctr_drbg;
141
Hanno Becker54ebf992017-08-23 06:45:38 +0100142 mbedtls_pk_context pk;
143 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
144
Paul Bakkered56b222011-07-13 11:26:43 +0000145 /*
146 * Set to sane values
147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_entropy_init(&entropy);
149 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_pk_init(&pk);
152 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +0000153
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200154#if defined(MBEDTLS_USE_PSA_CRYPTO)
155 psa_status_t status = psa_crypto_init();
156 if (status != PSA_SUCCESS) {
157 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
158 (int) status);
159 goto cleanup;
160 }
161#endif /* MBEDTLS_USE_PSA_CRYPTO */
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
164 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
165 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +0100166
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000167 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100168usage:
169 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300170 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000171 }
172
173 opt.mode = DFL_MODE;
174 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000175 opt.password = DFL_PASSWORD;
176 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000179 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000181 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 }
Paul Bakkered56b222011-07-13 11:26:43 +0000183 *q++ = '\0';
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (strcmp(p, "mode") == 0) {
186 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000187 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000189 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000191 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 }
193 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000194 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000196 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000198 opt.password_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000200 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 }
Paul Bakkered56b222011-07-13 11:26:43 +0000202 }
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (opt.mode == MODE_PRIVATE) {
205 if (strlen(opt.password) && strlen(opt.password_file)) {
206 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000207 goto usage;
208 }
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000211 FILE *f;
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_printf("\n . Loading the password file ...");
214 if ((f = fopen(opt.password_file, "rb")) == NULL) {
215 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300216 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000217 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (fgets(buf, sizeof(buf), f) == NULL) {
219 fclose(f);
220 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300221 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200222 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 i = (int) strlen(buf);
226 if (buf[i - 1] == '\n') {
227 buf[i - 1] = '\0';
228 }
229 if (buf[i - 2] == '\r') {
230 buf[i - 2] = '\0';
231 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000232 opt.password = buf;
233 }
234
Paul Bakkered56b222011-07-13 11:26:43 +0000235 /*
236 * 1.1. Load the key
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_printf("\n . Loading the private key ...");
239 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
242 (const unsigned char *) pers,
243 strlen(pers))) != 0) {
244 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
245 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200246 goto cleanup;
247 }
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password,
250 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkered56b222011-07-13 11:26:43 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (ret != 0) {
253 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
254 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300255 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000256 }
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000259
260 /*
261 * 1.2 Print the key
262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
266 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
269 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
270 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300271 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100272 }
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
275 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
276 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
277 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
278 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
279 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
280 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
281 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
282 } else
Paul Bakker15254952013-09-17 11:24:56 +0200283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200286 if (show_ecp_key(mbedtls_pk_ec(pk), 1) != 0) {
287 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
288 goto cleanup;
289 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 } else
Paul Bakker15254952013-09-17 11:24:56 +0200291#endif
292 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300294 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200295 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000297 /*
298 * 1.1. Load the key
299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 mbedtls_printf("\n . Loading the public key ...");
301 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (ret != 0) {
306 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
307 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300308 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
316 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
319 NULL, &E)) != 0) {
320 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300321 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100322 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
324 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
325 } else
Paul Bakker15254952013-09-17 11:24:56 +0200326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200329 if (show_ecp_key(mbedtls_pk_ec(pk), 0) != 0) {
330 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
331 goto cleanup;
332 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else
Paul Bakker15254952013-09-17 11:24:56 +0200334#endif
335 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300337 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200338 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000340 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Paul Bakkered56b222011-07-13 11:26:43 +0000342
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100343 exit_code = MBEDTLS_EXIT_SUCCESS;
344
Ron Eldor6a9257b2017-08-24 14:20:17 +0300345cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100349 mbedtls_printf("Error code: %d", ret);
350 /* mbedtls_strerror(ret, buf, sizeof(buf));
351 mbedtls_printf(" ! Last error was: %s\n", buf); */
Hanno Becker54ebf992017-08-23 06:45:38 +0100352 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200353#endif
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 mbedtls_ctr_drbg_free(&ctr_drbg);
356 mbedtls_entropy_free(&entropy);
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_pk_free(&pk);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200358#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200359 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200360#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
362 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
363 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Paul Bakkered56b222011-07-13 11:26:43 +0000364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 mbedtls_exit(exit_code);
Paul Bakkered56b222011-07-13 11:26:43 +0000366}
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200367#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
368 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */