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