Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based signature verification program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 9 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 10 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 12 | #endif |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 14 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 16 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 17 | !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \ |
| 18 | !defined(MBEDTLS_FS_IO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 19 | int main(void) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 20 | { |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 21 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or " |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 22 | "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or " |
| 23 | "MBEDTLS_FS_IO not defined.\n"); |
| 24 | mbedtls_exit(0); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 25 | } |
| 26 | #else |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 27 | |
| 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 Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 35 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 37 | { |
| 38 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 39 | int ret = 1; |
Andres Amaya Garcia | 9f3379d | 2018-04-29 22:30:05 +0100 | [diff] [blame] | 40 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 41 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_pk_context pk; |
Manuel Pégourié-Gonnard | 102a620 | 2015-08-27 21:51:44 +0200 | [diff] [blame] | 43 | unsigned char hash[32]; |
Gilles Peskine | 96a7cd1 | 2019-11-08 19:22:35 +0100 | [diff] [blame] | 44 | unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 45 | char filename[512]; |
| 46 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | mbedtls_pk_init(&pk); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 48 | |
Przemek Stekiel | 9c0fc2d | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 49 | #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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | if (argc != 3) { |
| 59 | mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 60 | |
| 61 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 62 | mbedtls_printf("\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | goto exit; |
| 66 | } |
| 67 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | mbedtls_printf("\n . Reading public key from '%s'", argv[1]); |
| 69 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 70 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 74 | goto exit; |
| 75 | } |
| 76 | |
| 77 | /* |
Manuel Pégourié-Gonnard | 1d8f2da | 2015-08-27 21:42:27 +0200 | [diff] [blame] | 78 | * Extract the signature from the file |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 79 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 81 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | if ((f = fopen(filename, "rb")) == NULL) { |
| 83 | mbedtls_printf("\n ! Could not open %s\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 84 | goto exit; |
| 85 | } |
| 86 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 87 | i = fread(buf, 1, sizeof(buf), f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 88 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 90 | |
| 91 | /* |
Manuel Pégourié-Gonnard | ce7a08b | 2015-08-27 21:59:58 +0200 | [diff] [blame] | 92 | * Compute the SHA-256 hash of the input file and |
| 93 | * verify the signature |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 94 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | mbedtls_printf("\n . Verifying the SHA-256 signature"); |
| 96 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 102 | goto exit; |
| 103 | } |
| 104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | 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 Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 108 | goto exit; |
| 109 | } |
| 110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | mbedtls_printf("\n . OK (the signature is valid)\n\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 112 | |
Andres Amaya Garcia | 9f3379d | 2018-04-29 22:30:05 +0100 | [diff] [blame] | 113 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 114 | |
| 115 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | mbedtls_pk_free(&pk); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | 9c0fc2d | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 118 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | d4d049b | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 119 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | 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é-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 125 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 126 | #endif |
| 127 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 128 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 130 | fflush(stdout); getchar(); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 131 | #endif |
| 132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 134 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C && |
| 136 | MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ |