1 /*
2 * RSA simple data encryption 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 #endif
35
36 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
37 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
38 defined(MBEDTLS_CTR_DRBG_C)
39 #include "mbedtls/error.h"
40 #include "mbedtls/pk.h"
41 #include "mbedtls/entropy.h"
42 #include "mbedtls/ctr_drbg.h"
43
44 #include <stdio.h>
45 #include <string.h>
46 #endif
47
48 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
49 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
50 !defined(MBEDTLS_CTR_DRBG_C)
main(void)51 int main( void )
52 {
53 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
54 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
55 "MBEDTLS_CTR_DRBG_C not defined.\n");
56 return( 0 );
57 }
58 #else
main(int argc,char * argv[])59 int main( int argc, char *argv[] )
60 {
61 FILE *f;
62 int ret;
63 size_t i, olen = 0;
64 mbedtls_pk_context pk;
65 mbedtls_entropy_context entropy;
66 mbedtls_ctr_drbg_context ctr_drbg;
67 unsigned char input[1024];
68 unsigned char buf[512];
69 const char *pers = "mbedtls_pk_encrypt";
70
71 ret = 1;
72 mbedtls_ctr_drbg_init( &ctr_drbg );
73
74 if( argc != 3 )
75 {
76 mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
77
78 #if defined(_WIN32)
79 mbedtls_printf( "\n" );
80 #endif
81
82 goto exit;
83 }
84
85 mbedtls_printf( "\n . Seeding the random number generator..." );
86 fflush( stdout );
87
88 mbedtls_entropy_init( &entropy );
89 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
90 (const unsigned char *) pers,
91 strlen( pers ) ) ) != 0 )
92 {
93 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
94 goto exit;
95 }
96
97 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
98 fflush( stdout );
99
100 mbedtls_pk_init( &pk );
101
102 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
103 {
104 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
105 goto exit;
106 }
107
108 if( strlen( argv[2] ) > 100 )
109 {
110 mbedtls_printf( " Input data larger than 100 characters.\n\n" );
111 goto exit;
112 }
113
114 memcpy( input, argv[2], strlen( argv[2] ) );
115
116 /*
117 * Calculate the RSA encryption of the hash.
118 */
119 mbedtls_printf( "\n . Generating the encrypted value" );
120 fflush( stdout );
121
122 if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
123 buf, &olen, sizeof(buf),
124 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
125 {
126 mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
127 goto exit;
128 }
129
130 /*
131 * Write the signature into result-enc.txt
132 */
133 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
134 {
135 ret = 1;
136 mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
137 goto exit;
138 }
139
140 for( i = 0; i < olen; i++ )
141 mbedtls_fprintf( f, "%02X%s", buf[i],
142 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
143
144 fclose( f );
145
146 mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
147
148 exit:
149 mbedtls_ctr_drbg_free( &ctr_drbg );
150 mbedtls_entropy_free( &entropy );
151
152 #if defined(MBEDTLS_ERROR_C)
153 if( ret != 0 )
154 {
155 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
156 mbedtls_printf( " ! Last error was: %s\n", buf );
157 }
158 #endif
159
160 #if defined(_WIN32)
161 mbedtls_printf( " + Press Enter to exit this program.\n" );
162 fflush( stdout ); getchar();
163 #endif
164
165 return( ret );
166 }
167 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
168 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
169