xref: /aosp_15_r20/external/mbedtls/programs/pkey/rsa_verify.c (revision 62c56f9862f102b96d72393aff6076c951fb8148)
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     unsigned char hash[32];
41     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
42     char filename[512];
43 
44     mbedtls_rsa_init(&rsa);
45 
46     if (argc != 2) {
47         mbedtls_printf("usage: rsa_verify <filename>\n");
48 
49 #if defined(_WIN32)
50         mbedtls_printf("\n");
51 #endif
52 
53         goto exit;
54     }
55 
56     mbedtls_printf("\n  . Reading public key from rsa_pub.txt");
57     fflush(stdout);
58 
59     if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
60         mbedtls_printf(" failed\n  ! Could not open rsa_pub.txt\n" \
61                        "  ! Please run rsa_genkey first\n\n");
62         goto exit;
63     }
64 
65     if ((ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(N), 16, f)) != 0 ||
66         (ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(E), 16, f)) != 0) {
67         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret);
68         fclose(f);
69         goto exit;
70     }
71 
72     rsa.MBEDTLS_PRIVATE(len) = (mbedtls_mpi_bitlen(&rsa.MBEDTLS_PRIVATE(N)) + 7) >> 3;
73 
74     fclose(f);
75 
76     /*
77      * Extract the RSA signature from the text file
78      */
79     mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
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 = 0;
87     while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
88            i < (int) sizeof(buf)) {
89         buf[i++] = (unsigned char) c;
90     }
91 
92     fclose(f);
93 
94     if (i != rsa.MBEDTLS_PRIVATE(len)) {
95         mbedtls_printf("\n  ! Invalid RSA signature format\n\n");
96         goto exit;
97     }
98 
99     /*
100      * Compute the SHA-256 hash of the input file and
101      * verify the signature
102      */
103     mbedtls_printf("\n  . Verifying the RSA/SHA-256 signature");
104     fflush(stdout);
105 
106     if ((ret = mbedtls_md_file(
107              mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
108              argv[1], hash)) != 0) {
109         mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[1]);
110         goto exit;
111     }
112 
113     if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256,
114                                         32, hash, buf)) != 0) {
115         mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n",
116                        (unsigned int) -ret);
117         goto exit;
118     }
119 
120     mbedtls_printf("\n  . OK (the signature is valid)\n\n");
121 
122     exit_code = MBEDTLS_EXIT_SUCCESS;
123 
124 exit:
125 
126     mbedtls_rsa_free(&rsa);
127 
128     mbedtls_exit(exit_code);
129 }
130 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
131           MBEDTLS_FS_IO */
132