Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * RSA simple data encryption program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 9 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 10 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 12 | #endif |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 14 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \ |
| 17 | defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \ |
| 18 | defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 19 | #include "mbedtls/error.h" |
| 20 | #include "mbedtls/pk.h" |
| 21 | #include "mbedtls/entropy.h" |
| 22 | #include "mbedtls/ctr_drbg.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 23 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #endif |
| 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \ |
| 29 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \ |
| 30 | !defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | int main(void) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 32 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or " |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 34 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or " |
| 35 | "MBEDTLS_CTR_DRBG_C not defined.\n"); |
| 36 | mbedtls_exit(0); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 37 | } |
| 38 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 39 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 40 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 42 | { |
| 43 | FILE *f; |
Andres Amaya Garcia | 5289817 | 2018-04-29 22:19:26 +0100 | [diff] [blame] | 44 | int ret = 1; |
| 45 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 46 | size_t i, olen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | mbedtls_pk_context pk; |
| 48 | mbedtls_entropy_context entropy; |
| 49 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 50 | unsigned char input[1024]; |
| 51 | unsigned char buf[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | const char *pers = "mbedtls_pk_encrypt"; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 53 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 55 | mbedtls_entropy_init(&entropy); |
| 56 | mbedtls_pk_init(&pk); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 57 | |
Przemek Stekiel | 9c0fc2d | 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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | if (argc != 3) { |
| 68 | mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 69 | |
| 70 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +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 | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 88 | mbedtls_printf("\n . Reading public key from '%s'", argv[1]); |
| 89 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 90 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) { |
| 92 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", |
| 93 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 94 | goto exit; |
| 95 | } |
| 96 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | if (strlen(argv[2]) > 100) { |
| 98 | mbedtls_printf(" Input data larger than 100 characters.\n\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 99 | goto exit; |
| 100 | } |
| 101 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | memcpy(input, argv[2], strlen(argv[2])); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Calculate the RSA encryption of the hash. |
| 106 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | mbedtls_printf("\n . Generating the encrypted value"); |
| 108 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 109 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | if ((ret = mbedtls_pk_encrypt(&pk, input, strlen(argv[2]), |
| 111 | buf, &olen, sizeof(buf), |
| 112 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 113 | mbedtls_printf(" failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", |
| 114 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Write the signature into result-enc.txt |
| 120 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | if ((f = fopen("result-enc.txt", "wb+")) == NULL) { |
| 122 | mbedtls_printf(" failed\n ! Could not create %s\n\n", |
| 123 | "result-enc.txt"); |
Hanno Becker | 55c11ba | 2018-08-23 14:36:33 +0100 | [diff] [blame] | 124 | ret = 1; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 125 | goto exit; |
| 126 | } |
| 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | for (i = 0; i < olen; i++) { |
| 129 | mbedtls_fprintf(f, "%02X%s", buf[i], |
| 130 | (i + 1) % 16 == 0 ? "\r\n" : " "); |
Hanno Becker | ae513a5 | 2018-08-23 14:39:04 +0100 | [diff] [blame] | 131 | } |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 136 | |
Andres Amaya Garcia | 5289817 | 2018-04-29 22:19:26 +0100 | [diff] [blame] | 137 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 138 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 139 | exit: |
Hanno Becker | 55c11ba | 2018-08-23 14:36:33 +0100 | [diff] [blame] | 140 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | mbedtls_pk_free(&pk); |
| 142 | mbedtls_entropy_free(&entropy); |
| 143 | mbedtls_ctr_drbg_free(&ctr_drbg); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 144 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | 9c0fc2d | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 145 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 146 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { |
| 150 | mbedtls_strerror(ret, (char *) buf, sizeof(buf)); |
| 151 | mbedtls_printf(" ! Last error was: %s\n", buf); |
Manuel Pégourié-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 152 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 153 | #endif |
| 154 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 155 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 157 | fflush(stdout); getchar(); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 158 | #endif |
| 159 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 161 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C && |
| 163 | MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ |