blob: d6a68bff08f2a89f62e7e2aa86264f4e8011be13 [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 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;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020038 int ret = 1;
39 unsigned c;
Andres Amaya Garcia0a860f62018-04-29 20:27:09 +010040 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000041 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020043 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020045 char filename[512];
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);
Janos Follathf713b0a2016-03-17 15:21:39 +000048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010053 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000054#endif
55
56 goto exit;
57 }
58
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
60 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 if ((ret = mbedtls_mpi_read_file(&rsa.N, 16, f)) != 0 ||
69 (ret = mbedtls_mpi_read_file(&rsa.E, 16, f)) != 0) {
70 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
71 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000072 goto exit;
73 }
74
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 /*
80 * Extract the RSA signature from the text file
81 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 if ((f = fopen(filename, "rb")) == NULL) {
85 mbedtls_printf("\n ! Could not open %s\n\n", filename);
Paul Bakker5121ce52009-01-03 21:22:43 +000086 goto exit;
87 }
88
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020089 i = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
91 i < (int) sizeof(buf)) {
Paul Bakker5121ce52009-01-03 21:22:43 +000092 buf[i++] = (unsigned char) c;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 }
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 if (i != rsa.len) {
98 mbedtls_printf("\n ! Invalid RSA signature format\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000099 goto exit;
100 }
101
102 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200103 * Compute the SHA-256 hash of the input file and
104 * verify the signature
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
107 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 if ((ret = mbedtls_md_file(
110 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
111 argv[1], hash)) != 0) {
112 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 goto exit;
114 }
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
117 MBEDTLS_MD_SHA256, 20, hash, buf)) != 0) {
118 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n",
119 (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 goto exit;
121 }
122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 mbedtls_printf("\n . OK (the signature is valid)\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Andres Amaya Garcia0a860f62018-04-29 20:27:09 +0100125 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127exit:
128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 mbedtls_rsa_free(&rsa);
Janos Follathf713b0a2016-03-17 15:21:39 +0000130
Paul Bakkercce9d772011-11-18 14:26:47 +0000131#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 mbedtls_printf(" + Press Enter to exit this program.\n");
133 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000134#endif
135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
139 MBEDTLS_FS_IO */