Add DH_set_length.

OpenSSH uses this function.

Change-Id: I73c4c1647f61824f771022606f95bff7f6d3d77a
Update-Note: Set HAVE_DH_SET_LENGTH in OpenSSH.
Bug: 325
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/40365
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/dh/dh.c b/crypto/dh/dh.c
index 68d710d..88dc63f 100644
--- a/crypto/dh/dh.c
+++ b/crypto/dh/dh.c
@@ -174,6 +174,11 @@
   return 1;
 }
 
+int DH_set_length(DH *dh, unsigned priv_length) {
+  dh->priv_length = priv_length;
+  return 1;
+}
+
 int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
   // We generate DH parameters as follows
   // find a prime q which is prime_bits/2 bits long.
diff --git a/include/openssl/dh.h b/include/openssl/dh.h
index 7e10303..d6bfd21 100644
--- a/include/openssl/dh.h
+++ b/include/openssl/dh.h
@@ -108,6 +108,11 @@
 // |p| and |g| must either be specified or already configured on |dh|.
 OPENSSL_EXPORT int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
 
+// DH_set_length sets the number of bits to use for the secret exponent when
+// calling |DH_generate_key| on |dh| and returns one. If unset,
+// |DH_generate_key| will use the bit length of p.
+OPENSSL_EXPORT int DH_set_length(DH *dh, unsigned priv_length);
+
 
 // Standard parameters.