Skip to content

add getters for rsa key params p, q, n, e and d #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/rsa_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ k0LaJjYM2ycehinmuLHgY3qdDJgtEbt4WG5XNQzhyfaN
rsa.verify(new_digest, signature[0, 10], data).should be_false
end
end

describe "can get parameters for serialization into other formats" do
it "can get p and q" do
rsa = OpenSSL::RSA.new(1024)
rsa.p
rsa.q
end

it "can get n, e and d" do
rsa = OpenSSL::RSA.new(1024)
rsa.n
rsa.e
rsa.d
end
end

describe "can set parameters for more efficient decryption" do
it "can set dmp1, dmq1, iqmp" do
end
Expand Down
6 changes: 5 additions & 1 deletion src/openssl_ext/lib_crypto.cr
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ lib LibCrypto

fun bignum_new = BN_new : Bignum*
fun set_bignum_from_decimal = BN_dec2bn(a : Bignum**, str : LibC::Char*) : LibC::Int
fun bignum_to_bytes = BN_bn2bin(a : Bignum*, str : LibC::Char*) : LibC::Int
fun bignum_bits = BN_num_bits(a : Bignum*) : LibC::Int

fun evp_pkey_new = EVP_PKEY_new : EvpPKey*
fun evp_pkey_free = EVP_PKEY_free(pkey : EvpPKey*)
Expand All @@ -212,7 +214,9 @@ lib LibCrypto
fun rsa_public_encrypt = RSA_public_encrypt(flen : LibC::Int, from : UInt8*, to : UInt8*, rsa : Rsa*, padding : LibC::Int) : LibC::Int
fun rsa_private_decrypt = RSA_private_decrypt(flen : LibC::Int, from : UInt8*, to : UInt8*, rsa : Rsa*, padding : LibC::Int) : LibC::Int
fun rsa_public_decrypt = RSA_public_decrypt(flen : LibC::Int, from : UInt8*, to : UInt8*, rsa : Rsa*, padding : LibC::Int) : LibC::Int

{% for param in %i[n e d p q] %}
fun rsa_get_{{param.id}} = RSA_get0_{{param.id}}(rsa : Rsa*) : Bignum*
{% end %}
fun pem_read_bio_private_key = PEM_read_bio_PrivateKey(bp : Bio*, x : EvpPKey**, cb : (LibC::Char*, LibC::Int, LibC::Int, Void* -> LibC::Int), u : Void*) : EvpPKey*
fun pem_read_bio_public_key = PEM_read_bio_PUBKEY(bp : Bio*, x : EvpPKey**, cb : (LibC::Char*, LibC::Int, LibC::Int, Void* -> LibC::Int), u : Void*) : EvpPKey*
fun pem_write_bio_rsa_private_key = PEM_write_bio_RSAPrivateKey(bp : Bio*, x : Rsa*, enc : EVP_MD*, kstr : UInt8*, klen : LibC::Int, cb : (LibC::Char*, LibC::Int, LibC::Int, Void* -> LibC::Int), u : Void*) : LibC::Int
Expand Down
9 changes: 9 additions & 0 deletions src/openssl_ext/rsa.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,14 @@ module OpenSSL
LibCrypto.rsa_blinding_off(rsa)
@blinding_on = false
end

{% for param in %i[n e d p q] %}
def {{param.id}}
bignum = LibCrypto.rsa_get_{{param.id}}(rsa)
Bytes.new((LibCrypto.bignum_bits(bignum) + 7) / 8).tap do |bytes|
LibCrypto.bignum_to_bytes(bignum, bytes)
end
end
{% end %}
end
end