1 /*
2  *  RSA/SHA-256 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 #include "mbedtls/build_info.h"
9 
10 #include "mbedtls/platform.h"
11 /* md.h is included this early since MD_CAN_XXX macros are defined there. */
12 #include "mbedtls/md.h"
13 
14 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
15     !defined(MBEDTLS_MD_CAN_SHA256) || !defined(MBEDTLS_MD_C) || \
16     !defined(MBEDTLS_FS_IO)
main(void)17 int main(void)
18 {
19     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
20                    "MBEDTLS_MD_C and/or "
21                    "MBEDTLS_MD_CAN_SHA256 and/or MBEDTLS_FS_IO not defined.\n");
22     mbedtls_exit(0);
23 }
24 #else
25 
26 #include "mbedtls/rsa.h"
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 
main(int argc, char *argv[])32 int main(int argc, char *argv[])
33 {
34     FILE *f;
35     int ret = 1;
36     unsigned c;
37     int exit_code = MBEDTLS_EXIT_FAILURE;
38     size_t i;
39     mbedtls_rsa_context rsa;
40     mbedtls_mpi N, E;
41     unsigned char hash[32];
42     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
43     char filename[512];
44 
45     mbedtls_rsa_init(&rsa);
46     mbedtls_mpi_init(&N);
47     mbedtls_mpi_init(&E);
48 
49     if (argc != 2) {
50         mbedtls_printf("usage: rsa_verify <filename>\n");
51 
52 #if defined(_WIN32)
53         mbedtls_printf("\n");
54 #endif
55 
56         goto exit;
57     }
58 
59     mbedtls_printf("\n  . Reading public key from rsa_pub.txt");
60     fflush(stdout);
61 
62     if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
63         mbedtls_printf(" failed\n  ! Could not open rsa_pub.txt\n" \
64                        "  ! Please run rsa_genkey first\n\n");
65         goto exit;
66     }
67 
68     if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
69         (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
70         (ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E) != 0)) {
71         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret);
72         fclose(f);
73         goto exit;
74     }
75     fclose(f);
76 
77     /*
78      * Extract the RSA signature from the text file
79      */
80     mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
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 = 0;
88     while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
89            i < (int) sizeof(buf)) {
90         buf[i++] = (unsigned char) c;
91     }
92 
93     fclose(f);
94 
95     if (i != mbedtls_rsa_get_len(&rsa)) {
96         mbedtls_printf("\n  ! Invalid RSA signature format\n\n");
97         goto exit;
98     }
99 
100     /*
101      * Compute the SHA-256 hash of the input file and
102      * verify the signature
103      */
104     mbedtls_printf("\n  . Verifying the RSA/SHA-256 signature");
105     fflush(stdout);
106 
107     if ((ret = mbedtls_md_file(
108              mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
109              argv[1], hash)) != 0) {
110         mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[1]);
111         goto exit;
112     }
113 
114     if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256,
115                                         32, hash, buf)) != 0) {
116         mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n",
117                        (unsigned int) -ret);
118         goto exit;
119     }
120 
121     mbedtls_printf("\n  . OK (the signature is valid)\n\n");
122 
123     exit_code = MBEDTLS_EXIT_SUCCESS;
124 
125 exit:
126 
127     mbedtls_rsa_free(&rsa);
128     mbedtls_mpi_free(&N);
129     mbedtls_mpi_free(&E);
130 
131     mbedtls_exit(exit_code);
132 }
133 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
134           MBEDTLS_FS_IO */
135