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