blob: 3dbfde02bc875dabf64687ac60cbf908fa6b7d0c [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +02009
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) && defined(MBEDTLS_PK_PARSE_C) && \
13 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
14 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/pk.h"
16#include "mbedtls/entropy.h"
17#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020018
Rich Evans18b78c72015-02-11 14:06:19 +000019#include <stdio.h>
20#include <string.h>
21#endif
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
24 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
25 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010026int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010029 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
30 "MBEDTLS_CTR_DRBG_C not defined.\n");
31 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020032}
33#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000034
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Gilles Peskine449bd832023-01-11 14:50:10 +010036int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020037{
38 FILE *f;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020039 int ret = 1;
40 unsigned c;
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +010041 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020042 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_pk_context pk;
44 mbedtls_entropy_context entropy;
45 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020046 unsigned char result[1024];
47 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 const char *pers = "mbedtls_pk_decrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020049 ((void) argv);
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051 mbedtls_pk_init(&pk);
52 mbedtls_entropy_init(&entropy);
53 mbedtls_ctr_drbg_init(&ctr_drbg);
Hanno Beckerbd336c12017-10-08 16:44:10 +010054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 memset(result, 0, sizeof(result));
Paul Bakker940f9ce2013-09-18 15:34:57 +020056
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58 psa_status_t status = psa_crypto_init();
59 if (status != PSA_SUCCESS) {
60 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
61 (int) status);
62 goto exit;
63 }
64#endif /* MBEDTLS_USE_PSA_CRYPTO */
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (argc != 2) {
67 mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020068
69#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020071#endif
72
73 goto exit;
74 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_printf("\n . Seeding the random number generator...");
77 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
80 &entropy, (const unsigned char *) pers,
81 strlen(pers))) != 0) {
82 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
83 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020084 goto exit;
85 }
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
88 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
91 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
92 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
93 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020094 goto exit;
95 }
96
97 /*
98 * Extract the RSA encrypted value from the text file
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
101 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
Hanno Beckerbd336c12017-10-08 16:44:10 +0100102 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103 goto exit;
104 }
105
106 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
108 i < (int) sizeof(buf)) {
Paul Bakker940f9ce2013-09-18 15:34:57 +0200109 buf[i++] = (unsigned char) c;
Hanno Beckerae513a52018-08-23 14:39:04 +0100110 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113
114 /*
115 * Decrypt the encrypted RSA data and print the result.
116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_printf("\n . Decrypting the encrypted data");
118 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result),
121 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
122 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 */