blob: e3a6966050b3cf583d5ea97555c4939bf20af31a [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/error.h"
16#include "mbedtls/rsa.h"
Jaeden Ameroed736992018-10-26 16:55:14 +010017#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020018#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020020
Rich Evans18b78c72015-02-11 14:06:19 +000021#include <string.h>
22#endif
23
24#define MODE_NONE 0
25#define MODE_PRIVATE 1
26#define MODE_PUBLIC 2
27
28#define DFL_MODE MODE_NONE
29#define DFL_FILENAME "keyfile.key"
30#define DFL_PASSWORD ""
31#define DFL_PASSWORD_FILE ""
32#define DFL_DEBUG_LEVEL 0
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000033
Rich Evans18b78c72015-02-11 14:06:19 +000034#define USAGE \
35 "\n usage: key_app param=<>...\n" \
36 "\n acceptable parameters:\n" \
37 " mode=private|public default: none\n" \
38 " filename=%%s default: keyfile.key\n" \
39 " password=%%s default: \"\"\n" \
40 " password_file=%%s default: \"\"\n" \
41 "\n"
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_BIGNUM_C) || \
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +020044 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
45 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010046int main(void)
Paul Bakker15254952013-09-17 11:24:56 +020047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010049 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
50 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined.\n");
51 mbedtls_exit(0);
Paul Bakker15254952013-09-17 11:24:56 +020052}
53#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000054
Simon Butcher63cb97e2018-12-06 17:43:31 +000055
Gilles Peskine52cc2a62023-06-22 22:32:05 +020056#if defined(MBEDTLS_ECP_C)
57static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
58{
59 int ret = 0;
60
61 const mbedtls_ecp_curve_info *curve_info =
62 mbedtls_ecp_curve_info_from_grp_id(
63 mbedtls_ecp_keypair_get_group_id(ecp));
64 mbedtls_printf("curve: %s\n", curve_info->name);
65
66 mbedtls_ecp_group grp;
67 mbedtls_ecp_group_init(&grp);
68 mbedtls_mpi D;
69 mbedtls_mpi_init(&D);
70 mbedtls_ecp_point pt;
71 mbedtls_ecp_point_init(&pt);
72 mbedtls_mpi X, Y;
73 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
74
75 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
76 (has_private ? &D : NULL),
77 &pt));
78
79 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
80 size_t len = 0;
81 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
82 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
83 &len, point_bin, sizeof(point_bin)));
84 switch (mbedtls_ecp_get_type(&grp)) {
85 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
86 if ((len & 1) == 0 || point_bin[0] != 0x04) {
87 /* Point in an unxepected format. This shouldn't happen. */
88 ret = -1;
89 goto cleanup;
90 }
91 MBEDTLS_MPI_CHK(
92 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
93 MBEDTLS_MPI_CHK(
94 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
95 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
96 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
97 break;
98 case MBEDTLS_ECP_TYPE_MONTGOMERY:
99 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
100 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
101 break;
102 default:
103 mbedtls_printf(
104 "This program does not yet support listing coordinates for this curve type.\n");
105 break;
106 }
107
108 if (has_private) {
109 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
110 }
111
112cleanup:
113 mbedtls_ecp_group_free(&grp);
114 mbedtls_mpi_free(&D);
115 mbedtls_ecp_point_free(&pt);
116 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
117 return ret;
118}
119#endif
120
Paul Bakkered56b222011-07-13 11:26:43 +0000121/*
122 * global options
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124struct options {
Paul Bakkered56b222011-07-13 11:26:43 +0000125 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200126 const char *filename; /* filename of the key file */
127 const char *password; /* password for the private key */
128 const char *password_file; /* password_file for the private key */
Paul Bakkered56b222011-07-13 11:26:43 +0000129} opt;
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131int main(int argc, char *argv[])
Paul Bakkered56b222011-07-13 11:26:43 +0000132{
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100133 int ret = 1;
134 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakkered56b222011-07-13 11:26:43 +0000135 char buf[1024];
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000136 int i;
Paul Bakkered56b222011-07-13 11:26:43 +0000137 char *p, *q;
138
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200139 const char *pers = "pkey/key_app";
140 mbedtls_entropy_context entropy;
141 mbedtls_ctr_drbg_context ctr_drbg;
142
Hanno Becker54ebf992017-08-23 06:45:38 +0100143 mbedtls_pk_context pk;
144 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
145
Paul Bakkered56b222011-07-13 11:26:43 +0000146 /*
147 * Set to sane values
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_entropy_init(&entropy);
150 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_pk_init(&pk);
153 memset(buf, 0, sizeof(buf));
Paul Bakkered56b222011-07-13 11:26:43 +0000154
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200155#if defined(MBEDTLS_USE_PSA_CRYPTO)
156 psa_status_t status = psa_crypto_init();
157 if (status != PSA_SUCCESS) {
158 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
159 (int) status);
160 goto cleanup;
161 }
162#endif /* MBEDTLS_USE_PSA_CRYPTO */
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
165 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
166 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Becker54ebf992017-08-23 06:45:38 +0100167
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000168 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100169usage:
170 mbedtls_printf(USAGE);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300171 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000172 }
173
174 opt.mode = DFL_MODE;
175 opt.filename = DFL_FILENAME;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000176 opt.password = DFL_PASSWORD;
177 opt.password_file = DFL_PASSWORD_FILE;
Paul Bakkered56b222011-07-13 11:26:43 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 for (i = 1; i < argc; i++) {
Paul Bakkered56b222011-07-13 11:26:43 +0000180 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkered56b222011-07-13 11:26:43 +0000182 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Paul Bakkered56b222011-07-13 11:26:43 +0000184 *q++ = '\0';
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (strcmp(p, "mode") == 0) {
187 if (strcmp(q, "private") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000188 opt.mode = MODE_PRIVATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 } else if (strcmp(q, "public") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000190 opt.mode = MODE_PUBLIC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000192 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 }
194 } else if (strcmp(p, "filename") == 0) {
Paul Bakkered56b222011-07-13 11:26:43 +0000195 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 } else if (strcmp(p, "password") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000197 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 } else if (strcmp(p, "password_file") == 0) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000199 opt.password_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000201 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 }
Paul Bakkered56b222011-07-13 11:26:43 +0000203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (opt.mode == MODE_PRIVATE) {
206 if (strlen(opt.password) && strlen(opt.password_file)) {
207 mbedtls_printf("Error: cannot have both password and password_file\n");
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000208 goto usage;
209 }
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (strlen(opt.password_file)) {
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000212 FILE *f;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_printf("\n . Loading the password file ...");
215 if ((f = fopen(opt.password_file, "rb")) == NULL) {
216 mbedtls_printf(" failed\n ! fopen returned NULL\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300217 goto cleanup;
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000218 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (fgets(buf, sizeof(buf), f) == NULL) {
220 fclose(f);
221 mbedtls_printf("Error: fgets() failed to retrieve password\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300222 goto cleanup;
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200223 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 fclose(f);
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 i = (int) strlen(buf);
227 if (buf[i - 1] == '\n') {
228 buf[i - 1] = '\0';
229 }
230 if (buf[i - 2] == '\r') {
231 buf[i - 2] = '\0';
232 }
Paul Bakkerdb2509c2012-09-27 12:44:31 +0000233 opt.password = buf;
234 }
235
Paul Bakkered56b222011-07-13 11:26:43 +0000236 /*
237 * 1.1. Load the key
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_printf("\n . Loading the private key ...");
240 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
243 (const unsigned char *) pers,
244 strlen(pers))) != 0) {
245 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
246 (unsigned int) -ret);
Manuel Pégourié-Gonnard1503a9a2021-06-16 10:35:56 +0200247 goto cleanup;
248 }
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password,
251 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkered56b222011-07-13 11:26:43 +0000252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (ret != 0) {
254 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
255 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300256 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000260
261 /*
262 * 1.2 Print the key
263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
267 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
270 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
271 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300272 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100273 }
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
276 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
277 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL));
278 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL));
279 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL));
280 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL));
281 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL));
282 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL));
283 } else
Paul Bakker15254952013-09-17 11:24:56 +0200284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200287 if (show_ecp_key(mbedtls_pk_ec(pk), 1) != 0) {
288 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
289 goto cleanup;
290 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 } else
Paul Bakker15254952013-09-17 11:24:56 +0200292#endif
293 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300295 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200296 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 } else if (opt.mode == MODE_PUBLIC) {
Paul Bakkered56b222011-07-13 11:26:43 +0000298 /*
299 * 1.1. Load the key
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_printf("\n . Loading the public key ...");
302 fflush(stdout);
Paul Bakkered56b222011-07-13 11:26:43 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename);
Paul Bakkered56b222011-07-13 11:26:43 +0000305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (ret != 0) {
307 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
308 (unsigned int) -ret);
Ron Eldor6a9257b2017-08-24 14:20:17 +0300309 goto cleanup;
Paul Bakkered56b222011-07-13 11:26:43 +0000310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_printf(" ok\n");
Paul Bakkered56b222011-07-13 11:26:43 +0000313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_printf(" . Key information ...\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
317 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
Hanno Becker54ebf992017-08-23 06:45:38 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL,
320 NULL, &E)) != 0) {
321 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Ron Eldora5221472018-06-27 08:49:00 +0300322 goto cleanup;
Hanno Becker54ebf992017-08-23 06:45:38 +0100323 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL));
325 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL));
326 } else
Paul Bakker15254952013-09-17 11:24:56 +0200327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200330 if (show_ecp_key(mbedtls_pk_ec(pk), 0) != 0) {
331 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
332 goto cleanup;
333 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 } else
Paul Bakker15254952013-09-17 11:24:56 +0200335#endif
336 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_printf("Do not know how to print key information for this type\n");
Ron Eldor6a9257b2017-08-24 14:20:17 +0300338 goto cleanup;
Paul Bakker15254952013-09-17 11:24:56 +0200339 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 } else {
Paul Bakkered56b222011-07-13 11:26:43 +0000341 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 }
Paul Bakkered56b222011-07-13 11:26:43 +0000343
Andres Amaya Garcia0faf1a52018-04-29 20:02:18 +0100344 exit_code = MBEDTLS_EXIT_SUCCESS;
345
Ron Eldor6a9257b2017-08-24 14:20:17 +0300346cleanup:
Paul Bakkered56b222011-07-13 11:26:43 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
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 */