blob: 8b60440aba1df3fbc85eb885e460f7959bd65957 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature verification program
3 *
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 Bakker940f9ce2013-09-18 15:34:57 +02006 */
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 Bakker940f9ce2013-09-18 15:34:57 +020013
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é-Gonnard06d5d612015-05-28 16:23:18 +020016#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
18 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010019int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020020{
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020021 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010022 "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
23 "MBEDTLS_FS_IO not defined.\n");
24 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020025}
26#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020027
28#include "mbedtls/error.h"
29#include "mbedtls/md.h"
30#include "mbedtls/pk.h"
31
32#include <stdio.h>
33#include <string.h>
34
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020037{
38 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020039 int ret = 1;
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010040 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020041 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];
Gilles Peskine96a7cd12019-11-08 19:22:35 +010044 unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020045 char filename[512];
46
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 mbedtls_pk_init(&pk);
Paul Bakker0c226102014-04-17 16:02:36 +020048
Przemek Stekiel9c0fc2d2023-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 Peskine1b6c09a2023-01-11 14:52:35 +010058 if (argc != 3) {
59 mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020060
61#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020063#endif
64
65 goto exit;
66 }
67
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
69 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
72 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
73 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020074 goto exit;
75 }
76
77 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +020078 * Extract the signature from the file
Paul Bakker940f9ce2013-09-18 15:34:57 +020079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +020081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 if ((f = fopen(filename, "rb")) == NULL) {
83 mbedtls_printf("\n ! Could not open %s\n\n", filename);
Paul Bakker940f9ce2013-09-18 15:34:57 +020084 goto exit;
85 }
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 i = fread(buf, 1, sizeof(buf), f);
Paul Bakker940f9ce2013-09-18 15:34:57 +020088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +020090
91 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +020092 * Compute the SHA-256 hash of the input file and
93 * verify the signature
Paul Bakker940f9ce2013-09-18 15:34:57 +020094 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 mbedtls_printf("\n . Verifying the SHA-256 signature");
96 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 if ((ret = mbedtls_md_file(
99 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
100 argv[2], hash)) != 0) {
101 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200102 goto exit;
103 }
104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
106 buf, i)) != 0) {
107 mbedtls_printf(" failed\n ! mbedtls_pk_verify returned -0x%04x\n", (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200108 goto exit;
109 }
110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 mbedtls_printf("\n . OK (the signature is valid)\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +0100113 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200114
115exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_pk_free(&pk);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel9c0fc2d2023-04-19 09:38:12 +0200118 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200119#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
123 mbedtls_strerror(ret, (char *) buf, sizeof(buf));
124 mbedtls_printf(" ! Last error was: %s\n", buf);
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200125 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200126#endif
127
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 mbedtls_printf(" + Press Enter to exit this program.\n");
130 fflush(stdout); getchar();
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131#endif
132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
136 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */