blob: a3a7c1b4db7b1ed712ad2081430eba7f7baf6f1c [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 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_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
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_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
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_ENTROPY_C and/or MBEDTLS_FS_IO 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;
Andres Amaya Garcia52898172018-04-29 22:19:26 +010039 int ret = 1;
40 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020041 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_pk_context pk;
43 mbedtls_entropy_context entropy;
44 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020045 unsigned char input[1024];
46 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 const char *pers = "mbedtls_pk_encrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_ctr_drbg_init(&ctr_drbg);
50 mbedtls_entropy_init(&entropy);
51 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020052
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54 psa_status_t status = psa_crypto_init();
55 if (status != PSA_SUCCESS) {
56 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
57 (int) status);
58 goto exit;
59 }
60#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (argc != 3) {
63 mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020064
65#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020067#endif
68
69 goto exit;
70 }
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_printf("\n . Seeding the random number generator...");
73 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
76 &entropy, (const unsigned char *) pers,
77 strlen(pers))) != 0) {
78 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
79 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020080 goto exit;
81 }
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
84 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
87 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
88 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020089 goto exit;
90 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (strlen(argv[2]) > 100) {
93 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020094 goto exit;
95 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 memcpy(input, argv[2], strlen(argv[2]));
Paul Bakker940f9ce2013-09-18 15:34:57 +020098
99 /*
100 * Calculate the RSA encryption of the hash.
101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_printf("\n . Generating the encrypted value");
103 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if ((ret = mbedtls_pk_encrypt(&pk, input, strlen(argv[2]),
106 buf, &olen, sizeof(buf),
107 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
108 mbedtls_printf(" failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
109 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200110 goto exit;
111 }
112
113 /*
114 * Write the signature into result-enc.txt
115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
117 mbedtls_printf(" failed\n ! Could not create %s\n\n",
118 "result-enc.txt");
Hanno Becker55c11ba2018-08-23 14:36:33 +0100119 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 goto exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 for (i = 0; i < olen; i++) {
124 mbedtls_fprintf(f, "%02X%s", buf[i],
125 (i + 1) % 16 == 0 ? "\r\n" : " ");
Hanno Beckerae513a52018-08-23 14:39:04 +0100126 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131
Andres Amaya Garcia52898172018-04-29 22:19:26 +0100132 exit_code = MBEDTLS_EXIT_SUCCESS;
133
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134exit:
Hanno Becker55c11ba2018-08-23 14:36:33 +0100135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_pk_free(&pk);
137 mbedtls_entropy_free(&entropy);
138 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200140 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100145 mbedtls_printf("Error code: %d", ret);
146 /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
147 mbedtls_printf(" ! Last error was: %s\n", buf); */
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200148 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200149#endif
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
154 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */