1 /*
2 * Public key-based signature verification program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
17 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
18 !defined(MBEDTLS_FS_IO)
main(void)19 int main(void)
20 {
21 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
22 "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
23 "MBEDTLS_FS_IO not defined.\n");
24 mbedtls_exit(0);
25 }
26 #else
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
35
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38 FILE *f;
39 int ret = 1;
40 int exit_code = MBEDTLS_EXIT_FAILURE;
41 size_t i;
42 mbedtls_pk_context pk;
43 unsigned char hash[32];
44 unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
45 char filename[512];
46
47 mbedtls_pk_init(&pk);
48
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
58 if (argc != 3) {
59 mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n");
60
61 #if defined(_WIN32)
62 mbedtls_printf("\n");
63 #endif
64
65 goto exit;
66 }
67
68 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
69 fflush(stdout);
70
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);
74 goto exit;
75 }
76
77 /*
78 * Extract the signature from the file
79 */
80 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
81
82 if ((f = fopen(filename, "rb")) == NULL) {
83 mbedtls_printf("\n ! Could not open %s\n\n", filename);
84 goto exit;
85 }
86
87 i = fread(buf, 1, sizeof(buf), f);
88
89 fclose(f);
90
91 /*
92 * Compute the SHA-256 hash of the input file and
93 * verify the signature
94 */
95 mbedtls_printf("\n . Verifying the SHA-256 signature");
96 fflush(stdout);
97
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]);
102 goto exit;
103 }
104
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);
108 goto exit;
109 }
110
111 mbedtls_printf("\n . OK (the signature is valid)\n\n");
112
113 exit_code = MBEDTLS_EXIT_SUCCESS;
114
115 exit:
116 mbedtls_pk_free(&pk);
117 #if defined(MBEDTLS_USE_PSA_CRYPTO)
118 mbedtls_psa_crypto_free();
119 #endif /* MBEDTLS_USE_PSA_CRYPTO */
120
121 #if defined(MBEDTLS_ERROR_C)
122 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
123 mbedtls_strerror(ret, (char *) buf, sizeof(buf));
124 mbedtls_printf(" ! Last error was: %s\n", buf);
125 }
126 #endif
127
128 #if defined(_WIN32)
129 mbedtls_printf(" + Press Enter to exit this program.\n");
130 fflush(stdout); getchar();
131 #endif
132
133 mbedtls_exit(exit_code);
134 }
135 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
136 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
137