bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / programs / pkey / mpi_demo.c
blob2385cdff18180639c29452ccdb38b8c24a4cb0fb
1 /*
2 * Simple MPI demonstration program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
38 #endif
40 #include <stdio.h>
42 #include "tropicssl/bignum.h"
44 int main(void)
46 mpi E, P, Q, N, H, D, X, Y, Z;
48 mpi_init(&E); mpi_init(&P); mpi_init(&Q); mpi_init(&N);
49 mpi_init(&H); mpi_init(&D); mpi_init(&X); mpi_init(&Y);
50 mpi_init(&Z);
52 mpi_read_string(&P, 10, "2789");
53 mpi_read_string(&Q, 10, "3203");
54 mpi_read_string(&E, 10, "257");
55 mpi_mul_mpi(&N, &P, &Q);
57 printf("\n Public key:\n\n");
58 mpi_write_file(" N = ", &N, 10, NULL);
59 mpi_write_file(" E = ", &E, 10, NULL);
61 printf("\n Private key:\n\n");
62 mpi_write_file(" P = ", &P, 10, NULL);
63 mpi_write_file(" Q = ", &Q, 10, NULL);
65 mpi_sub_int(&P, &P, 1);
66 mpi_sub_int(&Q, &Q, 1);
67 mpi_mul_mpi(&H, &P, &Q);
68 mpi_inv_mod(&D, &E, &H);
70 mpi_write_file(" D = E^-1 mod (P-1)*(Q-1) = ", &D, 10, NULL);
72 mpi_read_string(&X, 10, "55555");
73 mpi_exp_mod(&Y, &X, &E, &N, NULL);
74 mpi_exp_mod(&Z, &Y, &D, &N, NULL);
76 printf("\n RSA operation:\n\n");
77 mpi_write_file(" X (plaintext) = ", &X, 10, NULL);
78 mpi_write_file(" Y (ciphertext) = X^E mod N = ", &Y, 10, NULL);
79 mpi_write_file(" Z (decrypted) = Y^D mod N = ", &Z, 10, NULL);
80 printf("\n");
82 mpi_free(&Z); mpi_free(&Y); mpi_free(&X); mpi_free(&D);
83 mpi_free(&H); mpi_free(&N); mpi_free(&Q); mpi_free(&P);
84 mpi_free(&E);
86 #ifdef WIN32
87 printf(" Press Enter to exit this program.\n");
88 fflush(stdout);
89 getchar();
90 #endif
92 return (0);