1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  RSA/SHA-256 signature creation 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    int exit_code = MBEDTLS_EXIT_FAILURE;
37a8e1175bSopenharmony_ci    size_t i;
38a8e1175bSopenharmony_ci    mbedtls_rsa_context rsa;
39a8e1175bSopenharmony_ci    unsigned char hash[32];
40a8e1175bSopenharmony_ci    unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
41a8e1175bSopenharmony_ci    char filename[512];
42a8e1175bSopenharmony_ci    mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci    mbedtls_rsa_init(&rsa);
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci    mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
47a8e1175bSopenharmony_ci    mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
48a8e1175bSopenharmony_ci    mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_ci    if (argc != 2) {
51a8e1175bSopenharmony_ci        mbedtls_printf("usage: rsa_sign <filename>\n");
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci#if defined(_WIN32)
54a8e1175bSopenharmony_ci        mbedtls_printf("\n");
55a8e1175bSopenharmony_ci#endif
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ci        goto exit;
58a8e1175bSopenharmony_ci    }
59a8e1175bSopenharmony_ci
60a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Reading private key from rsa_priv.txt");
61a8e1175bSopenharmony_ci    fflush(stdout);
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci    if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
64a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! Could not open rsa_priv.txt\n" \
65a8e1175bSopenharmony_ci                       "  ! Please run rsa_genkey first\n\n");
66a8e1175bSopenharmony_ci        goto exit;
67a8e1175bSopenharmony_ci    }
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_ci    if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
70a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
71a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
72a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
73a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
74a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
75a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
76a8e1175bSopenharmony_ci        (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
77a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret);
78a8e1175bSopenharmony_ci        fclose(f);
79a8e1175bSopenharmony_ci        goto exit;
80a8e1175bSopenharmony_ci    }
81a8e1175bSopenharmony_ci    fclose(f);
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ci    if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
84a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_rsa_import returned %d\n\n",
85a8e1175bSopenharmony_ci                       ret);
86a8e1175bSopenharmony_ci        goto exit;
87a8e1175bSopenharmony_ci    }
88a8e1175bSopenharmony_ci
89a8e1175bSopenharmony_ci    if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
90a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_rsa_complete returned %d\n\n",
91a8e1175bSopenharmony_ci                       ret);
92a8e1175bSopenharmony_ci        goto exit;
93a8e1175bSopenharmony_ci    }
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Checking the private key");
96a8e1175bSopenharmony_ci    fflush(stdout);
97a8e1175bSopenharmony_ci    if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) {
98a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_rsa_check_privkey failed with -0x%0x\n",
99a8e1175bSopenharmony_ci                       (unsigned int) -ret);
100a8e1175bSopenharmony_ci        goto exit;
101a8e1175bSopenharmony_ci    }
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci    /*
104a8e1175bSopenharmony_ci     * Compute the SHA-256 hash of the input file,
105a8e1175bSopenharmony_ci     * then calculate the RSA signature of the hash.
106a8e1175bSopenharmony_ci     */
107a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Generating the RSA/SHA-256 signature");
108a8e1175bSopenharmony_ci    fflush(stdout);
109a8e1175bSopenharmony_ci
110a8e1175bSopenharmony_ci    if ((ret = mbedtls_md_file(
111a8e1175bSopenharmony_ci             mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
112a8e1175bSopenharmony_ci             argv[1], hash)) != 0) {
113a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[1]);
114a8e1175bSopenharmony_ci        goto exit;
115a8e1175bSopenharmony_ci    }
116a8e1175bSopenharmony_ci
117a8e1175bSopenharmony_ci    if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
118a8e1175bSopenharmony_ci                                      32, hash, buf)) != 0) {
119a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n",
120a8e1175bSopenharmony_ci                       (unsigned int) -ret);
121a8e1175bSopenharmony_ci        goto exit;
122a8e1175bSopenharmony_ci    }
123a8e1175bSopenharmony_ci
124a8e1175bSopenharmony_ci    /*
125a8e1175bSopenharmony_ci     * Write the signature into <filename>.sig
126a8e1175bSopenharmony_ci     */
127a8e1175bSopenharmony_ci    mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
128a8e1175bSopenharmony_ci
129a8e1175bSopenharmony_ci    if ((f = fopen(filename, "wb+")) == NULL) {
130a8e1175bSopenharmony_ci        mbedtls_printf(" failed\n  ! Could not create %s\n\n", argv[1]);
131a8e1175bSopenharmony_ci        goto exit;
132a8e1175bSopenharmony_ci    }
133a8e1175bSopenharmony_ci
134a8e1175bSopenharmony_ci    for (i = 0; i < mbedtls_rsa_get_len(&rsa); i++) {
135a8e1175bSopenharmony_ci        mbedtls_fprintf(f, "%02X%s", buf[i],
136a8e1175bSopenharmony_ci                        (i + 1) % 16 == 0 ? "\r\n" : " ");
137a8e1175bSopenharmony_ci    }
138a8e1175bSopenharmony_ci
139a8e1175bSopenharmony_ci    fclose(f);
140a8e1175bSopenharmony_ci
141a8e1175bSopenharmony_ci    mbedtls_printf("\n  . Done (created \"%s\")\n\n", filename);
142a8e1175bSopenharmony_ci
143a8e1175bSopenharmony_ci    exit_code = MBEDTLS_EXIT_SUCCESS;
144a8e1175bSopenharmony_ci
145a8e1175bSopenharmony_ciexit:
146a8e1175bSopenharmony_ci
147a8e1175bSopenharmony_ci    mbedtls_rsa_free(&rsa);
148a8e1175bSopenharmony_ci    mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
149a8e1175bSopenharmony_ci    mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
150a8e1175bSopenharmony_ci    mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
151a8e1175bSopenharmony_ci
152a8e1175bSopenharmony_ci    mbedtls_exit(exit_code);
153a8e1175bSopenharmony_ci}
154a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
155a8e1175bSopenharmony_ci          MBEDTLS_FS_IO */
156