Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RSA simple data encryption program |
| 3 | * |
| 4 | * Copyright (C) 2006-2010, 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/havege.h" |
| 37 | |
| 38 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 39 | !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_FS_IO) |
| 40 | int main( void ) |
| 41 | { |
| 42 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 43 | "POLARSSL_HAVEGE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 44 | return( 0 ); |
| 45 | } |
| 46 | #else |
| 47 | int main( int argc, char *argv[] ) |
| 48 | { |
| 49 | FILE *f; |
| 50 | int ret; |
| 51 | size_t i; |
| 52 | rsa_context rsa; |
| 53 | havege_state hs; |
| 54 | unsigned char input[1024]; |
| 55 | unsigned char buf[512]; |
| 56 | |
| 57 | havege_init( &hs ); |
| 58 | |
| 59 | ret = 1; |
| 60 | |
| 61 | if( argc != 2 ) |
| 62 | { |
| 63 | printf( "usage: rsa_encrypt <string of max 100 characters>\n" ); |
| 64 | |
| 65 | #ifdef WIN32 |
| 66 | printf( "\n" ); |
| 67 | #endif |
| 68 | |
| 69 | goto exit; |
| 70 | } |
| 71 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame^] | 72 | printf( "\n . Reading public key from rsa_pub.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 73 | fflush( stdout ); |
| 74 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame^] | 75 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 76 | { |
| 77 | ret = 1; |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame^] | 78 | printf( " failed\n ! Could not open rsa_pub.txt\n" \ |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 79 | " ! Please run rsa_genkey first\n\n" ); |
| 80 | goto exit; |
| 81 | } |
| 82 | |
| 83 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 84 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame^] | 85 | if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || |
| 86 | ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 87 | { |
| 88 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 89 | goto exit; |
| 90 | } |
| 91 | |
| 92 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 93 | |
| 94 | fclose( f ); |
| 95 | |
| 96 | if( strlen( argv[1] ) > 100 ) |
| 97 | { |
| 98 | printf( " Input data larger than 100 characters.\n\n" ); |
| 99 | goto exit; |
| 100 | } |
| 101 | |
| 102 | memcpy( input, argv[1], strlen( argv[1] ) ); |
| 103 | |
| 104 | /* |
| 105 | * Calculate the RSA encryption of the hash. |
| 106 | */ |
| 107 | printf( "\n . Generating the RSA encrypted value" ); |
| 108 | fflush( stdout ); |
| 109 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame^] | 110 | if( ( ret = rsa_pkcs1_encrypt( &rsa, havege_rand, &hs, RSA_PUBLIC, strlen( argv[1] ), input, buf ) ) != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 111 | { |
| 112 | printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret ); |
| 113 | goto exit; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Write the signature into result-enc.txt |
| 118 | */ |
| 119 | if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) |
| 120 | { |
| 121 | ret = 1; |
| 122 | printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); |
| 123 | goto exit; |
| 124 | } |
| 125 | |
| 126 | for( i = 0; i < rsa.len; i++ ) |
| 127 | fprintf( f, "%02X%s", buf[i], |
| 128 | ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); |
| 129 | |
| 130 | fclose( f ); |
| 131 | |
| 132 | printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); |
| 133 | |
| 134 | exit: |
| 135 | |
| 136 | #ifdef WIN32 |
| 137 | printf( " + Press Enter to exit this program.\n" ); |
| 138 | fflush( stdout ); getchar(); |
| 139 | #endif |
| 140 | |
| 141 | return( ret ); |
| 142 | } |
| 143 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_HAVEGE_C && |
| 144 | POLARSSL_FS_IO */ |