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
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
17 #include "mbedtls/bignum.h"
18
19 #include <stdio.h>
20 #endif
21
22 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_FS_IO)
main(void)23 int main(void)
24 {
25 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
26 mbedtls_exit(0);
27 }
28 #else
29
30
main(void)31 int main(void)
32 {
33 int ret = 1;
34 int exit_code = MBEDTLS_EXIT_FAILURE;
35 mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
36
37 mbedtls_mpi_init(&E); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); mbedtls_mpi_init(&N);
38 mbedtls_mpi_init(&H); mbedtls_mpi_init(&D); mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
39 mbedtls_mpi_init(&Z);
40
41 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P, 10, "2789"));
42 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&Q, 10, "3203"));
43 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 10, "257"));
44 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&N, &P, &Q));
45
46 mbedtls_printf("\n Public key:\n\n");
47 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" N = ", &N, 10, NULL));
48 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" E = ", &E, 10, NULL));
49
50 mbedtls_printf("\n Private key:\n\n");
51 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" P = ", &P, 10, NULL));
52 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Q = ", &Q, 10, NULL));
53
54 #if defined(MBEDTLS_GENPRIME)
55 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P, &P, 1));
56 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q, &Q, 1));
57 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &P, &Q));
58 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&D, &E, &H));
59
60 mbedtls_mpi_write_file(" D = E^-1 mod (P-1)*(Q-1) = ",
61 &D, 10, NULL);
62 #else
63 mbedtls_printf("\nTest skipped (MBEDTLS_GENPRIME not defined).\n\n");
64 #endif
65 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&X, 10, "55555"));
66 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&Y, &X, &E, &N, NULL));
67 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&Z, &Y, &D, &N, NULL));
68
69 mbedtls_printf("\n RSA operation:\n\n");
70 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" X (plaintext) = ", &X, 10, NULL));
71 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Y (ciphertext) = X^E mod N = ", &Y, 10, NULL));
72 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Z (decrypted) = Y^D mod N = ", &Z, 10, NULL));
73 mbedtls_printf("\n");
74
75 exit_code = MBEDTLS_EXIT_SUCCESS;
76
77 cleanup:
78 mbedtls_mpi_free(&E); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); mbedtls_mpi_free(&N);
79 mbedtls_mpi_free(&H); mbedtls_mpi_free(&D); mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
80 mbedtls_mpi_free(&Z);
81
82 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
83 mbedtls_printf("\nAn error occurred.\n");
84 }
85
86 #if defined(_WIN32)
87 mbedtls_printf(" Press Enter to exit this program.\n");
88 fflush(stdout); getchar();
89 #endif
90
91 mbedtls_exit(exit_code);
92 }
93 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */
94