sshdss.c 14 KB

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