blob: 4a9af77faa4e90445c4e750f397a81796485070f [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 verification 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;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020035 int ret = 1;
36 unsigned c;
Andres Amaya Garcia0a860f62018-04-29 20:27:09 +010037 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000038 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_rsa_context rsa;
Minos Galanakis6e92df12024-01-12 15:13:47 +000040 mbedtls_mpi N, E;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020041 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020043 char filename[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 mbedtls_rsa_init(&rsa);
Minos Galanakis6e92df12024-01-12 15:13:47 +000046 mbedtls_mpi_init(&N);
47 mbedtls_mpi_init(&E);
Janos Follathf713b0a2016-03-17 15:21:39 +000048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (argc != 2) {
50 mbedtls_printf("usage: rsa_verify <filename>\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakkercce9d772011-11-18 14:26:47 +000052#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000054#endif
55
56 goto exit;
57 }
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
60 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
63 mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
64 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000065 goto exit;
66 }
67
Minos Galanakis6e92df12024-01-12 15:13:47 +000068 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
69 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
70 (ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E) != 0)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
72 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000073 goto exit;
74 }
Gilles Peskine449bd832023-01-11 14:50:10 +010075 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77 /*
78 * Extract the RSA signature from the text file
79 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if ((f = fopen(filename, "rb")) == NULL) {
83 mbedtls_printf("\n ! Could not open %s\n\n", filename);
Paul Bakker5121ce52009-01-03 21:22:43 +000084 goto exit;
85 }
86
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020087 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
89 i < (int) sizeof(buf)) {
Paul Bakker5121ce52009-01-03 21:22:43 +000090 buf[i++] = (unsigned char) c;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 }
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Minos Galanakis6e92df12024-01-12 15:13:47 +000095 if (i != mbedtls_rsa_get_len(&rsa)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010096 mbedtls_printf("\n ! Invalid RSA signature format\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000097 goto exit;
98 }
99
100 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200101 * Compute the SHA-256 hash of the input file and
102 * verify the signature
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
105 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if ((ret = mbedtls_md_file(
108 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
109 argv[1], hash)) != 0) {
110 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 goto exit;
112 }
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256,
115 32, hash, buf)) != 0) {
116 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n",
117 (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_printf("\n . OK (the signature is valid)\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Andres Amaya Garcia0a860f62018-04-29 20:27:09 +0100123 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125exit:
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_rsa_free(&rsa);
Minos Galanakis6e92df12024-01-12 15:13:47 +0000128 mbedtls_mpi_free(&N);
129 mbedtls_mpi_free(&E);
Janos Follathf713b0a2016-03-17 15:21:39 +0000130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132}
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100133#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 MBEDTLS_FS_IO */