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