Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based simple decryption program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 8 | #include "mbedtls/build_info.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 10 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12 | #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é-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 15 | #include "mbedtls/error.h" |
| 16 | #include "mbedtls/pk.h" |
| 17 | #include "mbedtls/entropy.h" |
| 18 | #include "mbedtls/ctr_drbg.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 19 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #endif |
| 23 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \ |
| 25 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 26 | !defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | int main(void) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 28 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 30 | "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or " |
| 31 | "MBEDTLS_CTR_DRBG_C not defined.\n"); |
| 32 | mbedtls_exit(0); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 33 | } |
| 34 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 35 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 36 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 38 | { |
| 39 | FILE *f; |
Gilles Peskine | a5fc939 | 2020-04-14 19:34:19 +0200 | [diff] [blame] | 40 | int ret = 1; |
| 41 | unsigned c; |
Andres Amaya Garcia | 0a7522c | 2018-04-29 22:23:22 +0100 | [diff] [blame] | 42 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 43 | size_t i, olen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | mbedtls_pk_context pk; |
| 45 | mbedtls_entropy_context entropy; |
| 46 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 47 | unsigned char result[1024]; |
| 48 | unsigned char buf[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | const char *pers = "mbedtls_pk_decrypt"; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 50 | ((void) argv); |
| 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | mbedtls_pk_init(&pk); |
| 53 | mbedtls_entropy_init(&entropy); |
| 54 | mbedtls_ctr_drbg_init(&ctr_drbg); |
Hanno Becker | bd336c1 | 2017-10-08 16:44:10 +0100 | [diff] [blame] | 55 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | memset(result, 0, sizeof(result)); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 57 | |
Przemek Stekiel | 2c1ef09 | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 59 | psa_status_t status = psa_crypto_init(); |
| 60 | if (status != PSA_SUCCESS) { |
| 61 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 62 | (int) status); |
| 63 | goto exit; |
| 64 | } |
| 65 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 66 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | if (argc != 2) { |
| 68 | mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 69 | |
| 70 | #if defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | mbedtls_printf("\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | goto exit; |
| 75 | } |
| 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 78 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, |
| 81 | &entropy, (const unsigned char *) pers, |
| 82 | strlen(pers))) != 0) { |
| 83 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", |
| 84 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 85 | goto exit; |
| 86 | } |
| 87 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | mbedtls_printf("\n . Reading private key from '%s'", argv[1]); |
| 89 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 90 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "", |
| 92 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 93 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", |
| 94 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 95 | goto exit; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Extract the RSA encrypted value from the text file |
| 100 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | if ((f = fopen("result-enc.txt", "rb")) == NULL) { |
| 102 | mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt"); |
Hanno Becker | bd336c1 | 2017-10-08 16:44:10 +0100 | [diff] [blame] | 103 | ret = 1; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 104 | goto exit; |
| 105 | } |
| 106 | |
| 107 | i = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | while (fscanf(f, "%02X", (unsigned int *) &c) > 0 && |
| 109 | i < (int) sizeof(buf)) { |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 110 | buf[i++] = (unsigned char) c; |
Hanno Becker | ae513a5 | 2018-08-23 14:39:04 +0100 | [diff] [blame] | 111 | } |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Decrypt the encrypted RSA data and print the result. |
| 117 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | mbedtls_printf("\n . Decrypting the encrypted data"); |
| 119 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result), |
| 122 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 123 | mbedtls_printf(" failed\n ! mbedtls_pk_decrypt returned -0x%04x\n", |
| 124 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 125 | goto exit; |
| 126 | } |
| 127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | mbedtls_printf("\n . OK\n\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | mbedtls_printf("The decrypted result is: '%s'\n\n", result); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 131 | |
Andres Amaya Garcia | 0a7522c | 2018-04-29 22:23:22 +0100 | [diff] [blame] | 132 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 133 | |
| 134 | exit: |
Hanno Becker | bd336c1 | 2017-10-08 16:44:10 +0100 | [diff] [blame] | 135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | mbedtls_pk_free(&pk); |
| 137 | mbedtls_entropy_free(&entropy); |
| 138 | mbedtls_ctr_drbg_free(&ctr_drbg); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 139 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | 2c1ef09 | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 140 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 141 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { |
| 145 | mbedtls_strerror(ret, (char *) buf, sizeof(buf)); |
| 146 | mbedtls_printf(" ! Last error was: %s\n", buf); |
Manuel Pégourié-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 147 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 148 | #endif |
| 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 151 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && |
| 153 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |