• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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_RSA_C) ||  \
17     !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
18     !defined(MBEDTLS_FS_IO)
main(void)19 int main(void)
20 {
21     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
22                    "MBEDTLS_MD_C and/or "
23                    "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
24     mbedtls_exit(0);
25 }
26 #else
27 
28 #include "mbedtls/rsa.h"
29 #include "mbedtls/md.h"
30 
31 #include <stdio.h>
32 #include <string.h>
33 
34 
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37     FILE *f;
38     int ret = 1;
39     unsigned c;
40     int exit_code = MBEDTLS_EXIT_FAILURE;
41     size_t i;
42     mbedtls_rsa_context rsa;
43     unsigned char hash[32];
44     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
45     char filename[512];
46 
47     mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
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(&rsa.N, 16, f)) != 0 ||
69         (ret = mbedtls_mpi_read_file(&rsa.E, 16, f)) != 0) {
70         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret);
71         fclose(f);
72         goto exit;
73     }
74 
75     rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3;
76 
77     fclose(f);
78 
79     /*
80      * Extract the RSA signature from the text file
81      */
82     mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
83 
84     if ((f = fopen(filename, "rb")) == NULL) {
85         mbedtls_printf("\n  ! Could not open %s\n\n", filename);
86         goto exit;
87     }
88 
89     i = 0;
90     while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
91            i < (int) sizeof(buf)) {
92         buf[i++] = (unsigned char) c;
93     }
94 
95     fclose(f);
96 
97     if (i != rsa.len) {
98         mbedtls_printf("\n  ! Invalid RSA signature format\n\n");
99         goto exit;
100     }
101 
102     /*
103      * Compute the SHA-256 hash of the input file and
104      * verify the signature
105      */
106     mbedtls_printf("\n  . Verifying the RSA/SHA-256 signature");
107     fflush(stdout);
108 
109     if ((ret = mbedtls_md_file(
110              mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
111              argv[1], hash)) != 0) {
112         mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[1]);
113         goto exit;
114     }
115 
116     if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
117                                         MBEDTLS_MD_SHA256, 20, hash, buf)) != 0) {
118         mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n",
119                        (unsigned int) -ret);
120         goto exit;
121     }
122 
123     mbedtls_printf("\n  . OK (the signature is valid)\n\n");
124 
125     exit_code = MBEDTLS_EXIT_SUCCESS;
126 
127 exit:
128 
129     mbedtls_rsa_free(&rsa);
130 
131 #if defined(_WIN32)
132     mbedtls_printf("  + Press Enter to exit this program.\n");
133     fflush(stdout); getchar();
134 #endif
135 
136     mbedtls_exit(exit_code);
137 }
138 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
139           MBEDTLS_FS_IO */
140