blob: d2bfde50f00ec8c1dc295360824d61e4036dc036 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based simple decryption program
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 Bakker940f9ce2013-09-18 15:34:57 +02006 */
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 Bakker940f9ce2013-09-18 15:34:57 +020011
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) && defined(MBEDTLS_PK_PARSE_C) && \
15 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
16 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/pk.h"
18#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020020
Rich Evans18b78c72015-02-11 14:06:19 +000021#include <stdio.h>
22#include <string.h>
23#endif
24
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
26 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
27 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010028int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010031 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
32 "MBEDTLS_CTR_DRBG_C not defined.\n");
33 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020034}
35#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000036
Simon Butcher63cb97e2018-12-06 17:43:31 +000037
Gilles Peskine449bd832023-01-11 14:50:10 +010038int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020039{
40 FILE *f;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020041 int ret = 1;
42 unsigned c;
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +010043 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020044 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_pk_context pk;
46 mbedtls_entropy_context entropy;
47 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020048 unsigned char result[1024];
49 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 const char *pers = "mbedtls_pk_decrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020051 ((void) argv);
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_pk_init(&pk);
54 mbedtls_entropy_init(&entropy);
55 mbedtls_ctr_drbg_init(&ctr_drbg);
Hanno Beckerbd336c12017-10-08 16:44:10 +010056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 memset(result, 0, sizeof(result));
Paul Bakker940f9ce2013-09-18 15:34:57 +020058
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020059#if defined(MBEDTLS_USE_PSA_CRYPTO)
60 psa_status_t status = psa_crypto_init();
61 if (status != PSA_SUCCESS) {
62 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
63 (int) status);
64 goto exit;
65 }
66#endif /* MBEDTLS_USE_PSA_CRYPTO */
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if (argc != 2) {
69 mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020070
71#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020073#endif
74
75 goto exit;
76 }
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_printf("\n . Seeding the random number generator...");
79 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
82 &entropy, (const unsigned char *) pers,
83 strlen(pers))) != 0) {
84 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
85 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020086 goto exit;
87 }
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
90 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020091
Ben Taylor440cb2a2025-03-05 09:40:08 +000092 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +010093 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
94 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
98 /*
99 * Extract the RSA encrypted value from the text file
100 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
102 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
Hanno Beckerbd336c12017-10-08 16:44:10 +0100103 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104 goto exit;
105 }
106
107 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
109 i < (int) sizeof(buf)) {
Paul Bakker940f9ce2013-09-18 15:34:57 +0200110 buf[i++] = (unsigned char) c;
Hanno Beckerae513a52018-08-23 14:39:04 +0100111 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200114
115 /*
116 * Decrypt the encrypted RSA data and print the result.
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_printf("\n . Decrypting the encrypted data");
119 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120
Ben Taylor440cb2a2025-03-05 09:40:08 +0000121 if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result))) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_printf(" failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
123 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200124 goto exit;
125 }
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_printf("\n . OK\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_printf("The decrypted result is: '%s'\n\n", result);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +0100131 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
133exit:
Hanno Beckerbd336c12017-10-08 16:44:10 +0100134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_pk_free(&pk);
136 mbedtls_entropy_free(&entropy);
137 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200138#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200139 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200140#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100144 mbedtls_printf("Error code: %d", ret);
145 /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
146 mbedtls_printf(" ! Last error was: %s\n", buf); */
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200147 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200148#endif
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
153 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */