blob: 28a849b38f95623691a4144e3861e478fe301466 [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]),
108 buf, &olen, sizeof(buf),
109 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
110 mbedtls_printf(" failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
111 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112 goto exit;
113 }
114
115 /*
116 * Write the signature into result-enc.txt
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
119 mbedtls_printf(" failed\n ! Could not create %s\n\n",
120 "result-enc.txt");
Hanno Becker55c11ba2018-08-23 14:36:33 +0100121 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122 goto exit;
123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 for (i = 0; i < olen; i++) {
126 mbedtls_fprintf(f, "%02X%s", buf[i],
127 (i + 1) % 16 == 0 ? "\r\n" : " ");
Hanno Beckerae513a52018-08-23 14:39:04 +0100128 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200133
Andres Amaya Garcia52898172018-04-29 22:19:26 +0100134 exit_code = MBEDTLS_EXIT_SUCCESS;
135
Paul Bakker940f9ce2013-09-18 15:34:57 +0200136exit:
Hanno Becker55c11ba2018-08-23 14:36:33 +0100137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_pk_free(&pk);
139 mbedtls_entropy_free(&entropy);
140 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200142 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200143#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Harry Ramsey9c664052024-10-16 14:08:19 +0100147 mbedtls_printf("Error code: %d", ret);
148 /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
149 mbedtls_printf(" ! Last error was: %s\n", buf); */
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200150 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200151#endif
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
156 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */