blob: e7d72fd52bb636fcda2e12d629075ecfe480319e [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;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020040 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020042 char filename[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_rsa_init(&rsa);
Janos Follathf713b0a2016-03-17 15:21:39 +000045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 if (argc != 2) {
47 mbedtls_printf("usage: rsa_verify <filename>\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Paul Bakkercce9d772011-11-18 14:26:47 +000049#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000051#endif
52
53 goto exit;
54 }
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
57 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
60 mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
61 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000062 goto exit;
63 }
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if ((ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(N), 16, f)) != 0 ||
66 (ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(E), 16, f)) != 0) {
67 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
68 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000069 goto exit;
70 }
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 rsa.MBEDTLS_PRIVATE(len) = (mbedtls_mpi_bitlen(&rsa.MBEDTLS_PRIVATE(N)) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76 /*
77 * Extract the RSA signature from the text file
78 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if ((f = fopen(filename, "rb")) == NULL) {
82 mbedtls_printf("\n ! Could not open %s\n\n", filename);
Paul Bakker5121ce52009-01-03 21:22:43 +000083 goto exit;
84 }
85
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020086 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
88 i < (int) sizeof(buf)) {
Paul Bakker5121ce52009-01-03 21:22:43 +000089 buf[i++] = (unsigned char) c;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 }
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (i != rsa.MBEDTLS_PRIVATE(len)) {
95 mbedtls_printf("\n ! Invalid RSA signature format\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000096 goto exit;
97 }
98
99 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200100 * Compute the SHA-256 hash of the input file and
101 * verify the signature
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
104 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if ((ret = mbedtls_md_file(
107 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
108 argv[1], hash)) != 0) {
109 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 goto exit;
111 }
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256,
114 32, hash, buf)) != 0) {
115 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n",
116 (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 goto exit;
118 }
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_printf("\n . OK (the signature is valid)\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Andres Amaya Garcia0a860f62018-04-29 20:27:09 +0100122 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124exit:
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_rsa_free(&rsa);
Janos Follathf713b0a2016-03-17 15:21:39 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000129}
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100130#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 MBEDTLS_FS_IO */