sshdh.c 7.7 KB

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