blob: afbbfa9d47c935d9c3a22b3555ad3deca2673bf5 [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSASSA-PSS/SHA-256 signature verification program
Paul Bakker2291f6c2011-03-25 14:07:53 +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 Bakker2291f6c2011-03-25 14:07:53 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker2291f6c2011-03-25 14:07:53 +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é-Gonnard06d5d612015-05-28 16:23:18 +020014#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010015 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020016 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
17 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010018int main(void)
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020019{
20 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010021 "MBEDTLS_RSA_C and/or MBEDTLS_MD_CAN_SHA256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010022 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
23 "MBEDTLS_CTR_DRBG_C not defined.\n");
24 mbedtls_exit(0);
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020025}
26#else
27
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/md.h"
29#include "mbedtls/pem.h"
30#include "mbedtls/pk.h"
Paul Bakker2291f6c2011-03-25 14:07:53 +000031
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
33#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000034
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Gilles Peskine449bd832023-01-11 14:50:10 +010036int main(int argc, char *argv[])
Paul Bakker2291f6c2011-03-25 14:07:53 +000037{
38 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020039 int ret = 1;
Andres Amaya Garciaa8332632018-04-29 20:33:22 +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_pk_context pk;
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];
Paul Bakker2291f6c2011-03-25 14:07:53 +000045 char filename[512];
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_pk_init(&pk);
Paul Bakker0c226102014-04-17 16:02:36 +020048
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020049#if defined(MBEDTLS_USE_PSA_CRYPTO)
50 psa_status_t status = psa_crypto_init();
51 if (status != PSA_SUCCESS) {
52 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
53 (int) status);
54 goto exit;
55 }
56#endif /* MBEDTLS_USE_PSA_CRYPTO */
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if (argc != 3) {
59 mbedtls_printf("usage: rsa_verify_pss <key_file> <filename>\n");
Paul Bakker2291f6c2011-03-25 14:07:53 +000060
Paul Bakkercce9d772011-11-18 14:26:47 +000061#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_printf("\n");
Paul Bakker2291f6c2011-03-25 14:07:53 +000063#endif
64
65 goto exit;
66 }
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
69 fflush(stdout);
Paul Bakker2291f6c2011-03-25 14:07:53 +000070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
72 mbedtls_printf(" failed\n ! Could not read key from '%s'\n", argv[1]);
73 mbedtls_printf(" ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret);
Paul Bakker30520d12013-09-17 11:39:31 +020074 goto exit;
75 }
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (!mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA)) {
78 mbedtls_printf(" failed\n ! Key is not an RSA key\n");
Paul Bakker2291f6c2011-03-25 14:07:53 +000079 goto exit;
80 }
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if ((ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk),
83 MBEDTLS_RSA_PKCS_V21,
84 MBEDTLS_MD_SHA256)) != 0) {
85 mbedtls_printf(" failed\n ! Invalid padding\n");
Ronald Cronea7631b2021-06-03 18:51:59 +020086 goto exit;
87 }
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010088
Paul Bakker2291f6c2011-03-25 14:07:53 +000089 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +020090 * Extract the RSA signature from the file
Paul Bakker2291f6c2011-03-25 14:07:53 +000091 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_snprintf(filename, 512, "%s.sig", argv[2]);
Paul Bakker2291f6c2011-03-25 14:07:53 +000093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if ((f = fopen(filename, "rb")) == NULL) {
95 mbedtls_printf("\n ! Could not open %s\n\n", filename);
Paul Bakker2291f6c2011-03-25 14:07:53 +000096 goto exit;
97 }
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 i = fread(buf, 1, MBEDTLS_MPI_MAX_SIZE, f);
Paul Bakker2291f6c2011-03-25 14:07:53 +0000100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 fclose(f);
Paul Bakker2291f6c2011-03-25 14:07:53 +0000102
Paul Bakker2291f6c2011-03-25 14:07:53 +0000103 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200104 * Compute the SHA-256 hash of the input file and
105 * verify the signature
Paul Bakker2291f6c2011-03-25 14:07:53 +0000106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
108 fflush(stdout);
Paul Bakker2291f6c2011-03-25 14:07:53 +0000109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if ((ret = mbedtls_md_file(
111 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
112 argv[2], hash)) != 0) {
113 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
Paul Bakker2291f6c2011-03-25 14:07:53 +0000114 goto exit;
115 }
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
118 buf, i)) != 0) {
119 mbedtls_printf(" failed\n ! mbedtls_pk_verify returned %d\n\n", ret);
Paul Bakker2291f6c2011-03-25 14:07:53 +0000120 goto exit;
121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_printf("\n . OK (the signature is valid)\n\n");
Paul Bakker2291f6c2011-03-25 14:07:53 +0000124
Andres Amaya Garciaa8332632018-04-29 20:33:22 +0100125 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker2291f6c2011-03-25 14:07:53 +0000126
127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_pk_free(&pk);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200130 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200131#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2291f6c2011-03-25 14:07:53 +0000132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_exit(exit_code);
Paul Bakker2291f6c2011-03-25 14:07:53 +0000134}
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100135#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */