gendss.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "signkey.h"
  27. #include "bignum.h"
  28. #include "dbrandom.h"
  29. #include "buffer.h"
  30. #include "gendss.h"
  31. #include "dss.h"
  32. #define QSIZE 20 /* 160 bit */
  33. /* This is just a test */
  34. #if DROPBEAR_DSS
  35. static void getq(const dropbear_dss_key *key);
  36. static void getp(const dropbear_dss_key *key, unsigned int size);
  37. static void getg(const dropbear_dss_key *key);
  38. static void getx(const dropbear_dss_key *key);
  39. static void gety(const dropbear_dss_key *key);
  40. dropbear_dss_key * gen_dss_priv_key(unsigned int size) {
  41. dropbear_dss_key *key;
  42. if (size != 1024) {
  43. dropbear_exit("DSS keys have a fixed size of 1024 bits");
  44. }
  45. key = m_malloc(sizeof(*key));
  46. m_mp_alloc_init_multi(&key->p, &key->q, &key->g, &key->y, &key->x, NULL);
  47. getq(key);
  48. getp(key, size/8);
  49. getg(key);
  50. getx(key);
  51. gety(key);
  52. return key;
  53. }
  54. static void getq(const dropbear_dss_key *key) {
  55. unsigned char buf[QSIZE];
  56. int trials;
  57. /* 160 bit prime */
  58. genrandom(buf, QSIZE);
  59. buf[0] |= 0x80; /* top bit high */
  60. buf[QSIZE-1] |= 0x01; /* bottom bit high */
  61. bytes_to_mp(key->q, buf, QSIZE);
  62. /* ask FIPS 186.4 how many Rabin-Miller trials are required */
  63. trials = mp_prime_rabin_miller_trials(mp_count_bits(key->q));
  64. if (mp_prime_next_prime(key->q, trials, 0) != MP_OKAY) {
  65. fprintf(stderr, "DSS key generation failed\n");
  66. exit(1);
  67. }
  68. }
  69. static void getp(const dropbear_dss_key *key, unsigned int size) {
  70. DEF_MP_INT(tempX);
  71. DEF_MP_INT(tempC);
  72. DEF_MP_INT(tempP);
  73. DEF_MP_INT(temp2q);
  74. int result, trials;
  75. unsigned char *buf;
  76. m_mp_init_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
  77. /* 2*q */
  78. if (mp_mul_d(key->q, 2, &temp2q) != MP_OKAY) {
  79. fprintf(stderr, "DSS key generation failed\n");
  80. exit(1);
  81. }
  82. buf = (unsigned char*)m_malloc(size);
  83. result = 0;
  84. do {
  85. genrandom(buf, size);
  86. buf[0] |= 0x80; /* set the top bit high */
  87. /* X is a random mp_int */
  88. bytes_to_mp(&tempX, buf, size);
  89. /* C = X mod 2q */
  90. if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) {
  91. fprintf(stderr, "DSS key generation failed\n");
  92. exit(1);
  93. }
  94. /* P = X - (C - 1) = X - C + 1*/
  95. if (mp_sub(&tempX, &tempC, &tempP) != MP_OKAY) {
  96. fprintf(stderr, "DSS key generation failed\n");
  97. exit(1);
  98. }
  99. if (mp_add_d(&tempP, 1, key->p) != MP_OKAY) {
  100. fprintf(stderr, "DSS key generation failed\n");
  101. exit(1);
  102. }
  103. /* ask FIPS 186.4 how many Rabin-Miller trials are required */
  104. trials = mp_prime_rabin_miller_trials(mp_count_bits(key->p));
  105. /* result == 1 => p is prime */
  106. if (mp_prime_is_prime(key->p, trials, &result) != MP_OKAY) {
  107. fprintf(stderr, "DSS key generation failed\n");
  108. exit(1);
  109. }
  110. } while (!result);
  111. mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
  112. m_burn(buf, size);
  113. m_free(buf);
  114. }
  115. static void getg(const dropbear_dss_key * key) {
  116. DEF_MP_INT(div);
  117. DEF_MP_INT(h);
  118. DEF_MP_INT(val);
  119. m_mp_init_multi(&div, &h, &val, NULL);
  120. /* get div=(p-1)/q */
  121. if (mp_sub_d(key->p, 1, &val) != MP_OKAY) {
  122. fprintf(stderr, "DSS key generation failed\n");
  123. exit(1);
  124. }
  125. if (mp_div(&val, key->q, &div, NULL) != MP_OKAY) {
  126. fprintf(stderr, "DSS key generation failed\n");
  127. exit(1);
  128. }
  129. /* initialise h=1 */
  130. mp_set(&h, 1);
  131. do {
  132. /* now keep going with g=h^div mod p, until g > 1 */
  133. if (mp_exptmod(&h, &div, key->p, key->g) != MP_OKAY) {
  134. fprintf(stderr, "DSS key generation failed\n");
  135. exit(1);
  136. }
  137. if (mp_add_d(&h, 1, &h) != MP_OKAY) {
  138. fprintf(stderr, "DSS key generation failed\n");
  139. exit(1);
  140. }
  141. } while (mp_cmp_d(key->g, 1) != MP_GT);
  142. mp_clear_multi(&div, &h, &val, NULL);
  143. }
  144. static void getx(const dropbear_dss_key *key) {
  145. gen_random_mpint(key->q, key->x);
  146. }
  147. static void gety(const dropbear_dss_key *key) {
  148. if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) {
  149. fprintf(stderr, "DSS key generation failed\n");
  150. exit(1);
  151. }
  152. }
  153. #endif /* DROPBEAR_DSS */