Path: blob/main/crypto/openssl/doc/HOWTO/keys.txt
104991 views
<DRAFT!>1HOWTO keys231. Introduction45Keys are the basis of public key algorithms and PKI. Keys usually6come in pairs, with one half being the public key and the other half7being the private key. With OpenSSL, the private key contains the8public key information as well, so a public key doesn't need to be9generated separately.1011Public keys come in several flavors, using different cryptographic12algorithms. The most popular ones associated with certificates are13RSA and DSA, and this HOWTO will show how to generate each of them.1415162. To generate an RSA key1718An RSA key can be used both for encryption and for signing.1920Generating a key for the RSA algorithm is quite easy, all you have to21do is the following:2223openssl genrsa -des3 -out privkey.pem 20482425With this variant, you will be prompted for a protecting password. If26you don't want your key to be protected by a password, remove the flag27'-des3' from the command line above.2829The number 2048 is the size of the key, in bits. Today, 2048 or30higher is recommended for RSA keys, as fewer amount of bits is31consider insecure or to be insecure pretty soon.3233343. To generate a DSA key3536A DSA key can be used for signing only. It is important to37know what a certificate request with a DSA key can really be used for.3839Generating a key for the DSA algorithm is a two-step process. First,40you have to generate parameters from which to generate the key:4142openssl dsaparam -out dsaparam.pem 20484344The number 2048 is the size of the key, in bits. Today, 2048 or45higher is recommended for DSA keys, as fewer amount of bits is46consider insecure or to be insecure pretty soon.4748When that is done, you can generate a key using the parameters in49question (actually, several keys can be generated from the same50parameters):5152openssl gendsa -des3 -out privkey.pem dsaparam.pem5354With this variant, you will be prompted for a protecting password. If55you don't want your key to be protected by a password, remove the flag56'-des3' from the command line above.5758594. To generate an EC key6061An EC key can be used both for key agreement (ECDH) and signing (ECDSA).6263Generating a key for ECC is similar to generating a DSA key. These are64two-step processes. First, you have to get the EC parameters from which65the key will be generated:6667openssl ecparam -name prime256v1 -out prime256v1.pem6869The prime256v1, or NIST P-256, which stands for 'X9.62/SECG curve over70a 256-bit prime field', is the name of an elliptic curve which generates the71parameters. You can use the following command to list all supported curves:7273openssl ecparam -list_curves7475When that is done, you can generate a key using the created parameters (several76keys can be produced from the same parameters):7778openssl genpkey -des3 -paramfile prime256v1.pem -out private.key7980With this variant, you will be prompted for a password to protect your key.81If you don't want your key to be protected by a password, remove the flag82'-des3' from the command line above.8384You can also directly generate the key in one step:8586openssl ecparam -genkey -name prime256v1 -out private.key8788or8990openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-25691925. To generate an ML-DSA key9394An ML-DSA key can be used for signing (and verification via the public key)95only.9697Generating a key for the ML-DSA algorithm is a one-step process.9899openssl genpkey -algorithm ML-DSA-44 -out key.pem100openssl genpkey -algorithm ML-DSA-65 -out key.pem101openssl genpkey -algorithm ML-DSA-87 -out key.pem102103See L<EVP_PKEY-ML-DSA(7)> for more detail.1041056. To generate an ML-KEM key106107An ML-KEM key can be used for decapsulation (and encapsulation via the public108key) only.109110Generating a key for the ML-KEM algorithm is a one-step process.111112openssl genpkey -algorithm ML-KEM-512 -out key.pem113openssl genpkey -algorithm ML-KEM-768 -out key.pem114openssl genpkey -algorithm ML-KEM-1024 -out key.pem115116See L<EVP_PKEY-ML-KEM(7)> for more detail.1171187. NOTE119120If you intend to use the key together with a server certificate,121it may be reasonable to avoid protecting it with a password, since122otherwise someone would have to type in the password every time the123server needs to access the key.124125X25519, X448, Ed25519 and Ed448 are treated as distinct algorithms and not as126one of the EC curves listed with 'ecparam -list_curves' option. You can use the127following command to generate an X25519 key:128129openssl genpkey -algorithm X25519 -out xkey.pem130131132