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