dh_genprime.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (prime generation)
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  22. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
  23. !defined(MBEDTLS_GENPRIME)
  24. int main(void)
  25. {
  26. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  27. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
  28. "MBEDTLS_GENPRIME not defined.\n");
  29. mbedtls_exit(0);
  30. }
  31. #else
  32. #include "mbedtls/bignum.h"
  33. #include "mbedtls/entropy.h"
  34. #include "mbedtls/ctr_drbg.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #define USAGE \
  38. "\n usage: dh_genprime param=<>...\n" \
  39. "\n acceptable parameters:\n" \
  40. " bits=%%d default: 2048\n"
  41. #define DFL_BITS 2048
  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. int main(int argc, char **argv)
  48. {
  49. int ret = 1;
  50. int exit_code = MBEDTLS_EXIT_FAILURE;
  51. mbedtls_mpi G, P, Q;
  52. mbedtls_entropy_context entropy;
  53. mbedtls_ctr_drbg_context ctr_drbg;
  54. const char *pers = "dh_genprime";
  55. FILE *fout;
  56. int nbits = DFL_BITS;
  57. int i;
  58. char *p, *q;
  59. mbedtls_mpi_init(&G); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  60. mbedtls_ctr_drbg_init(&ctr_drbg);
  61. mbedtls_entropy_init(&entropy);
  62. if (argc < 2) {
  63. usage:
  64. mbedtls_printf(USAGE);
  65. goto exit;
  66. }
  67. for (i = 1; i < argc; i++) {
  68. p = argv[i];
  69. if ((q = strchr(p, '=')) == NULL) {
  70. goto usage;
  71. }
  72. *q++ = '\0';
  73. if (strcmp(p, "bits") == 0) {
  74. nbits = atoi(q);
  75. if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) {
  76. goto usage;
  77. }
  78. } else {
  79. goto usage;
  80. }
  81. }
  82. if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) {
  83. mbedtls_printf(" failed\n ! mbedtls_mpi_read_string returned %d\n", ret);
  84. goto exit;
  85. }
  86. mbedtls_printf(" ! Generating large primes may take minutes!\n");
  87. mbedtls_printf("\n . Seeding the random number generator...");
  88. fflush(stdout);
  89. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  90. (const unsigned char *) pers,
  91. strlen(pers))) != 0) {
  92. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  93. goto exit;
  94. }
  95. mbedtls_printf(" ok\n . Generating the modulus, please wait...");
  96. fflush(stdout);
  97. /*
  98. * This can take a long time...
  99. */
  100. if ((ret = mbedtls_mpi_gen_prime(&P, nbits, 1,
  101. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  102. mbedtls_printf(" failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret);
  103. goto exit;
  104. }
  105. mbedtls_printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
  106. fflush(stdout);
  107. if ((ret = mbedtls_mpi_sub_int(&Q, &P, 1)) != 0) {
  108. mbedtls_printf(" failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret);
  109. goto exit;
  110. }
  111. if ((ret = mbedtls_mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
  112. mbedtls_printf(" failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret);
  113. goto exit;
  114. }
  115. if ((ret = mbedtls_mpi_is_prime_ext(&Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  116. mbedtls_printf(" failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret);
  117. goto exit;
  118. }
  119. mbedtls_printf(" ok\n . Exporting the value in dh_prime.txt...");
  120. fflush(stdout);
  121. if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
  122. mbedtls_printf(" failed\n ! Could not create dh_prime.txt\n\n");
  123. goto exit;
  124. }
  125. if (((ret = mbedtls_mpi_write_file("P = ", &P, 16, fout)) != 0) ||
  126. ((ret = mbedtls_mpi_write_file("G = ", &G, 16, fout)) != 0)) {
  127. mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
  128. fclose(fout);
  129. goto exit;
  130. }
  131. mbedtls_printf(" ok\n\n");
  132. fclose(fout);
  133. exit_code = MBEDTLS_EXIT_SUCCESS;
  134. exit:
  135. mbedtls_mpi_free(&G); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  136. mbedtls_ctr_drbg_free(&ctr_drbg);
  137. mbedtls_entropy_free(&entropy);
  138. mbedtls_exit(exit_code);
  139. }
  140. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
  141. MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */