dss.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "dss.h"
  28. #include "buffer.h"
  29. #include "ssh.h"
  30. #include "dbrandom.h"
  31. /* Handle DSS (Digital Signature Standard), aka DSA (D.S. Algorithm),
  32. * operations, such as key reading, signing, verification. Key generation
  33. * is in gendss.c, since it isn't required in the server itself.
  34. *
  35. * See FIPS186 or the Handbook of Applied Cryptography for details of the
  36. * algorithm */
  37. #ifdef DROPBEAR_DSS
  38. /* Load a dss key from a buffer, initialising the values.
  39. * The key will have the same format as buf_put_dss_key.
  40. * These should be freed with dss_key_free.
  41. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  42. int buf_get_dss_pub_key(buffer* buf, dropbear_dss_key *key) {
  43. TRACE(("enter buf_get_dss_pub_key"))
  44. dropbear_assert(key != NULL);
  45. m_mp_alloc_init_multi(&key->p, &key->q, &key->g, &key->y, NULL);
  46. key->x = NULL;
  47. buf_incrpos(buf, 4+SSH_SIGNKEY_DSS_LEN); /* int + "ssh-dss" */
  48. if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE
  49. || buf_getmpint(buf, key->q) == DROPBEAR_FAILURE
  50. || buf_getmpint(buf, key->g) == DROPBEAR_FAILURE
  51. || buf_getmpint(buf, key->y) == DROPBEAR_FAILURE) {
  52. TRACE(("leave buf_get_dss_pub_key: failed reading mpints"))
  53. return DROPBEAR_FAILURE;
  54. }
  55. if (mp_count_bits(key->p) < MIN_DSS_KEYLEN) {
  56. dropbear_log(LOG_WARNING, "DSS key too short");
  57. TRACE(("leave buf_get_dss_pub_key: short key"))
  58. return DROPBEAR_FAILURE;
  59. }
  60. TRACE(("leave buf_get_dss_pub_key: success"))
  61. return DROPBEAR_SUCCESS;
  62. }
  63. /* Same as buf_get_dss_pub_key, but reads a private "x" key at the end.
  64. * Loads a private dss key from a buffer
  65. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  66. int buf_get_dss_priv_key(buffer* buf, dropbear_dss_key *key) {
  67. int ret = DROPBEAR_FAILURE;
  68. dropbear_assert(key != NULL);
  69. ret = buf_get_dss_pub_key(buf, key);
  70. if (ret == DROPBEAR_FAILURE) {
  71. return DROPBEAR_FAILURE;
  72. }
  73. m_mp_alloc_init_multi(&key->x, NULL);
  74. ret = buf_getmpint(buf, key->x);
  75. if (ret == DROPBEAR_FAILURE) {
  76. m_free(key->x);
  77. }
  78. return ret;
  79. }
  80. /* Clear and free the memory used by a public or private key */
  81. void dss_key_free(dropbear_dss_key *key) {
  82. TRACE2(("enter dsa_key_free"))
  83. if (key == NULL) {
  84. TRACE2(("enter dsa_key_free: key == NULL"))
  85. return;
  86. }
  87. if (key->p) {
  88. mp_clear(key->p);
  89. m_free(key->p);
  90. }
  91. if (key->q) {
  92. mp_clear(key->q);
  93. m_free(key->q);
  94. }
  95. if (key->g) {
  96. mp_clear(key->g);
  97. m_free(key->g);
  98. }
  99. if (key->y) {
  100. mp_clear(key->y);
  101. m_free(key->y);
  102. }
  103. if (key->x) {
  104. mp_clear(key->x);
  105. m_free(key->x);
  106. }
  107. m_free(key);
  108. TRACE2(("leave dsa_key_free"))
  109. }
  110. /* put the dss public key into the buffer in the required format:
  111. *
  112. * string "ssh-dss"
  113. * mpint p
  114. * mpint q
  115. * mpint g
  116. * mpint y
  117. */
  118. void buf_put_dss_pub_key(buffer* buf, dropbear_dss_key *key) {
  119. dropbear_assert(key != NULL);
  120. buf_putstring(buf, SSH_SIGNKEY_DSS, SSH_SIGNKEY_DSS_LEN);
  121. buf_putmpint(buf, key->p);
  122. buf_putmpint(buf, key->q);
  123. buf_putmpint(buf, key->g);
  124. buf_putmpint(buf, key->y);
  125. }
  126. /* Same as buf_put_dss_pub_key, but with the private "x" key appended */
  127. void buf_put_dss_priv_key(buffer* buf, dropbear_dss_key *key) {
  128. dropbear_assert(key != NULL);
  129. buf_put_dss_pub_key(buf, key);
  130. buf_putmpint(buf, key->x);
  131. }
  132. #ifdef DROPBEAR_SIGNKEY_VERIFY
  133. /* Verify a DSS signature (in buf) made on data by the key given.
  134. * returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  135. int buf_dss_verify(buffer* buf, dropbear_dss_key *key, buffer *data_buf) {
  136. unsigned char msghash[SHA1_HASH_SIZE];
  137. hash_state hs;
  138. int ret = DROPBEAR_FAILURE;
  139. DEF_MP_INT(val1);
  140. DEF_MP_INT(val2);
  141. DEF_MP_INT(val3);
  142. DEF_MP_INT(val4);
  143. char * string = NULL;
  144. unsigned int stringlen;
  145. TRACE(("enter buf_dss_verify"))
  146. dropbear_assert(key != NULL);
  147. m_mp_init_multi(&val1, &val2, &val3, &val4, NULL);
  148. /* get blob, check length */
  149. string = buf_getstring(buf, &stringlen);
  150. if (stringlen != 2*SHA1_HASH_SIZE) {
  151. goto out;
  152. }
  153. /* hash the data */
  154. sha1_init(&hs);
  155. sha1_process(&hs, data_buf->data, data_buf->len);
  156. sha1_done(&hs, msghash);
  157. /* create the signature - s' and r' are the received signatures in buf */
  158. /* w = (s')-1 mod q */
  159. /* let val1 = s' */
  160. bytes_to_mp(&val1, (const unsigned char*) &string[SHA1_HASH_SIZE], SHA1_HASH_SIZE);
  161. if (mp_cmp(&val1, key->q) != MP_LT) {
  162. TRACE(("verify failed, s' >= q"))
  163. goto out;
  164. }
  165. /* let val2 = w = (s')^-1 mod q*/
  166. if (mp_invmod(&val1, key->q, &val2) != MP_OKAY) {
  167. goto out;
  168. }
  169. /* u1 = ((SHA(M')w) mod q */
  170. /* let val1 = SHA(M') = msghash */
  171. bytes_to_mp(&val1, msghash, SHA1_HASH_SIZE);
  172. /* let val3 = u1 = ((SHA(M')w) mod q */
  173. if (mp_mulmod(&val1, &val2, key->q, &val3) != MP_OKAY) {
  174. goto out;
  175. }
  176. /* u2 = ((r')w) mod q */
  177. /* let val1 = r' */
  178. bytes_to_mp(&val1, (const unsigned char*) &string[0], SHA1_HASH_SIZE);
  179. if (mp_cmp(&val1, key->q) != MP_LT) {
  180. TRACE(("verify failed, r' >= q"))
  181. goto out;
  182. }
  183. /* let val4 = u2 = ((r')w) mod q */
  184. if (mp_mulmod(&val1, &val2, key->q, &val4) != MP_OKAY) {
  185. goto out;
  186. }
  187. /* v = (((g)^u1 (y)^u2) mod p) mod q */
  188. /* val2 = g^u1 mod p */
  189. if (mp_exptmod(key->g, &val3, key->p, &val2) != MP_OKAY) {
  190. goto out;
  191. }
  192. /* val3 = y^u2 mod p */
  193. if (mp_exptmod(key->y, &val4, key->p, &val3) != MP_OKAY) {
  194. goto out;
  195. }
  196. /* val4 = ((g)^u1 (y)^u2) mod p */
  197. if (mp_mulmod(&val2, &val3, key->p, &val4) != MP_OKAY) {
  198. goto out;
  199. }
  200. /* val2 = v = (((g)^u1 (y)^u2) mod p) mod q */
  201. if (mp_mod(&val4, key->q, &val2) != MP_OKAY) {
  202. goto out;
  203. }
  204. /* check whether signatures verify */
  205. if (mp_cmp(&val2, &val1) == MP_EQ) {
  206. /* good sig */
  207. ret = DROPBEAR_SUCCESS;
  208. }
  209. out:
  210. mp_clear_multi(&val1, &val2, &val3, &val4, NULL);
  211. m_free(string);
  212. return ret;
  213. }
  214. #endif /* DROPBEAR_SIGNKEY_VERIFY */
  215. /* Sign the data presented with key, writing the signature contents
  216. * to the buffer */
  217. void buf_put_dss_sign(buffer* buf, dropbear_dss_key *key, buffer *data_buf) {
  218. unsigned char msghash[SHA1_HASH_SIZE];
  219. unsigned int writelen;
  220. unsigned int i;
  221. DEF_MP_INT(dss_k);
  222. DEF_MP_INT(dss_m);
  223. DEF_MP_INT(dss_temp1);
  224. DEF_MP_INT(dss_temp2);
  225. DEF_MP_INT(dss_r);
  226. DEF_MP_INT(dss_s);
  227. hash_state hs;
  228. TRACE(("enter buf_put_dss_sign"))
  229. dropbear_assert(key != NULL);
  230. /* hash the data */
  231. sha1_init(&hs);
  232. sha1_process(&hs, data_buf->data, data_buf->len);
  233. sha1_done(&hs, msghash);
  234. m_mp_init_multi(&dss_k, &dss_temp1, &dss_temp2, &dss_r, &dss_s,
  235. &dss_m, NULL);
  236. /* the random number generator's input has included the private key which
  237. * avoids DSS's problem of private key exposure due to low entropy */
  238. gen_random_mpint(key->q, &dss_k);
  239. /* now generate the actual signature */
  240. bytes_to_mp(&dss_m, msghash, SHA1_HASH_SIZE);
  241. /* g^k mod p */
  242. if (mp_exptmod(key->g, &dss_k, key->p, &dss_temp1) != MP_OKAY) {
  243. dropbear_exit("DSS error");
  244. }
  245. /* r = (g^k mod p) mod q */
  246. if (mp_mod(&dss_temp1, key->q, &dss_r) != MP_OKAY) {
  247. dropbear_exit("DSS error");
  248. }
  249. /* x*r mod q */
  250. if (mp_mulmod(&dss_r, key->x, key->q, &dss_temp1) != MP_OKAY) {
  251. dropbear_exit("DSS error");
  252. }
  253. /* (SHA1(M) + xr) mod q) */
  254. if (mp_addmod(&dss_m, &dss_temp1, key->q, &dss_temp2) != MP_OKAY) {
  255. dropbear_exit("DSS error");
  256. }
  257. /* (k^-1) mod q */
  258. if (mp_invmod(&dss_k, key->q, &dss_temp1) != MP_OKAY) {
  259. dropbear_exit("DSS error");
  260. }
  261. /* s = (k^-1(SHA1(M) + xr)) mod q */
  262. if (mp_mulmod(&dss_temp1, &dss_temp2, key->q, &dss_s) != MP_OKAY) {
  263. dropbear_exit("DSS error");
  264. }
  265. buf_putstring(buf, SSH_SIGNKEY_DSS, SSH_SIGNKEY_DSS_LEN);
  266. buf_putint(buf, 2*SHA1_HASH_SIZE);
  267. writelen = mp_unsigned_bin_size(&dss_r);
  268. dropbear_assert(writelen <= SHA1_HASH_SIZE);
  269. /* need to pad to 160 bits with leading zeros */
  270. for (i = 0; i < SHA1_HASH_SIZE - writelen; i++) {
  271. buf_putbyte(buf, 0);
  272. }
  273. if (mp_to_unsigned_bin(&dss_r, buf_getwriteptr(buf, writelen))
  274. != MP_OKAY) {
  275. dropbear_exit("DSS error");
  276. }
  277. mp_clear(&dss_r);
  278. buf_incrwritepos(buf, writelen);
  279. writelen = mp_unsigned_bin_size(&dss_s);
  280. dropbear_assert(writelen <= SHA1_HASH_SIZE);
  281. /* need to pad to 160 bits with leading zeros */
  282. for (i = 0; i < SHA1_HASH_SIZE - writelen; i++) {
  283. buf_putbyte(buf, 0);
  284. }
  285. if (mp_to_unsigned_bin(&dss_s, buf_getwriteptr(buf, writelen))
  286. != MP_OKAY) {
  287. dropbear_exit("DSS error");
  288. }
  289. mp_clear(&dss_s);
  290. buf_incrwritepos(buf, writelen);
  291. mp_clear_multi(&dss_k, &dss_temp1, &dss_temp2, &dss_r, &dss_s,
  292. &dss_m, NULL);
  293. /* create the signature to return */
  294. TRACE(("leave buf_put_dss_sign"))
  295. }
  296. #endif /* DROPBEAR_DSS */