blob: b5ae13e076301985b8998a5cd9937f84c8ac4c6c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Simple MPI demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker5121ce52009-01-03 21:22:43 +000035#include <stdio.h>
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker5690efc2011-05-26 13:16:06 +000039#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000040int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000041{
Paul Bakkercce9d772011-11-18 14:26:47 +000042 ((void) argc);
43 ((void) argv);
44
Rich Evansf90016a2015-01-19 14:26:37 +000045 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000046 return( 0 );
47}
48#else
Paul Bakkercce9d772011-11-18 14:26:47 +000049int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000050{
51 mpi E, P, Q, N, H, D, X, Y, Z;
52
Paul Bakkercce9d772011-11-18 14:26:47 +000053 ((void) argc);
54 ((void) argv);
55
Paul Bakker6c591fa2011-05-05 11:49:20 +000056 mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N );
57 mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y );
58 mpi_init( &Z );
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60 mpi_read_string( &P, 10, "2789" );
61 mpi_read_string( &Q, 10, "3203" );
62 mpi_read_string( &E, 10, "257" );
63 mpi_mul_mpi( &N, &P, &Q );
64
Rich Evansf90016a2015-01-19 14:26:37 +000065 polarssl_printf( "\n Public key:\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000066 mpi_write_file( " N = ", &N, 10, NULL );
67 mpi_write_file( " E = ", &E, 10, NULL );
68
Rich Evansf90016a2015-01-19 14:26:37 +000069 polarssl_printf( "\n Private key:\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000070 mpi_write_file( " P = ", &P, 10, NULL );
71 mpi_write_file( " Q = ", &Q, 10, NULL );
72
Paul Bakker5690efc2011-05-26 13:16:06 +000073#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000074 mpi_sub_int( &P, &P, 1 );
75 mpi_sub_int( &Q, &Q, 1 );
76 mpi_mul_mpi( &H, &P, &Q );
77 mpi_inv_mod( &D, &E, &H );
78
79 mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
80 &D, 10, NULL );
Paul Bakker5690efc2011-05-26 13:16:06 +000081#else
Rich Evansf90016a2015-01-19 14:26:37 +000082 polarssl_printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000084 mpi_read_string( &X, 10, "55555" );
85 mpi_exp_mod( &Y, &X, &E, &N, NULL );
86 mpi_exp_mod( &Z, &Y, &D, &N, NULL );
87
Rich Evansf90016a2015-01-19 14:26:37 +000088 polarssl_printf( "\n RSA operation:\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000089 mpi_write_file( " X (plaintext) = ", &X, 10, NULL );
90 mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
91 mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL );
Rich Evansf90016a2015-01-19 14:26:37 +000092 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N );
95 mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y );
96 mpi_free( &Z );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Paul Bakkercce9d772011-11-18 14:26:47 +000098#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000099 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 fflush( stdout ); getchar();
101#endif
102
103 return( 0 );
104}
Paul Bakker5690efc2011-05-26 13:16:06 +0000105#endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */