diffie-hellman.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 void dh_group15_construct(dh_ctx *ctx)
  32. {
  33. /* Command to recompute, from the expression in RFC 3526 section 4:
  34. spigot -B16 '2^3072 - 2^3008 - 1 + 2^64 * ( floor(2^2942 pi) + 1690314 )'
  35. */
  36. ctx->p = MP_LITERAL_WINSCP_STR("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF");
  37. ctx->g = mp_from_integer(2);
  38. }
  39. static void dh_group16_construct(dh_ctx *ctx)
  40. {
  41. /* Command to recompute, from the expression in RFC 3526 section 5:
  42. spigot -B16 '2^4096 - 2^4032 - 1 + 2^64 * ( floor(2^3966 pi) + 240904 )'
  43. */
  44. ctx->p = MP_LITERAL_WINSCP_STR("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199FFFFFFFFFFFFFFFF");
  45. ctx->g = mp_from_integer(2);
  46. }
  47. static void dh_group17_construct(dh_ctx *ctx)
  48. {
  49. /* Command to recompute, from the expression in RFC 3526 section 6:
  50. spigot -B16 '2^6144 - 2^6080 - 1 + 2^64 * ( floor(2^6014 pi) + 929484 )'
  51. */
  52. ctx->p = MP_LITERAL_WINSCP_STR("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF");
  53. ctx->g = mp_from_integer(2);
  54. }
  55. static void dh_group18_construct(dh_ctx *ctx)
  56. {
  57. /* Command to recompute, from the expression in RFC 3526 section 7:
  58. spigot -B16 '2^8192 - 2^8128 - 1 + 2^64 * ( floor(2^8062 pi) + 4743158 )'
  59. */
  60. ctx->p = MP_LITERAL_WINSCP_STR("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD922222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC50846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E7160C980DD98EDD3DFFFFFFFFFFFFFFFFF");
  61. ctx->g = mp_from_integer(2);
  62. }
  63. static const struct dh_extra extra_group1 = {
  64. false, dh_group1_construct,
  65. };
  66. const ssh_kex ssh_diffiehellman_group1_sha1 = {
  67. /*.name =*/ "diffie-hellman-group1-sha1",
  68. /*.groupname =*/ "group1",
  69. /*.main_type =*/ KEXTYPE_DH,
  70. /*.hash =*/ &ssh_sha1,
  71. NULL, // WINSCP
  72. /*.extra =*/ &extra_group1,
  73. };
  74. static const ssh_kex *const group1_list[] = {
  75. &ssh_diffiehellman_group1_sha1
  76. };
  77. const ssh_kexes ssh_diffiehellman_group1 = { lenof(group1_list), group1_list };
  78. static const struct dh_extra extra_group18 = {
  79. false, dh_group18_construct,
  80. };
  81. const ssh_kex ssh_diffiehellman_group18_sha512 = {
  82. /*.name =*/ "diffie-hellman-group18-sha512",
  83. /*.groupname =*/ "group18",
  84. /*.main_type =*/ KEXTYPE_DH,
  85. /*.hash =*/ &ssh_sha512,
  86. NULL, // WINSCP
  87. /*.extra =*/ &extra_group18,
  88. };
  89. static const ssh_kex *const group18_list[] = {
  90. &ssh_diffiehellman_group18_sha512,
  91. };
  92. const ssh_kexes ssh_diffiehellman_group18 = {
  93. lenof(group18_list), group18_list
  94. };
  95. static const struct dh_extra extra_group17 = {
  96. false, dh_group17_construct,
  97. };
  98. const ssh_kex ssh_diffiehellman_group17_sha512 = {
  99. /*.name =*/ "diffie-hellman-group17-sha512",
  100. /*.groupname =*/ "group17",
  101. /*.main_type =*/ KEXTYPE_DH,
  102. /*.hash =*/ &ssh_sha512,
  103. NULL, // WINSCP
  104. /*.extra =*/ &extra_group17,
  105. };
  106. static const ssh_kex *const group17_list[] = {
  107. &ssh_diffiehellman_group17_sha512,
  108. };
  109. const ssh_kexes ssh_diffiehellman_group17 = {
  110. lenof(group17_list), group17_list
  111. };
  112. static const struct dh_extra extra_group16 = {
  113. false, dh_group16_construct,
  114. };
  115. const ssh_kex ssh_diffiehellman_group16_sha512 = {
  116. /*.name =*/ "diffie-hellman-group16-sha512",
  117. /*.groupname =*/ "group16",
  118. /*.main_type =*/ KEXTYPE_DH,
  119. /*.hash =*/ &ssh_sha512,
  120. NULL, // WINSCP
  121. /*.extra =*/ &extra_group16,
  122. };
  123. static const ssh_kex *const group16_list[] = {
  124. &ssh_diffiehellman_group16_sha512,
  125. };
  126. const ssh_kexes ssh_diffiehellman_group16 = {
  127. lenof(group16_list), group16_list
  128. };
  129. static const struct dh_extra extra_group15 = {
  130. false, dh_group15_construct,
  131. };
  132. const ssh_kex ssh_diffiehellman_group15_sha512 = {
  133. /*.name =*/ "diffie-hellman-group15-sha512",
  134. /*.groupname =*/ "group15",
  135. /*.main_type =*/ KEXTYPE_DH,
  136. /*.hash =*/ &ssh_sha512,
  137. NULL, // WINSCP
  138. /*.extra =*/ &extra_group15,
  139. };
  140. static const ssh_kex *const group15_list[] = {
  141. &ssh_diffiehellman_group15_sha512,
  142. };
  143. const ssh_kexes ssh_diffiehellman_group15 = {
  144. lenof(group15_list), group15_list
  145. };
  146. static const struct dh_extra extra_group14 = {
  147. false, dh_group14_construct,
  148. };
  149. const ssh_kex ssh_diffiehellman_group14_sha256 = {
  150. /*.name =*/ "diffie-hellman-group14-sha256",
  151. /*.groupname =*/ "group14",
  152. /*.main_type =*/ KEXTYPE_DH,
  153. /*.hash =*/ &ssh_sha256,
  154. NULL, // WINSCP
  155. /*.extra =*/ &extra_group14,
  156. };
  157. const ssh_kex ssh_diffiehellman_group14_sha1 = {
  158. /*.name =*/ "diffie-hellman-group14-sha1",
  159. /*.groupname =*/ "group14",
  160. /*.main_type =*/ KEXTYPE_DH,
  161. /*.hash =*/ &ssh_sha1,
  162. NULL, // WINSCP
  163. /*.extra =*/ &extra_group14,
  164. };
  165. static const ssh_kex *const group14_list[] = {
  166. &ssh_diffiehellman_group14_sha256,
  167. &ssh_diffiehellman_group14_sha1
  168. };
  169. const ssh_kexes ssh_diffiehellman_group14 = {
  170. lenof(group14_list), group14_list
  171. };
  172. static const struct dh_extra extra_gex = { true };
  173. static const ssh_kex ssh_diffiehellman_gex_sha256 = {
  174. /*.name =*/ "diffie-hellman-group-exchange-sha256",
  175. /*.groupname =*/ NULL,
  176. /*.main_type =*/ KEXTYPE_DH,
  177. /*.hash =*/ &ssh_sha256,
  178. NULL, // WINSCP
  179. /*.extra =*/ &extra_gex,
  180. };
  181. static const ssh_kex ssh_diffiehellman_gex_sha1 = {
  182. /*.name =*/ "diffie-hellman-group-exchange-sha1",
  183. /*.groupname =*/ NULL,
  184. /*.main_type =*/ KEXTYPE_DH,
  185. /*.hash =*/ &ssh_sha1,
  186. NULL, // WINSCP
  187. /*.extra =*/ &extra_gex,
  188. };
  189. static const ssh_kex *const gex_list[] = {
  190. &ssh_diffiehellman_gex_sha256,
  191. &ssh_diffiehellman_gex_sha1
  192. };
  193. const ssh_kexes ssh_diffiehellman_gex = { lenof(gex_list), gex_list };
  194. static const ssh_kex ssh_gssk5_diffiehellman_gex_sha1 = {
  195. /*.name =*/ "gss-gex-sha1-" GSS_KRB5_OID_HASH,
  196. /*.groupname =*/ NULL,
  197. /*.main_type =*/ KEXTYPE_GSS,
  198. /*.hash =*/ &ssh_sha1,
  199. NULL, // WINSCP
  200. /*.extra =*/ &extra_gex,
  201. };
  202. static const ssh_kex ssh_gssk5_diffiehellman_group18_sha512 = {
  203. /*.name =*/ "gss-group18-sha512-" GSS_KRB5_OID_HASH,
  204. /*.groupname =*/ "group18",
  205. /*.main_type =*/ KEXTYPE_GSS,
  206. /*.hash =*/ &ssh_sha512,
  207. NULL, // WINSCP
  208. /*.extra =*/ &extra_group18,
  209. };
  210. static const ssh_kex ssh_gssk5_diffiehellman_group17_sha512 = {
  211. /*.name =*/ "gss-group17-sha512-" GSS_KRB5_OID_HASH,
  212. /*.groupname =*/ "group17",
  213. /*.main_type =*/ KEXTYPE_GSS,
  214. /*.hash =*/ &ssh_sha512,
  215. NULL, // WINSCP
  216. /*.extra =*/ &extra_group17,
  217. };
  218. static const ssh_kex ssh_gssk5_diffiehellman_group16_sha512 = {
  219. /*.name =*/ "gss-group16-sha512-" GSS_KRB5_OID_HASH,
  220. /*.groupname =*/ "group16",
  221. /*.main_type =*/ KEXTYPE_GSS,
  222. /*.hash =*/ &ssh_sha512,
  223. NULL, // WINSCP
  224. /*.extra =*/ &extra_group16,
  225. };
  226. static const ssh_kex ssh_gssk5_diffiehellman_group15_sha512 = {
  227. /*.name =*/ "gss-group15-sha512-" GSS_KRB5_OID_HASH,
  228. /*.groupname =*/ "group15",
  229. /*.main_type =*/ KEXTYPE_GSS,
  230. /*.hash =*/ &ssh_sha512,
  231. NULL, // WINSCP
  232. /*.extra =*/ &extra_group15,
  233. };
  234. static const ssh_kex ssh_gssk5_diffiehellman_group14_sha256 = {
  235. /*.name =*/ "gss-group14-sha256-" GSS_KRB5_OID_HASH,
  236. /*.groupname =*/ "group14",
  237. /*.main_type =*/ KEXTYPE_GSS,
  238. /*.hash =*/ &ssh_sha256,
  239. NULL, // WINSCP
  240. /*.extra =*/ &extra_group14,
  241. };
  242. static const ssh_kex *const gssk5_sha2_kex_list[] = {
  243. &ssh_gssk5_diffiehellman_group16_sha512,
  244. &ssh_gssk5_diffiehellman_group17_sha512,
  245. &ssh_gssk5_diffiehellman_group18_sha512,
  246. &ssh_gssk5_diffiehellman_group15_sha512,
  247. &ssh_gssk5_diffiehellman_group14_sha256,
  248. };
  249. const ssh_kexes ssh_gssk5_sha2_kex = {
  250. lenof(gssk5_sha2_kex_list), gssk5_sha2_kex_list
  251. };
  252. static const ssh_kex ssh_gssk5_diffiehellman_group14_sha1 = {
  253. /*.name =*/ "gss-group14-sha1-" GSS_KRB5_OID_HASH,
  254. /*.groupname =*/ "group14",
  255. /*.main_type =*/ KEXTYPE_GSS,
  256. /*.hash =*/ &ssh_sha1,
  257. NULL, // WINSCP
  258. /*.extra =*/ &extra_group14,
  259. };
  260. static const ssh_kex ssh_gssk5_diffiehellman_group1_sha1 = {
  261. /*.name =*/ "gss-group1-sha1-" GSS_KRB5_OID_HASH,
  262. /*.groupname =*/ "group1",
  263. /*.main_type =*/ KEXTYPE_GSS,
  264. /*.hash =*/ &ssh_sha1,
  265. NULL, // WINSCP
  266. /*.extra =*/ &extra_group1,
  267. };
  268. static const ssh_kex *const gssk5_sha1_kex_list[] = {
  269. &ssh_gssk5_diffiehellman_gex_sha1,
  270. &ssh_gssk5_diffiehellman_group14_sha1,
  271. &ssh_gssk5_diffiehellman_group1_sha1
  272. };
  273. const ssh_kexes ssh_gssk5_sha1_kex = {
  274. lenof(gssk5_sha1_kex_list), gssk5_sha1_kex_list
  275. };
  276. /*
  277. * Common DH initialisation.
  278. */
  279. static void dh_init(dh_ctx *ctx)
  280. {
  281. ctx->q = mp_rshift_fixed(ctx->p, 1);
  282. ctx->x = ctx->e = NULL;
  283. }
  284. bool dh_is_gex(const ssh_kex *kex)
  285. {
  286. const struct dh_extra *extra = (const struct dh_extra *)kex->extra;
  287. return extra->gex;
  288. }
  289. /*
  290. * Initialise DH for a standard group.
  291. */
  292. dh_ctx *dh_setup_group(const ssh_kex *kex)
  293. {
  294. const struct dh_extra *extra = (const struct dh_extra *)kex->extra;
  295. pinitassert(!extra->gex);
  296. dh_ctx *ctx = snew(dh_ctx);
  297. extra->construct(ctx);
  298. dh_init(ctx);
  299. return ctx;
  300. }
  301. /*
  302. * Initialise DH for a server-supplied group.
  303. */
  304. dh_ctx *dh_setup_gex(mp_int *pval, mp_int *gval)
  305. {
  306. dh_ctx *ctx = snew(dh_ctx);
  307. ctx->p = mp_copy(pval);
  308. ctx->g = mp_copy(gval);
  309. dh_init(ctx);
  310. return ctx;
  311. }
  312. /*
  313. * Return size of DH modulus p.
  314. */
  315. int dh_modulus_bit_size(const dh_ctx *ctx)
  316. {
  317. return mp_get_nbits(ctx->p);
  318. }
  319. /*
  320. * Clean up and free a context.
  321. */
  322. void dh_cleanup(dh_ctx *ctx)
  323. {
  324. if (ctx->x)
  325. mp_free(ctx->x);
  326. if (ctx->e)
  327. mp_free(ctx->e);
  328. if (ctx->p)
  329. mp_free(ctx->p);
  330. if (ctx->g)
  331. mp_free(ctx->g);
  332. if (ctx->q)
  333. mp_free(ctx->q);
  334. sfree(ctx);
  335. }
  336. /*
  337. * DH stage 1: invent a number x between 1 and q, and compute e =
  338. * g^x mod p. Return e.
  339. */
  340. mp_int *dh_create_e(dh_ctx *ctx)
  341. {
  342. /*
  343. * Lower limit is just 2.
  344. */
  345. mp_int *lo = mp_from_integer(2);
  346. /*
  347. * Upper limit.
  348. */
  349. mp_int *hi = mp_copy(ctx->q);
  350. mp_sub_integer_into(hi, hi, 1);
  351. /*
  352. * Make a random number in that range.
  353. */
  354. ctx->x = mp_random_in_range(lo, hi);
  355. mp_free(lo);
  356. mp_free(hi);
  357. /*
  358. * Now compute e = g^x mod p.
  359. */
  360. ctx->e = mp_modpow(ctx->g, ctx->x, ctx->p);
  361. return ctx->e;
  362. }
  363. /*
  364. * DH stage 2-epsilon: given a number f, validate it to ensure it's in
  365. * range. (RFC 4253 section 8: "Values of 'e' or 'f' that are not in
  366. * the range [1, p-1] MUST NOT be sent or accepted by either side."
  367. * Also, we rule out 1 and p-1 too, since that's easy to do and since
  368. * they lead to obviously weak keys that even a passive eavesdropper
  369. * can figure out.)
  370. */
  371. const char *dh_validate_f(dh_ctx *ctx, mp_int *f)
  372. {
  373. if (!mp_hs_integer(f, 2)) {
  374. return "f value received is too small";
  375. } else {
  376. mp_int *pm1 = mp_copy(ctx->p);
  377. mp_sub_integer_into(pm1, pm1, 1);
  378. { // WINSCP
  379. unsigned cmp = mp_cmp_hs(f, pm1);
  380. mp_free(pm1);
  381. if (cmp)
  382. return "f value received is too large";
  383. } // WINSCP
  384. }
  385. return NULL;
  386. }
  387. /*
  388. * DH stage 2: given a number f, compute K = f^x mod p.
  389. */
  390. mp_int *dh_find_K(dh_ctx *ctx, mp_int *f)
  391. {
  392. return mp_modpow(f, ctx->x, ctx->p);
  393. }