source: trunk/programs/pkey/mpi_demo.c @ 1122

Revision 1122, 3.0 KB checked in by paul, 6 months ago (diff)
  • Lots of minimal changes to better support WINCE as a build target
Line 
1/*
2 *  Simple MPI demonstration program
3 *
4 *  Copyright (C) 2006-2011, Brainspark B.V.
5 *
6 *  This file is part of PolarSSL (http://www.polarssl.org)
7 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 *  All rights reserved.
10 *
11 *  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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <stdio.h>
31
32#include "polarssl/config.h"
33#include "polarssl/bignum.h"
34
35#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO)
36int main( int argc, char *argv[] )
37{
38    ((void) argc);
39    ((void) argv);
40
41    printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n");
42    return( 0 );
43}
44#else
45int main( int argc, char *argv[] )
46{
47    mpi E, P, Q, N, H, D, X, Y, Z;
48
49    ((void) argc);
50    ((void) argv);
51
52    mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N );
53    mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y );
54    mpi_init( &Z );
55
56    mpi_read_string( &P, 10, "2789" );
57    mpi_read_string( &Q, 10, "3203" );
58    mpi_read_string( &E, 10,  "257" );
59    mpi_mul_mpi( &N, &P, &Q );
60
61    printf( "\n  Public key:\n\n" );
62    mpi_write_file( "  N = ", &N, 10, NULL );
63    mpi_write_file( "  E = ", &E, 10, NULL );
64
65    printf( "\n  Private key:\n\n" );
66    mpi_write_file( "  P = ", &P, 10, NULL );
67    mpi_write_file( "  Q = ", &Q, 10, NULL );
68
69#if defined(POLARSSL_GENPRIME)
70    mpi_sub_int( &P, &P, 1 );
71    mpi_sub_int( &Q, &Q, 1 );
72    mpi_mul_mpi( &H, &P, &Q );
73    mpi_inv_mod( &D, &E, &H );
74
75    mpi_write_file( "  D = E^-1 mod (P-1)*(Q-1) = ",
76                    &D, 10, NULL );
77#else
78    printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n");
79#endif
80    mpi_read_string( &X, 10, "55555" );
81    mpi_exp_mod( &Y, &X, &E, &N, NULL );
82    mpi_exp_mod( &Z, &Y, &D, &N, NULL );
83
84    printf( "\n  RSA operation:\n\n" );
85    mpi_write_file( "  X (plaintext)  = ", &X, 10, NULL );
86    mpi_write_file( "  Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
87    mpi_write_file( "  Z (decrypted)  = Y^D mod N = ", &Z, 10, NULL );
88    printf( "\n" );
89
90    mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N );
91    mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y );
92    mpi_free( &Z );
93
94#if defined(_WIN32)
95    printf( "  Press Enter to exit this program.\n" );
96    fflush( stdout ); getchar();
97#endif
98
99    return( 0 );
100}
101#endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?