/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/pkey/mpi_demo.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 174 $ : Last Committed Revision
00003  * $Date: 2008-06-24 10:50:29 -0700 (Tue, 24 Jun 2008) $ : Last Committed Date */
00004 /*
00005  *  Simple MPI demonstration program
00006  *
00007  *  Copyright (C) 2006-2007  Christophe Devine
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License, version 2.1 as published by the Free Software Foundation.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021  *  MA  02110-1301  USA
00022  */
00023 
00024 #ifndef _CRT_SECURE_NO_DEPRECATE
00025 #define _CRT_SECURE_NO_DEPRECATE 1
00026 #endif
00027 
00028 #include <stdio.h>
00029 
00030 #include "xyssl/bignum.h"
00031 
00032 int main( void )
00033 {
00034     mpi E, P, Q, N, H, D, X, Y, Z;
00035 
00036     mpi_init( &E, &P, &Q, &N, &H,
00037               &D, &X, &Y, &Z, NULL );
00038 
00039     mpi_read_string( &P, 10, "2789" );
00040     mpi_read_string( &Q, 10, "3203" );
00041     mpi_read_string( &E, 10,  "257" );
00042     mpi_mul_mpi( &N, &P, &Q );
00043 
00044     printf( "\n  Public key:\n\n" );
00045     mpi_write_file( "  N = ", &N, 10, NULL );
00046     mpi_write_file( "  E = ", &E, 10, NULL );
00047 
00048     printf( "\n  Private key:\n\n" );
00049     mpi_write_file( "  P = ", &P, 10, NULL );
00050     mpi_write_file( "  Q = ", &Q, 10, NULL );
00051 
00052     mpi_sub_int( &P, &P, 1 );
00053     mpi_sub_int( &Q, &Q, 1 );
00054     mpi_mul_mpi( &H, &P, &Q );
00055     mpi_inv_mod( &D, &E, &H );
00056 
00057     mpi_write_file( "  D = E^-1 mod (P-1)*(Q-1) = ",
00058                     &D, 10, NULL );
00059 
00060     mpi_read_string( &X, 10, "55555" );
00061     mpi_exp_mod( &Y, &X, &E, &N, NULL );
00062     mpi_exp_mod( &Z, &Y, &D, &N, NULL );
00063 
00064     printf( "\n  RSA operation:\n\n" );
00065     mpi_write_file( "  X (plaintext)  = ", &X, 10, NULL );
00066     mpi_write_file( "  Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
00067     mpi_write_file( "  Z (decrypted)  = Y^D mod N = ", &Z, 10, NULL );
00068     printf( "\n" );
00069 
00070     mpi_free( &Z, &Y, &X, &D, &H,
00071               &N, &Q, &P, &E, NULL );
00072 
00073 #ifdef WIN32
00074     printf( "  Press Enter to exit this program.\n" );
00075     fflush( stdout ); getchar();
00076 #endif
00077 
00078     return( 0 );
00079 }

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4