New SP 800-56A compliant version of DH_compute_key().
authorDr. Stephen Henson <steve@openssl.org>
Tue, 8 Mar 2011 19:07:26 +0000 (19:07 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Tue, 1 Oct 2013 13:01:17 +0000 (14:01 +0100)
(cherry picked from commit bc91494e064ebdcff68f987947f97e404fbca0b5)

crypto/dh/dh.h
crypto/dh/dh_key.c

index 523d3464ac6c02d9d0cfcd6cdb80c136083e1206..a4095c1adbc1a7d34ae61ac683e20724ea75f845 100644 (file)
@@ -213,6 +213,7 @@ int DH_check(const DH *dh,int *codes);
 int    DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes);
 int    DH_generate_key(DH *dh);
 int    DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
+int    DH_compute_key_padded(unsigned char *key,const BIGNUM *pub_key,DH *dh);
 DH *   d2i_DHparams(DH **a,const unsigned char **pp, long length);
 int    i2d_DHparams(const DH *a,unsigned char **pp);
 DH *   d2i_DHxparams(DH **a,const unsigned char **pp, long length);
index 89a74db4e691ba0ba953a5ea59e887a03ffda68f..6cb0d022563e7e4ef10682658240daa51f05b04d 100644 (file)
@@ -97,6 +97,21 @@ int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
        return dh->meth->compute_key(key, pub_key, dh);
        }
 
+int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
+       {
+       int rv, pad;
+       rv = dh->meth->compute_key(key, pub_key, dh);
+       if (rv <= 0)
+               return rv;
+       pad = BN_num_bytes(dh->p) - rv;
+       if (pad > 0)
+               {
+               memmove(key + pad, key, rv);
+               memset(key, 0, pad);
+               }
+       return rv + pad;
+       }
+
 static DH_METHOD dh_ossl = {
 "OpenSSL DH Method",
 generate_key,