blob: 1ede030590278d6760cf22926064650d340f836b [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 Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020017 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
18 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010019int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010022 "MBEDTLS_MD_C and/or "
23 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
24 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000025}
26#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020027
28#include "mbedtls/rsa.h"
29#include "mbedtls/md.h"
30
31#include <stdio.h>
32#include <string.h>
33
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +000036{
37 FILE *f;
Andres Amaya Garcia1a660562018-04-29 20:16:46 +010038 int ret = 1;
39 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000040 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020042 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020044 char filename[512];
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010045 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
50 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
51 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 if (argc != 2) {
54 mbedtls_printf("usage: rsa_sign <filename>\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkercce9d772011-11-18 14:26:47 +000056#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000058#endif
59
60 goto exit;
61 }
62
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
64 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
67 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
68 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000069 goto exit;
70 }
71
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
73 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
74 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
75 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
76 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
77 (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
78 (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
79 (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
80 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
81 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000082 goto exit;
83 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
87 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
88 ret);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010089 goto exit;
90 }
91
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
93 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
94 ret);
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010095 goto exit;
96 }
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 mbedtls_printf("\n . Checking the private key");
99 fflush(stdout);
100 if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) {
101 mbedtls_printf(" failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n",
102 (unsigned int) -ret);
Paul Bakker5ef9db22012-09-27 13:19:22 +0000103 goto exit;
104 }
105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000107 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 * then calculate the RSA signature of the hash.
109 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 mbedtls_printf("\n . Generating the RSA/SHA-256 signature");
111 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if ((ret = mbedtls_md_file(
114 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
115 argv[1], hash)) != 0) {
116 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 goto exit;
118 }
119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
121 20, hash, buf)) != 0) {
122 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n",
123 (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 goto exit;
125 }
126
127 /*
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200128 * Write the signature into <filename>.sig
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 if ((f = fopen(filename, "wb+")) == NULL) {
133 mbedtls_printf(" failed\n ! Could not create %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 goto exit;
135 }
136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 for (i = 0; i < rsa.len; i++) {
138 mbedtls_fprintf(f, "%02X%s", buf[i],
139 (i + 1) % 16 == 0 ? "\r\n" : " ");
140 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 mbedtls_printf("\n . Done (created \"%s\")\n\n", filename);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Andres Amaya Garcia1a660562018-04-29 20:16:46 +0100146 exit_code = MBEDTLS_EXIT_SUCCESS;
147
Paul Bakker5121ce52009-01-03 21:22:43 +0000148exit:
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_rsa_free(&rsa);
151 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
152 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
153 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Janos Follathf713b0a2016-03-17 15:21:39 +0000154
Paul Bakkercce9d772011-11-18 14:26:47 +0000155#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_printf(" + Press Enter to exit this program.\n");
157 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000158#endif
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
163 MBEDTLS_FS_IO */