blob: f99f175468f1c7015ad380c365b38d265d241057 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * RSA simple data encryption program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker940f9ce2013-09-18 15:34:57 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#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é-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/error.h"
20#include "mbedtls/pk.h"
21#include "mbedtls/entropy.h"
22#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020023
Rich Evans18b78c72015-02-11 14:06:19 +000024#include <stdio.h>
25#include <string.h>
26#endif
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#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 Peskine1b6c09a2023-01-11 14:52:35 +010031int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
35 "MBEDTLS_CTR_DRBG_C not defined.\n");
36 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020037}
38#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000039
Simon Butcher63cb97e2018-12-06 17:43:31 +000040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020042{
43 FILE *f;
Andres Amaya Garcia52898172018-04-29 22:19:26 +010044 int ret = 1;
45 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020046 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_pk_context pk;
48 mbedtls_entropy_context entropy;
49 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020050 unsigned char input[1024];
51 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 const char *pers = "mbedtls_pk_encrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 mbedtls_ctr_drbg_init(&ctr_drbg);
55 mbedtls_entropy_init(&entropy);
56 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020057
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +020058#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 Peskine1b6c09a2023-01-11 14:52:35 +010067 if (argc != 3) {
68 mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020069
70#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020072#endif
73
74 goto exit;
75 }
76
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mbedtls_printf("\n . Seeding the random number generator...");
78 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 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 Bakker940f9ce2013-09-18 15:34:57 +020085 goto exit;
86 }
87
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
89 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 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 Bakker940f9ce2013-09-18 15:34:57 +020094 goto exit;
95 }
96
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 if (strlen(argv[2]) > 100) {
98 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020099 goto exit;
100 }
101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 memcpy(input, argv[2], strlen(argv[2]));
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103
104 /*
105 * Calculate the RSA encryption of the hash.
106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 mbedtls_printf("\n . Generating the encrypted value");
108 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 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 Bakker940f9ce2013-09-18 15:34:57 +0200115 goto exit;
116 }
117
118 /*
119 * Write the signature into result-enc.txt
120 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
122 mbedtls_printf(" failed\n ! Could not create %s\n\n",
123 "result-enc.txt");
Hanno Becker55c11ba2018-08-23 14:36:33 +0100124 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200125 goto exit;
126 }
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 for (i = 0; i < olen; i++) {
129 mbedtls_fprintf(f, "%02X%s", buf[i],
130 (i + 1) % 16 == 0 ? "\r\n" : " ");
Hanno Beckerae513a52018-08-23 14:39:04 +0100131 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200136
Andres Amaya Garcia52898172018-04-29 22:19:26 +0100137 exit_code = MBEDTLS_EXIT_SUCCESS;
138
Paul Bakker940f9ce2013-09-18 15:34:57 +0200139exit:
Hanno Becker55c11ba2018-08-23 14:36:33 +0100140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_pk_free(&pk);
142 mbedtls_entropy_free(&entropy);
143 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200145 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200146#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 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é-Gonnardcf9ab632015-08-27 22:03:33 +0200152 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200153#endif
154
Paul Bakker940f9ce2013-09-18 15:34:57 +0200155#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_printf(" + Press Enter to exit this program.\n");
157 fflush(stdout); getchar();
Paul Bakker940f9ce2013-09-18 15:34:57 +0200158#endif
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
163 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */