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