gendss.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifdef DROPBEAR_DSS
  35. static void getq(dropbear_dss_key *key);
  36. static void getp(dropbear_dss_key *key, unsigned int size);
  37. static void getg(dropbear_dss_key *key);
  38. static void getx(dropbear_dss_key *key);
  39. static void gety(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(dropbear_dss_key *key) {
  55. unsigned char buf[QSIZE];
  56. /* 160 bit prime */
  57. genrandom(buf, QSIZE);
  58. buf[0] |= 0x80; /* top bit high */
  59. buf[QSIZE-1] |= 0x01; /* bottom bit high */
  60. bytes_to_mp(key->q, buf, QSIZE);
  61. /* 18 rounds are required according to HAC */
  62. if (mp_prime_next_prime(key->q, 18, 0) != MP_OKAY) {
  63. fprintf(stderr, "DSS key generation failed\n");
  64. exit(1);
  65. }
  66. }
  67. static void getp(dropbear_dss_key *key, unsigned int size) {
  68. DEF_MP_INT(tempX);
  69. DEF_MP_INT(tempC);
  70. DEF_MP_INT(tempP);
  71. DEF_MP_INT(temp2q);
  72. int result;
  73. unsigned char *buf;
  74. m_mp_init_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
  75. /* 2*q */
  76. if (mp_mul_d(key->q, 2, &temp2q) != MP_OKAY) {
  77. fprintf(stderr, "DSS key generation failed\n");
  78. exit(1);
  79. }
  80. buf = (unsigned char*)m_malloc(size);
  81. result = 0;
  82. do {
  83. genrandom(buf, size);
  84. buf[0] |= 0x80; /* set the top bit high */
  85. /* X is a random mp_int */
  86. bytes_to_mp(&tempX, buf, size);
  87. /* C = X mod 2q */
  88. if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) {
  89. fprintf(stderr, "DSS key generation failed\n");
  90. exit(1);
  91. }
  92. /* P = X - (C - 1) = X - C + 1*/
  93. if (mp_sub(&tempX, &tempC, &tempP) != MP_OKAY) {
  94. fprintf(stderr, "DSS key generation failed\n");
  95. exit(1);
  96. }
  97. if (mp_add_d(&tempP, 1, key->p) != MP_OKAY) {
  98. fprintf(stderr, "DSS key generation failed\n");
  99. exit(1);
  100. }
  101. /* now check for prime, 5 rounds is enough according to HAC */
  102. /* result == 1 => p is prime */
  103. if (mp_prime_is_prime(key->p, 5, &result) != MP_OKAY) {
  104. fprintf(stderr, "DSS key generation failed\n");
  105. exit(1);
  106. }
  107. } while (!result);
  108. mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
  109. m_burn(buf, size);
  110. m_free(buf);
  111. }
  112. static void getg(dropbear_dss_key * key) {
  113. DEF_MP_INT(div);
  114. DEF_MP_INT(h);
  115. DEF_MP_INT(val);
  116. m_mp_init_multi(&div, &h, &val, NULL);
  117. /* get div=(p-1)/q */
  118. if (mp_sub_d(key->p, 1, &val) != MP_OKAY) {
  119. fprintf(stderr, "DSS key generation failed\n");
  120. exit(1);
  121. }
  122. if (mp_div(&val, key->q, &div, NULL) != MP_OKAY) {
  123. fprintf(stderr, "DSS key generation failed\n");
  124. exit(1);
  125. }
  126. /* initialise h=1 */
  127. mp_set(&h, 1);
  128. do {
  129. /* now keep going with g=h^div mod p, until g > 1 */
  130. if (mp_exptmod(&h, &div, key->p, key->g) != MP_OKAY) {
  131. fprintf(stderr, "DSS key generation failed\n");
  132. exit(1);
  133. }
  134. if (mp_add_d(&h, 1, &h) != MP_OKAY) {
  135. fprintf(stderr, "DSS key generation failed\n");
  136. exit(1);
  137. }
  138. } while (mp_cmp_d(key->g, 1) != MP_GT);
  139. mp_clear_multi(&div, &h, &val, NULL);
  140. }
  141. static void getx(dropbear_dss_key *key) {
  142. gen_random_mpint(key->q, key->x);
  143. }
  144. static void gety(dropbear_dss_key *key) {
  145. if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) {
  146. fprintf(stderr, "DSS key generation failed\n");
  147. exit(1);
  148. }
  149. }
  150. #endif /* DROPBEAR_DSS */