| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Simple MPI demonstration program | 
|  | 3 | * | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2011, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 26 | #include "polarssl/config.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | #include <stdio.h> | 
|  | 29 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 30 | #include "polarssl/bignum.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 32 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 33 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 34 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 35 | ((void) argc); | 
|  | 36 | ((void) argv); | 
|  | 37 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 38 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n"); | 
|  | 39 | return( 0 ); | 
|  | 40 | } | 
|  | 41 | #else | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 42 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | { | 
|  | 44 | mpi E, P, Q, N, H, D, X, Y, Z; | 
|  | 45 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 46 | ((void) argc); | 
|  | 47 | ((void) argv); | 
|  | 48 |  | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 49 | mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N ); | 
|  | 50 | mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y ); | 
|  | 51 | mpi_init( &Z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 |  | 
|  | 53 | mpi_read_string( &P, 10, "2789" ); | 
|  | 54 | mpi_read_string( &Q, 10, "3203" ); | 
|  | 55 | mpi_read_string( &E, 10,  "257" ); | 
|  | 56 | mpi_mul_mpi( &N, &P, &Q ); | 
|  | 57 |  | 
|  | 58 | printf( "\n  Public key:\n\n" ); | 
|  | 59 | mpi_write_file( "  N = ", &N, 10, NULL ); | 
|  | 60 | mpi_write_file( "  E = ", &E, 10, NULL ); | 
|  | 61 |  | 
|  | 62 | printf( "\n  Private key:\n\n" ); | 
|  | 63 | mpi_write_file( "  P = ", &P, 10, NULL ); | 
|  | 64 | mpi_write_file( "  Q = ", &Q, 10, NULL ); | 
|  | 65 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 66 | #if defined(POLARSSL_GENPRIME) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | mpi_sub_int( &P, &P, 1 ); | 
|  | 68 | mpi_sub_int( &Q, &Q, 1 ); | 
|  | 69 | mpi_mul_mpi( &H, &P, &Q ); | 
|  | 70 | mpi_inv_mod( &D, &E, &H ); | 
|  | 71 |  | 
|  | 72 | mpi_write_file( "  D = E^-1 mod (P-1)*(Q-1) = ", | 
|  | 73 | &D, 10, NULL ); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 74 | #else | 
|  | 75 | printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n"); | 
|  | 76 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | mpi_read_string( &X, 10, "55555" ); | 
|  | 78 | mpi_exp_mod( &Y, &X, &E, &N, NULL ); | 
|  | 79 | mpi_exp_mod( &Z, &Y, &D, &N, NULL ); | 
|  | 80 |  | 
|  | 81 | printf( "\n  RSA operation:\n\n" ); | 
|  | 82 | mpi_write_file( "  X (plaintext)  = ", &X, 10, NULL ); | 
|  | 83 | mpi_write_file( "  Y (ciphertext) = X^E mod N = ", &Y, 10, NULL ); | 
|  | 84 | mpi_write_file( "  Z (decrypted)  = Y^D mod N = ", &Z, 10, NULL ); | 
|  | 85 | printf( "\n" ); | 
|  | 86 |  | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 87 | mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N ); | 
|  | 88 | mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y ); | 
|  | 89 | mpi_free( &Z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 91 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | printf( "  Press Enter to exit this program.\n" ); | 
|  | 93 | fflush( stdout ); getchar(); | 
|  | 94 | #endif | 
|  | 95 |  | 
|  | 96 | return( 0 ); | 
|  | 97 | } | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 98 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */ |