Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Simple MPI demonstration program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 36 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 37 | #include "mbedtls/bignum.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 39 | #include <stdio.h> |
| 40 | #endif |
| 41 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 42 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 43 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 44 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 45 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | return( 0 ); |
| 47 | } |
| 48 | #else |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 49 | int main( void ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | { |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 51 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | mpi E, P, Q, N, H, D, X, Y, Z; |
| 53 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 54 | mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N ); |
| 55 | mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y ); |
| 56 | mpi_init( &Z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 58 | MPI_CHK( mpi_read_string( &P, 10, "2789" ) ); |
| 59 | MPI_CHK( mpi_read_string( &Q, 10, "3203" ) ); |
| 60 | MPI_CHK( mpi_read_string( &E, 10, "257" ) ); |
| 61 | MPI_CHK( mpi_mul_mpi( &N, &P, &Q ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 63 | polarssl_printf( "\n Public key:\n\n" ); |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 64 | MPI_CHK( mpi_write_file( " N = ", &N, 10, NULL ) ); |
| 65 | MPI_CHK( mpi_write_file( " E = ", &E, 10, NULL ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 67 | polarssl_printf( "\n Private key:\n\n" ); |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 68 | MPI_CHK( mpi_write_file( " P = ", &P, 10, NULL ) ); |
| 69 | MPI_CHK( mpi_write_file( " Q = ", &Q, 10, NULL ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 71 | #if defined(POLARSSL_GENPRIME) |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 72 | MPI_CHK( mpi_sub_int( &P, &P, 1 ) ); |
| 73 | MPI_CHK( mpi_sub_int( &Q, &Q, 1 ) ); |
| 74 | MPI_CHK( mpi_mul_mpi( &H, &P, &Q ) ); |
| 75 | MPI_CHK( mpi_inv_mod( &D, &E, &H ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
| 77 | mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ", |
| 78 | &D, 10, NULL ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 79 | #else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 80 | polarssl_printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 81 | #endif |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 82 | MPI_CHK( mpi_read_string( &X, 10, "55555" ) ); |
| 83 | MPI_CHK( mpi_exp_mod( &Y, &X, &E, &N, NULL ) ); |
| 84 | MPI_CHK( mpi_exp_mod( &Z, &Y, &D, &N, NULL ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 86 | polarssl_printf( "\n RSA operation:\n\n" ); |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 87 | MPI_CHK( mpi_write_file( " X (plaintext) = ", &X, 10, NULL ) ); |
| 88 | MPI_CHK( mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL ) ); |
| 89 | MPI_CHK( mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 90 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 92 | cleanup: |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 93 | mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N ); |
| 94 | mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y ); |
| 95 | mpi_free( &Z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 97 | if( ret != 0 ) |
| 98 | { |
| 99 | polarssl_printf( "\nAn error occured.\n" ); |
| 100 | ret = 1; |
| 101 | } |
| 102 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 103 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 104 | polarssl_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | fflush( stdout ); getchar(); |
| 106 | #endif |
| 107 | |
Manuel Pégourié-Gonnard | f53df4f | 2015-02-14 15:48:23 +0000 | [diff] [blame] | 108 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 110 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */ |