diffie-hellman.c 16 KB

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