blob: e14953bc3fa5266ee040022b6e5f60470e3c7460 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSA/SHA-256 signature creation program
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
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 Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040011/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040012#include "mbedtls/md.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_RSA_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010015 !defined(MBEDTLS_MD_CAN_SHA256) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020016 !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010017int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010020 "MBEDTLS_MD_C and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010021 "MBEDTLS_MD_CAN_SHA256 and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000023}
24#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020025
26#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020027
28#include <stdio.h>
29#include <string.h>
30
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010031
Gilles Peskine449bd832023-01-11 14:50:10 +010032int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +000033{
34 FILE *f;
Andres Amaya Garcia1a660562018-04-29 20:16:46 +010035 int ret = 1;
36 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000037 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020039 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020041 char filename[512];
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010042 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_rsa_init(&rsa);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
47 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
48 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (argc != 2) {
51 mbedtls_printf("usage: rsa_sign <filename>\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakkercce9d772011-11-18 14:26:47 +000053#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000055#endif
56
57 goto exit;
58 }
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
61 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
64 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
65 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000066 goto exit;
67 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
70 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
71 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
72 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
73 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
74 (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
75 (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
76 (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
77 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
78 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000079 goto exit;
80 }
Gilles Peskine449bd832023-01-11 14:50:10 +010081 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
84 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
85 ret);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010086 goto exit;
87 }
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
90 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
91 ret);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010092 goto exit;
93 }
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_printf("\n . Checking the private key");
96 fflush(stdout);
97 if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) {
98 mbedtls_printf(" failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n",
99 (unsigned int) -ret);
Paul Bakker5ef9db22012-09-27 13:19:22 +0000100 goto exit;
101 }
102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000104 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 * then calculate the RSA signature of the hash.
106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_printf("\n . Generating the RSA/SHA-256 signature");
108 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if ((ret = mbedtls_md_file(
111 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
112 argv[1], hash)) != 0) {
113 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 goto exit;
115 }
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
118 32, hash, buf)) != 0) {
119 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n",
120 (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 goto exit;
122 }
123
124 /*
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200125 * Write the signature into <filename>.sig
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((f = fopen(filename, "wb+")) == NULL) {
130 mbedtls_printf(" failed\n ! Could not create %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 goto exit;
132 }
133
Minos Galanakis992f0b82024-01-12 15:07:24 +0000134 for (i = 0; i < mbedtls_rsa_get_len(&rsa); i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_fprintf(f, "%02X%s", buf[i],
136 (i + 1) % 16 == 0 ? "\r\n" : " ");
137 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Andres Amaya Garcia1a660562018-04-29 20:16:46 +0100143 exit_code = MBEDTLS_EXIT_SUCCESS;
144
Paul Bakker5121ce52009-01-03 21:22:43 +0000145exit:
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_rsa_free(&rsa);
148 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
149 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
150 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Janos Follathf713b0a2016-03-17 15:21:39 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153}
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100154#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 MBEDTLS_FS_IO */