mpi_demo.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Simple MPI demonstration program
  3. *
  4. * Copyright (C) 2006-2010, 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. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #ifdef PRINTF_STDLIB
  29. #include <stdio.h>
  30. #endif
  31. #ifdef PRINTF_CUSTOM
  32. #include "tinystdio.h"
  33. #endif
  34. #include "polarssl/config.h"
  35. #include "polarssl/bignum.h"
  36. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO)
  37. int main( void )
  38. {
  39. printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n");
  40. return( 0 );
  41. }
  42. #else
  43. int main( void )
  44. {
  45. mpi E, P, Q, N, H, D, X, Y, Z;
  46. mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N );
  47. mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y );
  48. mpi_init( &Z );
  49. mpi_read_string( &P, 10, "2789" );
  50. mpi_read_string( &Q, 10, "3203" );
  51. mpi_read_string( &E, 10, "257" );
  52. mpi_mul_mpi( &N, &P, &Q );
  53. printf( "\n Public key:\n\n" );
  54. mpi_write_file( " N = ", &N, 10, NULL );
  55. mpi_write_file( " E = ", &E, 10, NULL );
  56. printf( "\n Private key:\n\n" );
  57. mpi_write_file( " P = ", &P, 10, NULL );
  58. mpi_write_file( " Q = ", &Q, 10, NULL );
  59. #if defined(POLARSSL_GENPRIME)
  60. mpi_sub_int( &P, &P, 1 );
  61. mpi_sub_int( &Q, &Q, 1 );
  62. mpi_mul_mpi( &H, &P, &Q );
  63. mpi_inv_mod( &D, &E, &H );
  64. mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
  65. &D, 10, NULL );
  66. #else
  67. printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n");
  68. #endif
  69. mpi_read_string( &X, 10, "55555" );
  70. mpi_exp_mod( &Y, &X, &E, &N, NULL );
  71. mpi_exp_mod( &Z, &Y, &D, &N, NULL );
  72. printf( "\n RSA operation:\n\n" );
  73. mpi_write_file( " X (plaintext) = ", &X, 10, NULL );
  74. mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL );
  75. mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL );
  76. printf( "\n" );
  77. mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N );
  78. mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y );
  79. mpi_free( &Z );
  80. #ifdef WIN32
  81. printf( " Press Enter to exit this program.\n" );
  82. fflush( stdout ); getchar();
  83. #endif
  84. return( 0 );
  85. }
  86. #endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */