1 /*
2  *  RSA/SHA-256 signature creation program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 
20 #include "mbedtls/build_info.h"
21 
22 #include "mbedtls/platform.h"
23 
24 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
25     !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
26     !defined(MBEDTLS_FS_IO)
main(void)27 int main(void)
28 {
29     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
30                    "MBEDTLS_MD_C and/or "
31                    "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
32     mbedtls_exit(0);
33 }
34 #else
35 
36 #include "mbedtls/rsa.h"
37 #include "mbedtls/md.h"
38 
39 #include <stdio.h>
40 #include <string.h>
41 
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45     FILE *f;
46     int ret = 1;
47     int exit_code = MBEDTLS_EXIT_FAILURE;
48     size_t i;
49     mbedtls_rsa_context rsa;
50     unsigned char hash[32];
51     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
52     char filename[512];
53     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
54 
55     mbedtls_rsa_init(&rsa);
56 
57     mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
58     mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
59     mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
60 
61     if (argc != 2) {
62         mbedtls_printf("usage: rsa_sign <filename>\n");
63 
64 #if defined(_WIN32)
65         mbedtls_printf("\n");
66 #endif
67 
68         goto exit;
69     }
70 
71     mbedtls_printf("\n  . Reading private key from rsa_priv.txt");
72     fflush(stdout);
73 
74     if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
75         mbedtls_printf(" failed\n  ! Could not open rsa_priv.txt\n" \
76                        "  ! Please run rsa_genkey first\n\n");
77         goto exit;
78     }
79 
80     if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
81         (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
82         (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
83         (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
84         (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
85         (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
86         (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
87         (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
88         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret);
89         fclose(f);
90         goto exit;
91     }
92     fclose(f);
93 
94     if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
95         mbedtls_printf(" failed\n  ! mbedtls_rsa_import returned %d\n\n",
96                        ret);
97         goto exit;
98     }
99 
100     if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
101         mbedtls_printf(" failed\n  ! mbedtls_rsa_complete returned %d\n\n",
102                        ret);
103         goto exit;
104     }
105 
106     mbedtls_printf("\n  . Checking the private key");
107     fflush(stdout);
108     if ((ret = mbedtls_rsa_check_privkey(&rsa)) != 0) {
109         mbedtls_printf(" failed\n  ! mbedtls_rsa_check_privkey failed with -0x%0x\n",
110                        (unsigned int) -ret);
111         goto exit;
112     }
113 
114     /*
115      * Compute the SHA-256 hash of the input file,
116      * then calculate the RSA signature of the hash.
117      */
118     mbedtls_printf("\n  . Generating the RSA/SHA-256 signature");
119     fflush(stdout);
120 
121     if ((ret = mbedtls_md_file(
122              mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
123              argv[1], hash)) != 0) {
124         mbedtls_printf(" failed\n  ! Could not open or read %s\n\n", argv[1]);
125         goto exit;
126     }
127 
128     if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
129                                       32, hash, buf)) != 0) {
130         mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n",
131                        (unsigned int) -ret);
132         goto exit;
133     }
134 
135     /*
136      * Write the signature into <filename>.sig
137      */
138     mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[1]);
139 
140     if ((f = fopen(filename, "wb+")) == NULL) {
141         mbedtls_printf(" failed\n  ! Could not create %s\n\n", argv[1]);
142         goto exit;
143     }
144 
145     for (i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++) {
146         mbedtls_fprintf(f, "%02X%s", buf[i],
147                         (i + 1) % 16 == 0 ? "\r\n" : " ");
148     }
149 
150     fclose(f);
151 
152     mbedtls_printf("\n  . Done (created \"%s\")\n\n", filename);
153 
154     exit_code = MBEDTLS_EXIT_SUCCESS;
155 
156 exit:
157 
158     mbedtls_rsa_free(&rsa);
159     mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
160     mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
161     mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
162 
163     mbedtls_exit(exit_code);
164 }
165 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
166           MBEDTLS_FS_IO */
167