1 /*
2  *  RSASSA-PSS/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 #include <stdlib.h>
33 #define mbedtls_snprintf        snprintf
34 #define mbedtls_printf          printf
35 #define mbedtls_exit            exit
36 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39 
40 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
41     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) ||        \
42     !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) ||    \
43     !defined(MBEDTLS_CTR_DRBG_C)
main(void)44 int main( void )
45 {
46     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
47            "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
48            "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
49            "MBEDTLS_CTR_DRBG_C not defined.\n");
50     return( 0 );
51 }
52 #else
53 
54 #include "mbedtls/entropy.h"
55 #include "mbedtls/ctr_drbg.h"
56 #include "mbedtls/md.h"
57 #include "mbedtls/rsa.h"
58 #include "mbedtls/md.h"
59 #include "mbedtls/x509.h"
60 
61 #include <stdio.h>
62 #include <string.h>
63 
64 #if defined(MBEDTLS_CHECK_PARAMS)
65 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)66 void mbedtls_param_failed( const char *failure_condition,
67                            const char *file,
68                            int line )
69 {
70     mbedtls_printf( "%s:%i: Input param failed - %s\n",
71                     file, line, failure_condition );
72     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
73 }
74 #endif
75 
main(int argc,char * argv[])76 int main( int argc, char *argv[] )
77 {
78     FILE *f;
79     int ret = 1;
80     int exit_code = MBEDTLS_EXIT_FAILURE;
81     mbedtls_pk_context pk;
82     mbedtls_entropy_context entropy;
83     mbedtls_ctr_drbg_context ctr_drbg;
84     unsigned char hash[32];
85     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
86     char filename[512];
87     const char *pers = "rsa_sign_pss";
88     size_t olen = 0;
89 
90     mbedtls_entropy_init( &entropy );
91     mbedtls_pk_init( &pk );
92     mbedtls_ctr_drbg_init( &ctr_drbg );
93 
94     if( argc != 3 )
95     {
96         mbedtls_printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
97 
98 #if defined(_WIN32)
99         mbedtls_printf( "\n" );
100 #endif
101 
102         goto exit;
103     }
104 
105     mbedtls_printf( "\n  . Seeding the random number generator..." );
106     fflush( stdout );
107 
108     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
109                                (const unsigned char *) pers,
110                                strlen( pers ) ) ) != 0 )
111     {
112         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
113         goto exit;
114     }
115 
116     mbedtls_printf( "\n  . Reading private key from '%s'", argv[1] );
117     fflush( stdout );
118 
119     if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
120     {
121         mbedtls_printf( " failed\n  ! Could not read key from '%s'\n", argv[1] );
122         mbedtls_printf( "  ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
123         goto exit;
124     }
125 
126     if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
127     {
128         mbedtls_printf( " failed\n  ! Key is not an RSA key\n" );
129         goto exit;
130     }
131 
132     mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
133 
134     /*
135      * Compute the SHA-256 hash of the input file,
136      * then calculate the RSA signature of the hash.
137      */
138     mbedtls_printf( "\n  . Generating the RSA/SHA-256 signature" );
139     fflush( stdout );
140 
141     if( ( ret = mbedtls_md_file(
142                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
143                     argv[2], hash ) ) != 0 )
144     {
145         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[2] );
146         goto exit;
147     }
148 
149     if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
150                          mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
151     {
152         mbedtls_printf( " failed\n  ! mbedtls_pk_sign returned %d\n\n", ret );
153         goto exit;
154     }
155 
156     /*
157      * Write the signature into <filename>.sig
158      */
159     mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
160 
161     if( ( f = fopen( filename, "wb+" ) ) == NULL )
162     {
163         mbedtls_printf( " failed\n  ! Could not create %s\n\n", filename );
164         goto exit;
165     }
166 
167     if( fwrite( buf, 1, olen, f ) != olen )
168     {
169         mbedtls_printf( "failed\n  ! fwrite failed\n\n" );
170         fclose( f );
171         goto exit;
172     }
173 
174     fclose( f );
175 
176     mbedtls_printf( "\n  . Done (created \"%s\")\n\n", filename );
177 
178     exit_code = MBEDTLS_EXIT_SUCCESS;
179 
180 exit:
181     mbedtls_pk_free( &pk );
182     mbedtls_ctr_drbg_free( &ctr_drbg );
183     mbedtls_entropy_free( &entropy );
184 
185 #if defined(_WIN32)
186     mbedtls_printf( "  + Press Enter to exit this program.\n" );
187     fflush( stdout ); getchar();
188 #endif
189 
190     return( exit_code );
191 }
192 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
193           MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
194           MBEDTLS_CTR_DRBG_C */
195