genrsa.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #include "dbutil.h"
  26. #include "bignum.h"
  27. #include "dbrandom.h"
  28. #include "rsa.h"
  29. #include "genrsa.h"
  30. #define RSA_E 65537
  31. #if DROPBEAR_RSA
  32. static void getrsaprime(mp_int* prime, mp_int *primeminus,
  33. mp_int* rsa_e, unsigned int size_bytes);
  34. /* mostly taken from libtomcrypt's rsa key generation routine */
  35. dropbear_rsa_key * gen_rsa_priv_key(unsigned int size) {
  36. dropbear_rsa_key * key;
  37. DEF_MP_INT(pminus);
  38. DEF_MP_INT(qminus);
  39. DEF_MP_INT(lcm);
  40. if (size < 512 || size > 4096 || (size % 8 != 0)) {
  41. dropbear_exit("Bits must satisfy 512 <= bits <= 4096, and be a"
  42. " multiple of 8");
  43. }
  44. key = m_malloc(sizeof(*key));
  45. m_mp_alloc_init_multi(&key->e, &key->n, &key->d, &key->p, &key->q, NULL);
  46. m_mp_init_multi(&pminus, &lcm, &qminus, NULL);
  47. mp_set_ul(key->e, RSA_E);
  48. while (1) {
  49. getrsaprime(key->p, &pminus, key->e, size/16);
  50. getrsaprime(key->q, &qminus, key->e, size/16);
  51. if (mp_mul(key->p, key->q, key->n) != MP_OKAY) {
  52. fprintf(stderr, "RSA generation failed\n");
  53. exit(1);
  54. }
  55. if ((unsigned int)mp_count_bits(key->n) == size) {
  56. break;
  57. }
  58. }
  59. /* lcm(p-1, q-1) */
  60. if (mp_lcm(&pminus, &qminus, &lcm) != MP_OKAY) {
  61. fprintf(stderr, "RSA generation failed\n");
  62. exit(1);
  63. }
  64. /* de = 1 mod lcm(p-1,q-1) */
  65. /* therefore d = (e^-1) mod lcm(p-1,q-1) */
  66. if (mp_invmod(key->e, &lcm, key->d) != MP_OKAY) {
  67. fprintf(stderr, "RSA generation failed\n");
  68. exit(1);
  69. }
  70. mp_clear_multi(&pminus, &qminus, &lcm, NULL);
  71. return key;
  72. }
  73. /* return a prime suitable for p or q */
  74. static void getrsaprime(mp_int* prime, mp_int *primeminus,
  75. mp_int* rsa_e, unsigned int size_bytes) {
  76. unsigned char *buf;
  77. int trials;
  78. DEF_MP_INT(temp_gcd);
  79. buf = (unsigned char*)m_malloc(size_bytes);
  80. m_mp_init(&temp_gcd);
  81. do {
  82. /* generate a random odd number with MSB set, then find the
  83. the next prime above it */
  84. genrandom(buf, size_bytes);
  85. buf[0] |= 0x80;
  86. bytes_to_mp(prime, buf, size_bytes);
  87. /* find the next integer which is prime */
  88. trials = mp_prime_rabin_miller_trials(mp_count_bits(prime));
  89. if (mp_prime_next_prime(prime, trials, 0) != MP_OKAY) {
  90. fprintf(stderr, "RSA generation failed\n");
  91. exit(1);
  92. }
  93. /* subtract one to get p-1 */
  94. if (mp_sub_d(prime, 1, primeminus) != MP_OKAY) {
  95. fprintf(stderr, "RSA generation failed\n");
  96. exit(1);
  97. }
  98. /* check relative primality to e */
  99. if (mp_gcd(primeminus, rsa_e, &temp_gcd) != MP_OKAY) {
  100. fprintf(stderr, "RSA generation failed\n");
  101. exit(1);
  102. }
  103. } while (mp_cmp_d(&temp_gcd, 1) != MP_EQ); /* while gcd(p-1, e) != 1 */
  104. /* now we have a good value for result */
  105. mp_clear(&temp_gcd);
  106. m_burn(buf, size_bytes);
  107. m_free(buf);
  108. }
  109. #endif /* DROPBEAR_RSA */