source: trunk/programs/pkey/rsa_encrypt.c @ 1143

Revision 1143, 4.3 KB checked in by paul, 6 months ago (diff)
  • Moved all examples programs to use the new entropy and CTR_DRBG
Line 
1/*
2 *  RSA simple data encryption program
3 *
4 *  Copyright (C) 2006-2011, Brainspark B.V.
5 *
6 *  This file is part of PolarSSL (http://www.polarssl.org)
7 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 *  All rights reserved.
10 *
11 *  This program is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License as published by
13 *  the Free Software Foundation; either version 2 of the License, or
14 *  (at your option) any later version.
15 *
16 *  This program is distributed in the hope that it will be useful,
17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *  GNU General Public License for more details.
20 *
21 *  You should have received a copy of the GNU General Public License along
22 *  with this program; if not, write to the Free Software Foundation, Inc.,
23 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
33#include "polarssl/config.h"
34
35#include "polarssl/rsa.h"
36#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
38
39#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) ||  \
40    !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
41    !defined(POLARSSL_CTR_DRBG_C)
42int main( int argc, char *argv[] )
43{
44    ((void) argc);
45    ((void) argv);
46
47    printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
48           "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
49           "POLARSSL_CTR_DRBG_C not defined.\n");
50    return( 0 );
51}
52#else
53int main( int argc, char *argv[] )
54{
55    FILE *f;
56    int ret;
57    size_t i;
58    rsa_context rsa;
59    entropy_context entropy;
60    ctr_drbg_context ctr_drbg;
61    unsigned char input[1024];
62    unsigned char buf[512];
63    char *pers = "rsa_encrypt";
64
65    ret = 1;
66
67    if( argc != 2 )
68    {
69        printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
70
71#if defined(_WIN32)
72        printf( "\n" );
73#endif
74
75        goto exit;
76    }
77
78    printf( "\n  . Seeding the random number generator..." );
79    fflush( stdout );
80
81    entropy_init( &entropy );
82    if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
83                               (unsigned char *) pers, strlen( pers ) ) ) != 0 )
84    {
85        printf( " failed\n  ! ctr_drbg_init returned %d\n", ret );
86        goto exit;
87    }
88
89    printf( "\n  . Reading public key from rsa_pub.txt" );
90    fflush( stdout );
91
92    if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
93    {
94        ret = 1;
95        printf( " failed\n  ! Could not open rsa_pub.txt\n" \
96                "  ! Please run rsa_genkey first\n\n" );
97        goto exit;
98    }
99
100    rsa_init( &rsa, RSA_PKCS_V15, 0 );
101   
102    if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
103        ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
104    {
105        printf( " failed\n  ! mpi_read_file returned %d\n\n", ret );
106        goto exit;
107    }
108
109    rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
110
111    fclose( f );
112
113    if( strlen( argv[1] ) > 100 )
114    {
115        printf( " Input data larger than 100 characters.\n\n" );
116        goto exit;
117    }
118
119    memcpy( input, argv[1], strlen( argv[1] ) );
120
121    /*
122     * Calculate the RSA encryption of the hash.
123     */
124    printf( "\n  . Generating the RSA encrypted value" );
125    fflush( stdout );
126
127    if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
128                                   RSA_PUBLIC, strlen( argv[1] ),
129                                   input, buf ) ) != 0 )
130    {
131        printf( " failed\n  ! rsa_pkcs1_encrypt returned %d\n\n", ret );
132        goto exit;
133    }
134
135    /*
136     * Write the signature into result-enc.txt
137     */
138    if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
139    {
140        ret = 1;
141        printf( " failed\n  ! Could not create %s\n\n", "result-enc.txt" );
142        goto exit;
143    }
144
145    for( i = 0; i < rsa.len; i++ )
146        fprintf( f, "%02X%s", buf[i],
147                 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
148
149    fclose( f );
150
151    printf( "\n  . Done (created \"%s\")\n\n", "result-enc.txt" );
152
153exit:
154
155#if defined(_WIN32)
156    printf( "  + Press Enter to exit this program.\n" );
157    fflush( stdout ); getchar();
158#endif
159
160    return( ret );
161}
162#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C &&
163          POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?