sshdh.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Diffie-Hellman implementation for PuTTY.
  3. */
  4. #include <assert.h>
  5. #include "ssh.h"
  6. #include "misc.h"
  7. #include "mpint.h"
  8. struct dh_ctx {
  9. mp_int *x, *e, *p, *q, *g;
  10. };
  11. struct dh_extra {
  12. bool gex;
  13. void (*construct)(dh_ctx *ctx);
  14. };
  15. static void dh_group1_construct(dh_ctx *ctx)
  16. {
  17. ctx->p = MP_LITERAL_WINSCP_STR("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF");
  18. ctx->g = mp_from_integer(2);
  19. }
  20. static void dh_group14_construct(dh_ctx *ctx)
  21. {
  22. ctx->p = MP_LITERAL_WINSCP_STR("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF");
  23. ctx->g = mp_from_integer(2);
  24. }
  25. static const struct dh_extra extra_group1 = {
  26. false, dh_group1_construct,
  27. };
  28. static const ssh_kex ssh_diffiehellman_group1_sha1 = {
  29. "diffie-hellman-group1-sha1", "group1",
  30. KEXTYPE_DH, &ssh_sha1, &extra_group1,
  31. };
  32. static const ssh_kex *const group1_list[] = {
  33. &ssh_diffiehellman_group1_sha1
  34. };
  35. const ssh_kexes ssh_diffiehellman_group1 = { lenof(group1_list), group1_list };
  36. static const struct dh_extra extra_group14 = {
  37. false, dh_group14_construct,
  38. };
  39. static const ssh_kex ssh_diffiehellman_group14_sha256 = {
  40. "diffie-hellman-group14-sha256", "group14",
  41. KEXTYPE_DH, &ssh_sha256, &extra_group14,
  42. };
  43. static const ssh_kex ssh_diffiehellman_group14_sha1 = {
  44. "diffie-hellman-group14-sha1", "group14",
  45. KEXTYPE_DH, &ssh_sha1, &extra_group14,
  46. };
  47. static const ssh_kex *const group14_list[] = {
  48. &ssh_diffiehellman_group14_sha256,
  49. &ssh_diffiehellman_group14_sha1
  50. };
  51. const ssh_kexes ssh_diffiehellman_group14 = {
  52. lenof(group14_list), group14_list
  53. };
  54. static const struct dh_extra extra_gex = { true };
  55. static const ssh_kex ssh_diffiehellman_gex_sha256 = {
  56. "diffie-hellman-group-exchange-sha256", NULL,
  57. KEXTYPE_DH, &ssh_sha256, &extra_gex,
  58. };
  59. static const ssh_kex ssh_diffiehellman_gex_sha1 = {
  60. "diffie-hellman-group-exchange-sha1", NULL,
  61. KEXTYPE_DH, &ssh_sha1, &extra_gex,
  62. };
  63. static const ssh_kex *const gex_list[] = {
  64. &ssh_diffiehellman_gex_sha256,
  65. &ssh_diffiehellman_gex_sha1
  66. };
  67. const ssh_kexes ssh_diffiehellman_gex = { lenof(gex_list), gex_list };
  68. /*
  69. * Suffix on GSSAPI SSH protocol identifiers that indicates Kerberos 5
  70. * as the mechanism.
  71. *
  72. * This suffix is the base64-encoded MD5 hash of the byte sequence
  73. * 06 09 2A 86 48 86 F7 12 01 02 02, which in turn is the ASN.1 DER
  74. * encoding of the object ID 1.2.840.113554.1.2.2 which designates
  75. * Kerberos v5.
  76. *
  77. * (The same encoded OID, minus the two-byte DER header, is defined in
  78. * pgssapi.c as GSS_MECH_KRB5.)
  79. */
  80. #define GSS_KRB5_OID_HASH "toWM5Slw5Ew8Mqkay+al2g=="
  81. static const ssh_kex ssh_gssk5_diffiehellman_gex_sha1 = {
  82. "gss-gex-sha1-" GSS_KRB5_OID_HASH, NULL,
  83. KEXTYPE_GSS, &ssh_sha1, &extra_gex,
  84. };
  85. static const ssh_kex ssh_gssk5_diffiehellman_group14_sha1 = {
  86. "gss-group14-sha1-" GSS_KRB5_OID_HASH, "group14",
  87. KEXTYPE_GSS, &ssh_sha1, &extra_group14,
  88. };
  89. static const ssh_kex ssh_gssk5_diffiehellman_group1_sha1 = {
  90. "gss-group1-sha1-" GSS_KRB5_OID_HASH, "group1",
  91. KEXTYPE_GSS, &ssh_sha1, &extra_group1,
  92. };
  93. static const ssh_kex *const gssk5_sha1_kex_list[] = {
  94. &ssh_gssk5_diffiehellman_gex_sha1,
  95. &ssh_gssk5_diffiehellman_group14_sha1,
  96. &ssh_gssk5_diffiehellman_group1_sha1
  97. };
  98. const ssh_kexes ssh_gssk5_sha1_kex = {
  99. lenof(gssk5_sha1_kex_list), gssk5_sha1_kex_list
  100. };
  101. /*
  102. * Common DH initialisation.
  103. */
  104. static void dh_init(dh_ctx *ctx)
  105. {
  106. ctx->q = mp_rshift_fixed(ctx->p, 1);
  107. ctx->x = ctx->e = NULL;
  108. }
  109. bool dh_is_gex(const ssh_kex *kex)
  110. {
  111. const struct dh_extra *extra = (const struct dh_extra *)kex->extra;
  112. return extra->gex;
  113. }
  114. /*
  115. * Initialise DH for a standard group.
  116. */
  117. dh_ctx *dh_setup_group(const ssh_kex *kex)
  118. {
  119. const struct dh_extra *extra = (const struct dh_extra *)kex->extra;
  120. pinitassert(!extra->gex);
  121. dh_ctx *ctx = snew(dh_ctx);
  122. extra->construct(ctx);
  123. dh_init(ctx);
  124. return ctx;
  125. }
  126. /*
  127. * Initialise DH for a server-supplied group.
  128. */
  129. dh_ctx *dh_setup_gex(mp_int *pval, mp_int *gval)
  130. {
  131. dh_ctx *ctx = snew(dh_ctx);
  132. ctx->p = mp_copy(pval);
  133. ctx->g = mp_copy(gval);
  134. dh_init(ctx);
  135. return ctx;
  136. }
  137. /*
  138. * Return size of DH modulus p.
  139. */
  140. int dh_modulus_bit_size(const dh_ctx *ctx)
  141. {
  142. return mp_get_nbits(ctx->p);
  143. }
  144. /*
  145. * Clean up and free a context.
  146. */
  147. void dh_cleanup(dh_ctx *ctx)
  148. {
  149. if (ctx->x)
  150. mp_free(ctx->x);
  151. if (ctx->e)
  152. mp_free(ctx->e);
  153. if (ctx->p)
  154. mp_free(ctx->p);
  155. if (ctx->g)
  156. mp_free(ctx->g);
  157. if (ctx->q)
  158. mp_free(ctx->q);
  159. sfree(ctx);
  160. }
  161. /*
  162. * DH stage 1: invent a number x between 1 and q, and compute e =
  163. * g^x mod p. Return e.
  164. *
  165. * If `nbits' is greater than zero, it is used as an upper limit
  166. * for the number of bits in x. This is safe provided that (a) you
  167. * use twice as many bits in x as the number of bits you expect to
  168. * use in your session key, and (b) the DH group is a safe prime
  169. * (which SSH demands that it must be).
  170. *
  171. * P. C. van Oorschot, M. J. Wiener
  172. * "On Diffie-Hellman Key Agreement with Short Exponents".
  173. * Advances in Cryptology: Proceedings of Eurocrypt '96
  174. * Springer-Verlag, May 1996.
  175. */
  176. mp_int *dh_create_e(dh_ctx *ctx, int nbits)
  177. {
  178. /*
  179. * Lower limit is just 2.
  180. */
  181. mp_int *lo = mp_from_integer(2);
  182. /*
  183. * Upper limit.
  184. */
  185. mp_int *hi = mp_copy(ctx->q);
  186. mp_sub_integer_into(hi, hi, 1);
  187. if (nbits) {
  188. mp_int *pow2 = mp_power_2(nbits+1);
  189. mp_min_into(pow2, pow2, hi);
  190. mp_free(hi);
  191. hi = pow2;
  192. }
  193. /*
  194. * Make a random number in that range.
  195. */
  196. ctx->x = mp_random_in_range(lo, hi);
  197. mp_free(lo);
  198. mp_free(hi);
  199. /*
  200. * Now compute e = g^x mod p.
  201. */
  202. ctx->e = mp_modpow(ctx->g, ctx->x, ctx->p);
  203. return ctx->e;
  204. }
  205. /*
  206. * DH stage 2-epsilon: given a number f, validate it to ensure it's in
  207. * range. (RFC 4253 section 8: "Values of 'e' or 'f' that are not in
  208. * the range [1, p-1] MUST NOT be sent or accepted by either side."
  209. * Also, we rule out 1 and p-1 too, since that's easy to do and since
  210. * they lead to obviously weak keys that even a passive eavesdropper
  211. * can figure out.)
  212. */
  213. const char *dh_validate_f(dh_ctx *ctx, mp_int *f)
  214. {
  215. if (!mp_hs_integer(f, 2)) {
  216. return "f value received is too small";
  217. } else {
  218. mp_int *pm1 = mp_copy(ctx->p);
  219. mp_sub_integer_into(pm1, pm1, 1);
  220. { // WINSCP
  221. unsigned cmp = mp_cmp_hs(f, pm1);
  222. mp_free(pm1);
  223. if (cmp)
  224. return "f value received is too large";
  225. } // WINSCP
  226. }
  227. return NULL;
  228. }
  229. /*
  230. * DH stage 2: given a number f, compute K = f^x mod p.
  231. */
  232. mp_int *dh_find_K(dh_ctx *ctx, mp_int *f)
  233. {
  234. return mp_modpow(f, ctx->x, ctx->p);
  235. }