diffie-hellman.c 7.1 KB

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