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 | 16799db | 2023-11-02 19:47:20 +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 | |
Felix Conway | 998760a | 2025-03-24 11:37:33 +0000 | [diff] [blame] | 8 | #define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS |
| 9 | |
Ronald Cron | 0815c67 | 2025-04-12 11:52:18 +0200 | [diff] [blame] | 10 | #include "tf-psa-crypto/build_info.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/platform.h" |
Andrzej Kurek | 0af3248 | 2023-04-07 03:10:28 -0400 | [diff] [blame] | 13 | /* md.h is included this early since MD_CAN_XXX macros are defined there. */ |
Andrzej Kurek | 1b75e5f | 2023-04-04 09:55:06 -0400 | [diff] [blame] | 14 | #include "mbedtls/md.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) || \ |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 17 | !defined(PSA_WANT_ALG_SHA_256) || !defined(MBEDTLS_PK_PARSE_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | !defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 " |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 22 | "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_PK_PARSE_C and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 28 | #include "mbedtls/pk.h" |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 33 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 35 | { |
| 36 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 37 | int ret = 1; |
Andres Amaya Garcia | 9f3379d | 2018-04-29 22:30:05 +0100 | [diff] [blame] | 38 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 39 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_pk_context pk; |
Manuel Pégourié-Gonnard | 102a620 | 2015-08-27 21:51:44 +0200 | [diff] [blame] | 41 | unsigned char hash[32]; |
Gilles Peskine | 96a7cd1 | 2019-11-08 19:22:35 +0100 | [diff] [blame] | 42 | unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 43 | char filename[512]; |
| 44 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | mbedtls_pk_init(&pk); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 46 | |
Przemek Stekiel | 2c1ef09 | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 48 | psa_status_t status = psa_crypto_init(); |
| 49 | if (status != PSA_SUCCESS) { |
| 50 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 51 | (int) status); |
| 52 | goto exit; |
| 53 | } |
| 54 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 55 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | if (argc != 3) { |
| 57 | mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 58 | |
| 59 | #if defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | mbedtls_printf("\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
| 63 | goto exit; |
| 64 | } |
| 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | mbedtls_printf("\n . Reading public key from '%s'", argv[1]); |
| 67 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) { |
| 70 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", |
| 71 | (unsigned int) -ret); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 72 | goto exit; |
| 73 | } |
| 74 | |
| 75 | /* |
Manuel Pégourié-Gonnard | 1d8f2da | 2015-08-27 21:42:27 +0200 | [diff] [blame] | 76 | * Extract the signature from the file |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 77 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | if ((f = fopen(filename, "rb")) == NULL) { |
| 81 | mbedtls_printf("\n ! Could not open %s\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 82 | goto exit; |
| 83 | } |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | i = fread(buf, 1, sizeof(buf), f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 86 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 88 | |
| 89 | /* |
Manuel Pégourié-Gonnard | ce7a08b | 2015-08-27 21:59:58 +0200 | [diff] [blame] | 90 | * Compute the SHA-256 hash of the input file and |
| 91 | * verify the signature |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 92 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | mbedtls_printf("\n . Verifying the SHA-256 signature"); |
| 94 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 95 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | if ((ret = mbedtls_md_file( |
| 97 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), |
| 98 | argv[2], hash)) != 0) { |
| 99 | 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] | 100 | goto exit; |
| 101 | } |
| 102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0, |
| 104 | buf, i)) != 0) { |
| 105 | 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] | 106 | goto exit; |
| 107 | } |
| 108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | mbedtls_printf("\n . OK (the signature is valid)\n\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 110 | |
Andres Amaya Garcia | 9f3379d | 2018-04-29 22:30:05 +0100 | [diff] [blame] | 111 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 112 | |
| 113 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | mbedtls_pk_free(&pk); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 115 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | 2c1ef09 | 2023-04-19 09:38:12 +0200 | [diff] [blame] | 116 | mbedtls_psa_crypto_free(); |
Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 117 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { |
Harry Ramsey | 9c66405 | 2024-10-16 14:08:19 +0100 | [diff] [blame] | 121 | mbedtls_printf("Error code: %d", ret); |
| 122 | /* mbedtls_strerror(ret, (char *) buf, sizeof(buf)); |
| 123 | mbedtls_printf(" ! Last error was: %s\n", buf); */ |
Manuel Pégourié-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 124 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 125 | #endif |
| 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 128 | } |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 129 | #endif /* MBEDTLS_BIGNUM_C && PSA_WANT_ALG_SHA_256 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ |