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

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

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4