blob: a916bc6e25e8376c1fc4eaf35e1d0a74f568026d [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/error.h"
16#include "mbedtls/pk.h"
17#include "mbedtls/entropy.h"
18#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020019
Rich Evans18b78c72015-02-11 14:06:19 +000020#include <stdio.h>
21#include <string.h>
22#endif
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
25 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
26 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010030 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
31 "MBEDTLS_CTR_DRBG_C not defined.\n");
32 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020033}
34#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Simon Butcher63cb97e2018-12-06 17:43:31 +000036
Gilles Peskine449bd832023-01-11 14:50:10 +010037int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020038{
39 FILE *f;
Andres Amaya Garcia52898172018-04-29 22:19:26 +010040 int ret = 1;
41 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020042 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_pk_context pk;
44 mbedtls_entropy_context entropy;
45 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020046 unsigned char input[1024];
47 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 const char *pers = "mbedtls_pk_encrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_ctr_drbg_init(&ctr_drbg);
51 mbedtls_entropy_init(&entropy);
52 mbedtls_pk_init(&pk);
Paul Bakker940f9ce2013-09-18 15:34:57 +020053
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020054#if defined(MBEDTLS_USE_PSA_CRYPTO)
55 psa_status_t status = psa_crypto_init();
56 if (status != PSA_SUCCESS) {
57 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
58 (int) status);
59 goto exit;
60 }
61#endif /* MBEDTLS_USE_PSA_CRYPTO */
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (argc != 3) {
64 mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020065
66#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020068#endif
69
70 goto exit;
71 }
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_printf("\n . Seeding the random number generator...");
74 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
77 &entropy, (const unsigned char *) pers,
78 strlen(pers))) != 0) {
79 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
80 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020081 goto exit;
82 }
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
85 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
88 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
89 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020090 goto exit;
91 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (strlen(argv[2]) > 100) {
94 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098 memcpy(input, argv[2], strlen(argv[2]));
Paul Bakker940f9ce2013-09-18 15:34:57 +020099
100 /*
101 * Calculate the RSA encryption of the hash.
102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_printf("\n . Generating the encrypted value");
104 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if ((ret = mbedtls_pk_encrypt(&pk, input, strlen(argv[2]),
107 buf, &olen, sizeof(buf),
108 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
109 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) {
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 */