blob: f5b45242019b020397affab9480c6ac878f1c076 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Simple MPI demonstration program
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
28#include <stdio.h>
29
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32int main( void )
33{
34 mpi E, P, Q, N, H, D, X, Y, Z;
35
36 mpi_init( &E, &P, &Q, &N, &H,
37 &D, &X, &Y, &Z, NULL );
38
39 mpi_read_string( &P, 10, "2789" );
40 mpi_read_string( &Q, 10, "3203" );
41 mpi_read_string( &E, 10, "257" );
42 mpi_mul_mpi( &N, &P, &Q );
43
44 printf( "\n Public key:\n\n" );
45 mpi_write_file( " N = ", &N, 10, NULL );
46 mpi_write_file( " E = ", &E, 10, NULL );
47
48 printf( "\n Private key:\n\n" );
49 mpi_write_file( " P = ", &P, 10, NULL );
50 mpi_write_file( " Q = ", &Q, 10, NULL );
51
52 mpi_sub_int( &P, &P, 1 );
53 mpi_sub_int( &Q, &Q, 1 );
54 mpi_mul_mpi( &H, &P, &Q );
55 mpi_inv_mod( &D, &E, &H );
56
57 mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
58 &D, 10, NULL );
59
60 mpi_read_string( &X, 10, "55555" );
61 mpi_exp_mod( &Y, &X, &E, &N, NULL );
62 mpi_exp_mod( &Z, &Y, &D, &N, NULL );
63
64 printf( "\n RSA operation:\n\n" );
65 mpi_write_file( " X (plaintext) = ", &X, 10, NULL );
66 mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
67 mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL );
68 printf( "\n" );
69
70 mpi_free( &Z, &Y, &X, &D, &H,
71 &N, &Q, &P, &E, NULL );
72
73#ifdef WIN32
74 printf( " Press Enter to exit this program.\n" );
75 fflush( stdout ); getchar();
76#endif
77
78 return( 0 );
79}