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