mpi_demo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Simple MPI demonstration program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_exit exit
  58. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  59. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  60. #endif /* MBEDTLS_PLATFORM_C */
  61. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
  62. #include "mbedtls/bignum.h"
  63. #include <stdio.h>
  64. #endif
  65. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_FS_IO)
  66. int main( void )
  67. {
  68. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
  69. mbedtls_exit( 0 );
  70. }
  71. #else
  72. int main( void )
  73. {
  74. int ret = 1;
  75. int exit_code = MBEDTLS_EXIT_FAILURE;
  76. mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
  77. mbedtls_mpi_init( &E ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &N );
  78. mbedtls_mpi_init( &H ); mbedtls_mpi_init( &D ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  79. mbedtls_mpi_init( &Z );
  80. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P, 10, "2789" ) );
  81. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &Q, 10, "3203" ) );
  82. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 10, "257" ) );
  83. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &N, &P, &Q ) );
  84. mbedtls_printf( "\n Public key:\n\n" );
  85. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " N = ", &N, 10, NULL ) );
  86. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " E = ", &E, 10, NULL ) );
  87. mbedtls_printf( "\n Private key:\n\n" );
  88. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " P = ", &P, 10, NULL ) );
  89. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Q = ", &Q, 10, NULL ) );
  90. #if defined(MBEDTLS_GENPRIME)
  91. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P, &P, 1 ) );
  92. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q, &Q, 1 ) );
  93. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P, &Q ) );
  94. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &D, &E, &H ) );
  95. mbedtls_mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
  96. &D, 10, NULL );
  97. #else
  98. mbedtls_printf("\nTest skipped (MBEDTLS_GENPRIME not defined).\n\n");
  99. #endif
  100. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &X, 10, "55555" ) );
  101. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &Y, &X, &E, &N, NULL ) );
  102. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &Z, &Y, &D, &N, NULL ) );
  103. mbedtls_printf( "\n RSA operation:\n\n" );
  104. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " X (plaintext) = ", &X, 10, NULL ) );
  105. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL ) );
  106. MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) );
  107. mbedtls_printf( "\n" );
  108. exit_code = MBEDTLS_EXIT_SUCCESS;
  109. cleanup:
  110. mbedtls_mpi_free( &E ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &N );
  111. mbedtls_mpi_free( &H ); mbedtls_mpi_free( &D ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  112. mbedtls_mpi_free( &Z );
  113. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  114. {
  115. mbedtls_printf( "\nAn error occurred.\n" );
  116. }
  117. #if defined(_WIN32)
  118. mbedtls_printf( " Press Enter to exit this program.\n" );
  119. fflush( stdout ); getchar();
  120. #endif
  121. mbedtls_exit( exit_code );
  122. }
  123. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */