1 /*
2  *  Diffie-Hellman-Merkle key exchange (prime generation)
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include "mbedtls/build_info.h"
9 
10 #include "mbedtls/platform.h"
11 
12 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \
13     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) ||     \
14     !defined(MBEDTLS_GENPRIME)
main(void)15 int main(void)
16 {
17     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
18                    "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
19                    "MBEDTLS_GENPRIME not defined.\n");
20     mbedtls_exit(0);
21 }
22 #else
23 
24 #include "mbedtls/bignum.h"
25 #include "mbedtls/entropy.h"
26 #include "mbedtls/ctr_drbg.h"
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #define USAGE \
32     "\n usage: dh_genprime param=<>...\n"                                   \
33     "\n acceptable parameters:\n"                                           \
34     "    bits=%%d           default: 2048\n"
35 
36 #define DFL_BITS    2048
37 
38 /*
39  * Note: G = 4 is always a quadratic residue mod P,
40  * so it is a generator of order Q (with P = 2*Q+1).
41  */
42 #define GENERATOR "4"
43 
44 
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47     int ret = 1;
48     int exit_code = MBEDTLS_EXIT_FAILURE;
49     mbedtls_mpi G, P, Q;
50     mbedtls_entropy_context entropy;
51     mbedtls_ctr_drbg_context ctr_drbg;
52     const char *pers = "dh_genprime";
53     FILE *fout;
54     int nbits = DFL_BITS;
55     int i;
56     char *p, *q;
57 
58     mbedtls_mpi_init(&G); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
59     mbedtls_ctr_drbg_init(&ctr_drbg);
60     mbedtls_entropy_init(&entropy);
61 
62     if (argc < 2) {
63 usage:
64         mbedtls_printf(USAGE);
65         goto exit;
66     }
67 
68     for (i = 1; i < argc; i++) {
69         p = argv[i];
70         if ((q = strchr(p, '=')) == NULL) {
71             goto usage;
72         }
73         *q++ = '\0';
74 
75         if (strcmp(p, "bits") == 0) {
76             nbits = atoi(q);
77             if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) {
78                 goto usage;
79             }
80         } else {
81             goto usage;
82         }
83     }
84 
85     if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) {
86         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_string returned %d\n", ret);
87         goto exit;
88     }
89 
90     mbedtls_printf("  ! Generating large primes may take minutes!\n");
91 
92     mbedtls_printf("\n  . Seeding the random number generator...");
93     fflush(stdout);
94 
95     if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
96                                      (const unsigned char *) pers,
97                                      strlen(pers))) != 0) {
98         mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret);
99         goto exit;
100     }
101 
102     mbedtls_printf(" ok\n  . Generating the modulus, please wait...");
103     fflush(stdout);
104 
105     /*
106      * This can take a long time...
107      */
108     if ((ret = mbedtls_mpi_gen_prime(&P, nbits, 1,
109                                      mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
110         mbedtls_printf(" failed\n  ! mbedtls_mpi_gen_prime returned %d\n\n", ret);
111         goto exit;
112     }
113 
114     mbedtls_printf(" ok\n  . Verifying that Q = (P-1)/2 is prime...");
115     fflush(stdout);
116 
117     if ((ret = mbedtls_mpi_sub_int(&Q, &P, 1)) != 0) {
118         mbedtls_printf(" failed\n  ! mbedtls_mpi_sub_int returned %d\n\n", ret);
119         goto exit;
120     }
121 
122     if ((ret = mbedtls_mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
123         mbedtls_printf(" failed\n  ! mbedtls_mpi_div_int returned %d\n\n", ret);
124         goto exit;
125     }
126 
127     if ((ret = mbedtls_mpi_is_prime_ext(&Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
128         mbedtls_printf(" failed\n  ! mbedtls_mpi_is_prime returned %d\n\n", ret);
129         goto exit;
130     }
131 
132     mbedtls_printf(" ok\n  . Exporting the value in dh_prime.txt...");
133     fflush(stdout);
134 
135     if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
136         mbedtls_printf(" failed\n  ! Could not create dh_prime.txt\n\n");
137         goto exit;
138     }
139 
140     if (((ret = mbedtls_mpi_write_file("P = ", &P, 16, fout)) != 0) ||
141         ((ret = mbedtls_mpi_write_file("G = ", &G, 16, fout)) != 0)) {
142         mbedtls_printf(" failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret);
143         fclose(fout);
144         goto exit;
145     }
146 
147     mbedtls_printf(" ok\n\n");
148     fclose(fout);
149 
150     exit_code = MBEDTLS_EXIT_SUCCESS;
151 
152 exit:
153 
154     mbedtls_mpi_free(&G); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
155     mbedtls_ctr_drbg_free(&ctr_drbg);
156     mbedtls_entropy_free(&entropy);
157 
158     mbedtls_exit(exit_code);
159 }
160 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
161           MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
162