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