blob: af6156cdba584b18592c9dad61d39156874711a4 [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
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040013/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040014#include "mbedtls/md.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) || \
Elena Uziunaite0916cd72024-05-23 17:01:07 +010017 !defined(PSA_WANT_ALG_SHA_256) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020018 !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010022 "MBEDTLS_MD_C and/or "
Elena Uziunaite0916cd72024-05-23 17:01:07 +010023 "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010024 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"
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020029
30#include <stdio.h>
31#include <string.h>
32
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010033
Gilles Peskine449bd832023-01-11 14:50:10 +010034int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +000035{
36 FILE *f;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020037 int ret = 1;
38 unsigned c;
Andres Amaya Garcia0a860f62018-04-29 20:27:09 +010039 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;
Minos Galanakis6e92df12024-01-12 15:13:47 +000042 mbedtls_mpi N, E;
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 Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_rsa_init(&rsa);
Minos Galanakis6e92df12024-01-12 15:13:47 +000048 mbedtls_mpi_init(&N);
49 mbedtls_mpi_init(&E);
Janos Follathf713b0a2016-03-17 15:21:39 +000050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (argc != 2) {
52 mbedtls_printf("usage: rsa_verify <filename>\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkercce9d772011-11-18 14:26:47 +000054#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000056#endif
57
58 goto exit;
59 }
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
62 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
65 mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
66 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000067 goto exit;
68 }
69
Minos Galanakis6e92df12024-01-12 15:13:47 +000070 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
71 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
72 (ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E) != 0)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
74 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000075 goto exit;
76 }
Gilles Peskine449bd832023-01-11 14:50:10 +010077 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 /*
80 * Extract the RSA signature from the text file
81 */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010093 }
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Minos Galanakis6e92df12024-01-12 15:13:47 +000097 if (i != mbedtls_rsa_get_len(&rsa)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010098 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 Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
107 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100116 if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256,
117 32, 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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_rsa_free(&rsa);
Minos Galanakis6e92df12024-01-12 15:13:47 +0000130 mbedtls_mpi_free(&N);
131 mbedtls_mpi_free(&E);
Janos Follathf713b0a2016-03-17 15:21:39 +0000132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100135#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 MBEDTLS_FS_IO */