blob: a9c3190bf3f741fbc67280bb207a60bbe5db118b [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
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000016
Rich Evans18b78c72015-02-11 14:06:19 +000017#include <stdio.h>
18#endif
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010021int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010024 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000025}
26#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010027
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028
Gilles Peskine449bd832023-01-11 14:50:10 +010029int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000030{
Andres Amaya Garciad905db62018-04-29 22:12:21 +010031 int ret = 1;
32 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Gilles Peskine449bd832023-01-11 14:50:10 +010035 mbedtls_mpi_init(&E); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); mbedtls_mpi_init(&N);
36 mbedtls_mpi_init(&H); mbedtls_mpi_init(&D); mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
37 mbedtls_mpi_init(&Z);
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Gilles Peskine449bd832023-01-11 14:50:10 +010039 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P, 10, "2789"));
40 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&Q, 10, "3203"));
41 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 10, "257"));
42 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&N, &P, &Q));
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_printf("\n Public key:\n\n");
45 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" N = ", &N, 10, NULL));
46 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" E = ", &E, 10, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_printf("\n Private key:\n\n");
49 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" P = ", &P, 10, NULL));
50 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Q = ", &Q, 10, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010053 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P, &P, 1));
54 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q, &Q, 1));
55 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&H, &P, &Q));
56 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&D, &E, &H));
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 mbedtls_mpi_write_file(" D = E^-1 mod (P-1)*(Q-1) = ",
59 &D, 10, NULL);
Paul Bakker5690efc2011-05-26 13:16:06 +000060#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_printf("\nTest skipped (MBEDTLS_GENPRIME not defined).\n\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000062#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010063 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&X, 10, "55555"));
64 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&Y, &X, &E, &N, NULL));
65 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&Z, &Y, &D, &N, NULL));
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_printf("\n RSA operation:\n\n");
68 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" X (plaintext) = ", &X, 10, NULL));
69 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Y (ciphertext) = X^E mod N = ", &Y, 10, NULL));
70 MBEDTLS_MPI_CHK(mbedtls_mpi_write_file(" Z (decrypted) = Y^D mod N = ", &Z, 10, NULL));
71 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Andres Amaya Garciad905db62018-04-29 22:12:21 +010073 exit_code = MBEDTLS_EXIT_SUCCESS;
74
Manuel Pégourié-Gonnardf53df4f2015-02-14 15:48:23 +000075cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_mpi_free(&E); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); mbedtls_mpi_free(&N);
77 mbedtls_mpi_free(&H); mbedtls_mpi_free(&D); mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
78 mbedtls_mpi_free(&Z);
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
81 mbedtls_printf("\nAn error occurred.\n");
Manuel Pégourié-Gonnardf53df4f2015-02-14 15:48:23 +000082 }
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +000085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */