blob: 1ab2a3d60ec80108472bd169b47a3c44b09e331a [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
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
15 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
16 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/pk.h"
18#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020020
Rich Evans18b78c72015-02-11 14:06:19 +000021#include <stdio.h>
22#include <string.h>
23#endif
24
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
27 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010028int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010031 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
32 "MBEDTLS_CTR_DRBG_C not defined.\n");
33 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020034}
35#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000036
Simon Butcher63cb97e2018-12-06 17:43:31 +000037
Gilles Peskine449bd832023-01-11 14:50:10 +010038int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020039{
40 FILE *f;
Andres Amaya Garcia52898172018-04-29 22:19:26 +010041 int ret = 1;
42 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020043 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_pk_context pk;
45 mbedtls_entropy_context entropy;
46 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020047 unsigned char input[1024];
48 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 const char *pers = "mbedtls_pk_encrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 mbedtls_ctr_drbg_init(&ctr_drbg);
52 mbedtls_entropy_init(&entropy);
53 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020054
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020055#if defined(MBEDTLS_USE_PSA_CRYPTO)
56 psa_status_t status = psa_crypto_init();
57 if (status != PSA_SUCCESS) {
58 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
59 (int) status);
60 goto exit;
61 }
62#endif /* MBEDTLS_USE_PSA_CRYPTO */
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (argc != 3) {
65 mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020066
67#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020069#endif
70
71 goto exit;
72 }
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_printf("\n . Seeding the random number generator...");
75 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
78 &entropy, (const unsigned char *) pers,
79 strlen(pers))) != 0) {
80 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
81 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020082 goto exit;
83 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
86 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
89 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
90 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020091 goto exit;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (strlen(argv[2]) > 100) {
95 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020096 goto exit;
97 }
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 memcpy(input, argv[2], strlen(argv[2]));
Paul Bakker940f9ce2013-09-18 15:34:57 +0200100
101 /*
102 * Calculate the RSA encryption of the hash.
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_printf("\n . Generating the encrypted value");
105 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if ((ret = mbedtls_pk_encrypt(&pk, input, strlen(argv[2]),
Ben Taylor440cb2a2025-03-05 09:40:08 +0000108 buf, &olen, sizeof(buf))) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_printf(" failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
110 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200111 goto exit;
112 }
113
114 /*
115 * Write the signature into result-enc.txt
116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
118 mbedtls_printf(" failed\n ! Could not create %s\n\n",
119 "result-enc.txt");
Hanno Becker55c11ba2018-08-23 14:36:33 +0100120 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121 goto exit;
122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 for (i = 0; i < olen; i++) {
125 mbedtls_fprintf(f, "%02X%s", buf[i],
126 (i + 1) % 16 == 0 ? "\r\n" : " ");
Hanno Beckerae513a52018-08-23 14:39:04 +0100127 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200132
Andres Amaya Garcia52898172018-04-29 22:19:26 +0100133 exit_code = MBEDTLS_EXIT_SUCCESS;
134
Paul Bakker940f9ce2013-09-18 15:34:57 +0200135exit:
Hanno Becker55c11ba2018-08-23 14:36:33 +0100136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_pk_free(&pk);
138 mbedtls_entropy_free(&entropy);
139 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200141 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200142#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100146 mbedtls_printf("Error code: %d", ret);
147 /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
148 mbedtls_printf(" ! Last error was: %s\n", buf); */
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200149 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200150#endif
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
155 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */