sshdss.c 14 KB

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