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 #define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9 
10 #include "tf-psa-crypto/build_info.h"
11 
12 #include "mbedtls/platform.h"
13 /* md.h is included this early since MD_CAN_XXX macros are defined there. */
14 #include "mbedtls/md.h"
15 
16 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
17     !defined(PSA_WANT_ALG_SHA_256) || !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                    "PSA_WANT_ALG_SHA_256 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/pk.h"
29 #if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
30 #include <mbedtls/private/pk_private.h>
31 #endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
32 
33 #include <stdio.h>
34 #include <string.h>
35 
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39     FILE *f;
40     int ret = 1;
41     int exit_code = MBEDTLS_EXIT_FAILURE;
42     size_t i;
43     mbedtls_pk_context pk;
44     unsigned char hash[32];
45     unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
46     char filename[512];
47 
48     mbedtls_pk_init(&pk);
49 
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 
57     if (argc != 3) {
58         mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n");
59 
60 #if defined(_WIN32)
61         mbedtls_printf("\n");
62 #endif
63 
64         goto exit;
65     }
66 
67     mbedtls_printf("\n  . Reading public key from '%s'", argv[1]);
68     fflush(stdout);
69 
70     if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
71         mbedtls_printf(" failed\n  ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
72                        (unsigned int) -ret);
73         goto exit;
74     }
75 
76     /*
77      * Extract the signature from the file
78      */
79     mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
80 
81     if ((f = fopen(filename, "rb")) == NULL) {
82         mbedtls_printf("\n  ! Could not open %s\n\n", filename);
83         goto exit;
84     }
85 
86     i = fread(buf, 1, sizeof(buf), f);
87 
88     fclose(f);
89 
90     /*
91      * Compute the SHA-256 hash of the input file and
92      * verify the signature
93      */
94     mbedtls_printf("\n  . Verifying the SHA-256 signature");
95     fflush(stdout);
96 
97     if ((ret = mbedtls_md_file(
98              mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
99              argv[2], hash)) != 0) {
100         mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[2]);
101         goto exit;
102     }
103 
104     if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
105                                  buf, i)) != 0) {
106         mbedtls_printf(" failed\n  ! mbedtls_pk_verify returned -0x%04x\n", (unsigned int) -ret);
107         goto exit;
108     }
109 
110     mbedtls_printf("\n  . OK (the signature is valid)\n\n");
111 
112     exit_code = MBEDTLS_EXIT_SUCCESS;
113 
114 exit:
115     mbedtls_pk_free(&pk);
116     mbedtls_psa_crypto_free();
117 
118 #if defined(MBEDTLS_ERROR_C)
119     if (exit_code != MBEDTLS_EXIT_SUCCESS) {
120         mbedtls_printf("Error code: %d", ret);
121         /* mbedtls_strerror(ret, (char *) buf, sizeof(buf));
122            mbedtls_printf("  !  Last error was: %s\n", buf); */
123     }
124 #endif
125 
126     mbedtls_exit(exit_code);
127 }
128 #endif /* MBEDTLS_BIGNUM_C && PSA_WANT_ALG_SHA_256 &&
129           MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
130