blob: 2e9bf934c411f95b800c5fb8a9867a9780e162a5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Simple MPI demonstration program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <stdio.h>
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker5690efc2011-05-26 13:16:06 +000036#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000037int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000038{
Paul Bakkercce9d772011-11-18 14:26:47 +000039 ((void) argc);
40 ((void) argv);
41
Paul Bakker5690efc2011-05-26 13:16:06 +000042 printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n");
43 return( 0 );
44}
45#else
Paul Bakkercce9d772011-11-18 14:26:47 +000046int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000047{
48 mpi E, P, Q, N, H, D, X, Y, Z;
49
Paul Bakkercce9d772011-11-18 14:26:47 +000050 ((void) argc);
51 ((void) argv);
52
Paul Bakker6c591fa2011-05-05 11:49:20 +000053 mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N );
54 mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y );
55 mpi_init( &Z );
Paul Bakker5121ce52009-01-03 21:22:43 +000056
57 mpi_read_string( &P, 10, "2789" );
58 mpi_read_string( &Q, 10, "3203" );
59 mpi_read_string( &E, 10, "257" );
60 mpi_mul_mpi( &N, &P, &Q );
61
62 printf( "\n Public key:\n\n" );
63 mpi_write_file( " N = ", &N, 10, NULL );
64 mpi_write_file( " E = ", &E, 10, NULL );
65
66 printf( "\n Private key:\n\n" );
67 mpi_write_file( " P = ", &P, 10, NULL );
68 mpi_write_file( " Q = ", &Q, 10, NULL );
69
Paul Bakker5690efc2011-05-26 13:16:06 +000070#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000071 mpi_sub_int( &P, &P, 1 );
72 mpi_sub_int( &Q, &Q, 1 );
73 mpi_mul_mpi( &H, &P, &Q );
74 mpi_inv_mod( &D, &E, &H );
75
76 mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
77 &D, 10, NULL );
Paul Bakker5690efc2011-05-26 13:16:06 +000078#else
79 printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n");
80#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000081 mpi_read_string( &X, 10, "55555" );
82 mpi_exp_mod( &Y, &X, &E, &N, NULL );
83 mpi_exp_mod( &Z, &Y, &D, &N, NULL );
84
85 printf( "\n RSA operation:\n\n" );
86 mpi_write_file( " X (plaintext) = ", &X, 10, NULL );
87 mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
88 mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL );
89 printf( "\n" );
90
Paul Bakker6c591fa2011-05-05 11:49:20 +000091 mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N );
92 mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y );
93 mpi_free( &Z );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakkercce9d772011-11-18 14:26:47 +000095#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000096 printf( " Press Enter to exit this program.\n" );
97 fflush( stdout ); getchar();
98#endif
99
100 return( 0 );
101}
Paul Bakker5690efc2011-05-26 13:16:06 +0000102#endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */