Skip to content

Commit 28edf6b

Browse files
committed
pkey: reimplement PKey::DH#compute_key and PKey::EC#dh_compute_key
Use the new OpenSSL::PKey::PKey#derive instead of the raw {EC,}DH_compute_key(), mainly to reduce amount of the C code.
1 parent 28f0059 commit 28edf6b

File tree

3 files changed

+33
-67
lines changed

3 files changed

+33
-67
lines changed

ext/openssl/ossl_pkey_dh.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -518,40 +518,6 @@ ossl_dh_generate_key(VALUE self)
518518
return self;
519519
}
520520

521-
/*
522-
* call-seq:
523-
* dh.compute_key(pub_bn) -> aString
524-
*
525-
* Returns a String containing a shared secret computed from the other party's public value.
526-
* See DH_compute_key() for further information.
527-
*
528-
* === Parameters
529-
* * _pub_bn_ is a OpenSSL::BN, *not* the DH instance returned by
530-
* DH#public_key as that contains the DH parameters only.
531-
*/
532-
static VALUE
533-
ossl_dh_compute_key(VALUE self, VALUE pub)
534-
{
535-
DH *dh;
536-
const BIGNUM *pub_key, *dh_p;
537-
VALUE str;
538-
int len;
539-
540-
GetDH(self, dh);
541-
DH_get0_pqg(dh, &dh_p, NULL, NULL);
542-
if (!dh_p)
543-
ossl_raise(eDHError, "incomplete DH");
544-
pub_key = GetBNPtr(pub);
545-
len = DH_size(dh);
546-
str = rb_str_new(0, len);
547-
if ((len = DH_compute_key((unsigned char *)RSTRING_PTR(str), pub_key, dh)) < 0) {
548-
ossl_raise(eDHError, NULL);
549-
}
550-
rb_str_set_len(str, len);
551-
552-
return str;
553-
}
554-
555521
/*
556522
* Document-method: OpenSSL::PKey::DH#set_pqg
557523
* call-seq:
@@ -629,7 +595,6 @@ Init_ossl_dh(void)
629595
rb_define_method(cDH, "public_key", ossl_dh_to_public_key, 0);
630596
rb_define_method(cDH, "params_ok?", ossl_dh_check_params, 0);
631597
rb_define_method(cDH, "generate_key!", ossl_dh_generate_key, 0);
632-
rb_define_method(cDH, "compute_key", ossl_dh_compute_key, 1);
633598

634599
DEF_OSSL_PKEY_BN(cDH, dh, p);
635600
DEF_OSSL_PKEY_BN(cDH, dh, q);

ext/openssl/ossl_pkey_ec.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -582,37 +582,6 @@ static VALUE ossl_ec_key_check_key(VALUE self)
582582
return Qtrue;
583583
}
584584

585-
/*
586-
* call-seq:
587-
* key.dh_compute_key(pubkey) => String
588-
*
589-
* See the OpenSSL documentation for ECDH_compute_key()
590-
*/
591-
static VALUE ossl_ec_key_dh_compute_key(VALUE self, VALUE pubkey)
592-
{
593-
EC_KEY *ec;
594-
EC_POINT *point;
595-
int buf_len;
596-
VALUE str;
597-
598-
GetEC(self, ec);
599-
GetECPoint(pubkey, point);
600-
601-
/* BUG: need a way to figure out the maximum string size */
602-
buf_len = 1024;
603-
str = rb_str_new(0, buf_len);
604-
/* BUG: take KDF as a block */
605-
buf_len = ECDH_compute_key(RSTRING_PTR(str), buf_len, point, ec, NULL);
606-
if (buf_len < 0)
607-
ossl_raise(eECError, "ECDH_compute_key");
608-
609-
rb_str_resize(str, buf_len);
610-
611-
return str;
612-
}
613-
614-
/* sign_setup */
615-
616585
/*
617586
* call-seq:
618587
* key.dsa_sign_asn1(data) => String
@@ -1752,7 +1721,6 @@ void Init_ossl_ec(void)
17521721
rb_define_alias(cEC, "generate_key", "generate_key!");
17531722
rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
17541723

1755-
rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 1);
17561724
rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
17571725
rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
17581726
/* do_sign/do_verify */

lib/openssl/pkey.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
module OpenSSL::PKey
1010
class DH
1111
include OpenSSL::Marshal
12+
13+
# :call-seq:
14+
# dh.compute_key(pub_bn) -> string
15+
#
16+
# Returns a String containing a shared secret computed from the other
17+
# party's public value.
18+
#
19+
# This method is provided for backwards compatibility, and calls #derive
20+
# internally.
21+
#
22+
# === Parameters
23+
# * _pub_bn_ is a OpenSSL::BN, *not* the DH instance returned by
24+
# DH#public_key as that contains the DH parameters only.
25+
def compute_key(pub_bn)
26+
peer = dup
27+
peer.set_key(pub_bn, nil)
28+
derive(peer)
29+
end
1230
end
1331

1432
class DSA
@@ -18,7 +36,22 @@ class DSA
1836
if defined?(EC)
1937
class EC
2038
include OpenSSL::Marshal
39+
40+
# :call-seq:
41+
# ec.dh_compute_key(pubkey) -> string
42+
#
43+
# Derives a shared secret by ECDH. _pubkey_ must be an instance of
44+
# OpenSSL::PKey::EC::Point and must belong to the same group.
45+
#
46+
# This method is provided for backwards compatibility, and calls #derive
47+
# internally.
48+
def dh_compute_key(pubkey)
49+
peer = OpenSSL::PKey::EC.new(group)
50+
peer.public_key = pubkey
51+
derive(peer)
52+
end
2153
end
54+
2255
class EC::Point
2356
# :call-seq:
2457
# point.to_bn([conversion_form]) -> OpenSSL::BN

0 commit comments

Comments
 (0)