blob: a7b9001fc96528941777c29312407cffbcc8ef69 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
93 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
94 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
95 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020096 goto exit;
97 }
98
99 /*
100 * Extract the RSA encrypted value from the text file
101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
103 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
Hanno Beckerbd336c12017-10-08 16:44:10 +0100104 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200105 goto exit;
106 }
107
108 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
110 i < (int) sizeof(buf)) {
Paul Bakker940f9ce2013-09-18 15:34:57 +0200111 buf[i++] = (unsigned char) c;
Hanno Beckerae513a52018-08-23 14:39:04 +0100112 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115
116 /*
117 * Decrypt the encrypted RSA data and print the result.
118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_printf("\n . Decrypting the encrypted data");
120 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result),
123 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
124 mbedtls_printf(" failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
125 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200126 goto exit;
127 }
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_printf("\n . OK\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf("The decrypted result is: '%s'\n\n", result);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +0100133 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134
135exit:
Hanno Beckerbd336c12017-10-08 16:44:10 +0100136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_pk_free(&pk);
138 mbedtls_entropy_free(&entropy);
139 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200141 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200142#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100146 mbedtls_printf("Error code: %d", ret);
147 /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
148 mbedtls_printf(" ! Last error was: %s\n", buf); */
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200149 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200150#endif
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
155 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */