dsa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. 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 *dsa_cache_str(ssh_key *key)
  60. {
  61. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  62. strbuf *sb = strbuf_new();
  63. if (!dsa->p) {
  64. strbuf_free(sb);
  65. return NULL;
  66. }
  67. append_hex_to_strbuf(sb, dsa->p);
  68. append_hex_to_strbuf(sb, dsa->q);
  69. append_hex_to_strbuf(sb, dsa->g);
  70. append_hex_to_strbuf(sb, dsa->y);
  71. return strbuf_to_str(sb);
  72. }
  73. static key_components *dsa_components(ssh_key *key)
  74. {
  75. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  76. key_components *kc = key_components_new();
  77. key_components_add_text(kc, "key_type", "DSA");
  78. assert(dsa->p);
  79. key_components_add_mp(kc, "p", dsa->p);
  80. key_components_add_mp(kc, "q", dsa->q);
  81. key_components_add_mp(kc, "g", dsa->g);
  82. key_components_add_mp(kc, "public_y", dsa->y);
  83. if (dsa->x)
  84. key_components_add_mp(kc, "private_x", dsa->x);
  85. return kc;
  86. }
  87. static char *dsa_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 dsa_verify(ssh_key *key, ptrlen sig, ptrlen data)
  93. {
  94. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  95. BinarySource src[1];
  96. unsigned char hash[20];
  97. bool toret;
  98. if (!dsa->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, dsa->q);
  134. invalid |= mp_cmp_hs(s, dsa->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, dsa->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, dsa->q);
  155. /*
  156. * Step 3. u2 <- r * w mod q.
  157. */
  158. mp_int *u2 = mp_modmul(r, w, dsa->q);
  159. /*
  160. * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
  161. */
  162. mp_int *gu1p = mp_modpow(dsa->g, u1, dsa->p);
  163. mp_int *yu2p = mp_modpow(dsa->y, u2, dsa->p);
  164. mp_int *gu1yu2p = mp_modmul(gu1p, yu2p, dsa->p);
  165. mp_int *v = mp_mod(gu1yu2p, dsa->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 dsa_public_blob(ssh_key *key, BinarySink *bs)
  183. {
  184. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  185. put_stringz(bs, "ssh-dss");
  186. put_mp_ssh2(bs, dsa->p);
  187. put_mp_ssh2(bs, dsa->q);
  188. put_mp_ssh2(bs, dsa->g);
  189. put_mp_ssh2(bs, dsa->y);
  190. }
  191. static void dsa_private_blob(ssh_key *key, BinarySink *bs)
  192. {
  193. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  194. put_mp_ssh2(bs, dsa->x);
  195. }
  196. static ssh_key *dsa_new_priv(const ssh_keyalg *self, ptrlen pub, ptrlen priv)
  197. {
  198. BinarySource src[1];
  199. ssh_key *sshk;
  200. struct dsa_key *dsa;
  201. ptrlen hash;
  202. unsigned char digest[20];
  203. mp_int *ytest;
  204. sshk = dsa_new_pub(self, pub);
  205. if (!sshk)
  206. return NULL;
  207. dsa = container_of(sshk, struct dsa_key, sshk);
  208. BinarySource_BARE_INIT_PL(src, priv);
  209. dsa->x = get_mp_ssh2(src);
  210. if (get_err(src)) {
  211. dsa_freekey(&dsa->sshk);
  212. return NULL;
  213. }
  214. /*
  215. * Check the obsolete hash in the old DSA 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, dsa->p);
  221. put_mp_ssh2(h, dsa->q);
  222. put_mp_ssh2(h, dsa->g);
  223. ssh_hash_final(h, digest);
  224. if (!smemeq(hash.ptr, digest, 20)) {
  225. dsa_freekey(&dsa->sshk);
  226. return NULL;
  227. }
  228. }
  229. /*
  230. * Now ensure g^x mod p really is y.
  231. */
  232. ytest = mp_modpow(dsa->g, dsa->x, dsa->p);
  233. if (!mp_cmp_eq(ytest, dsa->y)) {
  234. mp_free(ytest);
  235. dsa_freekey(&dsa->sshk);
  236. return NULL;
  237. }
  238. mp_free(ytest);
  239. return &dsa->sshk;
  240. }
  241. static ssh_key *dsa_new_priv_openssh(const ssh_keyalg *self,
  242. BinarySource *src)
  243. {
  244. struct dsa_key *dsa;
  245. dsa = snew(struct dsa_key);
  246. dsa->sshk.vt = &ssh_dsa;
  247. dsa->p = get_mp_ssh2(src);
  248. dsa->q = get_mp_ssh2(src);
  249. dsa->g = get_mp_ssh2(src);
  250. dsa->y = get_mp_ssh2(src);
  251. dsa->x = get_mp_ssh2(src);
  252. if (get_err(src) ||
  253. mp_eq_integer(dsa->q, 0) || mp_eq_integer(dsa->p, 0)) {
  254. /* Invalid key. */
  255. dsa_freekey(&dsa->sshk);
  256. return NULL;
  257. }
  258. return &dsa->sshk;
  259. }
  260. static void dsa_openssh_blob(ssh_key *key, BinarySink *bs)
  261. {
  262. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  263. put_mp_ssh2(bs, dsa->p);
  264. put_mp_ssh2(bs, dsa->q);
  265. put_mp_ssh2(bs, dsa->g);
  266. put_mp_ssh2(bs, dsa->y);
  267. put_mp_ssh2(bs, dsa->x);
  268. }
  269. static bool dsa_has_private(ssh_key *key)
  270. {
  271. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  272. return dsa->x != NULL;
  273. }
  274. static int dsa_pubkey_bits(const ssh_keyalg *self, ptrlen pub)
  275. {
  276. ssh_key *sshk;
  277. struct dsa_key *dsa;
  278. int ret;
  279. sshk = dsa_new_pub(self, pub);
  280. if (!sshk)
  281. return -1;
  282. dsa = container_of(sshk, struct dsa_key, sshk);
  283. ret = mp_get_nbits(dsa->p);
  284. dsa_freekey(&dsa->sshk);
  285. return ret;
  286. }
  287. static void dsa_sign(ssh_key *key, ptrlen data, unsigned flags, BinarySink *bs)
  288. {
  289. struct dsa_key *dsa = container_of(key, struct dsa_key, sshk);
  290. unsigned char digest[20];
  291. int i;
  292. hash_simple(&ssh_sha1, data, digest);
  293. /* Generate any valid exponent k, using the RFC 6979 deterministic
  294. * procedure. */
  295. mp_int *k = rfc6979(&ssh_sha1, dsa->q, dsa->x, data);
  296. mp_int *kinv = mp_invert(k, dsa->q); /* k^-1 mod q */
  297. /*
  298. * Now we have k, so just go ahead and compute the signature.
  299. */
  300. mp_int *gkp = mp_modpow(dsa->g, k, dsa->p); /* g^k mod p */
  301. mp_int *r = mp_mod(gkp, dsa->q); /* r = (g^k mod p) mod q */
  302. mp_free(gkp);
  303. mp_int *hash = mp_from_bytes_be(make_ptrlen(digest, 20));
  304. mp_int *xr = mp_mul(dsa->x, r);
  305. mp_int *hxr = mp_add(xr, hash); /* hash + x*r */
  306. mp_int *s = mp_modmul(kinv, hxr, dsa->q); /* s = k^-1 * (hash+x*r) mod q */
  307. mp_free(hxr);
  308. mp_free(xr);
  309. mp_free(kinv);
  310. mp_free(k);
  311. mp_free(hash);
  312. put_stringz(bs, "ssh-dss");
  313. put_uint32(bs, 40);
  314. for (i = 0; i < 20; i++)
  315. put_byte(bs, mp_get_byte(r, 19 - i));
  316. for (i = 0; i < 20; i++)
  317. put_byte(bs, mp_get_byte(s, 19 - i));
  318. mp_free(r);
  319. mp_free(s);
  320. }
  321. static char *dsa_alg_desc(const ssh_keyalg *self) { return dupstr("DSA"); }
  322. const ssh_keyalg ssh_dsa = {
  323. .new_pub = dsa_new_pub,
  324. .new_priv = dsa_new_priv,
  325. .new_priv_openssh = dsa_new_priv_openssh,
  326. .freekey = dsa_freekey,
  327. .invalid = dsa_invalid,
  328. .sign = dsa_sign,
  329. .verify = dsa_verify,
  330. .public_blob = dsa_public_blob,
  331. .private_blob = dsa_private_blob,
  332. .openssh_blob = dsa_openssh_blob,
  333. .has_private = dsa_has_private,
  334. .cache_str = dsa_cache_str,
  335. .components = dsa_components,
  336. .base_key = nullkey_base_key,
  337. .pubkey_bits = dsa_pubkey_bits,
  338. .supported_flags = nullkey_supported_flags,
  339. .alternate_ssh_id = nullkey_alternate_ssh_id,
  340. .alg_desc = dsa_alg_desc,
  341. .variable_size = nullkey_variable_size_yes,
  342. .ssh_id = "ssh-dss",
  343. .cache_id = "dss",
  344. };