dsa.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Digital Signature Algorithm implementation for PuTTY.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include "ssh.h"
  8. #include "mpint.h"
  9. #include "misc.h"
  10. static void dsa_freekey(ssh_key *key); /* forward reference */
  11. static ssh_key *dsa_new_pub(const ssh_keyalg *self, ptrlen data)
  12. {
  13. BinarySource src[1];
  14. struct dsa_key *dsa;
  15. BinarySource_BARE_INIT_PL(src, data);
  16. if (!ptrlen_eq_string(get_string(src), "ssh-dss"))
  17. return NULL;
  18. dsa = snew(struct dsa_key);
  19. dsa->sshk.vt = &ssh_dsa;
  20. dsa->p = get_mp_ssh2(src);
  21. dsa->q = get_mp_ssh2(src);
  22. dsa->g = get_mp_ssh2(src);
  23. dsa->y = get_mp_ssh2(src);
  24. dsa->x = NULL;
  25. if (get_err(src) ||
  26. mp_eq_integer(dsa->p, 0) || mp_eq_integer(dsa->q, 0)) {
  27. /* Invalid key. */
  28. dsa_freekey(&dsa->sshk);
  29. return NULL;
  30. }
  31. return &dsa->sshk;
  32. }
  33. static void dsa_freekey(ssh_key *key)
  34. {
  35. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  36. if (dsa->p)
  37. mp_free(dsa->p);
  38. if (dsa->q)
  39. mp_free(dsa->q);
  40. if (dsa->g)
  41. mp_free(dsa->g);
  42. if (dsa->y)
  43. mp_free(dsa->y);
  44. if (dsa->x)
  45. mp_free(dsa->x);
  46. sfree(dsa);
  47. }
  48. static void append_hex_to_strbuf(strbuf *sb, mp_int *x)
  49. {
  50. if (sb->len > 0)
  51. put_byte(sb, ',');
  52. put_data(sb, "0x", 2);
  53. { // WINSCP
  54. char *hex = mp_get_hex(x);
  55. size_t hexlen = strlen(hex);
  56. put_data(sb, hex, hexlen);
  57. smemclr(hex, hexlen);
  58. sfree(hex);
  59. } // WINSCP
  60. }
  61. static char *dsa_cache_str(ssh_key *key)
  62. {
  63. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  64. strbuf *sb = strbuf_new();
  65. if (!dsa->p) {
  66. strbuf_free(sb);
  67. return NULL;
  68. }
  69. append_hex_to_strbuf(sb, dsa->p);
  70. append_hex_to_strbuf(sb, dsa->q);
  71. append_hex_to_strbuf(sb, dsa->g);
  72. append_hex_to_strbuf(sb, dsa->y);
  73. return strbuf_to_str(sb);
  74. }
  75. static key_components *dsa_components(ssh_key *key)
  76. {
  77. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  78. key_components *kc = key_components_new();
  79. key_components_add_text(kc, "key_type", "DSA");
  80. assert(dsa->p);
  81. key_components_add_mp(kc, "p", dsa->p);
  82. key_components_add_mp(kc, "q", dsa->q);
  83. key_components_add_mp(kc, "g", dsa->g);
  84. key_components_add_mp(kc, "public_y", dsa->y);
  85. if (dsa->x)
  86. key_components_add_mp(kc, "private_x", dsa->x);
  87. return kc;
  88. }
  89. static char *dsa_invalid(ssh_key *key, unsigned flags)
  90. {
  91. /* No validity criterion will stop us from using a DSA key at all */
  92. return NULL;
  93. }
  94. static bool dsa_verify(ssh_key *key, ptrlen sig, ptrlen data)
  95. {
  96. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  97. BinarySource src[1];
  98. unsigned char hash[20];
  99. bool toret;
  100. if (!dsa->p)
  101. return false;
  102. BinarySource_BARE_INIT_PL(src, sig);
  103. /*
  104. * Commercial SSH (2.0.13) and OpenSSH disagree over the format
  105. * of a DSA signature. OpenSSH is in line with RFC 4253:
  106. * it uses a string "ssh-dss", followed by a 40-byte string
  107. * containing two 160-bit integers end-to-end. Commercial SSH
  108. * can't be bothered with the header bit, and considers a DSA
  109. * signature blob to be _just_ the 40-byte string containing
  110. * the two 160-bit integers. We tell them apart by measuring
  111. * the length: length 40 means the commercial-SSH bug, anything
  112. * else is assumed to be RFC-compliant.
  113. */
  114. if (sig.len != 40) { /* bug not present; read admin fields */
  115. ptrlen type = get_string(src);
  116. sig = get_string(src);
  117. if (get_err(src) || !ptrlen_eq_string(type, "ssh-dss") ||
  118. sig.len != 40)
  119. return false;
  120. }
  121. /* Now we're sitting on a 40-byte string for sure. */
  122. { // WINSCP
  123. mp_int *r = mp_from_bytes_be(make_ptrlen(sig.ptr, 20));
  124. mp_int *s = mp_from_bytes_be(make_ptrlen((const char *)sig.ptr + 20, 20));
  125. if (!r || !s) {
  126. if (r)
  127. mp_free(r);
  128. if (s)
  129. mp_free(s);
  130. return false;
  131. }
  132. /* Basic sanity checks: 0 < r,s < q */
  133. { // WINSCP
  134. unsigned invalid = 0;
  135. invalid |= mp_eq_integer(r, 0);
  136. invalid |= mp_eq_integer(s, 0);
  137. invalid |= mp_cmp_hs(r, dsa->q);
  138. invalid |= mp_cmp_hs(s, dsa->q);
  139. if (invalid) {
  140. mp_free(r);
  141. mp_free(s);
  142. return false;
  143. }
  144. /*
  145. * Step 1. w <- s^-1 mod q.
  146. */
  147. { // WINSCP
  148. mp_int *w = mp_invert(s, dsa->q);
  149. if (!w) {
  150. mp_free(r);
  151. mp_free(s);
  152. return false;
  153. }
  154. /*
  155. * Step 2. u1 <- SHA(message) * w mod q.
  156. */
  157. hash_simple(&ssh_sha1, data, hash);
  158. { // WINSCP
  159. mp_int *sha = mp_from_bytes_be(make_ptrlen(hash, 20));
  160. mp_int *u1 = mp_modmul(sha, w, dsa->q);
  161. /*
  162. * Step 3. u2 <- r * w mod q.
  163. */
  164. mp_int *u2 = mp_modmul(r, w, dsa->q);
  165. /*
  166. * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
  167. */
  168. mp_int *gu1p = mp_modpow(dsa->g, u1, dsa->p);
  169. mp_int *yu2p = mp_modpow(dsa->y, u2, dsa->p);
  170. mp_int *gu1yu2p = mp_modmul(gu1p, yu2p, dsa->p);
  171. mp_int *v = mp_mod(gu1yu2p, dsa->q);
  172. /*
  173. * Step 5. v should now be equal to r.
  174. */
  175. toret = mp_cmp_eq(v, r);
  176. mp_free(w);
  177. mp_free(sha);
  178. mp_free(u1);
  179. mp_free(u2);
  180. mp_free(gu1p);
  181. mp_free(yu2p);
  182. mp_free(gu1yu2p);
  183. mp_free(v);
  184. mp_free(r);
  185. mp_free(s);
  186. } // WINSCP
  187. } // WINSCP
  188. } // WINSCP
  189. } // WINSCP
  190. return toret;
  191. }
  192. static void dsa_public_blob(ssh_key *key, BinarySink *bs)
  193. {
  194. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  195. put_stringz(bs, "ssh-dss");
  196. put_mp_ssh2(bs, dsa->p);
  197. put_mp_ssh2(bs, dsa->q);
  198. put_mp_ssh2(bs, dsa->g);
  199. put_mp_ssh2(bs, dsa->y);
  200. }
  201. static void dsa_private_blob(ssh_key *key, BinarySink *bs)
  202. {
  203. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  204. put_mp_ssh2(bs, dsa->x);
  205. }
  206. static ssh_key *dsa_new_priv(const ssh_keyalg *self, ptrlen pub, ptrlen priv)
  207. {
  208. BinarySource src[1];
  209. ssh_key *sshk;
  210. struct dsa_key *dsa;
  211. ptrlen hash;
  212. unsigned char digest[20];
  213. mp_int *ytest;
  214. sshk = dsa_new_pub(self, pub);
  215. if (!sshk)
  216. return NULL;
  217. dsa = container_of(sshk, struct dsa_key, sshk);
  218. BinarySource_BARE_INIT_PL(src, priv);
  219. dsa->x = get_mp_ssh2(src);
  220. if (get_err(src)) {
  221. dsa_freekey(&dsa->sshk);
  222. return NULL;
  223. }
  224. /*
  225. * Check the obsolete hash in the old DSA key format.
  226. */
  227. hash = get_string(src);
  228. if (hash.len == 20) {
  229. ssh_hash *h = ssh_hash_new(&ssh_sha1);
  230. put_mp_ssh2(h, dsa->p);
  231. put_mp_ssh2(h, dsa->q);
  232. put_mp_ssh2(h, dsa->g);
  233. ssh_hash_final(h, digest);
  234. if (!smemeq(hash.ptr, digest, 20)) {
  235. dsa_freekey(&dsa->sshk);
  236. return NULL;
  237. }
  238. }
  239. /*
  240. * Now ensure g^x mod p really is y.
  241. */
  242. ytest = mp_modpow(dsa->g, dsa->x, dsa->p);
  243. if (!mp_cmp_eq(ytest, dsa->y)) {
  244. mp_free(ytest);
  245. dsa_freekey(&dsa->sshk);
  246. return NULL;
  247. }
  248. mp_free(ytest);
  249. return &dsa->sshk;
  250. }
  251. static ssh_key *dsa_new_priv_openssh(const ssh_keyalg *self,
  252. BinarySource *src)
  253. {
  254. struct dsa_key *dsa;
  255. dsa = snew(struct dsa_key);
  256. dsa->sshk.vt = &ssh_dsa;
  257. dsa->p = get_mp_ssh2(src);
  258. dsa->q = get_mp_ssh2(src);
  259. dsa->g = get_mp_ssh2(src);
  260. dsa->y = get_mp_ssh2(src);
  261. dsa->x = get_mp_ssh2(src);
  262. if (get_err(src) ||
  263. mp_eq_integer(dsa->q, 0) || mp_eq_integer(dsa->p, 0)) {
  264. /* Invalid key. */
  265. dsa_freekey(&dsa->sshk);
  266. return NULL;
  267. }
  268. return &dsa->sshk;
  269. }
  270. static void dsa_openssh_blob(ssh_key *key, BinarySink *bs)
  271. {
  272. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  273. put_mp_ssh2(bs, dsa->p);
  274. put_mp_ssh2(bs, dsa->q);
  275. put_mp_ssh2(bs, dsa->g);
  276. put_mp_ssh2(bs, dsa->y);
  277. put_mp_ssh2(bs, dsa->x);
  278. }
  279. static bool dsa_has_private(ssh_key *key)
  280. {
  281. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  282. return dsa->x != NULL;
  283. }
  284. static int dsa_pubkey_bits(const ssh_keyalg *self, ptrlen pub)
  285. {
  286. ssh_key *sshk;
  287. struct dsa_key *dsa;
  288. int ret;
  289. sshk = dsa_new_pub(self, pub);
  290. if (!sshk)
  291. return -1;
  292. dsa = container_of(sshk, struct dsa_key, sshk);
  293. ret = mp_get_nbits(dsa->p);
  294. dsa_freekey(&dsa->sshk);
  295. return ret;
  296. }
  297. mp_int *dsa_gen_k(const char *id_string, mp_int *modulus,
  298. mp_int *private_key,
  299. unsigned char *digest, int digest_len)
  300. {
  301. /*
  302. * The basic DSA signing algorithm is:
  303. *
  304. * - invent a random k between 1 and q-1 (exclusive).
  305. * - Compute r = (g^k mod p) mod q.
  306. * - Compute s = k^-1 * (hash + x*r) mod q.
  307. *
  308. * This has the dangerous properties that:
  309. *
  310. * - if an attacker in possession of the public key _and_ the
  311. * signature (for example, the host you just authenticated
  312. * to) can guess your k, he can reverse the computation of s
  313. * and work out x = r^-1 * (s*k - hash) mod q. That is, he
  314. * can deduce the private half of your key, and masquerade
  315. * as you for as long as the key is still valid.
  316. *
  317. * - since r is a function purely of k and the public key, if
  318. * the attacker only has a _range of possibilities_ for k
  319. * it's easy for him to work through them all and check each
  320. * one against r; he'll never be unsure of whether he's got
  321. * the right one.
  322. *
  323. * - if you ever sign two different hashes with the same k, it
  324. * will be immediately obvious because the two signatures
  325. * will have the same r, and moreover an attacker in
  326. * possession of both signatures (and the public key of
  327. * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
  328. * and from there deduce x as before.
  329. *
  330. * - the Bleichenbacher attack on DSA makes use of methods of
  331. * generating k which are significantly non-uniformly
  332. * distributed; in particular, generating a 160-bit random
  333. * number and reducing it mod q is right out.
  334. *
  335. * For this reason we must be pretty careful about how we
  336. * generate our k. Since this code runs on Windows, with no
  337. * particularly good system entropy sources, we can't trust our
  338. * RNG itself to produce properly unpredictable data. Hence, we
  339. * use a totally different scheme instead.
  340. *
  341. * What we do is to take a SHA-512 (_big_) hash of the private
  342. * key x, and then feed this into another SHA-512 hash that
  343. * also includes the message hash being signed. That is:
  344. *
  345. * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
  346. *
  347. * This number is 512 bits long, so reducing it mod q won't be
  348. * noticeably non-uniform. So
  349. *
  350. * k = proto_k mod q
  351. *
  352. * This has the interesting property that it's _deterministic_:
  353. * signing the same hash twice with the same key yields the
  354. * same signature.
  355. *
  356. * Despite this determinism, it's still not predictable to an
  357. * attacker, because in order to repeat the SHA-512
  358. * construction that created it, the attacker would have to
  359. * know the private key value x - and by assumption he doesn't,
  360. * because if he knew that he wouldn't be attacking k!
  361. *
  362. * (This trick doesn't, _per se_, protect against reuse of k.
  363. * Reuse of k is left to chance; all it does is prevent
  364. * _excessively high_ chances of reuse of k due to entropy
  365. * problems.)
  366. *
  367. * Thanks to Colin Plumb for the general idea of using x to
  368. * ensure k is hard to guess, and to the Cambridge University
  369. * Computer Security Group for helping to argue out all the
  370. * fine details.
  371. */
  372. ssh_hash *h;
  373. unsigned char digest512[64];
  374. /*
  375. * Hash some identifying text plus x.
  376. */
  377. h = ssh_hash_new(&ssh_sha512);
  378. put_asciz(h, id_string);
  379. put_mp_ssh2(h, private_key);
  380. ssh_hash_digest(h, digest512);
  381. /*
  382. * Now hash that digest plus the message hash.
  383. */
  384. ssh_hash_reset(h);
  385. put_data(h, digest512, sizeof(digest512));
  386. put_data(h, digest, digest_len);
  387. ssh_hash_final(h, digest512);
  388. /*
  389. * Now convert the result into a bignum, and coerce it to the
  390. * range [2,q), which we do by reducing it mod q-2 and adding 2.
  391. */
  392. { // WINSCP
  393. mp_int *modminus2 = mp_copy(modulus);
  394. mp_sub_integer_into(modminus2, modminus2, 2);
  395. { // WINSCP
  396. mp_int *proto_k = mp_from_bytes_be(make_ptrlen(digest512, 64));
  397. mp_int *k = mp_mod(proto_k, modminus2);
  398. mp_free(proto_k);
  399. mp_free(modminus2);
  400. mp_add_integer_into(k, k, 2);
  401. smemclr(digest512, sizeof(digest512));
  402. return k;
  403. } // WINSCP
  404. } // WINSCP
  405. }
  406. static void dsa_sign(ssh_key *key, ptrlen data, unsigned flags, BinarySink *bs)
  407. {
  408. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  409. unsigned char digest[20];
  410. int i;
  411. hash_simple(&ssh_sha1, data, digest);
  412. { // WINSCP
  413. mp_int *k = dsa_gen_k("DSA deterministic k generator", dsa->q, dsa->x,
  414. digest, sizeof(digest));
  415. mp_int *kinv = mp_invert(k, dsa->q); /* k^-1 mod q */
  416. /*
  417. * Now we have k, so just go ahead and compute the signature.
  418. */
  419. mp_int *gkp = mp_modpow(dsa->g, k, dsa->p); /* g^k mod p */
  420. mp_int *r = mp_mod(gkp, dsa->q); /* r = (g^k mod p) mod q */
  421. mp_free(gkp);
  422. { // WINSCP
  423. mp_int *hash = mp_from_bytes_be(make_ptrlen(digest, 20));
  424. mp_int *xr = mp_mul(dsa->x, r);
  425. mp_int *hxr = mp_add(xr, hash); /* hash + x*r */
  426. { // WINSCP
  427. mp_int *s = mp_modmul(kinv, hxr, dsa->q); /* s = k^-1 * (hash+x*r) mod q */
  428. mp_free(hxr);
  429. mp_free(xr);
  430. mp_free(kinv);
  431. mp_free(k);
  432. mp_free(hash);
  433. put_stringz(bs, "ssh-dss");
  434. put_uint32(bs, 40);
  435. for (i = 0; i < 20; i++)
  436. put_byte(bs, mp_get_byte(r, 19 - i));
  437. for (i = 0; i < 20; i++)
  438. put_byte(bs, mp_get_byte(s, 19 - i));
  439. mp_free(r);
  440. mp_free(s);
  441. } // WINSCP
  442. } // WINSCP
  443. } // WINSCP
  444. }
  445. static char *dsa_alg_desc(const ssh_keyalg *self) { return dupstr("DSA"); }
  446. const ssh_keyalg ssh_dsa = {
  447. // WINSCP
  448. /*.new_pub =*/ dsa_new_pub,
  449. /*.new_priv =*/ dsa_new_priv,
  450. /*.new_priv_openssh =*/ dsa_new_priv_openssh,
  451. /*.freekey =*/ dsa_freekey,
  452. /*.invalid =*/ dsa_invalid,
  453. /*.sign =*/ dsa_sign,
  454. /*.verify =*/ dsa_verify,
  455. /*.public_blob =*/ dsa_public_blob,
  456. /*.private_blob =*/ dsa_private_blob,
  457. /*.openssh_blob =*/ dsa_openssh_blob,
  458. .has_private = dsa_has_private,
  459. /*.cache_str =*/ dsa_cache_str,
  460. /*.components =*/ dsa_components,
  461. .base_key = nullkey_base_key,
  462. /*.pubkey_bits =*/ dsa_pubkey_bits,
  463. /*.ssh_id =*/ "ssh-dss",
  464. /*.cache_id =*/ "dss",
  465. NULL,
  466. .supported_flags = nullkey_supported_flags,
  467. .alternate_ssh_id = nullkey_alternate_ssh_id,
  468. .alg_desc = dsa_alg_desc,
  469. .variable_size = nullkey_variable_size_yes,
  470. };