Skip to content

Commit

Permalink
Change name to bits instead of keysize_in_bits and remove some additi…
Browse files Browse the repository at this point in the history
…onal spaces
  • Loading branch information
Antrapol committed Nov 16, 2023
1 parent db2ac8a commit 5ce7e11
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 53 deletions.
19 changes: 7 additions & 12 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,18 +749,17 @@ ossl_pkey_inspect(VALUE self)
static VALUE
ossl_pkey_length_in_bits(VALUE self)
{
EVP_PKEY * pkey;
int bits;
EVP_PKEY * pkey;
int bits;

GetPKey(self, pkey);
GetPKey(self, pkey);

#if OSSL_OPENSSL_PREREQ(3, 0, 0)
bits = EVP_PKEY_get_bits(pkey);
bits = EVP_PKEY_get_bits(pkey);
#else
bits = EVP_PKEY_bits(pkey);
bits = EVP_PKEY_bits(pkey);
#endif
return INT2NUM(bits);

return INT2NUM(bits);
}

/*
Expand Down Expand Up @@ -1692,8 +1691,6 @@ ossl_pkey_decrypt(int argc, VALUE *argv, VALUE self)
return str;
}



/*
* INIT
*/
Expand Down Expand Up @@ -1793,7 +1790,7 @@ Init_ossl_pkey(void)
#endif
rb_define_method(cPKey, "oid", ossl_pkey_oid, 0);
rb_define_method(cPKey, "inspect", ossl_pkey_inspect, 0);
rb_define_method(cPKey, "keysize_in_bits", ossl_pkey_length_in_bits, 0);
rb_define_method(cPKey, "bits", ossl_pkey_length_in_bits, 0);
rb_define_method(cPKey, "to_text", ossl_pkey_to_text, 0);
rb_define_method(cPKey, "private_to_der", ossl_pkey_private_to_der, -1);
rb_define_method(cPKey, "private_to_pem", ossl_pkey_private_to_pem, -1);
Expand All @@ -1814,8 +1811,6 @@ Init_ossl_pkey(void)
rb_define_method(cPKey, "encrypt", ossl_pkey_encrypt, -1);
rb_define_method(cPKey, "decrypt", ossl_pkey_decrypt, -1);



id_private_q = rb_intern("private?");

/*
Expand Down
21 changes: 3 additions & 18 deletions test/openssl/test_pkey.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

require_relative 'utils'

class OpenSSL::TestPKey < OpenSSL::PKeyTestCase
Expand Down Expand Up @@ -244,24 +243,10 @@ def test_to_text
assert_include rsa.to_text, 'publicExponent'
end

def test_keysize_in_bits
# Test vector from RFC 7748 Section 6.1
alice_pem = <<~EOF
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VuBCIEIHcHbQpzGKV9PBbBclGyZkXfTC+H68CZKrF3+6UduSwq
-----END PRIVATE KEY-----
EOF
bob_pem = <<~EOF
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VuAyEA3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08=
-----END PUBLIC KEY-----
EOF
def test_keysize_bits
begin
alice = OpenSSL::PKey.read(alice_pem)
bob = OpenSSL::PKey.read(bob_pem)

assert_equal 253, alice.keysize_in_bits
assert_equal 253, bob.keysize_in_bits
x25519 = OpenSSL::PKey.generate_key("X25519")
assert_equal 253, x25519.bits
rescue OpenSSL::PKey::PKeyError
# OpenSSL < 1.1.0
pend 'X25519 is not implemented'
Expand Down
30 changes: 7 additions & 23 deletions test/openssl/test_pkey_ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,14 @@ def test_marshal
assert_equal key.to_der, deserialized.to_der
end

def test_keysize_in_bits
OpenSSL::PKey::EC.builtin_curves.map { |name, comment| name }.each do |curve|
if !openssl?(3, 0, 0)
ec = OpenSSL::PKey::EC.new(curve)
ec.generate_key!
else
ec = OpenSSL::PKey::EC.generate(curve)
end

case curve
when "secp224r1"
assert_equal(224, ec.keysize_in_bits)
when "secp256r1", "brainpoolP256r1", "brainpoolP256t1", "prime256v1"
assert_equal(256, ec.keysize_in_bits)
when "secp384r1", "brainpoolP384r1", "brainpoolP384t1"
assert_equal(384, ec.keysize_in_bits)
when "secp521r1"
assert_equal(521, ec.keysize_in_bits)
when "brainpoolP512r1", "brainpoolP512t1"
assert_equal(512, ec.keysize_in_bits)
when "brainpoolP320r1", "brainpoolP320t1"
assert_equal(320, ec.keysize_in_bits)
end
def test_keysize_bits
if !openssl?(3, 0, 0)
ec = OpenSSL::PKey::EC.new("prime256v1")
ec.generate_key!
else
ec = OpenSSL::PKey::EC.generate("prime256v1")
end
assert_equal(256, ec.bits)
end

def test_check_key
Expand Down

0 comments on commit 5ce7e11

Please sign in to comment.