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 #if defined(MBEDTLS_PLATFORM_C)
23 #include "mbedtls/platform.h"
24 #else
25 #include <stdio.h>
26 #include <stdlib.h>
27 #define mbedtls_fprintf         fprintf
28 #define mbedtls_printf          printf
29 #define mbedtls_snprintf        snprintf
30 #define mbedtls_exit            exit
31 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
32 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
33 #endif /* MBEDTLS_PLATFORM_C */
34 
35 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
36     !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
37     !defined(MBEDTLS_FS_IO)
main(void)38 int main( void )
39 {
40     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
41             "MBEDTLS_MD_C and/or "
42             "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
43     mbedtls_exit( 0 );
44 }
45 #else
46 
47 #include "mbedtls/rsa.h"
48 #include "mbedtls/md.h"
49 
50 #include <stdio.h>
51 #include <string.h>
52 
53 
main(int argc,char * argv[])54 int main( int argc, char *argv[] )
55 {
56     FILE *f;
57     int ret = 1;
58     int exit_code = MBEDTLS_EXIT_FAILURE;
59     size_t i;
60     mbedtls_rsa_context rsa;
61     unsigned char hash[32];
62     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
63     char filename[512];
64     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
65 
66     mbedtls_rsa_init( &rsa );
67 
68     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
69     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
70     mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
71 
72     if( argc != 2 )
73     {
74         mbedtls_printf( "usage: rsa_sign <filename>\n" );
75 
76 #if defined(_WIN32)
77         mbedtls_printf( "\n" );
78 #endif
79 
80         goto exit;
81     }
82 
83     mbedtls_printf( "\n  . Reading private key from rsa_priv.txt" );
84     fflush( stdout );
85 
86     if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
87     {
88         mbedtls_printf( " failed\n  ! Could not open rsa_priv.txt\n" \
89                 "  ! Please run rsa_genkey first\n\n" );
90         goto exit;
91     }
92 
93     if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
94         ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
95         ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
96         ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
97         ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
98         ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
99         ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
100         ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
101     {
102         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret );
103         fclose( f );
104         goto exit;
105     }
106     fclose( f );
107 
108     if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
109     {
110         mbedtls_printf( " failed\n  ! mbedtls_rsa_import returned %d\n\n",
111                         ret );
112         goto exit;
113     }
114 
115     if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
116     {
117         mbedtls_printf( " failed\n  ! mbedtls_rsa_complete returned %d\n\n",
118                         ret );
119         goto exit;
120     }
121 
122     mbedtls_printf( "\n  . Checking the private key" );
123     fflush( stdout );
124     if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
125     {
126         mbedtls_printf( " failed\n  ! mbedtls_rsa_check_privkey failed with -0x%0x\n", (unsigned int) -ret );
127         goto exit;
128     }
129 
130     /*
131      * Compute the SHA-256 hash of the input file,
132      * then calculate the RSA signature of the hash.
133      */
134     mbedtls_printf( "\n  . Generating the RSA/SHA-256 signature" );
135     fflush( stdout );
136 
137     if( ( ret = mbedtls_md_file(
138                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
139                     argv[1], hash ) ) != 0 )
140     {
141         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[1] );
142         goto exit;
143     }
144 
145     if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_MD_SHA256,
146                                 32, hash, buf ) ) != 0 )
147     {
148         mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", (unsigned int) -ret );
149         goto exit;
150     }
151 
152     /*
153      * Write the signature into <filename>.sig
154      */
155     mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
156 
157     if( ( f = fopen( filename, "wb+" ) ) == NULL )
158     {
159         mbedtls_printf( " failed\n  ! Could not create %s\n\n", argv[1] );
160         goto exit;
161     }
162 
163     for( i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++ )
164         mbedtls_fprintf( f, "%02X%s", buf[i],
165                  ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
166 
167     fclose( f );
168 
169     mbedtls_printf( "\n  . Done (created \"%s\")\n\n", filename );
170 
171     exit_code = MBEDTLS_EXIT_SUCCESS;
172 
173 exit:
174 
175     mbedtls_rsa_free( &rsa );
176     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
177     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
178     mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
179 
180 #if defined(_WIN32)
181     mbedtls_printf( "  + Press Enter to exit this program.\n" );
182     fflush( stdout ); getchar();
183 #endif
184 
185     mbedtls_exit( exit_code );
186 }
187 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
188           MBEDTLS_FS_IO */
189