blob: e83aa3259ceb9d7c2b13acde2bfeda3a31ec3b2a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Simple MPI demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000013#include "mbedtls/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000014
Rich Evans18b78c72015-02-11 14:06:19 +000015#include <stdio.h>
16#endif
17
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010019int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000023}
24#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010025
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010026
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000028{
Andres Amaya Garciad905db62018-04-29 22:12:21 +010029 int ret = 1;
30 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 mbedtls_mpi_init(&E); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); mbedtls_mpi_init(&N);
34 mbedtls_mpi_init(&H); mbedtls_mpi_init(&D); mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
35 mbedtls_mpi_init(&Z);
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Gilles Peskine449bd832023-01-11 14:50:10 +010037 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P, 10, "2789"));
38 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&Q, 10, "3203"));
39 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 10, "257"));
40 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&N, &P, &Q));
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_printf("\n Public key:\n\n");
43 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" N = ", &N, 10, NULL));
44 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" E = ", &E, 10, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 mbedtls_printf("\n Private key:\n\n");
47 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" P = ", &P, 10, NULL));
48 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Q = ", &Q, 10, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010051 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P, &P, 1));
52 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q, &Q, 1));
53 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &P, &Q));
54 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&D, &E, &H));
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 mbedtls_mpi_write_file(" D = E^-1 mod (P-1)*(Q-1) = ",
57 &D, 10, NULL);
Paul Bakker5690efc2011-05-26 13:16:06 +000058#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_printf("\nTest skipped (MBEDTLS_GENPRIME not defined).\n\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000060#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010061 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&X, 10, "55555"));
62 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&Y, &X, &E, &N, NULL));
63 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&Z, &Y, &D, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Gilles Peskine449bd832023-01-11 14:50:10 +010065 mbedtls_printf("\n RSA operation:\n\n");
66 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" X (plaintext) = ", &X, 10, NULL));
67 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Y (ciphertext) = X^E mod N = ", &Y, 10, NULL));
68 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Z (decrypted) = Y^D mod N = ", &Z, 10, NULL));
69 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Andres Amaya Garciad905db62018-04-29 22:12:21 +010071 exit_code = MBEDTLS_EXIT_SUCCESS;
72
Manuel Pégourié-Gonnardf53df4f2015-02-14 15:48:23 +000073cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_mpi_free(&E); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); mbedtls_mpi_free(&N);
75 mbedtls_mpi_free(&H); mbedtls_mpi_free(&D); mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
76 mbedtls_mpi_free(&Z);
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
79 mbedtls_printf("\nAn error occurred.\n");
Manuel Pégourié-Gonnardf53df4f2015-02-14 15:48:23 +000080 }
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +000083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */