Hello,
There is no built-in function to generate RSA key pairs. The crypto module itself is mostly bindings to OpenSSL, so I guess there must be a valid reason why RSA key generation is not exposed. Maybe the fact that it can be slow (explains why cutkey implementation uses a port driver instead of nifs). The generate function in crypto only generates Diffie-Hellman and ECDH key pairs.
Like you suggest, one option is to launch openssl to generate a PEM file and then read it:
iex(1)> :os.cmd 'openssl genrsa -out mykey.pem 2048'
iex(2)> {:RSAPrivateKey, :'two-prime', n , e, d, _p, _q, _e1, _e2, _c, _other} = File.read!("mykey.pem") |> :public_key.pem_decode |> List.first |> :public_key.pem_entry_decode
iex(3)> plain_text = "please don't read this"
iex(4)> priv_key = [:crypto.mpint(e), :crypto.mpint(n), :crypto.mpint(d)]
iex(5)> pub_key = [:crypto.mpint(e), :crypto.mpint(n)]
iex(6)> sekrit = :crypto.rsa_private_encrypt(plain_text, priv_key, :rsa_pkcs1_padding)
iex(7)> plain_text == :crypto.rsa_public_decrypt(sekrit, pub_key, :rsa_pkcs1_padding)
References:
ECDH (Elliptic Curves) on the other hand is well supported by the :crypto library, in case you have to option of using that instead of RSA (a 256 bit ECDH key is
supposedly as secure as a 3072 bit RSA key).
Regards,
Paulo